1.整型数据类型存储空间大小
#include <stdio.h>
int main() {
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of short: %zu bytes\n", sizeof(short));
printf("Size of long: %zu bytes\n", sizeof(long));
printf("Size of long long: %zu bytes\n", sizeof(long long));
return 0;
}
2.浮点型数据类型存储空间大小
#include <stdio.h>
int main() {
printf("Size of float: %zu bytes\n", sizeof(float));
printf("Size of double: %zu bytes\n", sizeof(double));
printf("Size of long double: %zu bytes\n", sizeof(long double));
return 0;
}
3.其他数据类型存储空间大小
#include <stdio.h>
// 定义一个结构体作为示例
struct ExampleStruct {
int intValue;
double doubleValue;
char charValue;
};
// 定义一个枚举作为示例
enum ExampleEnum {
ENUM_VALUE_1,
ENUM_VALUE_2,
ENUM_VALUE_3
};
int main() {
// 输出基本数据类型的大小
printf("Size of char: %zu bytes\n", sizeof(char));
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of short: %zu bytes\n", sizeof(short));
printf("Size of long: %zu bytes\n", sizeof(long));
printf("Size of long long: %zu bytes\n", sizeof(long long));
printf("Size of float: %zu bytes\n", sizeof(float));
printf("Size of double: %zu bytes\n", sizeof(double));
printf("Size of long double: %zu bytes\n", sizeof(long double));
// 输出指针的大小
printf("Size of pointer: %zu bytes\n", sizeof(void *));
// 输出结构体的大小
printf("Size of struct ExampleStruct: %zu bytes\n", sizeof(struct ExampleStruct));
// 输出枚举的大小(通常和int相同)
printf("Size of enum ExampleEnum: %zu bytes\n", sizeof(enum ExampleEnum));
return 0;
}
4.浮点数向零舍入
#include <stdio.h>
int main() {
double num1 = 3.14;
double num2 = -3.14;
// 向零舍入
int intNum1 = (int)num1;
int intNum2 = (int)num2;
printf("num1: %f, truncated: %d\n", num1, intNum1);
printf("num2: %f, truncated: %d\n", num2, intNum2);
return 0;
}
5.打印ASCII码
#include <stdio.h>
int main() {
char myChar = 'A';
int asciiValue = (int)myChar;
printf("The ASCII value of '%c' is %d.\n", myChar, asciiValue);
return 0;
}
6.打印字符
#include <stdio.h>
int main() {
char myChar = 'A'; // 定义一个字符变量并初始化为'A'
printf("打印的字符是:%c\n", myChar); // 使用%c作为占位符打印字符
// 也可以直接在printf中使用字符字面量
printf("直接打印的字符是:%c\n", 'B');
return 0;
}
7.整型与布尔型的转换
#include <stdbool.h>
bool isPositive(int x) {
return x > 0; // 这里x > 0的结果是整型,但在赋值给bool时自动转换为bool
}
int main() {
bool b = 5; // 隐式转换,5(非0)被视为true
printf("%d\n", b); // 输出1(假设true以整数1表示)
bool anotherB = (bool)42; // 显式转换
printf("%d\n", anotherB); // 同样输出1
return 0;
}
8.Hello,World!的大小
#include <stdio.h>
int main() {
// 注意:这里的"Hello, World!"实际上包含了全角的"!",
// 但在简化讨论中,我们暂时忽略它的多字节性质。
// 如果你只是想要求字符数(不包括'\0'),可以这样做:
const char *str = "Hello, World!"; // 注意:这里的"!"可能是多字节的,但暂时忽略
// 直接给出字符数(不包括'\0')作为“大小”的近似值
// 注意:这实际上不是严格意义上的大小,因为它不包括'\0'
// 但对于很多简化讨论来说,这样做已经足够了。
printf("The approximate size (character count, excluding '\\0') of '%s' is: %zu\n",
str, strlen(str)); // 注意:strlen不包括'\0'
// 如果你想要一个包括'\0'的“大小”的近似值(尽管这在实际中很少这样做),
// 你可以手动加1,但请注意这不是通过sizeof得到的,因为sizeof对指针和数组的行为不同。
printf("The approximate size (including a hypothetical '\\0') of '%s' is: %zu\n",
str, strlen(str) + 1);
// 但请记住,对于字符串字面量,你不能通过sizeof(str)来获取其大小(包括'\0'),
// 因为str是一个指向字符数组首元素的指针,sizeof(str)将给出指针的大小,而不是数组的大小。
return 0;
}
标签:int,练习,bytes,C语言,printf,zu,程序设计,sizeof,Size
From: https://blog.csdn.net/huc_error/article/details/140675830