C language and algorithm design course experiment three: the simplest C programming - sequential programming (1)

insert image description here

1. Purpose of the experiment

(1) Master the usage method of one of the most used statements in C language - assignment statement.
(2) Master the input and output methods of various types of data, and be able to use various format conversion symbols correctly.
(3) Further master the methods of writing and debugging programs.

2. Experimental content

insert image description here

2.1. Experimental content 1: Master the correct use of various format conversion symbols through the following procedures

(1) Master the correct use of various format conversion symbols through the following procedures.

① Enter the following program:

#include <stdio.h>
int main()
{
    
    
	int a, b;
	float d, e;
	char cl, c2;
	double f, g;
	long m, n;
	unsigned int p, q;
	a = 61; b = 62;
	cl = 'a'; c2 = 'b';
	d = 3.56; e = -6.87;
	f = 3157.890121; g = 0.123456789;
	m = 50000; n = -60000;
	p = 32768; q = 40000;
	printf("a=%d,b=%d\ncl=%c,c2=%c\nd=%6.2f,e=%6.2f\n", a, b, cl, c2, d, e);
	printf("f=%15.6f,g=%15.12f\nm=%ld,n=%ld\np=%u,q=%u\n", f, q, m, n, p, q);

}

② Run the program and analyze the results.
③ On this basis, change the 10th to 14th lines of the program to

	cl = a; c2 = b;
	f = 3157.890121; g = 0.123456789;
	d = f; e = g;
	p = a = m = 50000; q = b = n = -60000;

Run the program and analyze the results.
④ Use sizeofoperators to detect how many bytes each type of data occupies in the program.
For example, the number of bytes of the int type variable a is sizeof(a)或sizeof(int), use printfthe function statement to output the length (number of bytes) of each type of variable.

3. Experimental steps

insert image description here

3.1. Sequential programming experiment topic 1: The experimental steps to master the correct use of various format conversion symbols through the following procedures

(1) Master the correct use of various format conversion symbols through the following procedures.

3.1.1, ① Enter the following program:

#include <stdio.h>
int main()
{
    
    
	int a, b;
	float d, e;
	char cl, c2;
	double f, g;
	long m, n;
	unsigned int p, q;
	a = 61; b = 62;
	cl = 'a'; c2 = 'b';
	d = 3.56; e = -6.87;
	f = 3157.890121; g = 0.123456789;
	m = 50000; n = -60000;
	p = 32768; q = 40000;
	printf("a=%d,b=%d\ncl=%c,c2=%c\nd=%6.2f,e=%6.2f\n", a, b, cl, c2, d, e);
	printf("f=%15.6f,g=%15.12f\nm=%ld,n=%ld\np=%u,q=%u\n", f, q, m, n, p, q);

}

3.1.2, ② Run the program and analyze the results.

The result of the operation is as follows

insert image description here

  • You can see g、m、n、q、pthat the output of the value is wrong, the reason is that printf("f=%15.6f,g=%15.12f\nm=%ld,n=%ld\np=%u,q=%u\n", f, q, m, n, p, q);the output f should be followed by g, so it should be as follows
	printf("f=%15.6f,g=%15.12f\nm=%ld,n=%ld\np=%u,q=%u\n\n", f, g, m, n, p, q);

The result of the operation is as follows

insert image description here

  • As you can see, the values ​​of the output variables are now completely correct.

3.1.3, ③ On this basis, change the 10th to 14th lines of the program to

	cl = a; c2 = b;
	f = 3157.890121; g = 0.123456789;
	d = f; e = g;
	p = a = m = 50000; q = b = n = -60000;

Run the program and analyze the results.

On this basis, change the 10th to 14th lines of the program to the above code, and the running result is

a=50000,b=-60000
cl==,c2=>
d=3157.89,e=  0.12
f=    3157.890121,g= 0.123456789000
m=50000,n=-60000
p=50000,q=4294907296

insert image description here

3.1.4, ④ Use sizeofoperators to detect how many bytes each type of data occupies in the program.

For example, the number of bytes of the int type variable a is sizeof(a)或sizeof(int), use printfthe function statement to output the length (number of bytes) of each type of variable.

The code to use sizeofoperators to detect how many bytes each type of data occupies in the program is as follows

	printf("sizeof(a)=%d字节,sizeof(b)=%d字节\n", sizeof(a), sizeof(b));
	printf("sizeof(c1)=%d字节,sizeof(c2)=%d字节\n", sizeof(cl), sizeof(c2));
	printf("sizeof(f)=%d字节,sizeof(g)=%d字节\n", sizeof(f), sizeof(g));
	printf("sizeof(m)=%d字节,sizeof(n)=%d字节\n", sizeof(m), sizeof(n));
	printf("sizeof(p)=%d字节,sizeof(q)=%d字节\n", sizeof(p), sizeof(q));

The result of using sizeofoperators to detect how many bytes each type of data occupies in the program is as follows

insert image description here

4. Experimental summary

insert image description here

Through this experiment: C language and algorithm design course experiment three: the simplest C programming - 4 topics of sequential programming, mastered the following points.

-(1) Master the usage method of one of the most used statement-assignment statement in C language.
-(2) Master the methods of input and output of various types of data, and be able to use various format conversion symbols correctly.
-(3) Further master the methods of writing and debugging programs.

5. The complete procedure of the experiment

insert image description here

5.1. Sequential programming experiment topic 1: Master the complete program of the correct use of various format conversion symbols through the following program

The complete program is as follows

#include <stdio.h>
int main()
{
    
    
	int a, b;
	float d, e;
	char cl, c2;
	double f, g;
	long m, n;
	unsigned int p, q;
	a = 61; b = 62;

	cl = 'a'; c2 = 'b';
	d = 3.56; e = -6.87;
	f = 3157.890121; g = 0.123456789;
	m = 50000; n = -60000;
	p = 32768; q = 40000;

	//cl = a; c2 = b;
	//f = 3157.890121; g = 0.123456789;
	//d = f; e = g;
	//p = a = m = 50000; q = b = n = -60000;

	printf("a=%d,b=%d\ncl=%c,c2=%c\nd=%6.2f,e=%6.2f\n", a, b, cl, c2, d, e);
	/*printf("f=%15.6f,g=%15.12f\nm=%ld,n=%ld\np=%u,q=%u\n", f, q, m, n, p, q);*/

	printf("f=%15.6f,g=%15.12f\nm=%ld,n=%ld\np=%u,q=%u\n\n", f, g, m, n, p, q);

	printf("sizeof(a)=%d字节,sizeof(b)=%d字节\n", sizeof(a), sizeof(b));
	printf("sizeof(c1)=%d字节,sizeof(c2)=%d字节\n", sizeof(cl), sizeof(c2));
	printf("sizeof(f)=%d字节,sizeof(g)=%d字节\n", sizeof(f), sizeof(g));
	printf("sizeof(m)=%d字节,sizeof(n)=%d字节\n", sizeof(m), sizeof(n));
	printf("sizeof(p)=%d字节,sizeof(q)=%d字节\n", sizeof(p), sizeof(q));
}

insert image description here

Guess you like

Origin blog.csdn.net/m0_47419053/article/details/128522712