variables in C language

variable in simple terms is a storage place which has some memory allocated to it. Basically, a variable used to store some form of data. Different types of variables require different amounts of memory and have some specific set of operations which can be applied to them.
Variable Declaration:
A typical variable declaration is of the form:
  type variable_name;
    or for multiple variables:
  type variable1_name, variable2_name, variable3_name;
A variable name can consist of alphabets (both upper and lower case), numbers and the underscore ‘_’ character. However, the name must not start with a number.
Difference b/w variable declaration and definition
The variable declaration refers to the part where a variable is first declared or introduced before its first use. A variable definition is a part where the variable is assigned a memory location and a value. Most of the times, variable declaration and definition are done together.
See the following C program for better clarification:
filter_none
edit
play_arrow
brightness_4
#include <stdio.h>
int main()
{
    // declaration and definition of variable 'a123'
    char a123 = 'a'
  
    // This is also both declaration and definition as 'b' is allocated
    // memory and assigned some garbage value.   
    float b;  
  
    // multiple declarations and definitions
    int _c, _d45, e; 
  
    // Let us print a variable
    printf("%c \n", a123);
  
    return 0;
}
Output:
a
Is it possible to have a separate declaration and definition?
It is possible in case of extern variables and functions

No comments:

Post a Comment

Featured Post

Core and Advance Python

                      Language Fundamentals