当前位置:网站首页>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.h
file ;
-#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
边栏推荐
- Question 49: how to quickly determine the impact of IO latency on MySQL performance
- How to play dapr without kubernetes?
- C # implements a queue in which everything can be sorted
- How does Xiaobai buy a suitable notebook
- 2022 examination questions for safety managers of metal and nonmetal mines (underground mines) and examination papers for safety managers of metal and nonmetal mines (underground mines)
- go-zero微服务实战系列(九、极致优化秒杀性能)
- @Role of pathvariable annotation
- Four essential material websites for we media people to help you easily create popular models
- LinkedList in the list set is stored in order
- 4 small ways to make your Tiktok video clearer
猜你喜欢
[untitled] forwarding least square method
What if I forget the router password
Educational Codeforces Round 115 (Rated for Div. 2)
4 small ways to make your Tiktok video clearer
[CV] Wu Enda machine learning course notes | Chapter 9
小程序容器技术与物联网 IoT 可以碰撞出什么样的火花
随机事件的关系与运算
DM8 tablespace backup and recovery
ctfshow web255 web 256 web257
C语言-入门-基础-语法-数据类型(四)
随机推荐
Flutter integrated amap_ flutter_ location
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
Educational Codeforces Round 115 (Rated for Div. 2)
A subclass must use the super keyword to call the methods of its parent class
Use Alibaba cloud NPM image acceleration
Question 49: how to quickly determine the impact of IO latency on MySQL performance
Manjaro install wechat
ctfshow web255 web 256 web257
es6总结
Awk from entry to earth (7) conditional statements
How to solve the problem of computer jam and slow down
2022 tower crane driver examination and tower crane driver examination questions and analysis
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
Learn nuxt js
Getting started with microservices: gateway gateway
Talk about single case mode
Internal learning
SQL statement view SQL Server 2005 version number
manjaro安装微信
awk从入土到入门(10)awk内置函数