当前位置:网站首页>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
边栏推荐
- LinkedList in the list set is stored in order
- 上周热点回顾(6.27-7.3)
- C语言-入门-基础-语法-[主函数,头文件](二)
- Awk from entry to earth (14) awk output redirection
- Three paradigms of database design
- 学习Nuxt.js
- What should I do if there is a problem with the graphics card screen on the computer
- Leetcode topic [array] -136- numbers that appear only once
- awk从入土到入门(10)awk内置函数
- Awk from digging into the ground to getting started (10) awk built-in functions
猜你喜欢
How to solve the problem of computer jam and slow down
C语言-入门-基础-语法-[主函数,头文件](二)
ArcGIS application (XXII) ArcMap loading lidar Las format data
C语言-入门-基础-语法-数据类型(四)
4 small ways to make your Tiktok video clearer
CLion-控制台输出中文乱码
Codeforces Round #803 (Div. 2)(A-D)
Horizon sunrise X3 PI (I) first boot details
What should I do if there is a problem with the graphics card screen on the computer
How to re enable local connection when the network of laptop is disabled
随机推荐
awk从入门到入土(4)用户自定义变量
Display Chinese characters according to numbers
What if I forget the router password
Educational Codeforces Round 119 (Rated for Div. 2)
Awk from digging into the ground to getting started (10) awk built-in functions
1211 or chicken and rabbit in the same cage
Codeforces Global Round 21(A-E)
Horizon sunrise X3 PI (I) first boot details
Flutter integrated amap_ flutter_ location
Démarrage des microservices: passerelle
Four essential material websites for we media people to help you easily create popular models
[Chongqing Guangdong education] National Open University spring 2019 455 logistics practice reference questions
SQL statement view SQL Server 2005 version number
How to solve the problem that computers often flash
From scratch, use Jenkins to build and publish pipeline pipeline project
Awk from entry to earth (7) conditional statements
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
Leetcode topic [array] - 121 - the best time to buy and sell stocks
awk从入门到入土(9)循环语句
FOC control