08.1 (conspect) C programming language at a glance
Foreword: If we are talking about C in general, we will try to fill in the gaps in the topics we have already covered.
At the seminars there were questions that were related to C and C++ languages: in the first place, of course, about the notation += and -= and so on - any operation in C, whether bit or arithmetic, supports this syntax. In this case, the left part is the assignment place - the left place of the expression.
a += b/2; a = a + b/2;
- differ in the fact that if you do not include optimizations, the broadcast in 1 version is more efficient than in 2 lines. With optimization turned off C comes "silly" it turns everything it sees before it into an assembler code.
Although in the trailer it will be about the same - both designs are about the same. Let's try to look at them after compiling. But in some architectures with complex system or situation when variable is register - there can be a difference between translations of these two things.
If there is an add-on operation, this broadcast is more efficient. Symmantics +/- uveleniche a. But more importantly, in terms of efficiency, the reduction of the increase by 1.
Second question about..:
++а; а++;
- exists hardware. Let's call in one assembly instruction. There were many such architectures in the C era, for example, the architecture on which Thomsan and Wichie were working in both variants.
b = ++a; // - first assignment, then change a. b = a++ // on the contrary
Check
Let a = 5, b = 6.
Output:
11 6 12 11 13 13
A solicitation operation is an operation with a return value.
Another feature is:
c = b = а;
- it works in python, but it doesn't work in C => it records in C:
с = (b=а);
- now that's going to go to C. The arithmetic operation which is calculated starting from the 'right' as an erection to the extent and in fact we can use such That's the same trick used to base the semicolon operation In C, we rarely use it. When we write something like this:
а = b, с;
- it has a return value. The first element of the semicolon
а = b , с = 6;
- so, we'll replace both A and C. And A and C are just two operators. Also, we could rewrite is like:
а = б ; с = 6;
- just calculation difference.
this is rarely necessary. It is not recommended to use when we are not sure
a = (b,(с=6));
//а = 1,2,3,4 - diagnosis will come out.
Language C is a very kind language. In this case a = 1. Perfect language for simple commands. Nobody's gonna tell us anything. Wall - enable all warning messages - we will be told that we have comma expressions there that they use only the very first element, all other effects have no effect.
Variable C was nowhere involved in the operation. The brain is built into the C language that says you set the variable but didn't use it.
c = а , b = 6;
- the diagnostician doesn't come out, it's a valid design.
In terms of types:
Void is not a type at all in a way. Because there are no output type variables. It's used in two cases.
- 1. If we want the function not to return anything (return 'no' value), it must be void - that is, we may not write return;
- 2. Void pointer - a special pointer type where some address is stored but we don't know what type it is - for instance, malloc. It is not subject to address arithmetic. That is, we cannot add anything, we cannot take values by void* address - this operation is prohibited.
Modifier:
Modifier - a variable of two species - not overlapping:
- 1. When a global variable is declared, it is a manipulation of those very name tables - namely, statistically global variable will not get into the list of names. It is possible to refer to functions of . If we have dried up two files with global functions and variables, we can use them.
- 2. If you describe local variables as static, they are placed not on the stack but in real memory of the program being compiled.
#include <stdio.h> void fstat(void) { static int count = 1; int j = 1; printf("#%d\n", count++, j++); } int main(int argc, char *argv[]) { int i; for(i=0; i<5; i++) fstat(); return 0; }
We see the assemble code: vim static.c
Look. Here's a statistical i and dynamic j that starts on the stack when we call the fstsat function and kills when we get out of there if we hadn't initialized the garbage would be lying there. The statistical count will be initialised once during the compilation of the program, placed in memory in a special place of RAM, and it will be more and more. What will be the program output? 5 times it will print these lines... :
#1 ... 1 #2 ... 1 #3 ... 1 #4 ... 1 #5 ... 1
The statistics is initiated by the variable once at compile-time. While that local variable may still be counted in different places. There are no local labels in the assembler, so the compiler invented the name of сount.1954.
Functions:
Remember that the function can return some banners, and we can score! Scanf - return scanned fields If you want 3 fields, we entered 2 - scanf will return 2.
Initially, there was no 'void' in C - instead of 'void' there was 'int'. Functions return 'int' - we can use it, but we can not use it. The theory says: all the passes of all the parameters are passed only by value (in c++, pass by reference) in C only by value. The fact that the weight of the value you pass to the function provokes one thing: if you want your function to change a variable, you have to pass a pointer to it, not the value of this variable - for the function to write something where we want it to go - you have to pass its address!!!
So we put a &. At the beginning, because you have to pass the address.
Let's say that when we enter an array, we pass the array name to Potmo that this is the pointer in the arrays. Because of this, a pointer to a pointer appears in C - int **p - let's imagine that we wrote void findcell(int **p, int *arr, int len, int value) must find value and return this address to memory, but the void function - and it must write this address to memory into a variable. The value being rotated is not the result of the function's work. The returned errors (the error number) and if the function must change something - we must pass the pointer to the place it must change).
We want the function to find the value element if it does not return a NULL addressing error. Technically, NULL may not be 0.
#include <stdio.h> int A[10] = {1,2,3,4,5,6,7,8,9,0}; void findcell(int **p, int *arr, int len, int value) { int i; *p = NULL; for(i=0; i<len; i++) if(arr[i] == value) *p = &arr[i]; /* = arr+i */ }
int main(int argc, char *argv[]) { int *p; findcell(&p, A, 10, 7); printf(«(%d==%d\n", *p, A[6]); *p = 100500; printf(‘’%d==%d\n", *p, A[6]); return 0; }
As a result, p indicates a cell in array A. Data type inheritance reference int - no other way. We do not transfer by reference only by value. There are also three stars.
Control flow
The simplest things in C that might not be worth talking about: But since C and C++ are exactly different languages, we'll talk:
- Shape brackets - a group operator has a different meaning than in C++;
- In C: {{ - just brackets, combining several operators to put them into while, for, switch;
- That is, in if - parentheses make a composite operator that makes print return;
- And there are brackets that restrict the function;
- That is, the initialiser also has brackets - in C it is possible to combine several operators into one;
- They can be omitted when the operator is alone.
- Conditional operators - if, else
- If () - the expression inside the brackets is arithmetic;
- If this expression = 0 - false and true if it is not zero;
- The operators of the while ( pre-condition and post-condition) and for loops;
- While - in brackets the conditions after it are conditional operator or expression;
- Do {} while ()
- After do the operators are executed while the condition of truth;
- But first the loop body is executed and then the loop condition with postcondition is checked. It is not frequent in practice, but sometimes it is needed;
- Inside while - the expression if it is not 0 - is true, if 0 - is false.
- for(;;)
- 1. Classic canonical cycle diagram
- - Initialisation;
- - Checking conditions;
- - Modification.
- 1. Classic canonical cycle diagram
Many people think that a cycle for a cycle with a counter is not true.
int main(int argc, char *argv[]) { int i, j; for (I= 0; j= 1; i+j>0; scanf(«%d%d», &I, &j»)) printf(«=> %d %d/n», i, j); return 0; }
Output:
-12 -12 124 124
- All payloads are in the body of the cycle. We can choose and do.
int main(int argc, char *argv[]) { int i, j; for (i=0; j= 1; i+j>0;) { scanf(«%d%d», &I, &j»); printf(«=> %d %d %d/n», i, j, ); return 0; }
C programs:
int main(int argc, char *argv[]) { int i,j,k; for (i=k= 0; j= 1; i+j>0; k++) { scanf(«%d%d», &i, &j»); printf(«=> %d %d %d/n», i, j, k); return 0; }
Output:
23 23 => 23 23 0
We have a character in the input stream that scanf does not recognize as long as I ask for two integers on the assembler garbage scanf just returns here 0 fields entered. And we have this unrecognized symbol in the input stream.
int main(int argc, char *argv[]) { int i,j,k; for (i=k=0; j= 1; i+j>0; ) { k = scanf(«%d%d», &i, &j»); printf(«=> %d %d %d/n», i, j, k); return 0; }
int main(int argc, char *argv[]) { int i,j,k; for (i=k= 0; j= 1; i+j>0; ) { k = scanf(«%d%d», &I, &j»); if (k=0) printf(«=> %d %d %d/n», i, j, k); else print(«//%c//, getchar()); return 0; }
If we see an infinity when the program is processing an input, that's it:
switch() - unconventional conditional operator - breaks the principle of structural programming where each conditional operator should have one branch Inside the operator, the expression is an integer - it is calculated. The result of the expression will be a number, this expression calculates the further go on the label of the case which is according to the number - perform operators - no operator brackets here, as a label. That's why we should put 'break' after the operator sequence. If we don't, we get to the next one. And if no case is okay, we win, but we can put a break. 1. Line break is a separate character. 2. Until we click on line feed, the input is accumulated in the input buffer. Input a line, you can edit it. 3. You enter the characters into it as a whole number. 4. If the end of the file occurs when typing from the file or 2 ways to press specially / combination of keys ctrl + d When we work with the terminal, the managed bytes can be of different types - in particular, when we output. Control symbols - the Linux itself has processed it for us or crtl+d - a symbol with code 4, does not return 4, closes -1.
Eof is '-1' as a rule. There's a duuf link - it's for those who have understood the topic!