例329:时间日期预定义宏
#include <stdio.h>
int main(int argc, char const *argv[])
{
// 这里的时间编译时已经决定,所以程序每次执行都是一样的
// __DATE__:编译时刻的日期字符串 如“Jun 17 2017”
// __TIME__:编译时刻的时间字符串 如”10:00:00“
printf("Beta Testing: Last compiled %s %s\n", __DATE__, __TIME__);
return 0;
}
例330:使用宏定义计算两个数最大值和最小值
#include <stdio.h>
// 宏定义(替换)
#define MIN(x, y) (((x) < (y)) ? (x): (y))
#define MAX(x, y) (((x) > (y)) ? (x): (y))
int main(int argc, char const *argv[])
{
printf("Minimum of 3 and 5 is %d\n", MIN(3, 5));
printf("Maximum of 3.4 and 3.1 is %f\n", MAX(3.4, 3.1));
printf("Minimum of -100 and 1000 is %d\n", MIN(-100, 1000));
return 0;
}
例331:使用宏定义计算两数之和
#include <stdio.h>
#define SUM(x, y) ((x) + (y))
int main(int argc, char const *argv[])
{
printf("Adding 3 + 5 = %d\n", SUM(3, 5));
printf("Adding 3.4 + 3.1 = %f\n", SUM(3.4, 3.1));
printf("Adding -100 + 1000 = %d\n", SUM(-100, 1000));
return 0;
}
例332:使用宏定义计算平方和立方
#include <stdio.h>
#define SQUARE(x) ((x) * (x))
#define CUBE(x) ((x) * (x) * (x))
int main(int argc, char const *argv[])
{
printf("The square of 2 is %d\n", SQUARE(2));
printf("The cube of 100 is %f\n", CUBE(100.0));
return 0;
}
例333:字符串的结束
#include <stdio.h>
int main(int argc, char const *argv[])
{
char string[256];
int i;
// 前26个元素用26个字母赋值
for (i = 0; i < 26; i++)
string[i] = 'A' + i;
// 第10个元素赋值位NULL
string[10] = NULL;
printf ("The string contains %s\n", string);// The string contains ABCDEFGHIJ
return 0;
}
例334:字符串转数值1
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
int int_value;
float flt_value;
long long_value;
// 把字符串转换成整型
int_value = atoi("12345");
// 把字符串转换成浮点型
flt_value = atof("33.45");
// 把字符串转换成长整型
long_value = atol("12BAD");
printf("int %d float %5.2f long %ld\n", int_value, flt_value, long_value);// int 12345 float 33.45 long 12
return 0;
}
例335:字符串转数值2
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
int int_result;
float float_result;
long long_result;
int_result = atoi("1234");
float_result = atof("12345.678");
long_result = atol("1234567L");
printf("%d %f %ld\n", int_result, float_result, long_result);// 1234 12345.677734 1234567
return 0;
}
例336:字符串的结束2
#include <stdio.h>
int main(int argc, char const *argv[])
{
char string[256];
int i;
for (i = 0; i < 26; i++)
string[i] = 'A' + i;
string[i] = NULL;
printf ("The string contains %s\n", string);// The string contains ABCDEFGHIJKLMNOPQRSTUVWXYZ
return 0;
}
例337:字符串比较函数
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
// 比较字符串数组的一个方法或函数,但是不可以区分大小写
printf("Comparing Abc with Abc %d\n", stricmp("Abc", "Abc")); // Comparing Abc with Abc 0
printf("Comparing abc with Abc %d\n", stricmp("abc", "Abc")); // Comparing abc with Abc 0
// extern int strncmpi(char *str1,char * str2,int n)
// str1为第一个要比较的字符串,str2为第二个要比较的字符串,n为指定字符串str1和str2进行比较的字符的个数
printf("Comparing 3 letters abcd with ABC %d\n", strnicmp("abcd", "ABC", 3)); // Comparing 3 letters abcd with ABC 0
printf("Comparing 5 letters abc with Abcd %d\n", strnicmp("abc", "Abcd", 5)); // Comparing 5 letters abc with Abcd -1
return 0;
}
例338:字符字面量的双引号表示
#include <stdio.h>
int main(int argc, char const *argv[])
{
char string[] = "\"Stop!\", he said.";
printf(string);
return 0;
}
例339:输出用户输入的字符串和字符个数
#include <stdio.h>
int main(int argc, char const *argv[])
{
char string[256];
int i;
printf("Type a string of characters and press Enter:\n");
gets(string);
// 单个字符打印直到NULL
for (i = 0; string[i] != NULL; i++)
putchar(string[i]);
printf("\nThe number of characters in the string is %d\n", i);
return 0;
}
例340:字符串函数sprintf()
#include <stdio.h>
int main(int argc, char const *argv[])
{
int employee_number = 12345;
char region[] = "SEA";
char filename[64];
sprintf(filename, "%s%d", region, employee_number);
printf("Employee filename: %s\n", filename);
return 0;
}
例341:字符串函数sscanf()
#include <stdio.h>
int main(int argc, char const *argv[])
{
int age;
float salary;
char string[] = "33 25000.00";
// sscanf读取格式化的字符串中的数据
sscanf(string, "%d %f\n", &age, &salary);
printf("Age: %d Salary %f\n", age, salary);// Age: 33 Salary 25000.000000
return 0;
}