
C Variables Container Local/Global Lvalue/Rvalue Variable in C

C Variables Container Local/Global Lvalue Rvalue Variable
C Variable in C Programming Language: A variable is a name that may be used to store a data value. Unlike constant, variables are changeable, we can change the value of a variable during the execution of a program. A programmer can choose a meaningful variable name. It tells the compiler what the variable name is. It specifies what type of data the variable will hold.
Variables are containers
used to store a value in our program. A variable is a named area of memory
where we can store and manipulate the values of our program.
The variable name can be
chosen by the programmer in any meaningful way to reflect its function or
nature in the program. The rules for naming variables are the same as for
naming identifiers.
All variables are
temporarily stored in random access memory called RAM. For this reason, they
are automatically deleted upon completion of the function program.
Example: average, height, age, total, etc.
Rules To Define variables C Language
• Generally most of the compilers support eight characters excluding extension However the ANSI standard recognizes the maximum length of a variable up to 31 characters.
• Variable names must not start with a digit.
• Variable names can consist of alphabets, digits, and special symbols like underscore _.
• Blank or spaces are not allowed in the variable names.
• Keywords are not allowed as variable names.
There are some restrictions on variable names and symbolic constants.
Variables and constants are the main data objects processed by the program.
1. Variable Names are made up of letters and numbers.
2. The first character must be a letter.
An underscore is considered the letter "_" but does not start a variable name with an underscore.
3. Uppercase and lowercase letters are different, so v and V are two different names.
4. Traditional C practice is to use lowercase for the variable name and all uppercase for symbolic constants.
5. Keywords like other, int, float, etc. are Reserved, we cannot use them as a variable name.
6. The manifest lists the variables used and describes their types and, possibly, their initial values.
7. At least the first 31 characters of a local variable name are important. For function names and external variables, the number may be less than 31. For external names, the standard guarantees exclusivity for only 6 characters and one case.
Variables Types of C Programming Language
We are using basically two types of variables in the C Programming language.
1. Local variables
2. Global variables
Local Variable: Local variables are accessible only inside the function or block.
Global variables: A global variable can be accessed anywhere within the program.
Write a Program For Addition of Two Numbers in C Language
#include<stdio.h> #include<conio.h> void main() { int a,b,sum; //variable declaration a=30; b=20; sum=a+b; printf("Sum is: %d",sum); getch(); } *****OUTPUT***** Sum is:50 |
Initializing Variables: The variable declared can be assigned or initialized using the assignment operator “=”. The declaration and initialization can also be done in the same line.
Dynamic Initialization: The initialization of variables at run time is called dynamic initialization. Dynamic is the process during execution.
Write a Program in Dynamic Variables in C Language
#include<stdio.h> #include<conio.h> void main() { int r =2; float area ; area = 3.14 * r *r; clrscr(); printf(“area is = %f”,area); } *****OUTPUT***** area =12.56 |
Note: In the above program area is calculated and assigned to the variable area. The expression is solved at a run time and assigned to the area at a run time.
Hence it is called dynamic initialization.
Lvalue and Rvalues in C Language
There are two kinds of expressions in C:
lvalue: Expressions that refer to a memory location are called "lvalue" expressions. An lvalue may appear as either the left-hand or right-hand side of an assignment.
rvalue: The term rvalue refers to a data value stored at some memory address. An rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right-hand side but not on the left-hand side of an assignment.
int x = 10; // valid statement
10 = 20; // invalid statement; would generate compile-time error.
Constant Variables and Volatile Variables C Language
Constant Variable: To keep the value of the variable unchanged during the program execution. It can be done by declaring the variable as a constant. The keyword const is then prefixed before the declaration. It tells the compiler that the variable is a constant. Thus, constant declared variables are protected from modification.
Example: Const int x = 20;
Where const is a keyword and x is the variable name and 20 is a constant.
The Compiler protects the value of ‘x’ from modification.
The user can not assign any value ‘x’ but using a pointer the value can be changed.
If a user attempts to modify the value of the constant variable, the message ‘ cannot modify the constant object” will be displayed.
Volatile Variable: The volatile variable is those variables that can be changed at any time by another external program or the same program.
The syntax: volatile int d;
#include<stdio.h> #include<conio.h> Void main() { volatile int x; Volatile cons tint y = 10; clrscr(); printf(“Enter an integer:”); scanf(“%d”,&x); printf(“An Entered integer is : %d “,x); x = 3; printf(“\n The Changed value of x is : %d “,x ); printf(“\n Value of y: %d”,y); getch(); } *****OUTPUT***** Enter an integer: 5 An entered integer is: 5 The changed value of x is : 3 Value of y: 10 |
Read Also Post Other Programming Languages
👉Java Variables
👉Php Variables
👉C++ Variables
👉Python Variables