LANKAFREEZONE.COM
C PROGRAM

MAIN

 

C PROGRAM HISTORY

              By 1960, a number of computer languages had come into existence, almost each for a specific purpose. For example, FORTRAN for Engineering and Scientific Applications. At this stage, people started thinking that instead of learning and using so many languages, each for a different purpose, why not use only one language, which can program all possible applications. Therefore, an international committee was set up to develop such a language. This committee came out with different languages like ALGOL 60, Combined Programming Language (CPL), Basic Combined Programming Language (BCPL) etc. However, these turned out to be so big, having so many features, that it was hard to learn and difficult to implement. Around the same time, Ken Thompson at AT & T's Bell Labs, as a further simplification of CPL, wrote a language called B. But like BCPL, B too turned out to be very specific. Dennis Ritchie inherited the features of B and BCPL, added some of his own and developed C. Ritchie's main achievement is the restoration of the lost generality in BCPL and B, and still keeping it powerful. C's compactness and coherence is mainly due to the fact that it's a one-man language.

Let us now see how does C compares with other programming languages. All the programming languages can be divided into two categories:

·         Problem oriented languages or High level languages:
These languages have been designed to provide better programming efficiency,i.e., faster program development. Examples of languages falling in this category are FORTRAN, BASIC, and PASCAL.

·         Machine oriented languages or Low level languages:
These languages have been designed to provide better machine efficiency,i.e., faster program execution. Examples of languages falling in this category are Assembly language and Machine language.

C stands in between these two categories That's why it is often called a Middle level language, since it was designed to have both relatively good programming efficiency (as compared to Machine oriented languages) and relatively good machine efficiency (as compared to Problem oriented languages).

 Structure
Any 'C' program consists of one or more distinct units called 'functions'. These functions consist of valid C statements and are linked together through function calls. A function is analogous to a subroutine or a procedure in other high level languages. Every function in a program has a unique name and is designed to perform a specific task. The task to be accomplished by the function is defined by a group or block of instructions or statements. Each function with its block of statements is treated as one single unit in C language and can be placed anywhere in the program.

Each instruction in a function is written as a separate statement. These statements must appear in the same order in which we wish them to be executed; unless of course the logic of the problem demands a deliberate 'jump' or transfer of control to a statement, which is out of sequence.

However big a C program is, the following rules are applicable to all statements present in it:

·         Blank spaces may be inserted between two words to improve the readability of the statement. However, no blank spaces are allowed within a word.

  • Usually all C statements are entered in small case letters.

·         C has no specific rules about the position at which different parts of a statement are to be written. Not only can a C statement be written anywhere in a line, it can also be split over multiple lines. That is why it is many a time called a free form language.

Any C statement always ends with a semicolon ( ; )

 

The skeleton of a simple C program is given below :

main()
{

statement 1 ;
statement 2 ;

}
function1()
{

variable declarations;
statement 1;
statement 2;

}

·         A C program is nothing but a collection of one or more functions. A function name is always followed by a pair of parenthesis, as in case of main().

·         Every program must have a special function named main(). The program execution starts from this function.

·         The statements within a function are always enclosed within a pair of braces { }.

·         The group of statements within main() are executed sequentially. The closing brace of the main() function signals the end of the program. When this brace is reached, the program execution stops and the control is handed over to the operating system.

·         There should be a main() function somewhere in the program so that the computer can determine where to start the execution.

·         The main() function can be located anywhere in the program, but the general practice is to place it as the first function for better readability.

// Program to print "hello world"

#include < stdio.h >
main( )
{

printf("hello world \n");

}

After entering the program, you will have to compile the program and then execute it. But how to run this program, depends on the system you are using. C program is a collection of functions and variables. The function main( ) in the above program indicates the beginning of the program. Usually the function main( ) calls other functions to help perform its job. Some functions are written by you and others are from libraries that are provided for you. The first line of the program contains the header file,

#include < stdio.h >

which tells the compiler to include information about the standard input/ output library. This line appears at the beginning of many C source files. In this example, main is defined to be a function that expects no argument, which is indicated by the empty list ( ) .

The statements of a function are enclosed in braces { }. The function main contains only one statement,

printf("hello world\n");

A function is called by naming it, followed by a parenthesised list of arguments. The above statement calls the function printf with the argument "hello world\n". printf is a library function that prints output - in this case, the string of characters between the quotes. A sequence of characters in double quotes, like "hello world\n ", is called a character string or string constant. For the moment, our only use of character strings will be as arguments for printf and other functions.

The sequence '\n' in the string is C notation for the newline character, which when printed advances the output to the left margin on the next line. If you leave out the '\n', you will find that there is no line advance after the output is printed.

#include < stdio.h >
{

printf("hello");
printf(" world ") ;
printf("\n");

}

Notice that '\n' represents only a single character. An escape sequence like '\n' provides a general and extensible mechanism for representing hard-to-type or invisible characters. Among the others that C provides are '\t' for tab, '\b' for backspace, '\"' for the double quote, and '\\' for the backslash itself.

Similar to printf( ) function for printing on screen, we have scanf( ) function for entering data from keyboard.

The general form is:

scanf("%d", &a);

Explanation of scanf( ) will be detailed in later sections.

 

Variables:

In C, a quantity that may vary during program execution is called a variable. Variable names are names given to the locations in memory of a computer where different constants are stored. These locations can contain character, integer, real, or any such constants. In any language, the types of variables that it can support depend on the types of constants that it can handle. This is because a constant stored in a location with a particular type of variable name can hold only that type of constant.

Rules for constructing variable names:

A variable name is any combination of 1 to 8 alphabets, digits or underscores. Some compilers allow variables names whose length could be up to 40 characters. Still, it would be safer to stick to the rule of 8 characters.

  • The first character in the variable name must be an alphabet.
  • No commas or blanks are allowed within a variable name.
  • No special symbol other than an underscore (as in gross_sal) can be used in a variable name.

For example :

si_int
m_hra
bassal
pop_e_89

 

Variable Declaration:

The rules for constructing variables remain same for all types of variables: integer variables, real variables, character variables, string variables etc. For that matter, even all the secondary variables are constructed using the same rules mentioned above. C compiler is able to distinguish between the variable names, by making it compulsory for you to declare the type of any variable name, which you wish to use in a program. This type declaration is done at the beginning of the program.

Example:

int si, m_hra;
float basal;
char code;

It is a good practice to provide some meaningful variable names.

Data type:

'Data' is nothing but information. There are two fundamental types of data in C programming - integer and floating point. From these two, we can derive two others - character and double precision. There also exists a data type called void

Data type

Meaning

Size (bytes)

char

A character

1

int

An integer

2

float

A single precision real number

3

double

A double precision real number

8

void

Valueless

0

The data type char is used to store any character belonging to the C character set seen earlier. The datatype int type is used to store positive or negative integers. A float data type stores a single precision floating point (real) number. A double data type can store a floating-point value with a greater exactness than a float. The data type void is used to specify an empty set of values.

Integers: An integer constant is any number in the range -32768 to +32767. This is because an integer constant always occupies two bytes in memory. Out of the two bytes used to store an integer, the highest bit (sixteenth bit) is used to store the sign of the integer. This bit is 1 if the number is negative, and 0 if the number is positive. C offers a variation of the integer data type that will provide what are called long integer values. The long integers require twice the space in memory than ordinary int values do. Thus, long integers would occupy four bytes of memory. The long variables, which hold long integers, are declared using the keyword long, as in :

long int i;
long int abc;

The value of a long integer can vary from -214783648 to +2147483647. Short as well as integer values need less space in memory and thus help speed up program execution. Short integer variables are declared as :

short int j;
short int height;

In fact, a short int is nothing but our ordinary int, which we were using all the time without knowing that it was a short int. C allows us to abbreviate short int to int and long int to long. So the declarations made above can be written as :

long i;
long abc;
int j;
int height;

We can declare the variable to be unsigned, as in :

unsigned int num_students;

With such a declaration, the range of permissible integer values will shift from the range -32768 to +32767 to the range 0 to 65535. Thus declaring the integers as unsigned almost doubles the size of the largest possible value.

In fact, the unsigned int is nothing but a short unsigned int. Thus, all the following declarations are the same :

short unsigned int i;
unsigned int i;
unsigned i;

The way there exists a short unsigned int, there also exists a long unsigned int, which has a range of 0 to 429496795 and occupies four bytes of memory.

By default, a short int is a signed short int and a long int is a signed long int.

Signed & Unsigned Chars: There are signed and unsigned chars, both occupying one byte each, but having different range. A signed char is same as ordinary char and has a range from -128 to +127; whereas an unsigned char has a range from 0 to 255.

 

Constants:

A constant is a quantity that does not change. This quantity can be stored at a location in the memory of the computer. A variable can be considered as a name given to the location in memory where this constant is stored. Naturally the contents of the variable can change. For example in the equation :

3X + Y = 20

since 3 and 20 cannot change, they are called constants, whereas the quantities X and Y can vary or change and are hence called variables.

