Variables in C

Variables in C

Variable in C  is a named place in memory to store a value that can be modified by the program. Variables in C can be of three types-

1)    Synchronous Variables

2)    Asynchronous Variables

3)    Read Only Variables

1)    Synchronous Variables

The value of these variables can be changed only through the program code. All the variables used in c are synchronous except defined as constant or volatile.

2)    Asynchronous Variables

The value stored in these variables is in the control of the system itself, and can be modified asynchronously by the something outside the program code. Asynchronous variables are prefixed with volatile qualifiers.

Volatile qualifier tells the compiler:

i) Not to make assumption regarding the variable as it can change any time i.e. asynchronously.

ii) Not to make the variable register variable.

3)    Read Only Variables

The value of these variables is stored only once at the initialization time and can’t be changed after that. Although the value can be read many times hence called Read only variables. These variables are prefixed with const qualifier.

Before moving forward

let’s write our first C program

Hope you have downloaded and installed TurboC IDE cum Compiler from this Link . Now double click on the “TurboC++ for windows7” and TurboC IDE (Integrated Development Environment) would open as shown below-

Note : Editor is common for c and c++ but compiler for both differs as per file type. C Language source code files are saved with “.c” extension and C++ files as “.cpp”.

So, click on File -> new and write the following  code.

#include<stdio.h>
void main()
{
       printf("Hello India");
}

After the source codes are written, save the file with .c extension.

Explanation

#include<stdio.h> is a preprocessor directive. Its function is to include the contents of stdio.h (standard input/output) header file into the source code, before it is compiled.

The program written in c language are called source code.

Source code when compiled becomes object code, which  are in machine Language

void main() is the main function which controls the whole program. Here void is the return type, main is function name and () is function call operator. The opening curly braces indicates the begining of the program and closing curly braces indicates the end of the program.

printf() function is used to display output, here string enclosed in double quotes is shown as output.

Now to compile and run press Control+f9 key .

To view the output screen press alt+f5

which will show

Hello India

Declaration of Variables in C

The declaration tells the compiler the name and type of a variable to be used in the program.

Syntax : datatype variablename;

e.g. int x; to declare x is a variable and int is the data type.

char a;  to declare ‘a’ as character variable.

Why to declare a variable in C?

A variable in C must be declared before using for two reasons-

i) It makes it easy for the compiler to know about the name of the variable and data type, type of storage it would take.

ii) It should be declared after knowing about its requirement. A variable in C must be used if it has been declared.

 Rules for naming a variable in C-

  • A variable name should contain letters, digits and underscore only.
  • Name must begin with a letter or underscore only.
  • It can take any length but it should be between 8 to 32 characters long.
  • Variable names are case sensitive. ‘x’ and ‘X’ are two different variables in c.
  • Keywords can’t be used as variable name.

Initialization of a Variable

A declaration of a variable with some initial value is called  initialization of variable. So initialization of a variable is a process in which a variable is both declared to some data type and also assigned some initial value simultaneously.

Syntax- datatype variablename=somevalue;

int x=5;

Value assignment to Variables in C

Every variable must contain some value, when you declare a variable then no value is assigned to it. Which may result in any unpredictable value. So to assign a value to a variable after it has been declared assignment operator is used.

e.g. –      int x; // declaration

x=5; // assignment

Note- If a local variable in C is not assigned any value then it take unpredictable value , a global and static local variable takes 0 value if not initialized or assigned any value.

A variable in C can be declared at three place in a program-

  1. i) Inside a block of code or inside a function. These variables are called local variables in C.

Features of  local variables in C

  • Local variables can be used by the statements that are inside the same block in which local variables are declared.
  • You can declare a variable inside a code block that is nested inside any code block.
  • When a local variable declared within an inner block has the same name as that declared within the outer block, the local variable in the inner block hides the local variable in the outer block.
  • When we initialize the local variable to some value, this value is assigned to the variable each time it enters the block of code in which it is declared.

e.g

#include<stdio.h>
void main()
{
int guess;
printf("Think a number");
scanf("%d",&guess);
if(guess==121)
{
       int age;
       printf("Enter your age");
       scanf("%d",&age);
       printf("So, you are quite intelligent at the age of %d",age);
}    
}

Here guess and age are local variables. guess is local to outer block and age is local to inner block.

  1. ii) In the definition of function parameters. These variables are called formal parameters. Formal parameters is a list of variables separated by comma that will accept the different types of values being supplied to the function by the calling program.

Example-

#include<stdio.h>
float max(float x, float y)
{
if(x>y) return x;
else return y;
}
void main()
{
float a,b,c;
printf("Enter any two numbers");
scanf("%f%f",&a,&b);
c=max(a,b);
printf("The maximum of %f and %f is %f",a,b,c);
}

Here float x and float y are formal parameters and float a and float b are actual parameters.

iii)   Outside of all functions. These variables in c are called global functions. Global variables are available throughout the program and can be used in any part of the program.

float num;
float sum;
void main()
{
/* body of main */
}

in this example num and sum are global variables in c Language.

Variables in C has been explained in details. If you like the content so Please Like, Share and comment the Topic.

Similar Topics : Elements of C Language                   C Language Introduction

 

 

0 0 votes
Article Rating
Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Veerta Thakur
Veerta Thakur
3 years ago

Nice topic

trackback
3 years ago

[…] C Language Introduction         Elements of C Language              Variables in C […]

Please Subscribe to Stay Connected

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

DigitalSanjiv will use the information you provide on this form to be in touch with you and to provide updates and marketing.
Share via
Copy link
Powered by Social Snap