当前位置:网站首页>[C Language] Getting Started
[C Language] Getting Started
2022-08-11 03:42:00 【The spring breeze light preface】
一、基本类型
1、程序结构
#include <stdio.h>
int main()
{
/* 我的第一个 C 程序 */
printf("Hello, World! \n");
return 0;
}
接下来我们讲解一下上面这段程序:
程序的第一行 #include <stdio.h> 是预处理器指令,告诉 C 编译器在实际编译之前要包含 stdio.h 头文件.
下一行 int main() 是主函数,程序从这里开始执行.
下一行 /…/ 将会被编译器忽略,这里放置程序的注释内容.它们被称为程序的注释.
下一行 printf(…) 是 C 中另一个可用的函数,会在屏幕上显示消息 “Hello, World!”.
下一行 return 0; 终止 main() 函数,并返回值 0.
2、标识符
3、关键字
4、数据类型
参数
整数类型
浮点类型
5、变量
6、常量
整数常量
浮点常量
字符常量
常量的定义方式
在 C 中,有两种简单的定义常量的方式:
1、使用 #define 预处理器.
#define identifier value
2、使用 const 关键字.
const type variable = value;
7、判断语句
8、循环语句
二、函数
1、定义函数
C 语言中的函数定义的一般形式如下:
return_type function_name( parameter list )
{
body of the function
}
在 C 语言中,函数由一个函数头和一个函数主体组成.下面列出一个函数的所有组成部分:
返回类型:一个函数可以返回一个值.return_type 是函数返回的值的数据类型.有些函数执行所需的操作而不返回值,在这种情况下,return_type 是关键字 void.
函数名称:这是函数的实际名称.函数名和参数列表一起构成了函数签名.
参数:参数就像是占位符.当函数被调用时,您向参数传递一个值,这个值被称为实际参数.参数列表包括函数参数的类型、顺序、数量.参数是可选的,也就是说,函数可能不包含参数.
函数主体:函数主体包含一组定义函数执行任务的语句.
2、调用函数
#include <stdio.h>
/* 函数声明 */
int max(int num1, int num2);
int main ()
{
/* 局部变量定义 */
int a = 100;
int b = 200;
int ret;
/* 调用函数来获取最大值 */
ret = max(a, b);
printf( "Max value is : %d\n", ret );
return 0;
}
/* 函数返回两个数中较大的那个数 */
int max(int num1, int num2)
{
/* 局部变量声明 */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
把 max() 函数和 main() 函数放一块,编译源代码.当运行最后的可执行文件时,会产生下列结果:
Max value is : 200
三、数组
1、概念
2、声明数组
3、初始化数组
4、访问数组元素
数组元素可以通过数组名称加索引进行访问.元素的索引是放在方括号内,跟在数组名称的后边.例如:
double salary = balance[9];
上面的语句将把数组中第 10 个元素的值赋给 salary 变量.下面的实例使用了上述的三个概念,即,声明数组、数组赋值、访问数组:
#include <stdio.h>
int main ()
{
int n[ 10 ]; /* n 是一个包含 10 个整数的数组 */
int i,j;
/* 初始化数组元素 */
for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100; /* 设置元素 i 为 i + 100 */
}
/* 输出数组中每个元素的值 */
for (j = 0; j < 10; j++ )
{
printf("Element[%d] = %d\n", j, n[j] );
}
return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
四、枚举
1、First acquaintance with enumeration
枚举语法定义格式为:
enum 枚举名 {
枚举元素1,枚举元素2,……};
接下来我们举个例子,比如:一星期有 7 天,使用枚举的方式:
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
};
注意:第一个枚举成员的默认值为整型的 0,后续枚举成员的值在前一个成员上加 1.我们在这个实例中把第一个枚举成员的值定义为 1,第二个就为 2,以此类推.
2、枚举变量的定义
1、先定义枚举类型,再定义枚举变量
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
};
enum DAY day;
2、定义枚举类型的同时定义枚举变量
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
} day;
3、省略枚举名称,直接定义枚举变量
enum
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
} day;
3、实例
#include <stdio.h>
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
} day;
int main()
{
// 遍历枚举元素
for (day = MON; day <= SUN; day++) {
printf("枚举元素:%d \n", day);
}
}
结果:
五、指针
1、初识指针
学习 C 语言的指针既简单又有趣.通过指针,可以简化一些 C 编程任务的执行,还有一些任务,如动态内存分配,没有指针是无法执行的.所以,想要成为一名优秀的 C 程序员,学习指针是很有必要的.
正如您所知道的,每一个变量都有一个内存位置,每一个内存位置都定义了可使用 & 运算符访问的地址,它表示了在内存中的一个地址.
请看下面的实例,它将输出定义的变量地址:
#include <stdio.h>
int main ()
{
int var_runoob = 10;
int *p; // 定义指针变量
p = &var_runoob;
printf("var_runoob 变量的地址: %p\n", p);
return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
var_runoob 变量的地址: 0x7ffeeaae08d8
2、什么是指针
指针也就是内存地址,指针变量是用来存放内存地址的变量.就像其他变量或常量一样,您必须在使用指针存储其他变量地址之前,对其进行声明.指针变量声明的一般形式为:
type *var_name;
在这里,type 是指针的基类型,它必须是一个有效的 C 数据类型,var_name 是指针变量的名称.用来声明指针的星号 * 与乘法中使用的星号是相同的.但是,在这个语句中,星号是用来指定一个变量是指针.以下是有效的指针声明:
所有实际数据类型,不管是整型、浮点型、字符型,还是其他的数据类型,对应指针的值的类型都是一样的,都是一个代表内存地址的长的十六进制数.
不同数据类型的指针之间唯一的不同是,指针所指向的变量或常量的数据类型不同.
3、指针的使用
#include <stdio.h>
int main ()
{
int var = 20; /* 实际变量的声明 */
int *ip; /* 指针变量的声明 */
ip = &var; /* 在指针变量中存储 var 的地址 */
printf("var 变量的地址: %p\n", &var );
/* 在指针变量中存储的地址 */
printf("ip 变量存储的地址: %p\n", ip );
/* 使用指针访问值 */
printf("*ip 变量的值: %d\n", *ip );
return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
4、数组指针的应用
下面的实例用到了三个整数,它们将存储在一个指针数组中,如下所示:
#include <stdio.h>
const int MAX = 3;
int main ()
{
int var[] = {
10, 100, 200};
int i, *ptr[MAX];
for ( i = 0; i < MAX; i++)
{
ptr[i] = &var[i]; /* 赋值为整数的地址 */
}
for ( i = 0; i < MAX; i++)
{
printf("Value of var[%d] = %d\n", i, *ptr[i] );
}
return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
您也可以用一个指向字符的指针数组来存储一个字符串列表,如下:
#include <stdio.h>
const int MAX = 4;
int main ()
{
const char *names[] = {
"Zara Ali",
"Hina Ali",
"Nuha Ali",
"Sara Ali",
};
int i = 0;
for ( i = 0; i < MAX; i++)
{
printf("Value of names[%d] = %s\n", i, names[i] );
}
return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
5、函数指针的应用
以下实例声明了函数指针变量 p,指向函数 max:
#include <stdio.h>
int max(int x, int y)
{
return x > y ? x : y;
}
int main(void)
{
/* p 是函数指针 */
int (* p)(int, int) = & max; // &可以省略
int a, b, c, d;
printf("请输入三个数字:");
scanf("%d %d %d", & a, & b, & c);
/* 与直接调用函数等价,d = max(max(a, b), c) */
d = p(p(a, b), c);
printf("最大的数字是: %d\n", d);
return 0;
}
编译执行,输出结果如下:
边栏推荐
- 电商项目——商城限时秒杀功能系统
- 【FPGA】SDRAM
- What problems should we pay attention to when building a programmatic trading system?
- 程序化交易的策略类型可以分为哪几种?
- A simple JVM tuning, learn to write it on your resume
- 互换性与测量技术-公差原则与选用方法
- Is there any way for kingbaseES to not read the system view under sys_catalog by default?
- 元素的BFC属性
- Basic understanding of MongoDB (2)
- UNI-APP_iphone bottom safe area
猜你喜欢
A large horse carries 2 stone of grain, a middle horse carries 1 stone of grain, and two ponies carry one stone of grain. It takes 100 horses to carry 100 stone of grain. How to distribute it?
【FPGA】day20-I2C读写EEPROM
DNS separation resolution and intelligent resolution
浮点数在内存中的存储方式
C语言之自定义类型------结构体
Interchangeability Measurements and Techniques - Calculation of Deviations and Tolerances, Drawing of Tolerance Charts, Selection of Fits and Tolerance Classes
【FPGA】设计思路——I2C协议
DNS分离解析和智能解析
Design and Realization of Employment Management System in Colleges and Universities
CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
随机推荐
The most unlucky and the luckiest
The impact of programmatic trading and subjective trading on the profit curve!
索引的创建、查看、删除
论文精度 —— 2017 CVPR《High-Resolution Image Inpainting using Multi-Scale Neural Patch Synthesis》
Paper Accuracy - 2017 CVPR "High-Resolution Image Inpainting using Multi-Scale Neural Patch Synthesis"
rac备库双节点查询到的表最后更新时间不一致
Enter the starting position, the ending position intercepts the linked list
轮转数组问题:如何实现数组“整体逆序,内部有序”?“三步转换法”妙转数组
Detailed explanation of VIT source code
What problems should we pay attention to when building a programmatic trading system?
oracle的基数会影响到查询速度吗?
【愚公系列】2022年08月 Go教学课程 036-类型断言
Day20 FPGA 】 【 - block the I2C read and write EEPROM
你不知道的 console.log 替代品
What are port 80 and port 443?What's the difference?
荣威imax8ev魔方电池安全感,背后隐藏着哪些黑化膨胀?
FTP错误代码列表
机器学习可以应用在哪些场景?机器学习有什么用?
How does MSP430 download programs to the board?(IAR MSPFET CCS)
程序化交易与主观交易对盈利曲线的影响!