Tuesday, 12 February 2013

Conditional Compilation

What is conditional compilation?
You can understand it by its name i.e. a program is compiling according condition.
In other word, we can, if we want, have the compiler skip over part of a source code by inserting the preprocessing commands #ifdef and #endif, which have the general form:

#ifdef macroname
    statement 1;
    statement 2;
    statement 3;
    statement 4;
#endif

if macroname has been #defined, the block of code will be processed as usual, otherwise not.

File Inclusion

The first preprocessor directive is macro and the second preprocessor directive is file inclusion.
File inclusion causes one file to be included in another.

#include "filename"

What is the meaning of above syntax?
It is simply causes the entire contents of filename to be inserted into source code at that point in the program.

Why file inclusion is used?

If we have a very large program, the code is best divided into several different files, each containing a set of related functions. It is a good programming practice to keep different sections of a large program separate. These files are #included at the beginning of main program file.
There are some functions and some macro definitions that we need almost in all program that we write. These commonly needed functions and macro definitions can be stored in a file, and that file can be included in every program we write, which would add all the statements in this file to our program as if we have typed them in.

There are two way to write #include statement as:

1. #include "myfile.h"
   This command would look for the file myfile.h in the current directory as well as  the specified list of directories as mentioned in the include search path that might have been setup.

2. #include<myfile.h>
   This command would look for the file myfile.h in the specified list of directories only.

Monday, 11 February 2013

Macro Rules and Example

Macro without argument example

A #define directive is many a time used to define operators as shown below:

#include<stdio.h>
#define OR ||
#define AND &&
int main()
{
 int p=10,q=20,r=30;
 if((p==10) AND (q<25 OR r>=50))
    printf("You are winner!!");
 else
    printf("You are loser!!");
 getch();
 return 0;
}

The output of above program would be:
Output of using macro in C program
Figure: Screen shot of shows macro uses in C program


A #define directive could be used even to replace a condition as:


#include<stdio.h>
#define OR ||
#define AND &&
#define RESULT ((p==10) AND (q<25 OR r>=50))
int main()
{
 int p=10,q=20,r=30;
 if(RESULT)
    printf("You are winner!!");

 else
    printf("You are loser!!");
 getch();
 return 0;
}

The output of above program would be:
Output of macro C program
Figure: Screen shot of shows macro uses in C program

A #define directive could be used to replace even an entire C statement.

#include<stdio.h>
#define DISPLAY printf("Yes, I got iT");
int main()
{
 DISPLAY;
 getch();
 return 0;
}

The output of above program would be:
Output of using macro in C program
Figure: Screen shot for macro example C program


Macro with argument example

#include<stdio.h>
#define SQUARE(p) (p*p)
int main()
{
 int n,result;
 printf("Enter any number: ");
 scanf("%d", &n);
 result = SQUARE(n);
 printf("Square of %d is %d",n,result);
 getch();
 return 0;
}

The output of above program would be:
Output of macro square C program
Figure: Screen shot of macro square C program


Keep in mind some following point, when you create macro

1. If there are two or more macro expansions then, entire macro expansions should be enclosed within parentheses.
Example:
#define SQUARE(p) p*p  //wrong 
#define SQUARE(p) (p*p)  //right

2. There should be not a blank between the macro template and its argument while defining the macro.
Example:
#define SQUARE (p) p*p  //wrong 
#define SQUARE(p) (p*p)  //right

3. Macro can be split into multiple lines, with a '\'(back slash) present at the end of each line.
Example:
#include<stdio.h>
#define MYPROG for(c=1; c<=10; c++)        \
               {                           \
                if(c==5)                   \
                   printf("Good C blog."); \
               }
int main()
{
 int c;
 MYPROG
 getch();
 return 0;
}

The output of above program would be:
Output of macro split C program
Figure: Screen shot for macro split C program


Related article:

Sunday, 10 February 2013

Macro Expansion

What is macro?

Let's understand macro using following program:

/*c program for macro expansion*/
#include<stdio.h>
#define LENGTH 3
#define WIDTH 2
int main()
{
 int r,c;
 for(r=1; r<=LENGTH; r++)
 {
  for(c=1; c<=WIDTH; c++)
     printf("%d%d",c,r);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of macro C program
Figure: Screen shot of macro C program

In above program, instead of writing 5 in the for loop we are writing it in the form of text as LENGTH and WIDTH, which have already been defined before main() through the statement.
#define LENGTH 3
#define WIDTH 2
This statement is called 'macro definition' or macro.
LENGTH and WIDTH in the above program are often called 'macro templates' , whereas 5 and 3 are called their corresponding 'macro expansions'.

What is reason of using macro in the program?

Macro makes the program easier to read.
For example: if the phrase "%ls[$2]!@" cause the screen to clear, but which would you find easier to understand in the middle of your program "%ls[$2]!@" or "CLRSCREEN"? 
#define CLRSCREEN "%ls[$2]!@"

Macro makes the easyness if we want to change the vale of constant in program. We can change value values of a constant at all the places in the program by just making a change in the #define directive.
This conversion may not matter for small programs shown above, but with large programs, macro definitions are almost indispensable.

Now you will be thinking, there are any similarity in macro and variable? Can i use variable as macro?
(i recommended, before you read next line, you should think about macro and variable properties.)

After thinking you will be find that we also use variable as macro. Then why not use it?

Why the variable is not using as macro because:
  1. Variable may inadvertently get altered somewhere in the program. so it's no longer a constant that you think it is.
  2. Variable is inefficient, since the compiler can generate faster and more compact code for constant than it can for variables.
  3. Using a variable for what is really a constant encourages sloppy thinking and makes the program more difficult to understand: if something never changes, it is hard to imagine it as a variable.

Related Article:
  1. General rules and example of macro

Friday, 1 February 2013

Find Prime Factor of number


/*Program to find out prime factors of a given number.*/

#include<stdio.h>
#include<conio.h>

void main()
{
int number;
printf("Enter a number ");
scanf("%d",&number);
printf("\nThe prime factors of %d are:",number);
for(int num=2;num<32767;num++)
{
int i=2;
while(i<=num-1)
{
if(num%i==0)
{
break;
}
i++;
}
if(i==num)
{
if(number%num==0)
{
number=number/num;
printf("\t%d",num);
num=1;
}
else
{
continue;
}
}
}
getch();
clrscr();
}

Find Generic root of number


#include <stdio.h>
int main()
{        

int num,i;
printf("Enter any number: ");
scanf("%d",&num);
printf("Generic root: %d", (i = num % 9) ? i : 9);
return 0;
}

Add Two numbers without using operator


#include<stdio.h>

int main(){
  
    int a,b;
    int sum;

    printf("Enter any two integers: ");
    scanf("%d%d",&a,&b);

    //sum = a - (-b);
    sum = a - ~b -1;

    printf("Sum of two integers: %d",sum);

    return 0;
}