C Introduction
C Introduction
What is C?
C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was
designed and written by a man named Dennis Ritchie. In the late seventies C began to replace the
more familiar languages of that time like PL/I, ALGOL, etc. No one pushed C. It wasn’t made
the ‘official’ Bell Labs language. Thus, without any advertisement C’s reputation spread and its
pool of users grew. Ritchie seems to have been rather surprised that so many programmers
preferred C to older languages like FORTRAN or PL/I, or the newer ones like Pascal and APL.
But, that's what happened.
Possibly why C seems so popular is because it is reliable, simple and easy to use. Moreover, in
an industry where newer languages, tools and technologies emerge and vanish day in and day
out, a language that has survived for more than 3 decades has to be really good.
Why learn C?
C is the most commonly used programming language for writing operating systems. Unix was
the first operating system written in C. Later Microsoft Windows, Mac OS X, and GNU/Linux
were all written in C. Not only is C the language of operating systems, it is the precursor and
inspiration for almost all of the most popular high-level languages available today. In fact, Perl,
PHP are all written in C.
By way of analogy, let's say that you were going to be learning Spanish, Italian, French, or
Portuguese. Do you think knowing Latin would be helpful? Just as Latin was the basis of all of
those languages, knowing C will enable you to understand and appreciate an entire family of
programming languages built upon the traditions of C. Knowledge of C enables freedom.
Types of C Constants
C constants can be divided into two major categories:
a) Primary Constants - Integer Constant, Real Constant, Character Constant
b) Secondary Constants – Array, Pointer, Structure, Union, Enum, etc.
Types of C Variables
As we saw earlier, an entity that may vary during program execution is called a variable.
Variable names are names given to locations in memory. These locations can contain integer,
real or character 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 particular type of variable can hold
only the same type of constant. For example, an integer variable can hold only an integer
constant, a real variable can hold only a real constant and a character variable can hold only a
character constant.
C Keywords
Keywords are the words whose meaning has already been explained to the C compiler (or in a
broad sense to the computer). 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 that exactly resemble the keywords.
However, it would be safer not to mix up the variable names and the keywords. The keywords
are also called ‘Reserved words’. There are only 32 keywords available in C which are :-
auto double int struct break else long switch case enum register typedef char extern return union
const float short unsigned continue for signed void default goto sizeof volatile do if static while
The if Statement
The general form of if statement looks like this:
The keyword if tells the compiler that what follows is a decision control instruction. The
condition following the keyword if is always enclosed within a pair of parentheses. If the
condition, whatever it is, is true, then the statement is executed. If the condition is not true then
the statement is not executed; instead the program skips past it.
What this expression says is: “if expression 1 is true (that is, if its value is non-zero), then the
value returned will be expression 2, otherwise the value returned will be expression 3”.
Multiple statements can also be used in the incrementation expression of for loop; i.e., we can
increment (or decrement) two or more variables at the same time. However, only one expression
is allowed in the test expression. This expression may contain several conditions linked together
using logical operators.
Use of multiple statements in the initialisation expression also demonstrates why semicolons are
used to separate the three expressions in the for loop. If commas had been used, they could not
also have been used to separate multiple statements in the initialisation expression, without
confusing the compiler.
do
{
this ;
and this ;
and this ;
and this ;
} while ( this condition is true ) ;
There is a minor difference between the working of while and do-while loops. This difference is
the place where the condition is tested. The while tests the condition before executing any of the
statements within the while loop. As against this, the do-while tests the condition after having
executed the statements within the loop.
The integer expression following the keyword switch is any C expression that will yield an
integer value. It could be an integer constant like 1, 2 or 3, or an expression that evaluates to an
integer. The keyword case is followed by an integer or a character constant. Each constant in
each case must be different from all the others.
The “do this” lines in the above form of switch represent any valid C statement.
What is a Function?
A function is a self-contained block of statements that perform a coherent task of some kind.
Every C program can be thought of as a collection of these functions.
A number of conclusions can be drawn:
Any C program contains at least one function.
If a program contains only one function, it must be main( ).
If a C program contains more than one function, then one (and only one) of these
functions must be main( ), because program execution always begins with main( ).
There is no limit on the number of functions that might be present in a C program.
Each function in a program is called in the sequence specified by the function calls in
main( ).
After each function has done its thing, control returns to main( ).When main( ) runs out
of function calls, the program ends.
There are basically two types of functions:
a) Library functions
b) User-defined functions
As the name suggests, library functions are nothing but commonly required functions grouped
together and stored in what is called a Library. This library of functions is present on the disk
and is written for us by people who write compilers for us. Almost always a compiler comes with
a library of standard functions. The procedure of calling both types of functions is exactly same.