C Programming - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C Variables

C Variables

shape Description

The different memory locations where data resides are called objects. To access such an object it must have a name, and such a named object is called a Variable. The information stored inside an object is called as a value. Of course the machine itself typically has no idea how to interpret the information it contains, but the program certainly needs to know. For this reason a variable has a specific type. And it's this type that constrains the use of that variable, such that only operations appropriate to the type may be performed. Any variable in C language acts like a container of data. C variables can be changed as per the requirement of the user and can be represented by any datatype like int, char, float, etc. C language variables are categorized into three groups.

C Variables Names

Computers don't care about variable names, but those names are needed to refer the underlying objects like in local variables, function parameters, and so on.
  • Variable names can consist of letters (all alphabets a to z & A to Z) and digits (all digits 0 to 9) but must begin with a letter only.
  • Special characters such as colon : , semicolon ;, period ., underscore _, ampersand & etc. can also be used.
  • Variable names are case sensitive with lowercase letters typically used for variable names, and uppercase for constants.
  • C reserves a few keywords and these cannot be used as variable names.

Variable Types

shape Conceptual figure

Global Variable in C

shape Definition

The C variables declared inside a program outside the functions are called as global variables.Global variable can be accessed even in the sub functions.

Local Variables in C

shape Description

The C variables declared in a block of code are called as local variables.Local variables cannot be used in sub programming sections.

Environment variable

shape Description

One can access environmental variables from anywhere in a C program without declaration and initialization in an application or a C-program.

shape Example

[c] #include<stdio.h> void test(); int m = 3, n = 4; //global variables int main() { int a = 1, b = 2; //local variables printf(“All variables are accessed from main function”); printf(“\nvalues: m=%d:n=%d:a=%d:b=%d”,a,b); test(); } void test() { printf(“\nAll variables are accessed from test function”); printf(“\nvalues: m=%d:n=%d:a=%d:b=%d”, m,n); } [/c]

Scope of variables in C

shape Description

Scope states where a variable has its existence. If the scope rules are not followed, they are not accessible outside that program. There are 3 places for declaring the C- variables:

Scope of Formal parameters:

shape Description

The formal parameters can be used in functions.
  • Formal parameters lie in function definition section.
  • The formal parameters are local to the definition section and can be accessed by the main function of the program.

Scope of Local Variables

shape Description

The scope of the local variable is within the block or function only.
  • Local variables are not accessed by other blocks.
  • Even if the variable name matches with the variable names outside the block, local block value cannot be taken outside the block.

shape Example

[c] { int x=10; }[/c]

Scope of Global Variables

shape Description

The scope of the global variable is throughout the program.
  • Global variables can be used anywhere in the program.
  • Once the global variable value is changed, the block which uses it, also need to change the variable value.
  • These variable values can be even used by other programs also.

shape Example

[c] int a=10; void main() { int a; }[/c]

Declaration of variables in C

shape Description

Variables must be declared in the C program before to use.
  • While declaration, memory space is not allotted for the variables.
  • Value can be declared many times in the program.

shape Syntax

datatype variable_name;

Initialization of variables in C

shape Description

The values are assigned to the C variables.
  • Initialization allots the memory space as per the datatype given.
  • Value is declared only once.

shape Syntax

datatype variable_name=value;

shape Example

[c] #include<stdio.h> extern int a; //Declaration of variable a int a=5; //Initialization of variable a int main() { printf("%d",a); return 0; } [/c]

Summary

shape Key Points

  • Global variables can be declared anywhere.
  • Local variables cannot be used outside of the scope.
  • Every variable has to be initialized.

shape Programming Tips

C variables must be declared with a type at the beginning of the program.