Programming language C (C). Informatics labs, Unified State Exam C language


practical course

What it is?

At one time (1991-1992), choosing a language Si As the main programming language for mathematics classes, the author was faced with the problem of the lack of normal textbooks that could be recommended to schoolchildren. This forced him to write his lecture notes, which could be called "Practical programming course in Si» — it included the information that the author really needed in practice. Many years of teaching experience have shown that notes are really in demand and are widely used by both schoolchildren and graduates.

The development environment is a free, open source Dev-C++ shell that includes the GCC compiler.

The author will be grateful for feedback and constructive criticism regarding the content and design of these materials. If you notice an error or have suggestions, comments, complaints, requests or statements, write.

License agreement

  1. 1) publication of materials in any form, including posting of materials on other Web sites;
  2. 2) distribution of incomplete or altered materials;
  3. 3) inclusion of materials in collections on any media;
  4. 4) obtaining commercial benefits from the sale or other use of materials.

Downloading materials means you accept the terms of this license agreement.

Download

The abstract is distributed freely in the format PDF. Requires the free Acrobat Reader to view. The course outline is divided into 4 parts:

  1. Introduction to programming in C (860 Kb) 29.03.2014
    Studying the basic language constructs and techniques for writing programs.
  2. Data storage and processing (792 KB) 21.11.2013
    C language: arrays, matrices, structures, character strings, structures, recursion.
  3. Development of programs in C language (937 Kb) 01.03.2014
    Program design techniques, structured programming, integer algorithms, numerical methods, modeling.
  4. Dynamic data structures in C language (666 KB) 11.06.2009
    Lists, stacks, trees, graphs.

Presentations

Teachers and students may benefit from presentations based on this summary. They can be downloaded on the Presentations page.

Methodology

The main goal is to introduce students to the basic structures of the language Si: loops, conditional statements, procedures. The study of sections related to graphics and animation is very successful.

To work with graphics through a browser, you can use Anton Denikin’s online service.

8th grade

The program includes the following sections (part of the summary is indicated in brackets): Arrays (II), Working with Files (II), Character Strings (II), Rotating Objects (III), Modeling (III). Particular attention is paid to the study of algorithms for working with arrays, including search, sorting, etc. The concept of the effectiveness of computational methods is given.

9th grade

The main focus is on studying software development methods in the language Si. Students complete an individual task, during which they learn to construct graphs on the screen, become familiar with transformations of coordinate systems, study methods for solving equations, become familiar with numerical methods, and learn to correctly design programs (Sections 1-2 of Part III). One of the tasks is devoted to the use Monte Carlo method to calculate the area of ​​a complex figure. From the new sections we also study Matrices (II), Character String Arrays (II). The concept is introduced pointers and the simplest operations with them are studied.

Grade 10

Language learning Si reaches a more serious level. Main themes - Memory Management (II), Recursion (II), Structures (II). It is planned to study Pascal as a second programming language. From this moment on, algorithms in theoretical classes are written alternately in two languages.

Grade 11

It is not so much the features of languages ​​that are studied as algorithms. Main sections - Numerical methods (III), Modeling (III), Dynamic data structures (IV).

C programming language developed by Dennis Ritchie in the early 1970s to create the UNIX operating system. C remains the most widely used language for writing system software, and C is also used for writing applications. Programming for beginners.

Peculiarities

The C programming language has the following main features:

  • Focus on the procedural programming paradigm, with the convenience of programming in a structured style, is a good plus to learn C.
  • Access to the hardware level through the use of pointers to designate memory locations.
  • Parameters are always passed to a function by value, not by reference.
  • Scope of lexical variables (but does not support closures or functions defined inside other functions.)
  • A standard set of library programs that provide features that are useful but not strictly necessary.
  • Use of the language preprocessor, C preprocessor, for tasks such as defining macros and including multiple source code files.
  • "O" performance for all operators

The functionality of the C programming language is guaranteed by ANS/ISO C99 C89/90 and regulations that explicitly state when the compiler (or environment) issues diagnostics. Programming for dummies. The documents also indicate what behavior can be expected from code written in the C programming language that conforms to the standard.
For example, the following code, according to the standard, produces undefined behavior.

#include #include int main (void) ( char *s = "Hello World!\\n"; strcpy (s, s+1); /* probably the programmer wanted to remove the first character from the string S = S +1, */ return 0; )

Note: The style of indentation in program code varies depending on the preferences of programmers. See indentation style for more information in the programming basics section.
"Undefined behavior" means that as a result the program may do anything, including working as the programmer intended, or producing fatal errors every time it is run, or causing single crashes only every time the forty-second run of the program occurs, or causing crashes when the computer reboots, etc., ad infinitum.
Some compilers do not adhere to any of the standards in the default mode, which results in many programs being written that will only compile with a certain version of the compiler and on a certain platform (Windows, Linux or Mac).
Any program written only in the standard C programming language will compile unchanged on any platform that has appropriate C compiler implementations.
Although C is commonly called a high-level language, it is only a "high-level" language compared to assembly language, but is significantly lower level than most other programming languages. But unlike most, it gives the programmer the ability to completely control the contents of the computer's memory. C does not provide tools for array bounds checking or automatic garbage collection.
Memory management guidance provides the programmer with greater freedom to tune program performance, which is especially important for programs such as device drivers. However, it also makes it easy to accidentally create code with errors resulting from erroneous memory operations such as buffer overflows. Several tools have been created to help programmers avoid these errors, including libraries for performing array bounds checking and garbage collection, and source code inspection libraries. The deliberate use of programs written in C from scratch and containing a buffer overflow mechanism is very common in many computer viruses and is very popular among hackers who develop computer worms.
Some of the identified shortcomings of C were considered and taken into account in new programming languages ​​derived from C. The Cyclone programming language has features to protect against erroneous memory operations. C++ and Objective C provide constructs designed to facilitate object-oriented programming. Java and C# added object-oriented programming constructs as well as higher levels of abstraction such as automatic memory management.

