【02】C语言-变量的声明与赋值,printf格式化输出函数,sizeof()运算符
【02】C语言-变量的声明与赋值,printf格式化输出函数,sizeof()运算符
文章目录
- 1. 变量的声明与赋值
- 2. `printf()`格式化输出函数
- 2.1 基本语法
- 2.2 格式说明符
- 2.3 长度与精度控制
- 2.4 转义字符
- 2.5 `printf()`的返回值
- 2.6 常规输出练习
- 3.`sizeof()` 运算符
1. 变量的声明与赋值
变量的声明与赋值简单示例:
#include <stdio.h> int main()
{ // 1.先声明后赋值 int a; a = 100; // 2.直接声明,并赋值 int b = 200; printf("a=%d \n", a); // 使用输出语句,把变量a 值输出, %d 表示输出的形式为整数 printf("b=%d \n", b); // 使用输出语句,把变量b 值输出, %d 表示输出的形式为整数 // 3.修改变量 b 的值, 将变量 a 的值赋值给 bb = a; printf("b=%d \n", b);// 4.同时声明多个整型变量并赋值 int c1 = 10, c2 = 20, c3 = 30; printf("%d %d %d", c1, c2, c3);return 0;
}
2. printf()格式化输出函数
2.1 基本语法
printf()是C语言中最常用的输出函数之一。
其基本语法如下
int printf(const char *format, ...);
• format:格式控制字符串
• …:可变参数列表,对应格式控制字符串中的格式说明符
两个简单用法示例:
- 输出简单字符串
printf("Hello, World!\n");
- 输出变量的值
int age = 25;
printf("I am %d years old.\n", age);
2.2 格式说明符
printf()使用格式说明符来指定输出的格式,常见的格式说明符包括:
| 格式说明符 | 说明 |
|---|---|
| %d 有符号十进制整数 | |
| %i | 同%d |
| %u | 无符号十进制整数 |
| %f | 浮点数(默认6位小数) |
| %lf (这里的l是L的小写字母) | 双精度浮点数 |
| %c | 单个字符 |
| %s | 字符串 |
| %p | 指针地址 |
| %x | 十六进制整数(小写字母) |
| %X | 十六进制整数(大写字母) |
| %o | 八进制整数 |
| %% | 输出百分号 |
2.3 长度与精度控制
#include <stdio.h>int main(){//浮点精度 %5.3f 表示至少显示5个字符(小数点也算一个)、并保留3位小数的浮点数。printf("%5.3f\n",1.2 );printf("%5.3f\n",12345.2 );printf("%5.3f\n",1.12345);printf("%5.3f\n",123456.123456);//字符串精度 "%10s"表示打印的最小字段宽度为 10 个字符。如果打印的字符串长度少于 10 个字符,则会使用空格进行填充。printf("%10s\n","hello");printf("%10s\n","hello, world!");//固定精度且所有字符串的长度都不超过指定精度的情况下,默认右对齐printf("%10s\n","1");printf("%10s\n","12");printf("%10s\n","123");printf("%10s\n","1234");printf("%10s\n","12345");printf("%10s\n","123456");//在%符号后放置了负号。则可以消除字符串左侧的空格,实现左对齐。printf("%-10s\n","1");printf("%-10s\n","12");printf("%-10s\n","123");printf("%-10s\n","1234");printf("%-10s\n","12345");printf("%-10s\n","123456");
}
输出结果:
1.123
123456.123
hello
hello, world!
1
12
123
1234
12345
123456
1
12
123
1234
12345
123456
2.4 转义字符
printf() 中可以使用转义字符实现特殊效果:
| 转义字符 | 说明 |
|---|---|
| \n | 换行 |
| \t | 水平制表符 |
| \ | 反斜杠 |
| " | 双引号 |
| ’ | 单引号 |
| \b | 退格 |
| \r | 回车(回到行首) |
代码示例
#include <stdio.h>int main() {int age = 25;float height = 1.75f;char grade = 'A';char name[] = "Alice";printf("Name: %s\n", name);printf("Age: %d, Height: %.2f meters\n", age, height);printf("Grade: %c\n", grade);printf("Memory address of age: %p\n", &age);printf("Hexadecimal of age: %x\n", age);return 0;
}
输出结果:
Name: Alice
Age: 25, Height: 1.75 meters
Grade: A
Memory address of age: 000000e3bc1ff724
Hexadecimal of age: 19
2.5 printf()的返回值
printf()函数返回输出的字符数,如果出错则返回负值。
int count = printf("Hello\n");
printf("Printed %d characters\n", count); // 输出: Printed 6 characters
注意事项
- 格式说明符必须与参数类型匹配,否则可能导致未定义行为
- 参数数量必须与格式字符串中的格式说明符数量一致
- 在输出字符串时,要确保字符串以’\0’结尾
- 使用
%f输出float和double都可以,但更推荐float用%f,double用%lf。
2.6 常规输出练习
#include <stdio.h>
//定义符号常量
#define c1 5
#define c2 3+2
#define c3 2.2
#define c4 3e3
#define c5 3e-3
#define c6 'M'
#define c7 "abc"int main() {printf("hello, world!\n");printf("constant c1: %d\n",c1); //常量c1值为5printf("c2*3: %d\n",c2*3); //c2*3 = 3+2*3 = 9printf("constant c3: %f\n",c3); //常量c3,输出为浮点数,这里默认为6位小数printf("constant c4: %f\n",c4); //常量c4:3e3,输出为浮点数,这里默认为6位小数printf("constant c5: %f\n",c5); //常量c5:3-e3,输出为浮点数printf("constant c6: %c\n",c6); //%c表示一个字符。%s表示多个字符组成的字符串。printf("constant c7: %s\n",c7); printf("size of c7: %d\n",sizeof(c7)); //"abc"这个字符串,占了4个return 0;
}
输出结果:
hello, world!
constant c1: 5
c2*3: 9
constant c3: 2.200000
constant c4: 3000.000000
constant c5: 0.003000
constant c6: M
constant c7: abc
size of c7: 4
3.sizeof() 运算符
sizeof是C语言中的一个重要运算符,用于计算数据类型或变量在内存中所占的字节数。
#include <stdio.h>int main() {// 1. 基本数据类型的大小printf("=== 基本数据类型大小 ===\n");printf("char: %zu byte\n", sizeof(char));printf("short: %zu bytes\n", sizeof(short));printf("int: %zu bytes\n", sizeof(int));printf("long: %zu bytes\n", sizeof(long));printf("float: %zu bytes\n", sizeof(float));printf("double: %zu bytes\n", sizeof(double));printf("long double: %zu bytes\n", sizeof(long double));// 2. 变量的大小printf("\n=== 变量大小 ===\n");char c = 'A';int i = 42;double d = 3.14159;printf("变量c的大小: %zu byte\n", sizeof(c));printf("变量i的大小: %zu bytes\n", sizeof i); // 注意这里可以不加括号printf("变量d的大小: %zu bytes\n", sizeof(d));// 3. 数组的大小printf("\n=== 数组大小 ===\n");int arr1[5] = {1, 2, 3, 4, 5};char str[] = "Hello";printf("整型数组arr1的大小: %zu bytes\n", sizeof(arr1));printf("字符串str的大小: %zu bytes (包含'\\0')\n", sizeof(str));// 计算数组元素个数printf("arr1的元素个数: %zu\n", sizeof(arr1) / sizeof(arr1[0]));printf("str的字符数(含'\\0'): %zu\n", sizeof(str) / sizeof(str[0]));// 4. 指针的大小printf("\n=== 指针大小 ===\n");int *p1 = &i;char *p2 = &c;double *p3 = &d;printf("int指针大小: %zu bytes\n", sizeof(p1));printf("char指针大小: %zu bytes\n", sizeof(p2));printf("double指针大小: %zu bytes\n", sizeof(p3));printf("指针大小通常相同: %zu bytes\n", sizeof(void*));// 5. 结构体的大小printf("\n=== 结构体大小 ===\n");struct Student {char name[20];int age;float score;};printf("结构体Student的大小: %zu bytes\n", sizeof(struct Student));struct Student stu;printf("结构体变量stu的大小: %zu bytes\n", sizeof stu);return 0;
}
输出结果:
=== 基本数据类型大小 ===
char: 1 byte
short: 2 bytes
int: 4 bytes
long: 4 bytes
float: 4 bytes
double: 8 bytes
long double: 16 bytes=== 变量大小 ===
变量c的大小: 1 byte
变量i的大小: 4 bytes
变量d的大小: 8 bytes=== 数组大小 ===
整型数组arr1的大小: 20 bytes
字符串str的大小: 6 bytes (包含’\0’)
arr1的元素个数: 5
str的字符数(含’\0’): 6=== 指针大小 ===
int指针大小: 8 bytes
char指针大小: 8 bytes
double指针大小: 8 bytes
指针大小通常相同: 8 bytes=== 结构体大小 ===
结构体Student的大小: 28 bytes
结构体变量stu的大小: 28 bytes
