Tuesday, August 30, 2011

Technology Time

I basically believe or follow two approaches to learn something new:
1) Top-down approach
2) Bottom-up approach
For more information about learning things in your own way checkout the link http://en.wikipedia.org/wiki/Top-down_and_bottom-up_designweeblylink_new_window

Through out my blog I would like to follow the second method which is basically learning by example. However before getting to our first C program I would like to walk you through some basic things which I feel you might need to write your first C program.

1) If I am learning a new programming language, the first program I would want to write is print something to the output device which is usually a monitor. Below is a C program for the same:
__________________________________________________________________________________
#include
void main()
{
printf("This is my first C program, It is so exciting to learn a new language");
}
__________________________________________________________________________________
Now save your program,compile and run
o/p is as below:

This is my first C program, It is so exciting to learn a new language


Now Lets try to understand what each line means in our program:

#(hash)--> is called the pre processor directive It is a 

program that processes our source program before it is 
passed to the compiler. Preprocessor commands 
form what can almost be considered a language within 
C language. We can certainly write C programs 
without knowing anything about the preprocessor or its facilities.  So that is about the preprocessor directive. If you wanna know more about preprocessor please refer to the book - Let US C by Yeshwant kanetkar. I always admire the way he describes the technology. Ok lets move on to the next aspect.

stdio--> Standard input/output header file.. Prototypes of all input/output 
functions are provided in the file ‘stdio.h’,



2) Swapping two numbers using a temporary variable:
__________________________________________________________________________________

#include
void main()
{
int a,b,t;
printf("enter any 2 nums to swap");
scanf("%d%d",&a,&b);
printf("\n nums before swapping are a=%d and b=%d",a,b);

t=a;
a=b;
b=t;
printf("\n nums after swapping are a=%d and b=%d\n",a,b);
}
__________________________________________________________________________________

No comments: