当前位置:网站首页>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
- awk从入门到入土(4)用户自定义变量
- How to play dapr without kubernetes?
- How can we make a monthly income of more than 10000? We media people with low income come and have a look
- 到底什么才是DaaS数据即服务?别再被其他DaaS概念给误导了
- [untitled] forwarding least square method
- 09 softmax regression + loss function
- Industry depression has the advantages of industry depression
- ArcGIS application (XXII) ArcMap loading lidar Las format data
- CLion-控制台输出中文乱码
猜你喜欢

C, Numerical Recipes in C, solution of linear algebraic equations, Gauss Jordan elimination method, source code

地平线 旭日X3 PI (一)首次开机细节

DM database password policy and login restriction settings

Educational Codeforces Round 115 (Rated for Div. 2)

CLion-控制台输出中文乱码

How to solve the problem of computer jam and slow down

Horizon sunrise X3 PI (I) first boot details

How to choose solid state hard disk and mechanical hard disk in computer

Redis sentinel mechanism

Démarrage des microservices: passerelle
随机推荐
1211 or chicken and rabbit in the same cage
DM8 tablespace backup and recovery
A subclass must use the super keyword to call the methods of its parent class
Learn nuxt js
2022 electrician (intermediate) examination question bank and electrician (intermediate) examination questions and analysis
Snipaste convenient screenshot software, which can be copied on the screen
MySQL relearn 1-centos install mysql5.7
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
Four essential material websites for we media people to help you easily create popular models
What sparks can applet container technology collide with IOT
上周热点回顾(6.27-7.3)
FRP intranet penetration, reverse proxy
es6总结
Awk from entry to earth (18) GAW K line manual
C语言-入门-基础-语法-[运算符,类型转换](六)
OpenFeign 服务接口调用
How to solve the problem of computer jam and slow down
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
Live in a dream, only do things you don't say
[CV] Wu Enda machine learning course notes | Chapter 9