C language and algorithm design course experiment 1: the operating environment of C program and the method of running C program

insert image description here

1. Purpose of the experiment

  • (1) Understand the basic operation methods of the computer system used, and learn to use the system independently.
  • (2) Understand how to edit, compile, link and run a C program on this system.
  • (3) Get a preliminary understanding of the characteristics of the C source program by running a simple C program.
    insert image description here

2. Experimental content

  • (1) Check whether the computer system used has installed the C compilation system and determine its subdirectory.

  • (2) Enter the C compiler integration environment used.

  • (3) Familiar with the interface of the integrated environment and how to use the relevant menus.

2.1, input and run a simple correct program

  • ① Enter the following program
# include <stdio. h>
int main()
{
    
    
	printf ("This is a e program. \n"); 
	return 0;
}
  • ② Carefully observe the entered program on the screen. Check for errors.

  • ③Compile the source program according to the method introduced in Part 3 of this book, and observe the compilation information displayed on the screen. If
    "error message" appears, you should find out the reason and correct it, then compile, if there is no error, connect.

  • ④ If there is no error in compiling and connecting, run the program and observe and analyze the running results.

2.2. Input and edit a C program with errors

  • ①Enter the following program (Example 1.2 in Chapter 1 of Mincai, intentionally missed or mistyped a few characters).
#include <stdio.h>

int main()
{
    
    
	int a, b, sum
	a = 123; b = 456;
	sum = a+b
	print("sum is %d\n", sum);
	return 0;
}
  • ②Compile, carefully analyze the compilation information window, there may be multiple errors displayed, modify them one by one until no error occurs
    . Finally, please compare it with the program in the textbook.
  • ③ Run the program and analyze the running results.

2.3. Enter and run a program that requires data input at runtime

  • ① Enter the following program:
#include <stdio.h>

int main()
{
    
    
	int max(int x, int y);
	int a, b, c;
	printf("input a & b:");
	scanf("%d,%d", &a, &b);
	c = max(a, b);
	printf("max=%d\n", c);
	return 0;
}

int max(int x, int y)
{
    
    
	int z;
	if (x > y) z = x;
	else z = y;
	return (z);
}
  • ②Compile and run, input the integers 2 and 5 from the keyboard at runtime, and then press the "Enter" key to observe the running results.
  • ③ Change line 4 in the program to
int a; b; c;

Then compile and observe the result.

  • ④ Combine the 3rd and 4th lines in the max function into one line, that is
if (x > y)z = x; else z = y;

Compile and run, and analyze the results.

2.4. Run a program written by yourself

The topic is Question 6 of Chapter 1 of the textbook. That is, input a, b, c 3 values, and output the largest one.

  • ① Input the source program written by oneself.
  • ② Check the program for errors (including grammatical errors and logical errors), and correct them if any.
  • ③Compile and connect, carefully analyze the compiled information, if there is any error, find out the reason and correct it.
  • ④ Run the program, input data, and analyze the results.
  • ⑤ Modify the program by yourself (for example, deliberately change it to a wrong one), and analyze its compilation and operation.
  • ⑥Save the debugged program in your own user directory, and the file name can be customized.
  • ⑦Clear the editing window, read the file again, and check whether the content in the editing window is the program just saved.
  • ⑧Close the VisualC++ integrated environment used. Use "My Computer" in Windows to find the user subdirectory you just used, browse the files in it, and see if there are any files with the suffix .c and .exe that you just saved.

3. Experimental steps

insert image description here

3.1. Enter and run a simple correct program

  • ① Enter the following program
# include <stdio. h>
int main()
{
    
    
	printf ("This is a e program. \n"); 
	return 0;
}
  • ② Carefully observe the entered program on the screen. Check for errors.

The program has no errors.

已启动重新生成…
1>------ 已启动全部重新生成: 项目: 1-C程序的运行环境和运行C程序的方法, 配置: Debug Win32 ------
1>Main.c
1>1-C程序的运行环境和运行C程序的方法.vcxproj -> E:\Document\2-programmLanguageExper\C\1-课程-C语言及算法设计实验\C语言课程实验\Debug\1-C程序的运行环境和运行C程序的方法.exe
========== 全部重新生成: 成功 1 个,失败 0 个,跳过 0==========

1 successful, 0 failed, 0 skipped

insert image description here

  • ③Compile the source program according to the method introduced in Part 3 of this book, and observe the compilation information displayed on the screen. If
    "error message" appears, you should find out the reason and correct it, then compile, if there is no error, connect.
  • ④ If there is no error in compiling and connecting, run the program and observe and analyze the running results.

The result of program compilation and operation is as follows

insert image description here

3.2. Input and edit a C program with errors

  • ①Enter the following program (Example 1.2 in Chapter 1 of Mincai, intentionally missed or mistyped a few characters).
#include <stdio.h>

int main()
{
    
    
	int a, b, sum
	a = 123; b = 456;
	sum = a+b
	print("sum is %d\n", sum);
	return 0;
}

The following error message appears when compiling

insert image description here

  • ②Compile, carefully analyze the compilation information window, there may be multiple errors displayed, modify them one by one until no error occurs
    . Finally, please compare it with the program in the textbook.

The modified program is as follows

#include <stdio.h>

int main()
{
    
    
	int a, b, sum;
	a = 123; b = 456;
	sum = a + b;
	printf("sum is %d\n", sum);
	return 0;
}
  • ③ Run the program and analyze the running results.

The result of running the correct program is as follows

insert image description here

3.3. Enter and run a program that requires data input at runtime

  • ① Enter the following program:
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

