当前位置:网站首页>C language - Introduction - Foundation - syntax - [main function, header file] (II)
C language - Introduction - Foundation - syntax - [main function, header file] (II)
2022-07-04 08:49:00 【Hu Anmin】
Generally speaking ,C A program of language , It is composed of header file and main function .
The header file
The following code is the header file of our program .
#include <stdio.h>
This is a preprocessing instruction , Tell the compiler to do some preprocessing before running . Generally speaking, it means stdio.h The contents of are imported into the first line intact .
If we want to realize some basic functions , for instance printf() Output , Then we must write this code at the beginning , Otherwise the program will go wrong . There is more than one header file
such as :
- ctype.h Define character processing functions , For example, judge whether the character is a blank character 、 Character case conversion ;
- math.h Define data functions , For example, calculate the value of trigonometric function 、 exponential 、 logarithm 、 The absolute value 、 square 、 integer 、 To search for redundancy, etc ;
- stdio.h Define various inputs and outputs , Including standard input and output 、 File read and write 、 Format input and output, etc ;
- stdlib.h Define some general functions , For example, memory allocation and release 、 String and number types are converted to each other 、 Random function 、 Sort 、 Find functions, etc ;
- string.h Define string handler , For example, find the length of the string 、 String copy 、 String comparison 、 String lookup, etc ;
- time.h Define time and date processing functions , For example, get the current time .
notes : The above are commonly used standard library header files , Other follow-up time-consuming introductions .
#include The difference between the two ways of writing instructions
#incldue <stdio.h>Represents the address defined by the standard library in the system pathstdio.hfile ;
-#include "stdio.h"Means to search for customized... Under the current program directory first stdio.h The header file , If you can't find it , Then go to the system path to find ;- If you write your own header file ,
Use double quotes "", If it is a header file defined by a standard library or system , UseAngle brackets <>.
What is the main function ?

First , The main function main, A standard main function is as follows :
int main() // This is the main function
{
return 0; // The return value of the main function
}
main Translation into Chinese is the main 、 The most important meaning , And in the C The language represents a main function .( Later we will discuss what is a function in computer language , And the main function in C The meaning of existence in language programs .) After the double slash is the comment , Make a more vivid analogy , The notes are similar to the teacher's notes on the exercise book .
Generally speaking , Comments are used to mark the purpose of this code or explain ideas . Because comments are not compiled as code , So no matter what comments are added , Will not have any impact on the actual operation of the code .
In the code above , Annotate where the main function is , Where is the return value of the function .
What is a function ?
Let's first talk about what is a function ? In people's perception , Function is a noun in the field of Mathematics , It may be as shown in the figure below :
however , This is just a function in the field of Mathematics , It is completely different from the functions in the programming language .
In programming languages , You can think of a function as a box , This box has the following features :
- At the beginning of execution , Function can be input with some values
- In the process of execution , Functions can do some things
- After execution , Function can return some values

Let's take a look at our main function , Here 3 Features , What did they do .

among ,int Indicates the return value type of the function ,int yes integer( Integers ) Abbreviation .main Is the function name ,main The brackets that follow () Inside is the input parameter , It is currently empty .return Followed by the return value of the function , by 0. and 0 It's an integer , And before the function name int Corresponding .
Let's summarize the formula of function :
Function return value type Function name ( Function input parameter value )
{
Do something
return Function return value ;
}
Write your own function
We might as well strike while the iron is hot , According to the above formula of function writing , Write a function that adds two integers . This function needs to do : Enter two integers , Return the result of their addition . Since this function is used to calculate addition , We name the function add. Of course, the function name of the custom function can be written according to your preferences , Even if it's written as aaaaa It's OK . however , For function names to have semantics , It is convenient for people to read and understand , We usually use English as the function name .
// This piece of code is called add Function definition of function
int add(int a, int b)
{
return a+b;
}

well , Then we finished writing a add Function . This piece of code is called add Function definition of function .
The main function is C The entrance to language programs
Above, we have defined one by ourselves add function , How do we use it ?add Functions can run directly ?, The answer is No .
be-all C Language codes have a starting entry , And this entry is the main function main. After entering the main function , To call other functions through the main function .
It also means that , Every C The language code , There can only be one main function . Let's modify the code a little , Now the code is as follows .
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main()
{
int result;
result = add(2, 3);
printf("%d", result);
return 0;
}
When the program is running , First, we will enter the main function main. Then call what we just wrote add Function . We passed on 2 The values are integers 2 and 3 Of add function . The definition of function stipulates , Pass on 2 Parameters , When we call , It must also be passed on 2 individual , And the type also needs to be consistent , Otherwise the compilation will report an error .
So naturally , We will think , Who called the main function ? The return value of the main function must be int Do you ? The main function is called automatically at the beginning of the program , There is no need to actively call the main function in the program . The return value of the main function will be returned to the program calling this program .C The language standard stipulates that the main function has a return value and must be int. If the program ends normally , Generally, the return value is set to 0.
Call function , You must know the function first
Let's see , What does the compiler mean add Of this identifier . The compiler starts with code , Read the code from top to bottom . The compiler first sees the definition of a function , Describes a man called add Function of . next , stay main Required in add, Because the compiler already knows add The definition of , Therefore, the compiler can compile normally .
however , What if the function definition and function call are reversed ? First , The compiler sees add identifier , The compiler will be confused ,add What is it? ? The compiler cannot understand add What is it . therefore , The compiler will report an error , And stop compiling .
Let's see how it works in software 
summary : That is to say c All functions in must be declared in Main Function


边栏推荐
- AI Winter Olympics | is the future coming? Enter the entrance of the meta universe - virtual digital human
- 微服务入门:Gateway网关
- SSRF vulnerability exploitation - attack redis
- Comparison between sentinel and hystrix
- How college students choose suitable computers
- 4 small ways to make your Tiktok video clearer
- Codeforces Round #803 (Div. 2)(A-D)
- awk从入门到入土(11)awk getline函数详解
- Display Chinese characters according to numbers
- awk从入门到入土(18)gawk线上手册
猜你喜欢

DM8 command line installation and database creation
](/img/89/0f5f4ba07c637b09abe873016cba2d.png)
C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)

Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service

ES6 summary
](/img/dc/5c8077c10cdc7ad6e6f92dedfbe797.png)
C语言-入门-基础-语法-[变量,常亮,作用域](五)

System disk expansion in virtual machine

Comparison between sentinel and hystrix

随机事件的关系与运算

Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service

How does Xiaobai buy a suitable notebook
随机推荐
FRP intranet penetration, reverse proxy
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
Bishi blog (13) -- oral arithmetic test app
C#实现一个万物皆可排序的队列
awk从入门到入土(14)awk输出重定向
Awk from entry to soil (5) simple condition matching
C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,Gauss-Jordan消去法,源代码
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
go-zero微服务实战系列(九、极致优化秒杀性能)
学习Nuxt.js
AcWing 244. Enigmatic cow (tree array + binary search)
Educational Codeforces Round 119 (Rated for Div. 2)
C语言-入门-基础-语法-[主函数,头文件](二)
Leetcode topic [array] -136- numbers that appear only once
What sparks can applet container technology collide with IOT
Codeforces Round #803 (Div. 2)(A-D)
What is inner connection and outer connection? What are the uses and benefits
awk从入门到入土(15)awk执行外部命令
manjaro安装微信
小程序容器技术与物联网 IoT 可以碰撞出什么样的火花