In this article, I am going to share some rare tricks of the 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 are 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 an 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
8. 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 the “goes to” operator works, go through the below code snippet.
In the example, there is conditional’s code that decrements variable x, while returning x’s original (not decremented) value, and then compares the original value with 0 using the > operator.
int _tmain(){
int x = 10;
while( x –> 0 ) // x goes to 0
{
printf(“%d “, x);
}
printf(“\n”);
}
Output:
9 8 7 6 5 4 3 2 1 0
Press any key to continue . . .
9. Scanf Magic
scanf(“%[^,]”, a); // This doesn’t scrap the comma
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
10. Add numbers without ‘+’ operator
int Add(int x, int y)
{
if (y == 0)
return x;
else
return Add( x ^ y, (x & y) << 1);
}
12 Comments
Add numbers without ‘+’ operator
int Add1(int x,int y){
return (x|y);
}//Simple one
if x=1 and y=1 output according to your code will be 1
That is incorrect, there is nothing in “or” that concerns the carry bit –
1|1=1
Since the first bit of each number is 1, it keeps it on, but doesn’t carry.
Did you even try your suggestion? It does not work.
your code will be wrong, if two numbers are same. Ex:- 10,10;30,30;7,7.
It wont for same number like 4,4
in which compiler did you try to use those trips?
int a=1,b=3,sum;
sum=(a^b^(a&b)<<1);
Printf("%d",sum);
These tricks are not so much interesting as useless, and perhaps dangerous to teach people. Programs are intended to be read, that’s why people invented C in the first place, so that you can write code like this:
int main()
{
int i = 1;
return 0;
}
NOT like this:
55 (push %ebp)
89 E5 (mov %esp, %ebp)
83 EC 08 (sub $0x8, %esp)
C7 45 FC 01 00 00 00 (movl $0x1, -4(%ebp))
83 EC 0C (sub $0xc, %esp)
6A 00 (push $0x0)
E8 D1 FE FF FF (call 8048348)
There is a reason why OOP languages like Java and Python are created, because writing hard to understand code is useless to everyone. In the early days of computing before there were compilers, people had to write code in machine language. It was expensive, error prone, and only a select few can write programs, and it took weeks to do so. But look at how easy it is to write code today, kids in middle school across the country can do it.
Writing obscure code in C is worse than writing it in assembly language, or machine language, it makes it difficult to read. If you want to write difficult to read programs, why not just write it in machine language in the first place, like I did in the above example? It defeats the purpose of using a high level language. Plus if you work in a team, which most software people do, no one will want to work with you because no one wants to figure out your hard to read code, they have enough to do without you giving them extra work. Hard to read means hard to maintain, and companies don’t like code that’s hard to maintain, it means their product will be more error prone and will cost them money in the long run.
So the take away message from someone who has been using C since 10 years old it to write clear and easy to read code. I have programmed in C for almost 30 years, and I always write my C code to be very clear and easy to read. Like int a = 1; int b = 2; int sum = a + b; Can you write that in a more compact and more obscure way, certainly. But I never do, because it is meant to be read by people, and simple code equals happy co workers.
I agree with Luther. I have been programming in ‘C’ for 30+ years, and if I cannot read your code on a late Sunday evening while drunk, then your code is too complex.
I don’t even want have to figure out embedded ++ order. Keep it simple.
Just a heads up: for the one where you say to use while ( x -> 0), you were pretty close, but it’s actually while (x –> 0 )
sollu