Chapter III summary

Keywords: C

3.1 cited examples

[example 3.1] calculate the Celsius temperature corresponding to the Fahrenheit temperature of 100 ° F. Calculation formula:

                           c=

Where: represents the temperature in Celsius and f represents the temperature in Fahrenheit.

#include<stdio.h>
int main(void)
{
	/*Define 2 integer variables. celsius represents celsius temperature and fahr represents Fahrenheit temperature*/
	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);


}

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

  3.2 statements in C language

1. Description statement: the description statement is used to define the variables and types used by the program.

2. Expression statement: an expression statement consists of an expression plus a semicolon ".

3. Branch statement: the branch statement implements the branch control process and executes different statements according to whether the conditions are true or not. There are two branch structures: double branch if else statement and multi branch switch statement.

4. Loop statement: there are three statements in C language to realize loop control, namely, for statement, while statement and do while statement.

5. Turn statements: turn statements include break statements, continue statements, and return statements.

6. Compound statement: the statement formed by combining several statements in sequence with a pair of braces {} is called compound statement. In C language, all statements except compound statements end with semicolons.

7. Empty statement: an empty statement consists of only one semicolon structure. Empty statements are generally used in a certain position of the program and do not perform any actual operation.

8. Function definition and call: function is a small module to complete specific functions. It is the only program in C language. A C program often contains several functions. Complex tasks are completed by calling these functions.

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).

3.3 data input and output

3.31 output function (printf()) and input function (scanf())

1.printf() function

The format control string contains two kinds of information, format control description and ordinary characters.

(1) Format control Description: output data in the specified format. The format is the format control character starting with%. Different types of data adopt different format control characters to describe the type, form, length, decimal places, etc. of output data.

(2) Normal character: the character that needs to be output as is when outputting data.

[example 3.3] an example of outputting integer data in a 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);
}

  2.scanf() function

(1) Format control Description: input data in the specified format. The format is the format control character starting with%. Different types of data adopt different format control characters.

(2) Normal character: when inputting data, you need to input characters as they are.

3.3.2 input and output 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("Output results n");
	return 0;
}

 

  [example 3.5] input example 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;
}

[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);
		printf("a=%+4d,b=%+4d,c=%+4d,d=%+4d\n", a, b, c, d);
		return 0;
}

3.3.3 input and output of real data

 

 

  [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("%4,2f,d=%.3f\n", f, d);
	return 0;
}

  3.3.4 input and output of 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, &b, &c);
	printf("a=%3c,b=%c,c=%c\n", a, b, c);
	return 0;
}

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

3.4 mathematical library function  

Common mathematical functions are:

1. Exponential function exp(x): Calculation.

2. Absolute value function fabs(x): calculate | x |

3. Logarithm function loge (x) based on e: calculate lnx.

4. Power function pow(x,y): Calculation.

5. Square root function sqrt(x): Calculation.

[example 3.10] enter the radius of a ball according to the formulaCalculate and output the volume of the ball.

#include<stdio.h>
#include<stdio.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;
}

#include<stdio.h>
#include<stdio.h>
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);
		x1 = p + q; x2 = p - q;
		printf("x1=%5.2f\nx2=%5.2f\n", a, b, c);
		return 0;
}

 

 

Posted by olivarespablo on Fri, 05 Nov 2021 13:47:32 -0700