0% found this document useful (0 votes)
2 views5 pages

Variables in C

The document provides an overview of variables in the C programming language, explaining their purpose as memory location names used to store and modify data. It details the syntax for declaring, defining, and initializing variables, as well as the types of variables including local, global, static, automatic, and external variables. Additionally, it includes examples of valid and invalid variable names and outlines the components involved in variable declaration and initialization.

Uploaded by

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

Variables in C

The document provides an overview of variables in the C programming language, explaining their purpose as memory location names used to store and modify data. It details the syntax for declaring, defining, and initializing variables, as well as the types of variables including local, global, static, automatic, and external variables. Additionally, it includes examples of valid and invalid variable names and outlines the components involved in variable declaration and initialization.

Uploaded by

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

Variables in C

 A variable is the name of the memory location.


 It is used to store information.
 Its value can be altered and reused several times.
 It is a way to represent memory location through symbols so that it can be easily identified.
 Variables are key building elements of the C programming language used to store and modify data
in computer programs.
 A variable is a designated memory region that stores a specified data type value.
 Each variable has a unique identifier, its name, and a data type describing the type of data it may
hold.

Syntax: data_type variable_name;

Example:
int name;
int num=30;

Valid examples of variable names:

1. int age;
2. float salary_1;
3. char _status;
4. double average_score;
5. int studentCount;
Invalid examples of variable names:
6. int 1stNumber; // Starts with a digit
7. float my-salary; // Contains a hyphen (-)
8. char int; // Same as a C keyword
9. int double; // Same as a C keyword

Bhushan Sarde-------------------------------Ping: 7741967818----------------------------Email id: [email protected]

Infosys Computer, Navanit Nagar, Nagpur, 440023.


10. float my$var; // Contains an unsupported special character

The three components of declaring a variable


Let us explain the three aspects of defining a variable:

1. variable declaration – int x;


2. variable definition – int x=20;
3. variable initialization – int x=20; //declaration & defining the variable, that
would change it’s value (i,e; Initialisation)

x=30; /*after some line of codes or (within a loop)

the value of the variable is changed using

(assignment/increment/decrement)*/

1. Variable Declaration :
The process of telling the compiler about a variable's existence and data type is known as variable
declaration.

It notifies the compiler that a variable with a specific name and data type will be used in the program.

Still, no memory for the variable is allocated at this moment. It is usually seen at the start of a function or
block before the variable is utilized.

Syntax – Variable Declaration : data_type variable_name;

2. Variable Definition:
The process of reserving memory space for the variable to keep its contents during program execution is
known as a variable definition.

It is based on the data type and connects the variable name with a particular memory address of sufficient
size.

A variable in C can be declared and defined in the same statement, although they can also be separated if
necessary. Example : int x=20; //defining(Assigning) a value to the variable

Bhushan Sarde-------------------------------Ping: 7741967818----------------------------Email id: [email protected]

Infosys Computer, Navanit Nagar, Nagpur, 440023.


3. Variable Initialization:

During program execution the DATA/VALUE of a particular variable may change when applied to the
Loop/ Formula.

This change is possible only if the variable is Initiated(assigned) for the Loop/formula OR

that assigned variable is used in the loop/formula.

Example : int x=20; // Initiating (Assigning) a value to the variable

x=x+1; // Here the value is increamented by 1

printf(“%d”,x); // output:: 21

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

1: Local Variable
A variable that is Declared and Initialized inside the function or block is called a local variable.

It must be declared at the start of the block.

Example : void function1()


{ int x=10;//local variable (initialized within the funciton)
}

Bhushan Sarde-------------------------------Ping: 7741967818----------------------------Email id: [email protected]

Infosys Computer, Navanit Nagar, Nagpur, 440023.


2: Global Variable
A variable that it is declared outside the function or block is called a global variable.

Any function can change the value of the global variable.

It is available to all the functions.

It must be declared at the start of the block.

Example : int value=20;//global variable


void function1()
{
int x=10;//local variable
}

3:Static Variable
A variable that is declared with the static keyword is called static variable.

It retains its value between multiple function calls.

Example : void function1()


{
int x=10;//local variable
static int y=10;//static variable
x=x+1;
y=y+1;
printf("%d,%d",x,y);
}

If you call this function many times, the local variable will print the same value for each function call,
e.g, 11,11,11 and so on.

But the static variable will print the incremented value in each function call, e.g. 11, 12, 13 and so on.

Bhushan Sarde-------------------------------Ping: 7741967818----------------------------Email id: [email protected]

Infosys Computer, Navanit Nagar, Nagpur, 440023.


4: Automatic Variable
All variables in C that are declared inside the block, are automatic variables by default. We can explicitly
declare an automatic variable using auto keyword.

Example : void main()


{
int x=10;//local variable (also automatic)
auto int y=20;//automatic variable
}

5: External Variable
We can share a variable in multiple C source files by using an external variable.

To declare an external variable, you need to use extern keyword.

Example :
FILE_NAME :: Myfile.h

extern int x=10;//external variable (also global) //program_1

FILE_NAME :: program1.c

#include "myfile.h"
#include <stdio.h> //program_2
void printValue()
{
printf("Global variable: %d", global_variable);
}

Bhushan Sarde-------------------------------Ping: 7741967818----------------------------Email id: [email protected]

Infosys Computer, Navanit Nagar, Nagpur, 440023.

You might also like