0% found this document useful (0 votes)
33 views3 pages

Document 16

The document discusses variables in C programming. It defines a variable as a name for a memory location that can store data and have its value changed. It provides rules for defining variable names and describes different types of variables like local, global, static, automatic, and external. It also distinguishes between lvalues and rvalues, with lvalues referring to memory locations that can be assigned values and appear on the left side of assignments, while rvalues are data values stored in memory that cannot be assigned or appear on the left side. An example program is given to demonstrate using different variable types.

Uploaded by

Himanshu Jaiswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views3 pages

Document 16

The document discusses variables in C programming. It defines a variable as a name for a memory location that can store data and have its value changed. It provides rules for defining variable names and describes different types of variables like local, global, static, automatic, and external. It also distinguishes between lvalues and rvalues, with lvalues referring to memory locations that can be assigned values and appear on the left side of assignments, while rvalues are data values stored in memory that cannot be assigned or appear on the left side. An example program is given to demonstrate using different variable types.

Uploaded by

Himanshu Jaiswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Introduction:-

A variable is a name of the memory location. It is used to store data. Its value can be
changed, and it can be reused many times.
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.

Rules for defining variables


 A variable can have alphabets, digits, and underscore.
 A variable name can start with the alphabet, and underscore only. It can't start
with a digit.
 No whitespace is allowed within the variable name.
 A variable name must not be any reserved word or keyword, e.g. int, float, etc.

Types of Variables in C
There are many types of variables in c:

1. local variable
2. global variable
3. static variable
4. automatic variable
5. external variable

Lvalues and Rvalues in C


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 that is stored at some address in memory.
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.
Variables are lvalues and so they may appear on the left-hand side of an assignment. Numeric
literals are rvalues and so they may not be assigned and cannot appear on the left-hand side. Take
a look at the following valid and invalid statements −
int g = 20; // valid statement
10 = 20; // invalid statement; would generate compile-time error

What happens when we declare a variable without initialising it?

When we declare a variable without initialising it, then it just stores some
garbage value or zero value. But if we assign some value to it, then it will be
overwritten with the new value.

How does the variable work ?


Whenever a variable is declared it stores data for use in the program.it’s value can be changed ,
and it can be reused many times.

way to represent memory location through symbols so that it can be easily identified.

Let's see the syntax to declare a variable:

type variable_list;

Program for understanding the use of variable:-


#include <stdio.h> // a header file which has the necessary information to include the
input/output related functions in our program

int var0=20; //global variable defined out of the main part of program

extern int var3=30; //external variable & also used as a global variable

int main() // int is a keyword that references an integer data type

//main() function that indicates the function should return an integer value.

int var1 = 10; //local variable defined inside the program

static int var2=20; //static variable the value never will change
printf("local variable %d\n",var1);

printf("global variable %d\n",var0);

printf("static variable %d\n",var2);

printf("external variable %d\n",var3);

return 0;

Output of program::--

local variable 10

global variable 20

static variable 20

external variable 30

You might also like