Transfer:
Learning notes of Chapter 1 of understanding C language
Learning objectives:
Learn the third edition of "understanding C language"
Chapter I
Initial C language
Learning content:
1-1 display calculation results1-2 variables
1-3 input and display
Study time:
7-9 p.m. on October 22, 2020Study notes
1-1 display calculation results
1. calculate the sum of integers and display the results
#include //Be careful not to confuse it with studio. Studio is the abbreviation of standard I/O int main(void) { printf("%d",15+37); /*Display the sum of integers 15 and 37 in decimal numbers*/ return 0; }
Basic program and fixed code
#include
int main(void)
{
/*...*/ return 0; }
1. the notes are /... /, and the meaning you want to express can be recorded in the form of notes.
2.printf function: format output function
If you want to use the function of a function, you must implement it through function call.
ex: call the printf function to display the sum of 15 and 37
Function call: printf / function name / ("%d" / argument /, 15+37 / argument /);
The display result is 52
/*Calling this function makes a request to "display these contents", and then passes the contents to be displayed through the argument in parentheses. When there are more than two arguments, they should be separated by commas*/
/*Attention!! In principle, the statement must end with a semicolon*/
2. necessity of line feed
1-3
#include int main (void) { printf("15 The sum of and 37 is%d. /n",15+37); return 0; }
1.%d specifies that the arguments should be displayed in decimal numbers. This is the conversion specification.
2.n is a symbol representing newline, and N is a special newline character.
/*n will not be displayed in the screen, but a (invisible) line break character is entered.
! In most operating environments, after the program is executed, the input result of the program will be followed by a prompt. If you enter a newline character after a program, it will not be followed by a prompt.
Exercise 1-1
/* Write a program to calculate the result of 15 minus 37, and subtract 37 with "15 Results of -22. " Is displayed in the format of.*/ #include int main (void) { printf("15 Subtract 37 and you get%dn",15-37); return 0; }
Exercise 1-2
/* Line feed display heaven, earth and man */ #include int main(void) { printf("day n land n people n"); return 0; }
Exercise 1-3
/* Line feed means hello! Hello! bye! */ #include int main(void) { printf("Hello! nn Hello! n bye."); return 0; }
1-2 variables
1. assign integer values to two variables and display
#include int main(void) { int vx,vy; vx= 57; vy = vx + 10; printf("vx The value of is%d. n",vx); printf("vy The value of is%d. n",vy); return 0; }
int vx ; /* Variables*/
int vy ; /* Variables*/
Declare two variables.
vx = 54;
vy = vx +10;
Assign values to two variables.
2. initialization and assignment
Initialization: put values when generating variables.
Assignment: put a value into the generated variable.
Thin = indicates initialization, and bold = indicates assignment.
Exercise 1-4
/* Assign an initial value of a real value to a variable in the declaration of an int type variable (Such as 3.14 or 5.7, etc.)? */ #include int main(void) { int x = 3.14; printf("%d",x); return 0; }
1-3 input and display
1. format input function scanf
#include int main(void) { int no; pritnf("Please enter an integer:"); scanf("%d",&no); //Unlike printf, you need to use &!!! printf("You entered%d. n",no); return 0; }
Unlike the printf function, when reading with the scanf function, the variable name must be preceded by a special symbol &!
// printf("%d",no) //scanf("%d",&no)
Exercise 1-5
/* Reads an integer and displays the result of adding 12 to the integer. */ #include int main(void) { int no; printf("Please enter an integer:");scanf("%d",&no); printf("The result of adding 12 to this integer is%d. ", no + 12); return 0; }
Exercise 1-6
/* Reads an integer and displays the result of subtracting 6 from the integer */ #include int main(void) { int no; printf("Please enter an integer:");scanf("%d",&no); printf("The result of subtracting 6 from this integer is%d",no - 6); return 0; }
2. output function puts
#include int main(void) { int n1,n2; puts("Please enter two integers."); printf("Integer 1:");scanf("%d",&n1); printf("Integer 2:");scanf("%d",&n2); printf("Their sum is%d. n", n1+n2); return 0; }
The functions of puts("...") and printf("... n") are basically the same.
The puts function can sequentially output strings as arguments and wrap lines at the end.
//There can only be one argument to the puts function. The display method of symbol% is different from that of printf function.
Exercise 1-7
/* Output heaven, earth and man with puts function */ #include int main(void) { puts("day"); puts("land"); puts("people"); return 0; }
Exercise 1-8
/* Displays the product of two read integers 27 and 35. */ #include int main(void) { int a,b; puts("Please enter two integers."); printf("Integer 1:"); scanf("%d",&a); printf("Integer 2:"); scanf("%d",&b); printf("Their product is%d",a * b); return 0; }
Exercise 1-9
/* Displays the sum of the read three integers */ #include int main(void) { int a,b,c; puts("Please enter three integers"); printf("Integer 1:");scanf("%d",&a); printf("Integer 2:");scanf("%d",&b); printf("Integer 3:");scanf("%d",&c); printf("Their sum is%d",a+b+c); return 0; }
Transfer:
Learning notes of Chapter 1 of understanding C language
--Posted from Rpc