例54:使用sizeof运算符计算类型字节数
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("Variables of type int use %d bytes\n", sizeof(int)); // Variables of type int use 4 bytes
printf("Variables of type float use %d bytes\n", sizeof(float)); // Variables of type float use 4 bytes
printf("Variables of type double use %d bytes\n", sizeof(double)); // Variables of type double use 8 bytes
printf("Variables of type unsigned use %d bytes\n", sizeof(unsigned)); // Variables of type unsigned use 4 bytes
printf("Variables of type long use %d bytes\n", sizeof(long)); // Variables of type long use 4 bytes
return 0;
}
例55:字符串的使用
#include <stdio.h>
int main(int argc, char const *argv[])
{
// 使用字符数组存储字符串
char title[255] = "Jamsa's 1001 C & C++ Tips";
// %s控制台打印字符串
printf("The name of this book is %s\n", title); //The name of this book is Jamsa's 1001 C & C++ Tips
return 0;
}
例56:打印宽度的填充设置
#include <stdio.h>
int main(int argc, char const *argv[])
{
int value = 5;
// 打印字符宽度1,空字符用0填充
printf ("%01d\n", value);
// 打印字符宽度2,空字符用0填充
printf ("%02d\n", value);
// 打印字符宽度3,空字符用0填充
printf ("%03d\n", value);
// // 打印字符宽度4,空字符用0填充
printf ("%04d\n", value);
return 0;
}
例57:实现一个简单的命令行程序,可以查看当前路径的文件目录,可以检查磁盘,可以修改系统日期
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
char letter;
do {
// 提示用户功能
printf("A Display directory listing\n");
printf("B Display disk information\n");
printf("C Change system date\n");
printf("Q Quit\n");
printf("Choice: ");
// 获取用户输入的字符(需包含conio.h)
letter = getch();
// 将字符转为大写(达到不区分输入大小写的目的,需包含ctype.h)
letter = toupper(letter);
// 根据字符调用对应系统命令
switch (letter) {
case 'A': system("DIR");
break;
case 'B': system("CHKDSK");
break;
case 'C': system("DATE");
break;
};
}
// 只要在输入Q时才退出
while (letter != 'Q');
return 0;
}
例58:计算元音字母个数
#include <stdio.h>
int main(int argc, char const *argv[])
{
char letter;
// 记录个数
int vowel_count = 0;
// 从A到Z, 当字母是A、E、I、O、U就++
for (letter = 'A'; letter <= 'Z'; letter++)
switch (letter) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U': vowel_count++;
};
printf("The number of vowels is %d\n", vowel_count);
return 0;
}
例59:用break“提前”跳出循环
#include <stdio.h>
int main(int argc, char const *argv[])
{
int counter;
for (counter = 1; counter <= 100; counter++)
{
// 到50时就跳出了
if (counter == 50)
break;
printf("%d ", counter);
}
printf("\nNext loop\n");
for (counter = 100; counter >= 1; counter--)
{
// 到50时就跳出了
if (counter == 50)
break;
printf("%d ", counter);
}
return 0;
}
例60:只能输入Y或N的程序
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
int main(int argc, char const *argv[])
{
char letter;
printf("Do you want to continue? (Y/N): ");
letter = getch();
letter = toupper(letter);
// 不是Y或N就重新获取输入
while ((letter != 'Y') && (letter != 'N'))
{
// 这里打印7但运行时为什么没打印呢?
putch(7);
letter = getch();
letter = toupper(letter);
}
printf("\nYour response was %c\n", letter);
//这里数字7是ASCII码的7, 而码表中7无对应字符
return 0;
}
例61:有符号和无符号的区别
#include <stdio.h>
int main(int argc, char const *argv[])
{
unsigned int value = 2147483648;
printf("Displaying 2147483648as unsigned %u\n", value); // Displaying 2147483648 as unsigned 2147483648
printf("Displaying 2147483648as int %d\n", value); // Displaying 2147483648 as int -2147483648
// 我们定义了无符号整型, 使用不同格式输出得到不一样的结果
// 这说明了一点, 计算机内的数据存储值是确定相同的 - 2147483648的二进制码
// 关键在于怎样解释理解!!!
return 0;
}
例62:printf的返回值
#include <stdio.h>
int main(int argc, char const *argv[])
{
int result;
result = printf("Jamsa's 1001 C & C++ Tips!\n");
// 返回值为EOF就在错误输出流打印
if (result == EOF)
fprintf(stderr, "Error within printf\n");
return 0;
}
例63:一段没有看过的代码(老版本编译器有效果例如vc6.0)
#include <stdio.h>
int main(int argc, char const *argv[])
{
int first_count;
int second_count;
// %n是将其之前打印字符个数放到对应的变量中
printf("Jamsa%n's 1001 C & C++ Tips%n\n", &first_count, &second_count); // Jamsa's 1001 C & C++ Tips
printf("First count %d Second count %d\n", first_count, second_count); // First count 5 Second count 25
// 因为会造成格式化字符串漏洞的原因,Windows很早就弃用了%n
return 0;
}
例64:又一段没有看过的代码
#include <stdio.h>
int main(int argc, char const *argv[])
{
char *near_title = "Jamsa's 1001 C & C++ Tips";
// 有人说是远近指针, 听说只能在DOS有用(vc6.0、gcc、vs都编译报错)
char far *far_title = "Jamsa's 1001 C & C++ Tips";
printf("The book's title: %Ns\n", near_title);
printf("The book's title: %Fs\n", far_title);
return 0;
}