C Language Programming Chapter III summary

Example 3.1 calculate the Celsius temperature corresponding to Fahrenheit temperature ...

Example 3.1 calculate the Celsius temperature corresponding to Fahrenheit temperature

#include<stdio.h> int main(void) { 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; }

Enter Fahrenheit 80

  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; }

Please enter a lowercase letter A

  C language statement

In C language, a statement is a character sequence ending with a semicolon. It is the most basic execution unit of a program. Semicolon is a necessary part of C statement.

1. Description statement (used to define the variables and types used by the program)

2. Expression statement (used to define the variables and types used by the program)

3. Branch statements (implement the branch control process and execute different statements according to whether the conditions are true or not)
There are two branch structures, i.e. double branch if eles; switch statement

4. Sequential statements (there are three kinds of statements in c language to realize loop control, namely, for statement and do while statement)

5. Turn statements (turn statements include break statement, goto statement, continue statement and return statement)

6. Compound statement (the statement formed by sequentially combining several statements with a pair of braces {} is called compound statement, and all statements in c language end with semicolons except compound statements)

7. Empty statement (empty statement only consists of a semicolon)

8. Function definition and call (int max(int x,int y) is the function definition header, which is composed of function return value type, function name and parameter table)

9. Input and output (all inputs and outputs are realized through relevant functions provided by the system (such as scanf() and printf() functions)

Data input and output

Output function (printf()) and input function (scanf())

1.printf() function

(1) Format control Description: output data in the specified format. The format is format control characters starting with%. Different types
Type data adopts different format control characters to explain the type, form, length, decimal places, etc. of output data
For example, int type data adopts% d,float type and double type data adopt% f.
(2) Normal character: the character that needs to be output as is when outputting data.

Example 3.3

#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; }

  scanf() function

(1) Format control description

(2) Ordinary character

Input and output of integer data

Format charactermeaningdI / O signed integers in decimal formo

Enter / enter unsigned integer in octal form   

x,XInput / output unsigned integer in hexadecimal formuInput / output unsigned integer in decimal form




Example 3.4

#include<stdio.h> int main() { int a = 123; long int b = 32770; printf("a=%d,b=%1d\n",a,b); printf("a=%o,b=%1o\n", a, b); printf("a=%#x,b=%#1x\n", a, b); printf("a=%d,b=%1d\n", a); printf("a+b=%1d\n", a+b, b); printf("Output end!\n"); return 0; }

  Example 3.5

#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; }

  Example 3.6

#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; }

  Input and output of real data

  Example 3.7

#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; }

  Input and output of character data

1. Use scanf() function and printf() function to input and output character data

Example 3.8

#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; }

  2. Use getchar() function and putchar() function to input and output character data

(1) getchar() function.

(2) putchar() function.

Example 3.9   Input a character from the keyboard and output it to the screen

#include<stdio.h> int main() { char ch; ch = getchar(); putchar(ch); return 0; }

Input 7

  Mathematical library function

Example 3.10   Enter the radius of a ball as 9, and calculate and output the volume of the ball according to the formula

#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("Volume is:%.2f\n", v); return 0; }

  Example 3.11   Find the root of the quadratic equation *************************

#include<stdio.h> #include<math.h> int main() { float a, b, c, x1, x2, p, q; printf("Please enter three coefficients:"); 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; }

7 November 2021, 19:14 | Views: 5898

Add new comment

For adding a comment, please log in
or create account

0 comments