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 2.4 - Discuss How Variables Are Used In A Program | Part - 2


User-Defined Type Declaration


C supports a feature known as "type definition" that allows users to define an identifier that would represent an existing data type. The user-defined data type identifier can later be used to declare variables. It takes the general form. 

typedef type identifier;
Where type refers to an existing data type and "identifier" refers to the 'new" name given to the data type. The existing data type may belong to any class of type, including the user-defined ones. Remember that the new type is 'new' only in name, but not the data type. typedef cannot create a  new type. Some examples of type definition are:

typedef int units;     
typedef float marks;

Here, units symbolize int and marks symbolizes float. They can be later used to declare variables as follows:

units batch1, batch2;               

batch1 and batch2 are included as int variable and name1[50] and name2[50] are declared as 50 element floating point array variables. The main advantage of typedef is that we can create meaningful data type names for increasing the readability of the program.
Another user-defined data type is an enumerated data type provided by ANSI standard. It is defined as follows: 

enum identifier {value1, value2....valuen};

The "identifier" is a user-defined enumerated data type which can be used to declare variables that have one of the values enclosed within in braces (known as enumeration constants). After this definition, we can declare variables to be of this 'new' type as below:

enum identifier v1, v2,....vn;

The enumerated variables v1,v2....vn can only have one of the values value1, value2,...valuen. The assignments of the following types are valid:


v1 = value3;
v5 = value1;

An example:

enum day {monday, Tuesday,...Sunday};
enum day week_st, week_end;                 
week_st = Monday;                                   
week_end = Friday;                                   
if(week_st == Tuesday)                              
week_end = saturday;                                   

The compiler automatically assigns integer digits beginning with 0 to all the enumeration constants. That is, the enumeration constant value 1 is assigned 0, value2 is assigned 1, and so on. However, the automatic assignments can be overridden by assigning values explicitly to the enumeration constants.



             C Programming Star Pattern Programs     

For example:


enum day {Monday = 1, Tuesday,....Sunday};

Here, the constants Monday is assigned the value of 1. The remaining constants are assigned values that increase successively by 1.
The definition and declaration of enumerated variables can be combined in one statement.

Example:

enum day {Monday,......Sunday} week_st, week_end;


Chapter 2.4 - Discuss how variables are used in a program | Part -1



DECLARATION OF VARIABLES

After designing suitable variables names, we must declare them to the compiler.
The declaration does two things:
  1. It tells the compiler what the variable name is.
  2. It specifies what type of data the variable will hold.
The declare of variables must be done before they are used in the program.

     C language tutorials for beginners

    Download Let us C eBook | by Yashwant Kanetkar

DECLARATION OF VARIABLES


Primary Type Declaration

A variable can be used to store a value of any data type. That is, the name has nothing to do with its type. The syntax  of declaring a variable is as follows:

data-type v1,v2,....vn
v1,v2,...vn are the names of variables. Variables are separated by commas. A declaration statement must end with a semicolon. For example, valid declarations are:

int count;            
int number, total;
double ratio;       



int and double are the keywords to represent integer type and real type data values respectively. Table 2.9 shows various data types and their keywords equivalents. #variables_in_c

Discuss how variables are used in a program | Part -1, What is a variable in computer programming, Computer Programming Variables, C variable with examples, attributes of a variable in programming.
Discuss how variables are used in a program | Part -1, What is a variable in computer programming, Computer Programming Variables, C variable with examples, attributes of a variable in programming.  


The program segment given in Fig 2.7 illustrates the declaration of variables. main() is the beginning of the program. The opening brace { single the execution of the program. Declaration of variables is usually done immediately after the opening brace of the program. The variables can also be declared outside (either before or after) the main function. The importance of the place of the declaration will be dealt with in detail later while discussing functions. #allaboutprogramming62



Note-: C99 permits declaration of variables at any point within  a function or block, prior to their use

Discuss how variables are used in a program | Part -1, What is a variable in computer programming, Computer Programming Variables, C variable with examples, attributes of a variable in programming.

When an adjective (qualifier) short, long, or unsigned is used without a basic data type specifier, C compilers treat the data types as an int. If we want to declare a character variable as unsigned, then we must do so using both the terms like unsigned char


Default values of Constants 

Integer constants, by default, represent int type data. We can override this default by specifying unsigned or long after the number (by appending U or L) as shown below:

Literal                             Type                                      Value

+111                                int                                           111
-222                                 int                                           -222
45678U                           unsigned int                           45678
-56789L                           long int                                  -56789
987654UL                       unsigned long int                   987654




Similarly, floating point constants, by default represent double type data. If we want the resulting data type to be float or long double, we must append the letter f or F to the number for float and letter l or L long for long double as shown below:


Literal                             Type                                      Value

0.                                     double                                     0.0
-222                                 double                                     0.0
45678U                           double                                     12.0
-56789L                          double                                     1.234
987654UL                       float                                       -1.2
1.23456789L                   long double                          1.23456789


           Top 10 Useful Basic Commands for Linux Users

           C Pattern Programming Example Tutorial 
                                                                                          



Comments System

blogger/disqus/facebook