Integer constants: An integer constant must have at least one digit. It must not have a decimal point. It could be either positive or negative. If no sign precedes an integer constant it is assumed to be positive. No commas or blanks are allowed within an integer constant. The allowable range for an integer constant is -32768 to +32767.

For example:

243
+476
-8000
-7605

Real Constants: Real constants are often called Floating Point constants. The real constants could be written in two forms - fractional form or exponential form. A real constant must have at least one digit. It must have a decimal point. It could be either positive or negative. Default sign is positive. No commas or blanks are allowed within a real constant. In exponential form of representation, the real constant is represented in two parts. The part appearing before 'e' is called mantissa and the part following 'e' is called the exponent. In place of a small case 'e' a capital 'E' can also be used.

  • The mantissa part of the exponent part should be separated by a letter e.
  • The mantissa part may have a positive or a negative sign.
  • Default sign of mantissa part is positive.
  • The exponent must have at least one digit, which must be a positive, or a negative integer. Default sign is positive.
  • Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.

For example :

+3.2e-5
4.1e8
-0.2e+3
-3.2e-5

Character constants: A character constant is either a single alphabet, a single digit or a single special symbol enclosed within a pair of single inverted commas. Both the inverted commas should point to the left.

The maximum length of a character constant can be 1 character.

Examples : 'A'     'I'     '5'

The valid range of character constant is -128 to +127. It appears surprising that the character constant should have a numeric range. But this fact can be appreciated once we understand that character and integer constants are often used inter changeably.

For example : 'A' and 65 are one and the same, since when we say 'A' it is replaced by its ASCII value, which is 65.

Logical Constants: A logical constant can take either of two values: truth or falsity. In C, a non-zero value is always treated as truth whereas a zero is treated as falsity. Hence, values like 32, -45, 3.12 etc. are treated as logical constants having a 'true' value, whereas a 0 is treated as a logical constant with a 'false' value. The logical constants are very useful in evaluating logical expressions and complex conditions.

String Constants: A collection of characters enclosed within a pair of double inverted commas is treated as a string constant.

Examples:


"Hello"
"44-A, Gokulpeth, Nagpur"
"34.567"

In the above example, though 34.567 appears to be a real constant, once it is enclosed within inverted commas, it is treated as a string constant. Though not shown explicitly in the above example each string constant always ends with a special character '\0'. This character acts as a string terminator. Using this fact, we can find out where a string ends. The string of characters used in a string constant is always stored in adjacent memory locations.

Constant expression: A constant expression is an expression that involves only constants. Such expressions may be evaluated during compilation rather than run-time, and accordingly may be used in any place that a constant can occur, as in :

#define MAX 1000
char line[MAX + 1];

or

# define LEAP 1 /* in leap years */
int days[31+28+LEAP+31+30+31+30+31+31+30+31+30+31];

 

Variable Declaration:

All variables must be declared before use, although certain declarations can be made implicitly by context. A declaration specifies a type, and contains a list of one or more variables of that type.

For example:  int low, up, step;     char c, line[1000];

Variables can be distributed among declarations in any fashion. The lists above could equally well be written as :

int low;
int up;
char c;
char line[1000];

This latter form takes more space, but is convenient for adding a comment to each declaration or for subsequent modifications.

A variable may also be initialized in its declaration. If the name is followed by an equals sign and an expression, the expression serves as an initializer, as in :

char esc ='\\';
int i =0;
int limit = MAX+1;
float eps = 1.0e-5

C Keywords:

Keywords are the words whose meaning has already been explained to the C compiler. The keywords cannot be used as variable names because if we do so, we are trying to assign a new meaning to the keyword, which is not allowed by the computer. Some C compilers allow you to construct variable names, which exactly resemble the keywords. However, it would be safer not to mix up the variable names and the keywords. The keywords are also known as 'reserved words'.

There are only 32 keywords available in C. The following table shows the list of keywords.

auto

double

int

struct

break

else

long

switch

case

enum

register

typeof

char

extern

return

union

const

float

short

unsigned

continue

for

signed

void

default

goto

sizeof

volatile

do

if

static

while


Summary of datatype and their ranges:

Data Type

Range

Bytes

Format

Signed char

-128 to +127

1

%c

Unsigned char

0 to 255

1

%c

Short signed int

-32768 to +32767

2

%d

Short unsigned int

0 to 65535

2

%u

Long signed int

-2147483648 to + 2147483647

4

%ld

Long unsigned int

0 to 4294967295

4

%lu

Float

-3.4e38 to +3.4e38

4

%f

Double

-1.7e308 to +1.7e308

8

%lf

Long double

-1.7e4932 to 1.7e4932

10

%Lf