C Variables


Variables are data name that may be used to store a value. Variable may take different values at different times during execution.

 

Variables are may refer to some data types which are like int, float, char and etc.

C Variables

Syntax of variables

type variable_list;

Example of some valid variable declaration

int    a,b;
char   c;
float  d,month;
double k,salary;

Syntax of variables

  • A variable will have alphabets, digits, and underscore.
    Example: int _ab,a1,ab;
  • No white area is allowed at intervals variable name.
    Example: int a b;
  • A variable name will begin with alphabet and underscore solely.
    Example: int _ab;
  • A variable name should not begin with the digit.
    Example: int 5;
  • A variable name should not be any reserved word or keyword.
    Example: float, int and etc.

Types of variables

  • Local variable
  • Global variable
  • Static variable
  • Automatic variable
  • External variable

Local variable

Local variables are used in within the function only, these variables won’t work outside of the function.

void function(){
	int a=10;	// Local variable
}

Global variable

Global variables are used in the entire program, these will work anywhere in the program. These variables are defined outside of the function.

int b=20;	// Global variable
void function(){
	int a=10;	// Local variable
}

Automatic Variable

Automatic Variable is asserted within the block. The automatic variable is the default variable. Automatic variables are explicitly declared auto keyword.

void main(){  
	int a=14;	//local variable (It is also automatic variable)  
	auto int b=12;	//automatic variable  
} 

External Variable

External Variable declared with extern keyword. when you declare extern keyword for the variable then it can access in one or more source file.

file.h
extern int x=10;	//external variable (also global)  
-------------------------------------------------------
#include "file.h"  
#include   
void printValue(){  
		printf("Global variable: %d", global_variable);  
}

Static Variable

Static Variable is declared with static keyword.

void function1(){  
	int a=10;	//local variable  
	static int b=10;	//static variable   
	printf("%d,%d",a,b); 
	b++; 
}

Environment variable

Environment variables are applicable for all the C applications and C programs. There are 3 functions which can be used for these variables: setenv( ), getenv( ) and putenv( ).




OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.