C语言精讲-11
1.宏定义
1.1宏定义的基本运用
宏定义就是简单的文本替换。
格式: #define 宏名 常量
功能:宏名代替常量,做到⻅名知意
#define MAX 100
说明:
(1)表示MAX这个符号标识的值是100
(2)编译器会将宏替换成宏后⾯的内容
#include<stdio.h>
#define MAX 100
#define P 3.14
#define CH "abc"
int main(void)
{
printf("%d\n",MAX);
printf("%lf\n",P);
printf("%s\n",CH);
return 0;
}
1.2宏函数
宏函数是利用预处理指令#define定义的一种代码替换机制
#define 宏名(参数列表) 替换文本
#include <stdio.h>
// 定义一个宏函数,用于计算两个数的和
#define ADD(a, b) ((a) + (b))
int main() {
int x = 3;
int y = 5;
int result = ADD(x, y);
printf("两数之和: %d\n", result);
return 0;
}
定义了宏函数ADD,它接受两个参数a和b,并返回它们的和。在main函数中调用ADD(x, y),预处理阶段会把ADD(x, y)替换成((x) + (y))。
2.条件编译
预处理指令,用于条件编译
用法和if…else语句相近
1.示例:
#if 宏名
// C 语言代码 1
#else
// C 语言代码 2
#endif
#include<stdio.h>
int main(void)
{
#if 0 //0为假,不执行
printf("hello\n");
#else
printf("hhhhhhhh\n");
#endif
printf("abcd\n");
return 0;
}
#ifdef DEBUG
printf("DEBUG 宏已定义\n");
#else
printf("DEBUG 宏未定义\n");
#endif
3.其他常用函数
3.1字符串相关函数
1.strcmp函数
原型: int strcmp(const char *str1, const char *str2);
功能: 用于比较两个字符串;
返回值:若 str1 小于 str2,返回一个小于 0 的整数。若 str1 等于 str2,返回 0。若 str1 大于 str2,返回一个大于 0 的整数。(对应ASCII 码值的大小)
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "apple";
char str2[] = "banana";
char str3[] = "apple";
int result1 = strcmp(str1, str2);
int result2 = strcmp(str1, str3);
int result3 = strcmp(str2, str1);
if (result1 < 0) {
printf("'%s' 小于 '%s'\n", str1, str2);
} else if (result1 == 0) {
printf("'%s' 等于 '%s'\n", str1, str2);
} else {
printf("'%s' 大于 '%s'\n", str1, str2);
}
if (result2 < 0) {
printf("'%s' 小于 '%s'\n", str1, str3);
} else if (result2 == 0) {
printf("'%s' 等于 '%s'\n", str1, str3);
} else {
printf("'%s' 大于 '%s'\n", str1, str3);
}
if (result3 < 0) {
printf("'%s' 小于 '%s'\n", str2, str1);
} else if (result3 == 0) {
printf("'%s' 等于 '%s'\n", str2, str1);
} else {
printf("'%s' 大于 '%s'\n", str2, str1);
}
return 0;
}
2.strlen()函数
原型: size_t strlen(const char *str); %lu
功能: 用于计算字符串的长度,不包含’\0’;
#include<stdio.h>
#include<string.h>
int main(void)
{
char str[]="hello world";
printf("strlen=%lu\n",strlen(str));
printf("sizeof=%lu\n",sizeof(str));
return 0;
}
3.strcpy()函数
原型: char *strcpy(char *destination, const char *source);
功能: 将一个字符串复制到另一个字符串数组中;会将字符串中的内容覆盖
#include<stdio.h>
#include<string.h>
#define MAX 100
int main(void)
{
char str[MAX]="abcd";
printf("str=%s\n",str);
strcpy(str,"hello");
printf("str=%s\n",str);
return 0;
}
4.strncpy()函数
原型: char *strncpy(char *destination, const char *source, size_t n);
功能: 将一个字符串的一部分或全部复制到另一个字符串数组中,可对复制的字符数量进行控制;
#include<stdio.h>
#include<string.h>
#define MAX 100
int main(void)
{
char str[MAX]="abcd";
printf("str=%s\n",str);
strncpy(str,"hello",2);
printf("str=%s\n",str);
return 0;
}
~
~
5.strcat()函数
原型: char *strcat(char *destination, const char *source);
功能: 将一个字符串(源字符串)追加到另一个字符串(目标字符串)的末尾
#include<stdio.h>
#include<string.h>
#define MAX 100
int main(void)
{
char str1[MAX]="hello";
char str2[]="world";
printf("str1=%s\n",str1);
strcat(str1,str2);
printf("str=%s\n",str1);
return 0;
}
6.strncat()函数
原型: char *strncat(char *destination, const char *source, size_t n);
功能·: 将一个字符串追加到另一个字符串的末尾,但 strncat() 可以指定最多追加的字符数量,
#include<stdio.h>
#include<string.h>
#define MAX 100
int main(void)
{
char str1[MAX]="hello";
char str2[]="world";
printf("str1=%s\n",str1);
strncat(str1,str2,1);
printf("str=%s\n",str1);
return 0;
}
~
3.2数学相关函数
double exp(double x):返回 e的 x次幂的值。
double log(double x):返回 x的自然对数(基数为 e的对数)double pow(double x, double y):返回 x的 y次幂。
double sqrt(double x):返回 x的平方根。
double fabs(double x):返回 x的绝对值。
#include <stdio.h>
#include <math.h>
int main() {
double x = 2.0;
double y = 3.0;
// 计算 e 的 x 次幂
double exp_result = exp(x);
printf("e 的 %.2f 次幂是: %.2f\n", x, exp_result);
// 计算 x 的自然对数
double log_result = log(x);
printf("%.2f 的自然对数是: %.2f\n", x, log_result);
// 计算 x 的 y 次幂
double pow_result = pow(x, y);
printf("%.2f 的 %.2f 次幂是: %.2f\n", x, y, pow_result);
// 计算 x 的平方根
double sqrt_result = sqrt(x);
printf("%.2f 的平方根是: %.2f\n", x, sqrt_result);
// 计算 -x 的绝对值
double fabs_result = fabs(-x);
printf("-%.2f 的绝对值是: %.2f\n", x, fabs_result);
return 0;
}
记得用gcc编译加-lm 命令
3.3输入输出相关函数
1.printf()函数 scanf()函数
加:printf函数
//%5d 表示占5个终端位宽 右对齐
//%‐5d 表示占5个终端位宽 左对齐
//%05d 表示占5个终端位宽 右对齐 不足补0
#include<stdio.h>
int main(void)
{
char s1[100]={0};
printf("输入一个字符串:");
scanf("%s",s1);
printf("s1=%s\n",s1);
return 0;
}
1.scanf 函数在读取字符串时,遇到空格、制表符或换行符就会停止读取
2.scanf 函数读取完字符串后,输入缓冲区中可能会残留换行符等字符,这可能会影响后续的输入操作。可以使用 getchar() 函数来清除输入缓冲区中的多余字符。
2.getchar() putchar()
#include<stdio.h>
int main(void)
{
char ch;
printf("输入一个字符:");
ch=getchar();
printf("ch=");
putchar(ch);
putchar('\n');
return 0;
}
3.fgets fputs
#include<stdio.h>
int main(void)
{
char s[100]={0};
printf("input:");
fgets(s,100,stdin);
printf("output:");
fputs(s,stdout);
return 0;
}
4.其他常用关键字
auto自动类型,register 寄存器变量,static静态变量,const只读
typedef为已有的类型 重新取个别名
volatile 防止编译器优化
auto:只能用于声明局部变量,不能在文件作用域使用;
register :用于建议编译器将变量存储在寄存器中,以此提升变量的访问速度; 寄存器的变量 不能取地址 &;
volatile
–1. 访问硬件寄存器(中断服务函数)
–2. 多线程编程