这一章习题做着很舒服,毕竟很简单。所以很有感觉。
练习 2-1
Write a program to determine the ranges of char , short , int , and long variables, both signed and unsigned , by printing appropriate values from standard headers and by direct computation. Harder if you compute them: determine the ranges of the various floating-point types.
1 #include <stdio.h> 2 #include <limits.h> 3 int main() 4 { 5 printf("char max : %d min : %d ",CHAR_MAX, CHAR_MIN); 6 printf("unchar max : %u min : %u ",UCHAR_MAX, 0); 7 printf("int max : %d min : %d ",INT_MAX, INT_MIN); 8 printf("unsigned int max : %lu min : %d ",UINT_MAX, 0); 9 printf("long max : %ld min : %ld ",LONG_MAX, LONG_MIN); 10 printf("unsigned long max : %lu min : %d ",ULONG_MAX ,0); 11 12 return 0; 13 }
ps :假如unsigned int 最大值不用 %lu 而用 %lld 会导致 后面变量发生无法预知的错误, %ld 无法装下unsigned int 最大值
练习 2-2
Exercise 2-2 discusses a for loop from the text. Here it is:
1 int main(void) 2 { 3 /* 4 for (i = 0; i < lim-1 && (c=getchar()) != ' ' && c != EOF; ++i) 5 s[i] = c; 6 */ 7 8 int i = 0, 9 lim = MAX_STRING_LENGTH, 10 int c; 11 char s[MAX_STRING_LENGTH]; 12 13 while (i < (lim - 1)) 14 { 15 c = getchar(); 16 17 if (c == EOF) 18 break; 19 else if (c == ' ') 20 break; 21 22 s[i++] = c; 23 } 24 25 s[i] = '