Some unknown features or tricks in C language

I have found a small list of features or tricks in C language which I assume only few people would know.

1. Add any numbers without using the addition operator

Since the printf() function returns the number of characters that it prints, we could use this to add numbers as shown below:

 

Bitwise operators could also be used to achieve the same thing as below:

 

2. Use of Conditional Operator

We mostly use it in the following form:

But it could also be used as:

3. Write a return statement in a function that returns void

 

4. The use of comma operator:

It is used mostly in statements like:

 

But you can use this operator anywhere. Observe:

 

Each statement is evaluated, but the value of the expression will be that of the last statement evaluated.

5. Initializing structure to zero:

this will zero all stucture elements.

6. Multi-character constants

This sets x to 0x41424344 (or 0x44434241, depending on architecture).

7. Printf in C allows you to use variables for formatting format specifiers themselves

the * character achieves this effect.

2 Comments

    • The first program: look at the format in printf(), there is “%*c”.
      The “%*c” will take two parameters. the first must be an integer and the second must be a char. For an example:
      printf(“%*c”, 2, ‘a’); // equals to printf(“%2c”,’a’);
      And the printf() will return an integer indicates how many characters have been printed.

Leave a Reply