-->

Learn to code the on-demand coding Languages in an easy way. We help you understand programming concepts and coding Techniques Better.

Saturday, January 23, 2021

Variables in C

Variables in C:



Identifiers:

     The term identifiers are the names of variables, functions, and some other user-defined Terms. The length of these identifiers can vary from one to several characters.  The first character must be either Letter/Underscore and subsequent characters must be either letters, digits, or underscore.

Types of Identifiers:

        1.  Internal: Includes the names of local variables and Functions.

        2.  External: Includes function names and global variable names that are shared between source files.

Rules for Naming Identifiers :

1. The first character must be an Alphabet(a-z) or underscore(_).

2. You cannot use a keyword as identifiers.

3. Identifier names must be unique.

4. Blank spaces cannot be used in variable names.

5. Length should be at most 31 characters.

    Examples:

       Correct                    Incorrect

         hello23                     2hello

        _hi123                       hi hello

         hi_234                       $helo


Variables:

        A variable is a named location in memory that is used to hold a value that can be modified by the program. All the variables must be declared before they can be used. Once the Variable is declared compiler will allocate the memory depend on its data type. The general form of declaration is 

syntax;  Data Type     variable List/variable name

                    int i, j, k;          [Declaration]

                    int i=10,j=20;   [Initialization]

                    float  m, n;

                    char c;      

    Remember: Don't forget to put the semicolon(;) at the end of the declaration.

  Types of Variables: 

   1.  Local Variable

   2.  Global Variable

   3.  Static Variable

   4.  Automatic Variable

   5.  External Variable

        global, static, automatic, external is the storage class specifiers in C.  These Specifiers tell the compiler how to store the subsequent variable.    

Local Variable: A variable is declared inside the function or block is called local variable. It must be declared at the start of the block.

    Example:    


    Here a,b are the formal parameters. Once the execution of the function is completed the compiler deallocates the memory of local variables.

  Global Variable: A variable can be used through the program. These variables are declared outside the function or at the start of the code.

   Example:   



static variable:  The A Variable that is declared with the static keyword is called a static variable. The variable remains its value between multiple function calls.

   Example:    



  If the function call for times during its Execution, X will print 11,12,13,14,15 and y remain the same as 10,10,10,10,10. 

automatic variable: Variables that are declared inside the block(function), are automatic variables by default, we can explicitly(outside the function) declare an automatic variable as an auto keyword. 

   Example:          



external variable: We can share a variable in multiple C filesby using external variable. extern keyword is used to declare external variable.

Ex:   file1.h 

        extern int x=10; // external variable

        source.c

        


   Explanation: First we have to create a file(file1) with  .h  extension and declare an extern variable. Now create another file(source) with  .c  extension and also include the first file at the top. Now print the variable in the second file. This is how we can share the extern variable for different C files.


 So far we have learned about variable and their declarations. In the upcoming topics, we will be using Local and Global variables.


prev<---Data Types                                            Next Topic--->Operators in C

        

No comments:

Post a Comment