In this article, I am going to share some rare tricks of C programming language. To be honest, I collected those tricks from various websites. Some of the tricks are really interesting because I never heard about them before and I’m sure you too. Next time when your professor starts boasting about his/her knowledge of C programming, simply ask about these tricks, If you get the right answer with explanation, he/she is the best teacher you’ll ever find.
Interesting C Programming Tricks
1. Using the return value of “scanf()” to check for end of file:
while(~scanf(“%d”,&n)) {
/* Your solution */
}
Very useful at online judges where inputs are terminated by EOF.
2. “%m” when used within printf() prints “Success”:
printf(“%m”);
%m only prints “Success” when “errno == 0”, it’s short for a string representation of the last observed error state. For example if a function fails before the printf then it will print something rather different. *
3. Implicit initialization of integer variables with 0 and 1.
main(i){
printf(“g = %d, i = %d \n”,g,i);
}
4. brk(0); can be used as an alternative for return 0;:
5. Print Numbers 1 to 200 wihout using any loop, goto, recursion
#include<stdio.h>
#define STEP1 step();
#define STEP2 STEP1 STEP1
#define STEP4 STEP2 STEP2
#define STEP8 STEP4 STEP4
#define STEP16 STEP8 STEP8
#define STEP32 STEP16 STEP16
#define STEP64 STEP32 STEP32
#define STEP128 STEP64 STEP64
#define STEP256 STEP128 STEP128int n = 0;int step()
{
if (++n <= 200)
printf(“%d\n”, n);
}int main()
{
STEP256;
return 1;
}
7. C++ Sucks?
#include <stdio.h>
double m[]= {7709179928849219.0, 771};
int main()
{
m[1]–?m[0]*=2,main():printf((char*)m);
}
You will be amazed to see the output: C++Sucks

ConversionConversion EmoticonEmoticon