解释 C 语言中的不同部分
C 程序由一组协议定义,程序员在编写代码时必须遵循这些协议。
部分
完整的程序分为不同的部分,如下所示:
文档部分 - 在这里,我们可以给出有关程序的命令,例如作者姓名、创建或修改日期。在/* */或//之间编写的信 息称为注释行。编译器在执行时不会考虑这些行。
链接部分 - 在此部分中,包含了执行程序所需的标头文件。
定义部分 - 在这里,定义并初始化变量。
全局声明部分 - 在此部分中,定义了可以在整个程序中使用的全局变量。
函数原型声明部分 - 此部分提供有关返回值、参数、函数内部使用的名称的信息。
主函数 - C 程序将从此部分开始编译。通常,它有两个主要部分,称为声明部分和可执行部分。
用户定义部分 - 用户可以定义自己的函数并根据用户的需求执行特定任务。
‘C’ 程序的一般形式
C 程序的一般形式如下所示:
/* documentation section */
preprocessor directives
global declaration
main ( ){
local declaration
executable statements
}
returntype function name (argument list){
local declaration
executable statements
}示例
以下是使用带参数且无返回值的函数执行加法的 C 程序:
#include<stdio.h>
void main(){
//Function declaration - (function has void because we are not returning any values for function)//
void sum(int,int);
//Declaring actual parameters//
int a,b;
//Reading User I/p//
printf("Enter a,b :");
scanf("%d,%d",&a,&b);
//Function calling//
sum(a,b);
}
void sum(int a, int b){//Declaring formal parameters
//Declaring variables//
int add;
//Addition operation//
add=a+b;
//Printing O/p//
printf("Addition of a and b is %d",add);
}输出
您将看到以下输出:
Enter a,b :5,6 Addition of a and b is 11
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP