Variable Declaration & Initialization in C Language

C Variables

  • C variables is a named location in a memory where a c program can manipulate the data. Memory location is used to hold the value of the variable.
  • The value of the C variables may get change in the c program.
  • Variables are what make your c programs zoom.
  • C variables might be belonging to any of the data type like int, float, char,double,array etc.

Naming Rules for C Variables

  1. First Character should be Underscore or Alphabet
  2. Blanks & Commas are not allowed for declaration
  3. Characters Allowed for declaration :
    • Underscore(_)
    • Digits ( 0 – 9 )
    • Small Letters ( a – z )
    • Capital Letters ( A – Z )
  4. Variable name Should not be Reserved Word (Ex. if, else, switch, case etc.)
  5. No Special Symbols other than underscore(_) are allowed for declaration

Variable Declaration

syntax:

data_type variable_name;

OR

C Variables | Declaring Variables In C | Tech-Mirror
C Variables | Declaring Variables In C | Tech-Mirror

data_type variable1, variable2,…,variablen;

where, data_type is any valid c data type and variable_name is any valid identifier.

Example:

int qty;

float b;

char ch;

Constant and Volatile Variables

Constant Variables

C variables having same or unchanged value during the execution of a c program are called constant variables. A variable declared using keyword const.

Example:

const int a=50;

Volatile Variables

Those variables that can be changed at any time by some external sources from outside or same program are called volatile variables.
Any variable declared as volatile using keyword volatile.

syntax:

volatile data_type variable_name;

Variable Initialization

Variable initialization means assigning a value to the variable with ‘=’ operator.

syntax:

data_type variable_name=value;

Example:

int x = 10, y = 30;

char flag = ‘x’, ch=’l’;

Types of  Variables

  1. Local variable
  2. Global variable
  3. Environment variable

Local variable

  • The scope of local variables will be within the function body only.
  • These variables are declared within the function and can’t be accessed outside of the function.

Global variable

  • The scope of global variables will be throughout the program.
  • These variables can be accessed from anywhere in the program.
  • This variable is defined outside the main function. So that, this variable is visible to main function and all other sub functions.

Environment variable

  • Environment variable is a variable that will be available for all C applications and C programs.
  • We can access these variables from anywhere in a C program without declaring and initializing in C program.
  • The Inbuilt functions which are used to access, modify and set these environment variables are called Environment functions.

For More C Data Types

Leave a Reply

Your email address will not be published. Required fields are marked *