当前位置:网站首页>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


边栏推荐
- The basic syntax of mermaid in typera
- Codeforces Round #793 (Div. 2)(A-D)
- awk从入门到入土(11)awk getline函数详解
- Awk from entry to earth (8) array
- How to play dapr without kubernetes?
- deno debugger
- 老掉牙的 synchronized 锁优化,一次给你讲清楚!
- Learn nuxt js
- Educational Codeforces Round 119 (Rated for Div. 2)
- SQL statement view SQL Server 2005 version number
猜你喜欢
![Leetcode topic [array] -136- numbers that appear only once](/img/6d/f2e4b812e5dd872fbeb43732d6f27f.jpg)
Leetcode topic [array] -136- numbers that appear only once

Sequence model

L1 regularization and L2 regularization

Redis sentinel mechanism

What exactly is DAAS data as a service? Don't be misled by other DAAS concepts

C语言-入门-基础-语法-数据类型(四)

Newh3c - routing protocol (RIP, OSPF)

OpenFeign 服务接口调用

Codeforces Round #793 (Div. 2)(A-D)

4 small ways to make your Tiktok video clearer
随机推荐
2022 gas examination registration and free gas examination questions
Codeforces Round #803 (Div. 2)(A-D)
Codeforces Global Round 21(A-E)
Use Alibaba cloud NPM image acceleration
awk从入门到入土(11)awk getline函数详解
Take you to master the formatter of visual studio code
AcWing 244. Enigmatic cow (tree array + binary search)
微服務入門:Gateway網關
Snipaste convenient screenshot software, which can be copied on the screen
[BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
LinkedList in the list set is stored in order
@Role of requestparam annotation
Bishi blog (13) -- oral arithmetic test app
awk从入门到入土(14)awk输出重定向
C语言-入门-基础-语法-[变量,常亮,作用域](五)
DM8 uses different databases to archive and recover after multiple failures
Call Baidu map to display the current position
Awk from digging into the ground to getting started (10) awk built-in functions
What is inner connection and outer connection? What are the uses and benefits
Mouse over to change the transparency of web page image