int main()
{
    
    
	int max(int x, int y);
	int a, b, c;
	printf("input a & b:");
	scanf("%d,%d", &a, &b);
	c = max(a, b);
	printf("max=%d\n", c);
	return 0;
}

int max(int x, int y)
{
    
    
	int z;
	if (x > y) z = x;
	else z = y;
	return (z);
}
  • ②Compile and run, input the integers 2 and 5 from the keyboard at runtime, and then press the "Enter" key to observe the running results.

Compile and run results

The compilation results are as follows

已启动重新生成…
1>------ 已启动全部重新生成: 项目: 1-C程序的运行环境和运行C程序的方法, 配置: Debug Win32 ------
1>Main.c
1>Main2.c
1>Main3.c
1>正在生成代码...
1>1-C程序的运行环境和运行C程序的方法.vcxproj -> E:\Document\2-programmLanguageExper\C\1-课程-C语言及算法设计实验\C语言课程实验\Debug\1-C程序的运行环境和运行C程序的方法.exe
========== 全部重新生成: 成功 1 个,失败 0 个,跳过 0==========

  • 1 succeeded, 0 failed, 0 skipped.

The result of the operation is as follows

insert image description here

  • The result of the program running is correct, and the maximum value of the two numbers 2 and 5 can be found.
  • ③ Change line 4 in the program to
int a; b; c;

Then compile and observe the result.

Change line 4 in the program to int a; b; c;compile and run the result afterward, as shown below.

insert image description here

  • The program appears:error C2065: “c”: 未声明的标识符

  • int a; b; c;: Indicates that the b and c variables are not defined, but the a variable is defined.

  • ④ Combine the 3rd and 4th lines in the max function into one line, that is

if (x > y)z = x; else z = y;
  • Compile and run, and analyze the results.

Combine the 3rd and 4th lines in the max function and write them into one line to compile and run the results as follows

insert image description here

  • It is correct to combine the 3rd and 4th lines in the max function into one line to compile and run the results.

3.4. Run a program written by yourself

The topic is Question 6 of Chapter 1 of the textbook. That is, input a, b, c 3 values, and output the largest one.

  • ① Input the source program written by oneself.

The source program written is as follows

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>

int maxFn(int a, int b, int c);

int main()
{
    
    
	system("color 3E");

	int a, b, c, max;
	printf("input a & b & c:");
	scanf("%d%d%d", &a, &b, &c);

	max = maxFn(a, b, c);
	printf("max=%d\n", max);

	system("pause");
	return 0;
}


int maxFn(int a, int b, int c)
{
    
    
	int max;

	max = a;
	
	if (max < b)
	{
    
    
		max = b;
	}	 
	
	if(max < c)
	{
    
    
		max = c;
	}

	return max;
}
  • ② Check the program for errors (including grammatical errors and logical errors), and correct them if any.
  • ③Compile and connect, carefully analyze the compiled information, if there is any error, find out the reason and correct it.
  • ④ Run the program, input data, and analyze the results.

Compile and run the program as follows

insert image description here

  • ⑤ Modify the program by yourself (for example, deliberately change it to a wrong one), and analyze its compilation and operation.
  • ⑥Save the debugged program in your own user directory, and the file name can be customized.
  • ⑦Clear the editing window, read the file again, and check whether the content in the editing window is the program just saved.
  • ⑧Close the VisualC++ integrated environment used. Use "My Computer" in Windows to find the user subdirectory you just used, browse the files in it, and see if there are any files with the suffix .c and .exe that you just saved.

The .c and .exe files are as follows

insert image description here
insert image description here

4. Experimental summary

insert image description here

Through the course experiment of C language and algorithm design: the operating environment of C program and the method of running C program,
I have mastered the following points

  • (1) Understand the basic operation methods of the computer system used, and learn to use the system independently.
  • (2) Understand how to edit, compile, link and run a C program on this system.
  • (3) Get a preliminary understanding of the characteristics of the C source program by running a simple C program.

Five, the complete code of the experiment

5.1, Main1.c source file

Main1.c source file code is as follows

#include <stdio.h>
#include <stdlib.h>

int main()
{
    
    
	system("color 3E");

	printf("This is a e program. \n");

	system("pause");
	return 0;
}

5.2, Main2.c source file

Main2.c source file code is as follows

#include <stdio.h>

int main()
{
    
    
	int a, b, sum;
	a = 123; b = 456;
	sum = a + b;
	printf("sum is %d\n", sum);
	return 0;
}

5.3, Main3.c source file

Main3.c source file code is as follows

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

int main()
{
    
    
	int max(int x, int y);
	int a, b, c;
	printf("input a & b:");
	scanf("%d,%d", &a, &b);
	c = max(a, b);
	printf("max=%d\n", c);
	return 0;
}

int max(int x, int y)
{
    
    
	int z;
	/*if (x > y) z = x;
	else z = y;*/

	if (x > y)z = x; else z = y;
	return (z);
}

5.4, ​​MaxDigit.c source file

The source file code of MaxDigit.c is as follows

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>

int maxFn(int a, int b, int c);

int main()
{
    
    
	system("color 3E");

	int a, b, c, max;
	printf("input a & b & c:");
	scanf("%d%d%d", &a, &b, &c);

	max = maxFn(a, b, c);
	printf("max=%d\n", max);

	system("pause");
	return 0;
}


int maxFn(int a, int b, int c)
{
    
    
	int max;

	max = a;
	
	if (max < b)
	{
    
    
		max = b;
	}	 
	
	if(max < c)
	{
    
    
		max = c;
	}

	return max;
}

insert image description here

Guess you like

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