Chapter 2.4 - Discuss How Variables Are Used In A Program | Part - 3


DECLARATION OF STORAGE CLASS

Variables in C can have not only data type but also storage class that provides information about their location and visibility. The storage class decides the portion of the program within which the variables are recognized. Consider the following example:


             Download Turbo C++ Program Program Complier                                                                                    

DECLARATION OF STORAGE CLASS

/* Example of storage classes */
int m;                                 
main()                                
{                                          
     int i;                            
    float balance;          
.....                          
.....                          
     functions1();            
}                                        
             function1()                        
{                                         
int i;                       
  float sum;               
.....                          
.....                          
}            


The variable m which has been declared before the main is called a global variable. It can be used in all the functions in the program. It need not be declared in the functions. A global variable is also known as an external variable. 

           C Numbers Pattern Programs With Easy Solutions

           C Star Pattern Programs With Easy Solutions
                  
The variables i, balance and sum are called local variables because they are declared insides a function. Local variables are visible and meaningful only inside the functions in which they are declared.

They are not known to other functions. Note that the variable i has been declared in both the functions. Any change in the value of i in one function does not affect its value in the other.       
C provides a variety of storage class specifiers that can be used to declare explicitly the scope and lifetime of variables. The concepts of scope and lifetime are important only in multifunction and multiple file programs and therefore the storage classes are considered in detail later when functions are discussed. For now, remember that there are storage class specifiers (auto, register, static and extern) whose meanings are given in Table 2.10.

The storage class is another qualifier (like long or unsigned) that can be added to a variable declaration as shown below:

auto int count;        
register char ch;      
static int x;           
extern long total ;  

Static and external (extern) variables are automatically initialized to zero. Automatic (auto) variables contain undefined values (known as 'garbage') unless they are initialized explicitly.


DECLARATION OF STORAGE CLASS


Chapter 1.6 - Recognize the programming style of C language | Part - 3


Creating Your Own Executable File

Read first the previous two part of this post Part -1 & Part - 2 after reading of this two-part, then you can be understood of the following part - 3. 



Chapter 1.6 - Recognize the programming style of C language | Part - 3

Note that the linker always assigns the same name a.out. When we compile another program, this file will be overwritten by the executable object code of the new program. If we want to prevent from happening, we should rename the file immediately by using the command. 
                         mv a.out name
We may also achieve this by specifying an option in the cc command as follows:


                         cc -o name source-file
This will store the executable object code in the file name and prevent the old file a.out from being destroyed.



Multiple Source File

To compile and link multiple source program files, we must append all the file names to the cc command.

                        ccfilename - 1.cfilename-n.c                
These files will be separately compiled into object files called
                                     filename-i.o
and then linked to produce an executable program file a.out as shown in Fig. 11

program executing steps
Fig. 11. Compilation of multiple files
It is also possible to compile each file separately and link them later. For example, the commands
                                cc -c mod1.c                                cc -c mod2.c
will compile the source file mod1.c and mod2.c into object files mod1.o and mod2.o. They can be linked together by the command


                               cc mod1.o mod2.o
we may also combine the source files and object files as follows:
                               cc mod1.c mod2.o
The only mod1.c is compiled and then linked with the object file mod2.o. This approach is useful when one of the multiple source files need to be changed and recompiled or an already existing object files is to be used along with the program to be compiled. 



MS-DOS SYSTEM

The program can be created using any word processing software in non-document mode. The file name should end with the characters ".c" like the program.c, pay.c, etc. Then the command under the MS-DOS operating system would load the program stored in the file pay.c and generate the object code. This code has stored another file under name pay.obj. In case any language errors are found, the compilation is not completed. The program should then be corrected and compiled again.


This linking is done by the command
                                          LINK pay.obj
which generates the executable code with the filename pay.exe. Now the command
                                                   pay
would execute the program and given the result. # allaboutprogramming62

Comments System

blogger/disqus/facebook