下面这个程序输出什么?
- enum {false,true};
- int main()
- {
- int i=1;
- do
- {
- printf("%dn",i);
- i++;
- if(i < 15)
- continue;
- }while(false);
- return 0;
- }
你相信么?下面这个程序输出的两行东西不一样!
- #include <stdio.h>
- #define f(a,b) a##b
- #define g(a) #a
- #define h(a) g(a)
-
- int main()
- {
- printf("%sn",h(f(1,2)));
- printf("%sn",g(f(1,2)));
- return 0;
- }
下面的程序看似完全正确。你能看出它为什么通不过编译吗?
看出问题前不要去试着编译,不然你会后悔你没看出来这个低级的语法错误。
- #include<stdio.h>
-
- void OS_Solaris_print()
- {
- printf("Solaris - Sun Microsystemsn");
- }
-
- void OS_Windows_print()
- {
- printf("Windows - Microsoftn");
-
- }
- void OS_HP-UX_print()
- {
- printf("HP-UX - Hewlett Packardn");
- }
-
- int main()
- {
- int num;
- printf("Enter the number (1-3):n");
- scanf("%d",&num);
- switch(num)
- {
- case 1:
- OS_Solaris_print();
- break;
- case 2:
- OS_Windows_print();
- break;
- case 3:
- OS_HP-UX_print();
- break;
- default:
- printf("Hmm! only 1-3 :-)n");
- break;
- }
-
- return 0;
- }
为什么下面这个程序的输出不是NONE?看你多久才能看出来。
- #include<stdio.h>
- int main()
- {
- int a=10;
- switch(a)
- {
- case '1':
- printf("ONEn");
- break;
- case '2':
- printf("TWOn");
- break;
- defa1ut:
- printf("NONEn");
- }
- return 0;
- }
下面这个程序输出什么?
- #include <stdio.h>
- int main()
- {
- int i=43;
- printf("%dn",printf("%d",printf("%d",i)));
- return 0;
- }
下面这个程序输出什么?
- #include<stdio.h>
- int main()
- {
- int a=1;
- switch(a)
- { int b=20;
- case 1: printf("b is %dn",b);
- break;
- default:printf("b is %dn",b);
- break;
- }
- return 0;
- }
下面这个程序输出什么?
- #include <stdio.h>
- int main()
- {
- int i;
- i = 10;
- printf("i : %dn",i);
- printf("sizeof(i++) is: %dn",sizeof(i++));
- printf("i : %dn",i);
- return 0;
- }
下面这个程序输出什么?
- #include <stdio.h>
- #include <stdlib.h>
-
- #define SIZEOF(arr) (sizeof(arr)/sizeof(arr[0]))
-
- #define PrintInt(expr) printf("%s:%dn",#expr,(expr))
- int mai
- n()
- {
- /* The powers of 10 */
- int pot[] = {
- 0001,
- 0010,
- 0100,
- 1000
- };
- int i;
-
- for(i=0;i<SIZEOF(pot);i++)
- PrintInt(pot[i]);
- return 0;
- }
下面这个程序输出什么?
- #include <stdio.h>
- int main()
- {
- int a=3, b = 5;
-
- printf(&a["Ya!Hello! how is this? %sn"], &b["junk/super"]);
- printf(&a["WHAT%c%c%c %c%c %c !n"], 1["this"],
- 2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
- return 0;
- }
下面这个程序输出什么?
- #include <stdio.h>
- int main()
- {
- int i=23;
- printf("%d %dn",i++,i++);
- return 0;
- }
为什么下面这个程序的输出不是10?我故意取消了语法高亮:)
- #include <stdio.h>
- #define PrintInt(expr) printf("%s : %dn",#expr,(expr))
- int main()
- {
- int y = 100;
- int *p;
- p = malloc(sizeof(int));
- *p = 10;
- y = y/*p; /*dividing y by *p */;
- PrintInt(y);
- return 0;
- }
下面这个程序输出什么?
- #include <stdio.h>
- int main()
- {
- int i = 6;
- if( ((++i < 7) && ( i++/6)) || (++i <= 9))
- ;
- printf("%dn",i);
- return 0;
- }
下面这段代码是否合法?
- #include <stdio.h>
- #define PrintInt(expr) printf("%s : %dn",#expr,(expr))
- int max(int x, int y)
- {
- (x > y) ? return x : return y;
- }
-
- int main()
- {
- int a = 10, b = 20;
- PrintInt(a);
- PrintInt(b);
- PrintInt(max(a,b));
- }
这是什么意思?有什么潜在的问题?
- #define SWAP(a,b) ((a) ^= (b) ^= (a) ^= (b))
这是什么意思?
- #define ROUNDUP(x,n) ((x+n-1)&(~(n-1)))
一些C语言的教材上会给出一个很经典的宏定义
- #define isupper(c) (((c) >= 'A') && ((c) <= 'Z'))
但这种宏定义的方法存在不足之处,一旦遇到下面这种情况就出问题了:
- char c;
- /* ... */
- if(isupper(c++))
- {
- /* ... */
- }
为了避免这种问题,应该怎样来定义isupper?
怎样用printf函数打印"I can print %"?别忘了百分号是用于格式化输出的。
不用任何比较运算符,写一个程序找出三个数中的最小数。
不用+号,(用位运算)实现加法运算。
最有趣的一个问题:不用分号,写一个Hello World程序。
这是有可能的,而且办法非常简单,只用到了最基本的语法规则。
实在想不出来再看答案吧(同底色的):
查看更多:http://www.gowrikumar.com/c/index.php