c getting started 1
- Header file include < stdio. H >
- int main(){}
- gcc compile-o changed to executable name
c getting started 2
2.1 basic data types: character type, integer type and floating point type
- 2.1.1 character type. A single character needs to be enclosed in single quotation marks and placeholder% c
- 2.1.2 integer,% d
- 2.1.3 floating point,% f
2.2 operators: arithmetic operators, relational operators and logical operators
- 2.2.1 arithmetic operator: + - * /% remainder
*/% priority greater than +-
Note:
23% 10 single digits
Ten digits of 23 / 10
- 2.2.2 relational operators = =! = > < > =<=
- 2.2.3 logical operator & & and (both true are true) | or (one true is true)! Non (reverse)
Priority:! > Arithmetic operators > relational operators > & & >||
2.3 variables
- 2.3.1 type variable name
- 2.3.2 naming rules:
- Composed of letters, numbers and underscores
- You can't start with a number
- Case sensitive
- The keyword "auto break..." in c language cannot be used
- 2.3.3 variables must be initialized before use!!! (definition and assignment)
Three elements: type, variable name and value - 2.3.4 expression (computable formula must have result)
- 2.3.5 input
int n; int a,b; scanf ("%d %d %d",&n,&a,&b);
If the remainder is taken, only when the placeholder is written two%%, one will be displayed without error, such as printf ("% d%%% d \ n", a, b);
- 2.5.6 ++n
- It's a formula, and it's worth it
- It changes the value of variable n
Note: n + + suffix self increasing / N – the value of self decreasing expression is the original value. After calculation, the value of variable will be automatically increased by one
Prefix self increment (+ + n) | Prefix subtraction (– n) | Suffix self increment (n + +) | Suffix subtraction (n –) | |
---|---|---|---|---|
The value of the expression | n+1 | n-1 | n | n |
Value of variable | n+1 | n-1 | n+1 | n-1 |
1==n can avoid writing one less = 1=n will u report an error, left value and right value!!! To be added
stay vim If you want to make up, you can press ctrl+p
c getting started 3
-
Switch case: case needs to be followed by integer constant; Break is required for each condition. If there is no break, it will continue to be executed, which is equivalent to the same discrimination method as below;
-
3.2.1 while(){}
Important examples:
#include <stdio.h> int main(){ //Find all integers between mn /* int n,m; printf("input n,m:"); scanf("%d%d",&n,&m); while(n<m){ printf("%d ",++n); } printf("\n"); */ int n; scanf("%d",&n); //Enter an integer n and find the number of digits of this number. For example, the number of digits of 123 is 3 int count =0; while(0!=n){ n/=10; count++; } printf("%d\n",count); //Enter an integer n and find the sum of each integer int m; scanf("%d",&m); int sum=0; while(0!=m){ sum+=m%10; m=m/10; } printf("sum=%d\n",sum); //Enter an integer to reverse the number bit by bit. int a; scanf("%d",&a); int sum2=0; while(0!=a){ sum2=sum2*10+a%10; a/=10; } printf("sum2=%d\n",sum2); }
- 3.2.2
Difference between do while and while (send proposition):
Do while is to cycle before judgment, and the loop body is executed at least once.
while is to judge before execution. The loop body may not be executed at all. - 3.2.3 for statement
For (initial value; condition; increment or decrement) {}
Example:
#include<stdio.h> int main(){ int n; scanf("%d",&n); int sum=0; int res=1; for(int i=0;i<n;++i){ int m; scanf("%d",&m); sum+=m; res*=m; } printf("sum=%d\n",sum); printf("multi=%d\n",res); printf("average=%d\n",sum/n); }
- 3.2.4 break and continue statements
In while and for, break is to end the whole loop body, and continue is to end a single loop
Example code:
#include<stdio.h> int main(){ //Print the first ten ordinary years of the 21st century int count =10; for(int i=2000;i<2100;++i){ if(i%4==0&&i%100!=0||i%400==0)continue; printf("%d\n",i); --count; if(0==count)break; } * //Print the first ten leap years of the 21st century int count2 =10; for(int i=2000;i<2100;++i){ if(i%4==0&&i%100!=0||i%400==0){ printf("%d\n",i); --count2; if(0==count2)break; } } }
- 3.3.1 there is only one statement in if, while and for, and large brackets can be omitted
- 3.3.2 ternary operator:?
days=(year%4==0&&year%100!=0||year%400==0)?29:28;