Variable Declaration and Definition in C

Definition: variable is a container which hold the value. lt is a name of storage space which is used to store data. Its value is changeable. It always contains the last value stored to it. It is always declared with data type. 

Variable Declaration Usually variables are declared before use either at the start of a block of code after the opening and before any other statement or outside a function.

In programming language, a variable is a storage address (identified by a memory address) paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value. We can use any combination of letters and numbers for variable and function names, but it must start with a letter.

Best C Language Variables| Local Variable|Global Variable
Best C Language Variables| Local Variable|Global Variable

    There are five types of Declaration

    1. Local variable

    2. Global variable

    3. Static variable

    4. Automatic variable

    5. External variable


    Local Variable: A local variable is one that is declared inside the function or block. It has to be stated at the beginning of the block.

    #include<stdio.h>

    #include<conio.h>

    void add()

    {

     int a=10;

    printf("Value of a:%d",a);

    }

    void main()

    {

    add();

    getch();



    Global variable: A global variable is one that is declared outside the function or block. The global variable can have its value altered by any function.

    #include<stdio.h>

    #include<conio.h>

    int a=10;

    void add()

    {

    printf("Value of a:%d", a);

    }

    void main()

    {

    add();

    getch();



    Static variable: Static variables are those that have the static keyword in their declaration.

    #include<stdio.h>

    #include<conio.h>

    void data()

    {

    static int i=0;

    printf("%d\n",i)

    i++;

    }

    void main()

    {

    data();

    data();

    data();

    data();

    getch();

    }


    Automatic variable: In C, automatic variables are those that are defined inside the block. The auto keyword can be used to explicitly declare an automatic variable.

    #include<stdio.h>

    #include<conio.h>

    void data()

    {

    int a=100;

    auto int b=10;

    printf("value of a:%d\n",a);

    printf("value of b:%d\n",b);


    }

    int main()

    {

    data();

    data();

    getch();

    }


    External Variable: You can use an external variable. Using an external variable, a global variable can share a variable across many C source files. You must use the extern keyword to declare an external variable.

      c file name edata.h

      extern int x=100; 

      edata.c save file 


      #include "edata.h"  

      #include <stdio.h>  

      void printdata()

    {  

      printf("Global variable: %d", x);  

      }

    void main()

    {

    clrscr();

    printdata();

    getch();

    }


    What is variable rule in C?

    1. The first letter of a variable should be alphabet or underscore(-).

    2. The first letter of the variable should not be a digit.

    3. Hello and hello are different identifiers.

    4. After the first character, it may be a combination of alphabets and digits.

    5. Blank spaces are not allowed in variable name.

    6. Variable name should not be a keyword.


    What is variable syntax in C?

    Data_Type variable_name = value;

    Here data_Type is a(integer, float, char) and variable_name(like a, b, c any more variable name) but variable name not define keyword name.


    What is initializing a variable in C?

    int id;

    float marks;

    char grade;


    Why do we declare a variable in a program?

    int I'd=4754;

    char name[];

    float marks=78.65;

    char grade='B';


    What is variable in C with example?

    #include<stdio.h>

    #include<coino.h>

    void main()

    {

    clrscr();

    {

    int id=8556;

    float marks=87.67;

    char grade='A';

    printf("student id:%d\n", id);

    printf("student marks:%d\n", marks);

    printf("student grade:%d", grade);

    getch();

    }


    **************OUTPUT***************

    student id:8556

    student marks:87.67

    student grade:A


    FAQ Variable in C Language

    Q.1 What is variable in programming

    Ans: Programming is a process to write a code to construct an application and the code is called program. The data or variable on which the operation is performed is called variable operands.


    Q.2 What is a static variable in C?

    Ans: A variable that is declared with the static keyword is called a static variable.


    Q.3 What is a dynamic variable in C?

    Ans: A pointer is used to keep track of each value in a dynamic variable, which can be either a single variable or an array of values. Reallocating memory after a dynamic variable is no longer required is crucial.


    Q.4 Why we use variables in C?

    Ans: The name of a memory region that we use to store data is all that a variable really is. In C or any other language, we can modify a variable's value and reuse it several times.

    Q.5  Which is the best variable name?

    Ans: Variable best name is a meaningful declared variable name. it called the best variable name.


    Conclusion

    Variable name is very the most important part of every language. variable is a container which hold and store the variable. In this post every topic cover in this article like definition of variable, rules of variable and local variable and global variable used in this post.