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 
                                                                                          



Chapter 1.2 - Exemplify the elementary C concept through sample programs | Part - 3


SAMPLE PROGRAM OF 3 - INTEREST CALCULATION



sample program of interest calculation

The program in Fig. 5 calculates the value of money at the end of each year of investment, assuming an interest rate of 11 percent and prints the year, and the corresponding amount, in two columns. The output is shown in Fig. 6 for a period of 10 years with an initial investment of 5000.00. The program uses the following formula. 



Value at the end of the year = Value at the start of the year (1 + interest rate) 
In the program, the variable value represents the value of money at the end of the year while the amount represents the value of money at the start of the year. The statement 
                                           amount = value;  

makes the value at the end of the current years as the value start of the next year.


C concept through sample programs
Fig. 5 Program For Investment Problem 

Let us consider the new features introduced in this program. The 5 and 6 lines begin with #define instructions. A #define instruction defines value to a symbolic constant for use in the program. Whenever a symbolic name is encountered, the compiler substitutes the value associated with the name automatically. To change the value, we have to simply change the definition. In this example, we have defined two symbolic constant PERIOD and PRINCIPAL and assigned values 10 and 5000.00 respectively. These values remain constant throughout the execution of the program. 

Program For Investment Problem
Fig. 6 Output of the investment program

The #define Directive

A #define is a preprocessor compiler directive and not a statement. Therefore #define lines should not end with a semicolon. A symbolic constant is generally written in uppercase so that they are easily distinguished from lowercase variable names. #define instructions are usually placed at the beginning before the main() function. A symbolic constant is not declared in the declaration section.

We must note that the defined constants are not variables. We may not change their values within the program by the assignment statement. For example, the statement
                                          PRINCIPLE = 10000.00;

The declaration section declares years as integer and amount, value and inrate as floating point numbers. Note all the floating-point variables are declared in one statement. They can also be declared as 
                                                         float  amount;                                                                                                              float  value;                                                                                                                    float   inrate;

When two or more variables are declared in one statement, they are separated by a comma.


All computations and printing are accomplished in a while loop. while is a mechanism for evaluating repeatedly a statement or a group statements. In this case, as long as the value of year is less than or equal to the value of PERIOD, the four statement that follows while being executed. Note that these four statements are grouped by braces. We exit the loop when the year becomes greater than PERIOD
C supports the basic four arithmetic operators (*,/,+,-) along with serval other. 

Download Turbo C++ for Windows 7, 8, and Win 10 (32-64 Bit) with all features

Turbo C++ Compiler Download

Download free new features version of TURBO C++ compiler. Turbo C++ is a discontinued C++ compiler and integrated development environment and computer language originally from Borland. Get here the latest versions free download. Fast install, easy setup installation. Most recently it was distributed by Embarcadero Technologies, which acquired all of Borland's compilers tools with purchase of its CodeGear division in 2008. The original C++ product line was put on hold after 1994 and was reviewed in 2006 as an introductory-level IDE, essentially a stripped-down version of their flagship C++ builder. 

Program Compiler Windows Application Software

Turbo C++ 2006 was released on September 5, 2006, and was available in 'Explorer' and 'Professional Editions'.The Explorer editions were free to download and distribute while the professional editions was a commercial product.



The first release of Turbo C++ was made available during the MS-DOS era on personal computers. Version 1.0, running, on MS-DOS, was released in May 1990.  An OS/2 version was produced as well.



TURBO C++ 3.0 was released in 1991 (shipping on Nov 20) and came in amidst expectations of the coming release of Turbo C++ for Microsoft Windows.  Initially released as an MS-DOS compiler, 3.0 supported C++ templates, Borland's inline assembler, and generation of MS-DOS mode executables for both 8086 real modes and 286 protected mode (as well as the Intel 80186.)



Soon after the release of Windows 3.0, Borland update Turbo C++ to support Windows Application development. The Turbo C++ 3.0 for windows product was quickly followed by Turbo C++ 3.1 (and then  Turbo C++ 4.5).

Legacy Software 

  1. Turbo C++ v1.01 and Turbo C v2.01 can be downloaded, free of charge, from Borland's Antique Software Website.
  1. Turbo  C 3.0 (DOS) was included in the Turbo C Suite 1.0, which is no longer sold by Borland.

Comments System

blogger/disqus/facebook