Variable

                            1.6 VARIABLE
We so many times come across the term “variable” .we assume them in math’s for solving some
Equation . so let see what is the meaning of variable in programming . in c programming variable are
Simple names used to refer to some location in memory that hold a value with which we are working .
Syntax for declaration of variable:    data type   variablename :
#include <stdio.h>
int main(){
int firstNumber, secondNumber, sumOfTwoNumbers;
    printf("Enter two integers: ");
// Two integers entered by user is stored using scanf() function
    scanf("%d %d", &firstNumber, &secondNumber);
 // sum of two numbers in stored in variable sumOfTwoNumbers
    sumOfTwoNumbers = firstNumber + secondNumber;
    // Displays sum     
    printf("%d + %d = %d", firstNumber, secondNumber, sumOfTwoNumbers);
 return 0;
}
der a small situation   we have to add 2 numbers so we have to write a program about it.



As you can see that we have declare two variable in which we are giving  our desire valve during
Execution of program. As c is a programming language it contains some rule for give the name for
Variable let see them
1.variable name  should not be reserved word
2.firsr character should be an alphabet or underscore
3. blank spaces and commas are not allowed .
4.characters allowed : underscore ,uppercase(A-Z),lowercase(a-z),digits (0-9).
These are some rule you should kept in mind when giving a name to variable.
In c language there are two types of variable
1.Local variables : these variable is accessible only from function  or block in which it is declared .

2.global variables : these variable are globally available in the  sense  these can be accessible any block or function in the program.
From below program we see the difference between them
#include <stdio.h>
 /* global variable declaration */
int g;
 int main () {
 /* local variable declaration */
  int a, b;
   /* actual initialization */
  a = 10;
  b = 20;
 g = a + b;
printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
return 0;
}



Remember that local variable has higher priority that global variable .(it means that global variable is also visible  inside function ,provided that it should not be re declared with same name inside the function in that case complier consider  the local variable inside that function .)

Comments

  1. Harrah's Resort Southern California - Mapyro
    Find your 동해 출장샵 way around the 사천 출장안마 casino, find the best casino and entertainment experience 구리 출장마사지 in 목포 출장샵 Valley Center, CA. Save big with a Map! Harrah's Rincon Casino Resort 논산 출장샵

    ReplyDelete

Post a Comment

Popular posts from this blog

Basic input, output and variables

Data Types in C