[3.1] calculate the Celsius temperature corresponding to the Fahrenheit temperature of 100 ° F
#include<stdio.h> int main() { int celsius, fahr; printf("Enter Fahrenheit temperature:"); scanf_s("%d", &fahr); celsius = 5 * (fahr - 32) / 9; printf("Fahrenheit temperature is:%d,The temperature in Celsius is:%d\n", fahr, celsius); return 0; }
[example 3.2] input a lowercase letter from the keyboard and convert it into uppercase letters for output
#include<stdio.h> int main() { char c1, c2; printf("Please enter a lowercase letter:"); c1 = getchar(); printf("%c,%d\n", c1, c1); c2 = c1 - 32; printf("%c,%d\n", c2, c2); return 0; }
three point two C language statement
1. Description statement
Defines the variables used by the program
int a,b; /*Define two integer variables*/ char ch; /*Define a character variable*/
2. Expression statement
Assignment expression:
c=a+b
Expression statement:
c=a+b;
3. Branch statement
Double branch if else statement:
if(x>y) z=x; else z=y;
#include <stdio.h> int main() { int a; printf("Input integer number:"); scanf("%d",&a); if(a==1){ printf("Monday\n"); }else if(a==2){ printf("Tuesday\n"); }else if(a==3){ printf("Wednesday\n"); }else if(a==4){ printf("Thursday\n"); }else if(a==5){ printf("Friday\n"); }else if(a==6){ printf("Saturday\n"); }else if(a==7){ printf("Sunday\n"); }else{ printf("error\n"); } return 0; }
Multi branch switch statement:
#include <stdio.h> int main() { int a; printf("Input integer number:"); scanf("%d",&a); switch(a){ case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; case 3: printf("Wednesday\n"); break; case 4: printf("Thursday\n"); break; case 5: printf("Friday\n"); break; case 6: printf("Saturday\n"); break; case 7: printf("Sunday\n"); break; default:printf("error\n"); break; } return 0; }
four Circular statement
Find the sum of odd numbers within 100:
#include<stdio.h> int main() { int sum = 0, i = 1; while (i <= 99) { sum = sum + i; i = i + 2; } printf("sum=%d\n", sum); return 0; }
5. Steering statement
Turn statements include break statements, go statements, continue statements, and return statements.
6. Compound statement
The statement formed by combining several statements in sequence with a pair of {} is called compound statement. All statements except compound statements in C language end with semicolons. For example:
{ sum=sum+i; i=i+2; }
This is a compound statement.
7. Empty statement
Empty statements consist of only one semicolon. as
;
8. Function definition field call
Function is a small module to complete specific functions. It is the only seed program in C language. It often contains several functions in a C language program. Complex tasks are completed by calling these functions. Such as function:
int main(int x,int y) /*Function for finding the maximum value*/ { int z; /*Define temporary variable z*/ if(x>y) z=x; /*x>y When, the value of z is equal to x*/ else z=y; /*x<y The return value of z is equal to x*/ return z; /*End function, return z*/ }
9. Input and output
C language does not provide statements for data input and output. All input and output are realized through relevant functions provided by the system (such as scanf() and printf() functions).
three point three Data input and output
3.3.1 Output function (printf()) and input function (scanf())
1.printf() function
The general calling format is: printf ("format control string", output item table column)
The format control string is enclosed in double quotation marks to indicate the output format; while the output item table column is the data to be output, which can be constants, variables or expressions.
The format string contains two kinds of information, format control description and ordinary characters.
Format control Description: output data in the specified format. The format is the format control character starting with%. Different types of integer data adopt different format control characters. The type, form, length, decimal places, etc. of output data have been described.
int data adopts% d, float and double data adopt% f.
Ordinary characters: when outputting data, you need to output characters as they are.
[example 3.3] example of outputting integer data in specified format
#include<stdio.h> int main() { int a = 1, b = 2, c = 3; printf("a=%d,b=%d,c=%d\n", a, b, c); return 0; }
When using printf() function for data output, you should pay attention to:
1. Format characters should correspond to the types of output items one by one. Different types of data use different format characters.
2. Generally, the number of format characters and output items should be the same. If the number of format characters is more than the number of output items, the redundant format will output uncertain values; if the number of format characters is less than the number of output items, the redundant output items will not be output.
3. There may be no output item in printf() function. At this time, printf() function only outputs a string; there may also be multiple output items, and multiple output types are separated by commas. If the output type is an expression, printf() function will calculate their values from right to left and output them.
4 if you want to output "%", you should type "%%" in the printf() function
2.scanf() function
General calling format: scanf ("format control string", input item table column)
Description of format controller: int data adopts% d, float data adopts% f, and double data adopts% lf (where l is the initial letter of long, not the number 1)
Normal character: the character that needs to be input as is when entering data
Note: format characters should correspond to the type and number of input items one by one; the input items must be addresses, not variable names.
3.3.2 Input and output of integer data
Format character | meaning |
d | I / O signed integers in decimal form |
o | Input / output unsigned integer in octal form |
x,X | Input / output unsigned integer in hexadecimal form |
u | Input / output unsigned integer in decimal form |
Format character | meaning |
l | Long integer for output |
- | Align the output result to the left and fill in spaces on the right |
+ | Output symbol (plus or minus sign) |
# | It has no effect on the d and u format characters without symbols; for the o format characters, prefix 0 when outputting; for the x format words Symbol, prefixed with 0x when outputting |
m | Specifies the output width of data. When the actual number of bits of data is greater than m, it is output according to the actual number of bits When the actual bit of the data is less than m, if there is "-" in front of the output width, align the left and fill the right space, otherwise align the right and fill the left space Space |
[example 3.4] output example of integer data
#include<stdio.h> int main() { int a = 123; long int b = 32770; printf("a=%d,b=%ld\n", a, b); printf("a=%o,b=%lo\n", a, b); printf("a=%#x,b=%#lx\n", a, b); printf("a=%d,b=%ld\n", a); printf("a+b=%ld\n", a + b, b); printf("End of output!\n"); return 0; }
[example 3.5] input display of integer data
#include<stdio.h> int main() { int a, b; long int c; scanf_s("%d%d%ld", &a, &b, &c); printf("a=%d,b=%d,c=%ld\n",a,b,c); return 0; }
If the program is running, enter: 12 □ 23 □ 34 give the result as follows
If you enter 12, 23 □ 34 give the result as follows
Change the statement to "scanf ("% D,% D,% LD ", & A, & B, & C);" when the program runs, enter: 12 □ 23 □ 34 give the result as follows
[example 3.6] input / output example of integer data with modifier
#include<stdio.h> int main() { int a, b, c, d; scanf_s("%2d%3d%*d,%d%d", &a, &b, &c, &d); printf("a=%d,b=%d,c=%d,d=%d\n", a, b, c, d); printf("a=%4d,b=%-4d,c=%-4d,d=%4d\n", a, b, c, d); printf("a=%+4d,b=%+4d,c=%+4d,d=%+4d\n", a, b, c, d); return 0; }
According to the format control string "% 2d%3d%*d,%d%d" of scanf() function in the program, the compiling system will take two columns of numbers with width from the input content to make a=12, then take three columns of numbers to make b=345, then skip the following number 6789, and for the input content "123 □ 456" after comma , the compilation system is separated from the position of spaces, and assigns 123 and 456 to variables c and D respectively, so c=123 and d=456.
The printf() function in the program indicates the output width when outputting the values of variables a, b, c and d. when the actual width of the variable value is greater than the output width, it will be output according to the actual data. When the width of the variable value is less than the output width, if there is "-" before the output width, it will be left aligned and right filled with space, otherwise it will be right aligned and left filled with space. The modifier "+" between "%" and format character , indicating the symbol (plus or minus sign) of the data to be output.
3.3.3 Input and output of real data
function | Format character | meaning |
f | Output single precision or double precision numbers in decimal form (keep 6 decimal places) | |
printf | E,e | Output single precision or double precision numbers in exponential form |
g | Output single precision or double precision numbers with the shorter output width in% f,%e | |
f | Enter a single precision number as a decimal | |
scanf | E,e | Enter a single precision number as a decimal |
lf | Enter a double as a decimal | |
le | Enter a double as an exponent |
explain:
1. There can also be some modifiers between the '%' of the format string and the format characters. The modifier "#" in printf() function gives a decimal point only when the result has a decimal point for f, e and g format characters.
2. When outputting real data, you can also specify the output precision, that is, add "m.n" between the "%" of the format string and the format character, where m indicates the total number of output digits and N indicates the number of digits after the decimal point. If the actual number of output digits is greater than N, the excess part will be rounded; If the actual output bits are less than N, the insufficient part shall be supplemented with 0.
3. The precision cannot be specified when using scanf() function to input real data, that is, the format of "%. nf" is wrong.
[example 3.7] example of input and output of real data
#include<stdio.h> int main() { float f; double d; scanf_s("%f%lf", &f, &d); printf("f=%f,d=%f\n", f, d); printf("f=%e,d=%e\n", f, d); printf("f=%4.2f,d=%.3f\n", f, d); return 0; }
3.3.4 Input and output of character data
1.scanf() function and printf() function input and output character data
[example 3.8] input and output example of character data
#include<stdio.h> int main() { char a, b, c; scanf_s("%c%c%c", &a,1, &b,1, &c,1); printf("a=%3c,b=%c,c=%c\n", a, b, c); return 0; }
If the program runs, input: x □ y □ z The output results are as follows
This is because when entering character data (% c) in format, if there are no non format characters in the format control string, all entered characters are considered to be valid characters. Therefore, for the input "x □ y □ z" ”, The compiling system assigns the character 'x' to variable a, '□' to variable b and 'y' to variable c. only when the input is "xyz", can 'x', 'y' and 'z' be assigned to variables a, b and c respectively.
If the statement is changed to "scanf ("% C% C% C ", & A, & B, & C);", enter: x □ y □ z when the program runs The output results are as follows:
This is because spaces are added to the format control string characters. If non format characters need to be input as is, spaces can be used as intervals when inputting data. The compilation system will assign 'x', 'y' and 'z' to variables a, B and C respectively.
If the statement is changed to "scanf ("% 3C% 3C% * 3C% C ", & A, & B, & C);" When the program runs, enter "HowAreYou?" The output results are as follows
"% 3c" in the format control string indicates that three columns are intercepted from the input data and assigned to the corresponding variables, but the character data can only save one character, so the compilation system assigns the character 'H' to the variable A. similarly, the character 'a' is assigned to the variable b. "% * 3c" means to skip three columns of data and finally assign the character '?' to the variable c.
2. Use getchar() function and putchar() function to input and output character data
getchar() function
General call format: ch=getchar(); Read a character from the keyboard cache and assign it to Ch
putchar()
General call format: putchar (output parameter); only one character is output, and the output parameter is a character variable or character constant
[example 3.9] input a character from the keyboard and output it to the screen
1. The character obtained by getchar() function can be assigned to a character variable or integer variable, or it can be directly used as the parameter of putchar() function without assigning any value to any variable. This example can be rewritten as follows:
2. You can output the characters just received in the printf() function. For example:
three point four Mathematical library function
Common math library functions:
① Exponential function exp(x): Calculation
② Absolute value function fabs(x): calculate | x|
③ Withlog(x) as the base logarithm function: calculate lnx
④ Power function pow(x,y): Calculation
⑤ Square root function sqrt(x): Calculation
When calling a mathematical library function in a program, it is necessary to use #include<math.h> at the beginning of the program.
[example 3.10] enter the radius of a ball according to the formulaCalculate and output the volume of the ball
#include<stdio.h> #include<math.h> #define PI 3.14 int main() { float r, v; printf("Enter radius r:"); scanf_s("%f", &r); v = 4.0 / 3 * PI * pow(r, 3); printf("The volume is:%2f\n", v); return 0; }
[example 3.11] find the quadratic equation of one variable++=Root of 0, set.
#include<stdio.h> #include<math.h> #define PI 3.14 int main() { float a, b, c, x1, x2, p, q; printf("Please enter three factors:"); scanf_s("a=%f,b=%f,c=%f", &a, &b, &c); p = -b / (2 * a); q = sqrt(b * b - 4 * a * c) / (2 * a); x1 = p + q; x2 = p - q; printf("x1=%5.2f\nx2=%5.2f\n", a, b, c); return 0; }