Thursday 22 August 2013

C Programming beginners - Introduction, Variable declaration and simple iteration

/*
 Global variables can be accesses any where in the program;
Local variables accessed within the scope defined by curly braces.
*/
#include<stdio.h>
#include<conio.h>
//As user defined function declared after main(), its prototype must be declared before main
void DoSum(int,int);
int Total;
void main()
{
int ValA,ValB;
clrscr();
ValA=23;
ValB=15;
printf("ValA:%d\nValB;%d\n",ValA,ValB);
Total=ValA-ValB;
printf("Before DoSum(), Value of Total=ValA-ValB=%d\n",Total);
//Calling of Function, function will add value of ValA,ValB
// and stores it in Total
DoSum(ValA,ValB);
printf("After DoSum(), Value of Total:ValA+ValB=%d",Total);
getch();
}
//DoSum() is user defined function
void DoSum(int A,int B)
{
//As Sum is Global Variable it is accesible here
//And it will holds addition of value
Total = A+B;
}

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

//Global Variable declaration
int Total;

//DoSum() is user defined function
//As function definition written before main(), no need to declare function prototype before main
void DoSum(int A,int B)
{
//As Sum is Global Variable it is accesible here
//And it will holds addition of value
Total = A+B;
}
void main()
{
int ValA,ValB;
clrscr();
ValA=23;
ValB=15;
printf("ValA:%d\nValB;%d\n",ValA,ValB);
Total=ValA-ValB;
printf("Before DoSum(), Value of Total=ValA-ValB=%d\n",Total);
//Calling of Function, function will add value of ValA,ValB
// and stores it in Total
DoSum(ValA,ValB);
printf("After DoSum(), Value of Total:ValA+ValB=%d",Total);
getch();
}

No comments:

Post a Comment

Contact Form

Name

Email *

Message *