Interesting C Programming Tricks

  1. Using the return value of “scanf()” to check for end of file: Very useful at online judges where inputs are terminated by EOF.
  2. “%m” when used within printf() prints “Success”: %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.
  4. brk(0); can be used as an alternative for return 0;: 5. Print Numbers 1 to 200 wihout using any loop, goto, recursion
  5. C++ Sucks? int n = 0; int step() { if (++n <= 200) printf(“%d\n”, n); } int main() { STEP256; return 1; } You will be amazed to see the output: C++Sucks Here is the explanation
  6. Do you know “GOES TO–>” operator in C? Actually –> is not an operator. In fact, it’s a combination of two separate operators i.e. — and >. To understand how “goes to” operator works, go through the below code snippet. In the example, there is conditional’s code which decrements variable x, while returning x’s original (not decremented) value, and then compares the original value with 0 using the > operator. Output: 9 8 7 6 5 4 3 2 1 0 Press any key to continue . . .
  7. Scanf Magic
  8. Add numbers without ‘+’ operator scanf(“%[^,],”,a); // This one scraps the comma scanf(“%[^\n]\n”, a); // It will read until you meet ‘\n’, then trashes the ‘\n’ scanf(“%*s %s”, last_name); // last_name is a variable