Story
The initial development of C occurred between 1969 and 1973 (according to Ritchie, the most turbulent period in 1972). It was called "C" because many of the features were derived from a predecessor language called B, which itself was named Bon after Bonnie Ken Thompson's wife.
By 1973, the C programming language had become powerful enough that most of the kernel from the Unix operating system was rewritten in C In comparison, the Multics operating system is implemented in the A language, Tripos OS (implemented in the BCPL language. In 1978, Ritchie and Brian Kernighan published the "C Programming Language" (the so-called "white paper", or K&R.) for many years , this book served as a specification for the language, and even today, this edition is very popular as a manual and textbook.
C became very popular outside of Bell Labs from the 1980s, and was for a time the dominant language in microcomputer programming systems and applications. It is still the most commonly used system programming language, and is one of the most commonly used programming languages ​​in computers for science education.
In the late 1980s, Bjarne Stroustrup and others at Bell Labs worked to add object-oriented constructs to C. The language they developed along with the first Cfront compiler was called C++ (which avoids the question of what a successor to "B" " and "C" must be "D" or "P"). Currently, C++ is the language most often used for commercial applications on the Microsoft Windows operating system, although C remains very popular in the Unix world.

Versions C

K&R C (Kernighan and Ritchie C)
C has evolved continuously since its inception at Bell Labs. In 1978, the first edition of Kernighan and Ritchie's The C Programming Language was published. It introduced the following features for existing versions of C:

  • structure data type
  • long int data type
  • unsigned int data type
  • The =+ operator was changed to += , and so on (since the =+ operator kept causing lexical analyzer errors in C).

For several years, the first edition of "The C Programming Language" was widely used as the de facto language specification. The version of C described in this book is generally referred to as "K&R C" (The Second Edition covers the ANSI C standard, described below.)
K&R C is often considered the core part of the language that is needed for a C compiler. Since not all compilers currently in use have been updated to fully support ANSI C in its entirety, and reasonably well-written K&R C code is also correct from an ANSI standpoint. C. Therefore, K&R C is considered the lowest common denominator that programmers should adhere to to achieve maximum portability. For example, the bootstrap version of the GCC compiler, xgcc, is written in K&R C. This is because many of the platforms supported by GCC did not have a proper ANSI C GCC compiler, so only one basic implementation of K&R C was written .
However, ANSI C is now supported by almost all widely used compilers. Most C code is now written from scratch only to take advantage of language features that go beyond the scope of the original K&R specification.

ANSI C and ISO C
In 1989, C was first officially standardized by ANSI in ANSI X3.159-1989 "The C Programming Language". One of the goals of ANSI C was to create an enhanced K&R C. However, the standards committee also included several new features that introduced many innovations than had typically occurred in programming when standardizing the language.
Some of the new features were "unofficially" added to the language after the publication of K&R, but before the start of the ANSI C process. These include:

  • void functions
  • functions returning struct or union data types
  • void * data type
  • const qualifier to make an object read-only
  • struct field names into a separate namespace for each structure type
  • memory allocation for struct data types
  • stdio library and some other standard library functions became available in most implementations (it already existed, well at least one implementation at the time of K&R C's creation, but it was not really a standard, and therefore was not documented in the book )
  • stddef.h header file and a number of other standard header files.

Several functions were added to C during the ANSI standardization process, most notably function prototypes (borrowed from C++). ANSI C also established a standard set of library functions.
The C programming language according to the ANSI standard, with minor modifications, was adopted as an ISO standard under ISO 9899. The first ISO edition of this document was published in 1990 (ISO 9899:1990).

C 99
Following the ANSI standardization process, the C language specification has remained relatively unchanged for some time, while C++ has continued to evolve. (In fact, Regulatory Amendment 1 created a new version of the C language in 1995, but this version is rarely recognized.) However, the standards were revised in the late 1990s, leading to the creation of ISO 9899:1999, which was published in 1999 year. This standard is commonly referred to as "C99". It was adopted as an ANSI standard in March 2000.
New features added in C99 include:

  • built-in functions
  • releasing the constraint on variable declaration location (as per C++)
  • In addition to several new data types, including long long int (to reduce the problems associated with the transition from 32-bit to 64-bit processors looming over many older programs, which predicted the obsolescence of the x86 architecture), an explicit Boolean data type, and a type representing complex numbers
  • variable length arrays
  • Official support for comments starting with "/ /" as in C++ (which many C89 compilers already supported as a non-ANSI extension)
  • several new library functions, including snprintf
  • several new header files, including stdint.h

Interest in supporting new C99 features is mixed. Although GCC and several commercial compilers support most of the new features of C99, compilers made by Microsoft and Borland do not, and those two companies do not seem interested in adding such support.

Program "Hello, World!" in C language
The following simple application outputs "Hello, World" to standard output (usually the screen, but could be a file or some other hardware device). This program change appeared first on K&R.

#include int main(void) ( printf("Hello, World!\\n"); //comment return 0; /* comment on several lines */ )

Typically, text that is not a program, but serves as a hint for the programmer, is written as a comment. // single-line or /* multi-line*/

Anatomy of a C Program
A C program consists of functions and variables. C functions as Fortran subroutines and functions or Pascal procedures and functions. The peculiarity of the main function is that the program begins execution with the main function. This means that every C program must have a main function.
The main function typically calls other functions that help do its job, such as printf in the example above. The programmer can write some of these functions, and others can be called from the library. In the above example, return 0 gives the return value for the main functions. This indicates successful execution of the program to call the program shell.
A C function consists of a return type, a name, a list of parameters (or void in parentheses if there are none), and a function body. The body function syntax is equivalent to the compound operator.

Control structures

Compound Operators
Compound operators in C are of the form
{ }
and are used as the body of a function and in places where multiple actions are expected in one block.

Declaration operator
Type statement
;
is the expression of the declaration. If there is no expression, then such a statement is called an empty statement.

Selection operator
C has three types of selection statement: two types if and switch statement.
Two types of if,
if()
{
}
or
if()
{
}
else
{
}
In an if declaration, if the expression in parentheses is nonzero or true, then control passes to the statement following the if . If there is an else in the clause, then control will pass to the next set of actions after the else if the expression in parentheses is null or false.
Switch control statement - in this case, it is necessary to list several values ​​that the variable that is used as a selection variable can take, which must have an integer type. For each value of a choice variable, several actions can be performed that are selected. Each action branch can be marked with a case tag, which looks like the keyword case followed by a constant expression and then a colon (:). No values ​​associated with switch constants can have the same value. There can also be at most one default label associated with swith; control is transferred to the default label if none of the constant values ​​match the selection variable, which is in parentheses after switch . In the example below, if there is a match between the selection variable and the constant, then the set of actions following the colon will be executed

Switch () ( case: printf(); break; case: printf(); break; default: )

Repetitions (Cycles)
C has three forms of loop operators:

Do ( ) while (); while () ( ) for (; ;) ( )

In while and do statements, the body is executed repeatedly as long as the value of the expression remains nonzero or true. For while, the condition is checked every time before execution starts; for do, the condition is checked after the loop body is executed.
If all three expressions are present in the for statement

For (e1; e2; e3) s;

then this is equivalent to

E1; while (e2) ( s; e3; )

Any of the three expressions in the for loop statement can be omitted. The absence of a second expression makes the while condition test always non-null, creating an infinite loop.

Jump Operators
Unconditional jump operator. There are four types of jump statements in C: goto, continue, break, and return.
The goto statement looks like this:
goto<метка>;
The identifier must point to a label located in the current function. Control is transferred to the marked operator.
The continue statement can only appear within a loop iteration and forces the remaining steps to be skipped at the current location and proceed to the next step of the loop. That is, in each of the statements

While (expression) ( /* ... */ continue; ) do ( /* ... */ continue ; ) while (expression); for (optional-expr; optexp2; optexp3) ( /* ... */ continue ; )

The break statement is used to exit a for, while, do, or switch loop. Control is transferred to the operator following the one in which the action was interrupted.
The function returns to the location from which the value was called using the return statement. Return is followed by an expression whose value is returned to the place from which it was called. If a function does not contain a return statement, then it is equivalent to return without an expression. In any case, the return value is undefined.
Operator Application Order in C89

  • () ->. + + - (CAST) Postfix operators
  • + + - * & ~! + - SizeOf unary operators
  • * /% Multiplicative Operators
  • + - Additive operators
  • << >> Shift operators
  • < <= >>= Relational operators
  • =! == Equality operators
  • & Bitwise and
  • ^ Bitwise XOR
  • | Bitwise inclusive, or
  • & & Logical and
  • | | Logical or
  • ?: Conditional operator
  • = += -= *= /= %= <<= >>=
  • & = | = ^ Assignment operators =
  • , Comma operator

Data Declaration
Elementary data types
Values ​​in And header files define the ranges of the main data types. The float , double , and long double type ranges are generally mentioned in the IEEE 754 standard.

Arrays
If a variable is declared using square brackets () together with the indication, then an array is considered to be declared. Strings are arrays of characters. They end with a null character (represented in C as "\0", the null character).
Examples:
int myvector ;
char mystring ;
float mymatrix = (2.0 , 10.0, 20.0, 123.0, 1.0, 1.0)
char lexicon ; /* 10000 lines, each of which can contain a maximum of 300 characters. */
int a;
The last example creates an array of arrays, but can be treated as a multidimensional array for most purposes. To access the 12 int values ​​of array "a", you can use the following call:

a a a a
a a a a
a a a a

Signposts
If a variable is preceded by an asterisk (*) in its declaration, then it becomes a pointer.
Examples:
int *pi; /* pointer to integer */
int *api; /* array of pointers of integer type */
char **argv; pointer to pointer to char */

The value in the address is stored in a pointer variable and can be accessed in the program by calling the pointer variable with an asterisk. For example, if the first statement in the example above, *pi is an int . This is called "dereferencing" the pointer.
Another operator, & (ampersand), called the address-operator, returns the address of a variable, array, or function. So, with the above in mind
int i, *pi; /* declare an integer and a pointer to an integer */
pi =

I and *pi can be used interchangeably (at least until pi is set to something else).

Strings
To work with strings you do not need to include the library, because in the standard library C, functions are already present to process strings and blocks of data as if they were char arrays.

The most important string functions:

  • strcat(dest, source) - adds the string source to the end of the string dest
  • strchr(s, c) - finds the first turn of character c into string s and returns a pointer to it or a null pointer if c is not found
  • strcmp(a, b) - compares strings and a b (lexical order); returns negative if a b is less, 0 if equal, positive if greater.
  • strcpy(dest, source) - copies the string source to the string dest
  • strlen(st) - returns the length of the string st
  • strncat(dest, source, n) - adds a maximum of n characters from the source string to the end of the dest string; characters after the null character are not copied.
  • strncmp(a, b, n) - compares a maximum of n characters from the string and a b (lexical order); returns negative if a b is less, 0 if equal, positive if greater.
  • strncpy(dest, source, n) - copies a maximum of n characters from the source string to the dest string
  • strrchr(s, c) - finds the last instance of character c in string s and returns a pointer to it or a null pointer if c is not found

Less important string functions:

  • strcoll(s1, s2) - compare two strings according to the locale of the ordered sequence
  • strcspn(s1, s2) - returns the index of the first character in s1 , which matches any character in s2
  • strerror(err) - returns a string with an error message corresponding to the code in err
  • strpbrk(s1, s2) - returns a pointer to the first character in s1 that matches any character in s2, or a null pointer if not found
  • strspn(s1, s2) - returns the index of the first character in s1 that matches the non-character in s2
  • strstr(st, subst) - returns a pointer to the first occurrence of the string subst in st, or a null pointer if no such substring exists.
  • strtok(s1, s2) - returns a pointer to the token in s1 separated by characters in s2.
  • strxfrm(s1, s2, n) - converts s2 to s1 using the rules locale

File I/O In the C programming language, input and output are accomplished through a group of functions in the standard library. In ANSI/ISO C, those functions that are defined in header.
Three standard I/O streams
stdin standard input
stdout standard output
stderr standard error

These streams are automatically opened and closed by the runtime; they do not need to be opened explicitly.
The following example shows how a typical program for filtering letters is structured:

//Header files, for example, #include

Passing arguments to the command line
The parameters presented on the command line are passed to the C program with two predefined variables - the number of command line arguments is stored in the argc variable, and the individual arguments are stored as character arrays in the array pointer argv. So launch the program from the command line in the form
Myprog p1 p2 p3
will give results similar to (note: there is no guarantee that individual rows are contiguous):
Individual parameter values ​​can be accessed using argv , argv , or argv .

C libraries
Many features of the C language are provided by the C standard library. The "hosted" implementation provides all C libraries. (Most implementations are hosted, but some are not intended for use with the operating system). Access to the library is achieved, among other things, by standard headers through the #include preprocessor directive. See ANSI C standard libraries, GCC (GNU C Compiler).

Here are more than 200 free lessons on programming in C++. Online programming courses from scratch for beginners, which cover the basics and intricacies of the C++ programming language. Free programming training, namely a textbook with practical tasks and tests. Whether you're experienced or not, these programming lessons will help you get started creating, compiling, and debugging C++ programs in a variety of development environments: Visual Studio, Code::Blocks, Xcode, or Eclipse.

Lots of examples and detailed explanations. Perfect for both beginners (dummies) and more advanced ones. Everything is explained from scratch to the very details and it’s all absolutely free!

It also covers step-by-step game creation in C++, the SFML graphics library, and more than 70 tasks to test your programming skills. An additional bonus is.

Chapter No. 0. Introduction. Beginning of work

Chapter No. 1. C++ Basics

Chapter No. 2. Variables and basic data types in C++

Chapter No. 3. Operators in C++

Chapter No. 4. Scope and other types of variables in C++

Chapter No. 5. The order in which code is executed in a program. Loops and branches in C++

Select the C lessons that interest you:

C is a programming language that was created in 1972 by Dennis M. Ritchie. The standard of the language at that time was demonstrated in the book Ritchie co-authored with Brian Kernighan (The C Programming Language). This standard is called the K&R standard (after Kernighan and Ritchie). Any program written using K&R-C rules will be compiled successfully by any C compiler.

Programmers developing compilers for C began to modernize and develop the language. To avoid confusion, the American National Standards Institute developed the ANSI language standard in 1983. ANSI-C to this day sets the rules for the changes and development of the C language.

C is a compiled programming language. A program consists of instructions and functions that must be translated into binary codes in order to be executed by the computer hardware.
C is quite popular at the present time, this is due to such in-demand qualities as: speed, portability and structuring.

  • Speed

Among all high-level languages, C is a programming language that is closer to assembly language. Thus, many language instructions are addressed directly to the computer hardware, which is why the program runs very quickly. For this reason, C can be used to write operating systems and compilers.

Most developed C compilers generate highly optimized codes, i.e. in other words, small in size (the smaller the code the compiler generates, the more optimized it is).

  • Portability

In principle, it would make sense to write fast running programs in assembly language. But we would have to write them for each platform, since mnemonic assembly codes are adapted for microprocessor families.

C uses standard sets of keywords. Those. the program is written once for any platform, any computer with any operating system. But there are two types of compilers for the language: one for IBM, the other for Apple. But the program text itself is created once and for all.

  • Structuring

Programs written in the C language have their own structure and rules, which encourages the programmer to think logically.
The need to structure the program is only a “plus”, because it is thanks to this that a C program is very easy to design, maintain and debug.

  • Function libraries

C is a programming language that initially contains a small number of operations in its syntax. For example, the language does not have built-in operators for input and output of information, as well as operators for working with strings.

All C capabilities are provided through function libraries, some of which are supplied with the compiler. The library is a separate file attached to the compiler and containing functions for solving specific problems.







2024 gtavrl.ru.