Variables in c

 

Variables: -

  • A variable is a data name or an identifier which stores a value that can be changed during the program execution. 

  • Every variable might belong to any of the data types, like int, float, char, etc. 

  • The name of a variable can be composed of letters, digits, and the underscore character.

  • It must begin with either a letter or an underscore. Variables are case-sensitive because uppercase and lowercase letters are distinct.

  • C programming language also allows defining various other types of variables, like numeric array, character array, pointer, Structure, Union, Enumeration and etc.

             Example: -

sum,

height,

_value



Declaring a variable in C: -

  • Variable declaration means telling the compiler about the name and type of a variable before using it in the program.

  • Variable declaration is the process of reserving a memory location and defining the type of data that will be stored in that location.

  • Memory space is not allocated when a variable is declared.

 

Syntax: -   data_type   variable_name;

Example: -  int sum;



Initializing a variable in C: -

  • Variable initialization means assigning an initial value to a variable at the time of declaration.

  • Variable initialization is the process of assigning a value to a declared variable when it is created.

  • Memory space is allocated, when variable is initialized.

Syntax: -   data_type   variable_name  =  value;

 Example: - int sum = 10;

 

Types of variables: -

  • Based on scope, there are two types of variables in C, they are

  1. Local variable
  2. Global variable

In C programming, scope refers to where a variable can be accessed or used in the program.

 

  1)  Local variable: -

  • The variables which declared or defined inside the function and can’t be accessed outside the function are known as local variables.

  • Local variables are only accessible within that scope.

  • The default value of local variables is garbage value.

  • When a function is called, memory for its local variables is allocated on the stack, and when the function ends, that memory is automatically freed.

NOTE: -

The stack is a special region of memory in C programming. It grows automatically when a function is called and shrinks when the function ends. The stack follows the Last In, First Out (LIFO) structure, meaning the last function called is the first to finish. It is mainly used to store local variables, function parameters, and return addresses during function execution.

Example: -


output: - x = 10


2) Global variable: -

  • The variables which are declared or defined outside of all the functions and can be accessed in all functions is known as global variable.

  • Global variables are accessible by all functions in the same file or other files (with extern).

  • The default value of global variables is zero.

  • The global variables are stored in data segment of memory, it is a part of program’s RAM memory layout.

Note: -

The data segment is a portion of a C program’s memory where global and static variables are stored. It is a part of the program’s RAM memory layout, managed by the operating system when the program runs.

 Example: -




or 



output: -    x = 10


Note: -   if you write extern int x = 10; statement in the program which is outside of all the functions then this x variable can be used in same program and we can use in other programs also.












Popular posts from this blog

operators in c programming

Cloud Storage and Local Storage: Applications in Business