"A little progress everyday adds up to big results"

Monday, 12 January 2015

A C program without main

               As soon as you see the title, you might be wondered whether this is possible. But, it's actually possible.  

#include<stdio.h>

#define modify(t, r, a, i, n, s) i ## t ## a ## r
#define change modify(a, n, i, m, a, l)

int change()
{
    printf("C without main!\n");
    return 0;
}


            If you still don't trust this program, compile and execute it. Shocked ? In fact, there is a main in this program, but hidden. This is based on the concept of token pasting. What happens here is, change is initially replaced by modify(a, n, i, m, a, l). The arguments are then replaced as follows
t = a
r = n
a = i
i = m
n = a
s = l

           The statement i ## t ## a ## r gets changed to m ## a ## i ## n, which is then concatenated as main. Eventually, 'change' is changed to 'main'.  Since all these steps take place even before compilation (at the macro expansion stage), the compiler does not throw an error.
Now, become a magician by showing this trick to your friends! 

C Programming : Display only the significant digits after decimal point

Problem:
 
             In C++ programming, if we give the statements,

float pi = 3.1400f ;
cout<<pi ;

only 3.14 ( significant digits ) will be shown. 
          
             But, in C program , if we give
float pi = 3.1400f ;
printf ( "%f", pi ) ;

3.140000 will be displayed. 
            
The '%g' format specifier:      
       Of course we can use %0.2f. But, we may not always know how many decimal digits are there? In such situations we can make use of %g,


float pi = 3.1400f ;
printf ( "%g", pi ) ;

will print 3.14
 
Now, say 'Hi' to %g

C Programming : The (input) buffer problem

                      Usually in C programming, whatever we type gets stored in the keyboard buffer (temporary storage) and then the CPU takes input from the buffer. If a scanf is used to read input, this buffer is inspected first. If the input is available in the buffer itself, it is taken directly and the scanf statement terminates. Only if the buffer does not have the requested input, the user is prompted for input.
             
                    Whenever we input a number (int, float or double), we give the number and press enter to specify that our input is ready to be taken. Both (number and enter) are stored in the buffer.

                         After enter is pressed, the number gets stored in the (int, float or double) variable. But, where does the enter go? Yes, it remains in the buffer. Next time when a character or string is to be given as input, this enter will be taken directly from the keyboard buffer and we will not be prompted to input the character or string.

Example:
1.#include<stdio.h>
2.
3.int main()
4.{
5.          int num ;
6.          char ch ;
7.          printf ( "Number: " ) ;
8.          scanf ( "%d", &num ) ;
9.          printf ( "\nCharacter: " ) ;
10.        scanf ( "%c", &ch ) ;
11.        printf ( "%cBye", ch ) ;
12.        return 0 ;
13. }

Output:
Number: 2
Character:
Bye

Explanation:
2 is stored in num and '\n' ( enter ) is stored in ch.

Solution 1:
To get rid of this let us use getchar() before the second scanf(), that is at line number 10. getchar() takes the enter from the buffer. But, since we didn't assign it to any variable, it gets wasted (removed from the buffer).

Solution 2:
Instead of using getchar(), we may get the character twice, that is use scanf ( "%c %c", &ch, &ch ) ; instead of scanf ( "%c", &ch ) ; In this case, first enter is stored in ch and later, the input we give replaces it.

The best solution:Which one of the above do you think is the best solution? If you think it is solution 1, then you are wrong. If you think it is solution 2, then also you are wrong. Both of the above are only solutions, but not the best. The best and right solution is to clear the buffer using the inbuilt function.

              If you are a Turbo C programmer, use the following statement before line 10.
fflush (stdin);

                If you are a gcc programmer, use the following statement before line 10.
__fpurge (stdin); // It is double 'underscore' followed by fpurge
and include <stdio_ext.h>

Do not get caught in the buffer jail!

A C program without semicolon

C program to display “India” without using ';' anywhere in the program:

                           Before writing this program, we should first understand what printf returns and its prototype. The function declaration of printf is
int printf ( const char *, ... ) ;
                          It takes variable number of arguments, displays the string till a NULL character is found and returns the number of characters printed.

Now, the program:

#include<stdio.h>
void main()
{
    if ( printf ( "India" ) )
    {
    }
}

The printf () returns 5 in this case. Hence, the 'if' condition is true, but, we are not doing anything inside the block of 'if'.

Next time if someone asks you whether you can write a C program without semicolon, reply 'yes'

C Programming: Let the user specify the number of decimal digits after the decimal point

                   In a C program, what will we do if we want to display only specific number of digits (which is determined at run time, like user input) after decimal point? We can get the number from the user, put it in switch() and provide a case for each individual number, as follows.


int num;
float pi = 3.1415
93;
printf ( "Enter the number of decimal digits to be shown: ");
scanf ( "%d", &num );
switch ( num )
{
case 1 : printf ( "%0.1f", pi ); break ;
case 2 : printf ( "%0.2f", pi ); break ;
...................
}

                    But, is it possible to give for each and every number? Moreover, this is not the correct way of programming. Use it in a single statement, instead of switch.


int num;
float pi = 3.141593;
printf ( "Enter the number of decimal digits to be shown: " );
scanf ( "%d", &num );
printf ( "%*.*f", 0, num, pi );
 
This will display 'num' digits after the decimal point. In the last statement, the first asterisk is replaced by 0 and the second asterisk by the value of 'num'.

Give full freedom to the user!