Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Sunday, April 6, 2014

Introduction to C Preprocessor Directives

As the name suggest, C++ Preprocessor is an integrated program in C++ that
preprocesses every program we write before compilation.


Preprocessor commands or directives are special functions that are executed
or processed before the compilation of the program.


Although we can write program without the knowledge of preprocessor directives
but why underutilize features when we have them?


Every preprocessor directive starts with a # symbol. While
we can have them anywhere in the program but they are almost always used at
the starting of the program before any function is defined.


Knowingly or unknowingly, we have been using one preprocessor directive. It
is the #include directive, which is most commonly used to include
header files in the program.


While there are many types of preprocessor directives, here we will only be
discussing about Macro Expansion directives and that also in the simplest sense!


Macro Expansion Directives


Have a look at the code below:


  #define MAX 10
cout<<MAX;

Here, MAX is known as ‘Macro Template’ and 10,
Macro Expansion’.


Upon preprocessing every instance of the word MAX is replaced with 10 and only
then is the program executed.


It is just like search and replace, in which every word defined as macro template
(in this case MAX) is replaced by the corresponding macro expansion (in this
case 10).


It is the common way of defining constants that doesn’t needs to be change
throughout.


A few points to note:




  • Please note that it is necessary not to end directives with semicolon (;).




  • While it is not necessary to use ALL-CAPS for macro template but it has
    become the standard.




Below is a simple example program that illustrates macro expansion directives:


  // example program in c++ to show
// the use of macro directives
#include<iostream.h>
// below is the macro, which can be
// changed to change the behaviour
// of the whole program
// DONT END WITH ;
#define MAX 3

void main(void)
{
int arr[MAX];
int i;

cout<<"enter elements of the array:";
for(i=0;i<MAX;i++)
cin>>arr[i];

cout<<"elements are:";
for(i=0;i<MAX;i++)
cout<<"
"<<i<<":"<<arr[i];
}

Good-Bye!


Related Articles:


Read More..

Saturday, April 5, 2014

Array in C

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

Lets 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.

What is array?
  1. Array is a collection of similar elements.
  2. The first element in the array is numbered 0, so the last element is 1 less than the size of the array.
  3. An array is also known as a subscripted variable.
  4. Before using an array, its type and dimension must be declared.
  5. The array elements are always stored in contiguous memory locations. This is a very important features of array.
Thus, an array is collection of similar elements. These similar elements could be all ints, or all floats, or all chars.

Array of characters is called a string whereas an array of ints or floats is called simply an array.

Keep in mind that all elements of any array must be the same type. i.e. cannot have an array of 10 numbers, of which 5 are ints and 5 are floats.
Read More..