C Programming:Variables Introduction and Types, With Examples and Interview Questions

Variables in C Programming | Introduction and Types, With Examples and Interview Questions

C Programming; Variables in C Programming | Introduction and Types, With Examples and Interview Questions 

 

Variables :


  • Variables are nothing but are named memory locations where data get stored. The data stored inside the variable names is known as its value. The access to these data can get by their names. 

  • Each variable has a specific type which determines its size, space and layout in the memory and the set of operations applied to that variable.   

  • The value of the variable can be changed as per the programmer's need and can be reused multiple times in the program.



 
C variables can be divided into three categories:

➣Variables that stores single value at a time.
Variables that stores multiple values of same data type.
➣Variables that stores multiple values of different data type.


C Variables can have following data types:

char -1 byte
int - 2 byte
void - 0 byte (basically this type doesn't have any type)
float - 4 byte
double - 8 byte



Variable definition in C :


Variable definition tells the compiler where and how much space in the the memory can be kept for a variable. A variable definition contains the datatype and the list of one or more variables which are separated by commas.

Syntax for defining Variables:
datatype variable _name;

Examples of defining variables:
char a;
int a,b,c;
float a;
double a;


Variable initialization in C :

In initialization of variables the values are assigned to c variables.

Syntax for initializing Variable:
datatype variable _name=value;

Examples of initializing variable:
int a=10;
char b='t';



Variable declaration in C: 


  • A variable declaration provides assurance to the compiler that there exists a variable with the given type and name so that the compiler can proceed for further compilation without requiring the complete detail about the variable. 

  • A variable definition has its meaning at the time of compilation only, the compiler needs actual variable definition at the time of linking the program.
     
  • A variable declaration is useful when you are using multiple files and you define your variable in one of the files which will be available at the time of linking of the program. 

  • You will use the keyword "extern" to declare a variable at any place. 

  • Though you can declare a variable multiple times in your C program, it can be defined only once in a file, a function, or a block of code.

Syntax for declaring Variables:

extern datatype variable _name;

Examples of declaring variables:

extern char a;


Program to explain Defining variables, declaring variables, and initializing variables:

C Programming; Variables in C Programming | Introduction and Types, With Examples and Interview Questions



Lvalues and Rvalues in C :


There are two values associated with a symbolic variable:

➣It's data value, stored at some location in memory. This is sometimes referred to as a variable's rvalue.
➣Its location value,that is, the address in memory at which its data value is stored. This is sometimes referred to as variable's lvalue.


Below diagram illustrates the concept the of lvalue and rvalue :

Example- char A,C;
                  A='a';
                  C='X';


Memory address ➙ 1051                      1052                       1053                     1054
aX
                    ⇡                       A                                       C                
       Data value of variable               ⤷Variable's name 
                                                                lvalue of A is 1052                            lvalue of C is 1054     
                                                                rvalue of A is a                                  rvalue of C is X 





Types of variables in C :

  1. Local variable
  2. Global Variable
  3. Environment Variable

Local Variable : The variables which are declared inside a block of code or inside a function are known as local variables and are not valid outside its block in which it is declared.


Global Variable : The variables which are declared outside outside every function in the program are known as global variable. Global variables can be accessed in the sub functions also.

C Programming; Variables in C Programming | Introduction and Types, With Examples and Interview Questions


Environment Variable : Environment variable is a variable that will be available for all C applications and C programs.



We can access these variables from anywhere in a C program without declaring and initializing in an application or C program.


The inbuilt functions which are used to access, modify and set these environment variables are called environment functions.


 

There are 3 functions which are used to access, modify and assign an environment variable in C. They are,
1. setenv()
2. getenv()
3. putenv()



Example to illustrate the use of environment variable:


C Programming; Variables in C Programming | Introduction and Types, With Examples and Interview Questions

getenv function gets the current value of the environment variable. Let us assume that environment variable JAVA_HOME is assigned to “c:\Program Files\Java\jdk1.8.0_144”.


 Interview Questions On Variables                    


Q1. What is scope of a variable? How are variables scoped in C?A1. Scope of a variable is the part of the program where the variable may directly be accessible. In C, all identifiers are lexically (or statically) scoped. See this for more details.

Q2. What is lvalue and rvalue?

Q3. What is the difference between actual and formal parameters?


Previous                                                                                Next

For any query related to the content mail to devkumar082001@gmail.com

No comments:

Powered by Blogger.