Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Sunday 5 November 2017

How to Declare Variables in C Language


Variables in C Programming Language

Variables in C Language

In this article, We are going to discuss what is variable in c programming language, types of variables in c language, and how to declare variables in c language with simple syntax or examples.

In the last article of  c language tutorial, We saw how to print "hello world" in c and how to compile and run first program in c language. But here we will learn variable declaration and definition in c and types of variables in c programming.


What is Variable in C?

Variables names are names given to location in memory. These location can contain integer, real, or character constant. In c, a variable is an entity that may change during the program execution i.e the value of variable can be changed.

Variable is used to store the data of different-different types e.g integer, real, character, etc.

Variable is an identifier which stores the data in memory.

Syntax of C Variable

datatype variable_name;

             or

datatype variable_name, variable_name2, variable_name3;


Declaration of Variables 

Let's declare variables with data types.

int x;
char y;
float z;
double a;

In the above declaration, int, char, float, double is a data types and x, y, z, a are variables.


Initialization of Variables

We can initialize variables in c. In other words, we can put some values in variables.

For example:

int x = 10, b = 40;
char y = 'a';
float z = 2.1;

Some rules for declaring variables in c

There are some rules for declaring variables in c programming.
  • Each variable name can contains A - Z(capital letter), a - z(lower letter) and 0 - 9(digits), and the under score(_) character.
  • Each variable name should start with alphabets or underscore(_).
  • In variables, No white space are allowed.
  • Variable name cannot start with digits.
  • Except underscore symbol, we cannot use other symbols in the middle of the variable name. We can use only underscore(_) symbol in the middle of variable names.
  • A variable name must not be same as any keyword e.g int, float, etc. We cannot use int, float, char because it reserve word or predefined keyword.
  • Variable names are case sensitive.
  • Variable values can be alphabets or numeric.
  • Type of variable can be int, char, float, double, and void.

Let's talk about some types of variables in c programming language one -by-one.

Types of variables in C

There are some types of variables in c language and these are given below.

(1) Global Variable 

Global variable is a variable which we can declared only outside of a function or block is known as global variable. We cannot declare global variable inside any functions or blocks in c .

Global variable is available to all the functions. We can declare global variable on top of the program or above main() method.

For example:

#include<stdio.h>
#include<conio.h>

int i;//Global Variable
void main()
{}


(2) Local Variable

Local variable is a variable which we can declare inside any functions or blocks is known as local variable in c. It can only use within the function or block where it is declared. We can't declare local variable outside of the function or block.

Before accessing local variables in c we have to initialized first then access.

For example:

#include<stdio.h>
#include<conio.h>

void main()
{
int x = 30;//Local Variable
}


(3) Automatic Variable

All variables which declared inside the block or function is automatic variable by default. We can also use "auto" keyword to define automatic variable.

For example:

#include<stdio.h>
#include<conio.h>

void main()
{
int i = 40;//local variable(automatic variable also)
auto int x = 50;//automatic variable with auto keyword
}


(4) Static Variable

When we declared any variable with static keyword in known as static variable in c.

For example:

#include<stdio.h>
#include<conio.h>

void main()
{
int i = 40;//local variable
static int j = 50;//static variable
}


(5) External Variable

External variable is also a global variable and when we define any variable by using "extern" keyword is known as external variable in c.

For example:

//first.h

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

use in any c program by including e.g #include "first.h"


C program with variable

This is the simple program of c language with variables where we declare and initialize variable and print the value of global and local variable.

For example:

#include<stdio.h>
#include<conio.h>
int x = 30;//global variable
void main()
{
int y = 40;//local variable 
printf("Value of global variable %d ", x);
printf("Value of local variable %d ", y);
getch();
}

here we discussed some basic concepts of c language e.g what is variables, types of variables and how to declare and initialize variables in c language.




Share:

1 comment:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate