2.14.2013

Array in C program

 Array Declaration 

To declare an array we need three things as:
  1. What kind types(i.e. data type) of array?
  2. What is the name of array?
  3. What is the size of array?
In our C program, we have declare array as:

syntax: data_type array_name[SIZE];
example: int result[10];

Here, int is a data type and the word result is the name of array. The [10] is however is new. The number 10 tells how many elements of the type int will be in our array. This number is often called the 'dimension' of the array. The bracket ( [] ) tells the compiler that we are dealing with an array.

 Accessing element of an Array 

Once an array is declared, we can access it and use it in our program.
Now you know that, all the array elements are numbered, starting with 0. Thus, result[10] is not the 10th elements of the array, but the 9th.
We are using the variable i as a subscript to refer to various elements of the array. This variable can take different values and hence can refer to the different elements in the array in turn. This ability to use variables to represent subscripts is what makes arrays so useful.

 Entering Data into an Array 

int result[10];
for(i=0; i<10; i++)
{
 printf("\nEnter elements: ");
 scanf("%d", &result[i]);
}

2.13.2013

Array in C

Before you kick start to reading array, you should understand why array concept is comes? How it is requires in C?

Let's read following program:

#include<stdio.h>
int main()
{
 int r;
 r=1;
 r=2;
 printf("The value of r = %d",r);
 getch();
 return 0;
}

The output of above program would be:
The value of r = 2

So, you can understand that when the value 2 is assigned into r, the earlier value of r (i.e. 1) is lost. Thus the ordinary variables are capable of holding only one value at a time.
What we do if we would want to store more than one value at a time in a single variable?
Here, the comes the concept of array.

2.12.2013

Preprocessor directives

The #if directive can be used to test whether an expression evaluates to a nonzero value or not. If the result of the expression is nonzero, then subsequent lines upto a #else, #elif or #endif are compiled, otherwise they are skipped.

Syntax of #if directive as:

#if LABEL == 10
     statement 1;
     statement 2;
#else
     statement 3;
     statement 4;
     statement 5;
#endif

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

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?

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:

2.11.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?