0% found this document useful (0 votes)
53 views

C Introduction

C is a programming language developed in 1972 by Dennis Ritchie at Bell Labs. It became popular as a systems programming language as it is reliable, simple, and easy to use. C is commonly used for writing operating systems like Unix, Windows, and Linux. Knowledge of C is helpful for understanding other languages like C++, Java, and PHP that were influenced by its syntax and concepts. C programs use constants like integers and characters, as well as variables to store values. Keywords, if/else statements, loops (while, for), switch statements, and functions are some of the basic programming constructs in the C language.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

C Introduction

C is a programming language developed in 1972 by Dennis Ritchie at Bell Labs. It became popular as a systems programming language as it is reliable, simple, and easy to use. C is commonly used for writing operating systems like Unix, Windows, and Linux. Knowledge of C is helpful for understanding other languages like C++, Java, and PHP that were influenced by its syntax and concepts. C programs use constants like integers and characters, as well as variables to store values. Keywords, if/else statements, loops (while, for), switch statements, and functions are some of the basic programming constructs in the C language.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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:

if ( this condition is true )


execute this statement ;

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.

The if-else Statement


The if statement by itself will execute a single statement, or a group of statements, when the
expression following if evaluates to true. It does nothing when the expression evaluates to false.
We execute one group of statements if the expression evaluates to true using if statement and
another group of statements if the expression evaluates to false using the else statement.

Use of Logical Operators


C allows usage of three logical operators, namely, &&, || and !. These are to be read as ‘AND’
‘OR’ and ‘NOT’ respectively.
There are several things to note about these logical operators. Most obviously, two of them are
composed of double symbols: || and &&. These single symbols | and & also have a meaning.
They are bitwise operators.
The two operators, && and ||, allow two or more conditions to be combined in an if statement.
The NOT operator is often used to reverses the result of the expression it operates on.

The Conditional Operators


The conditional operators ? and : are sometimes called ternary operators since they take three
arguments. In fact, they form a kind of foreshortened if-then-else. Their general form is,

expression 1 ? expression 2 : expression 3

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”.

The while Loop


The general form of while is as shown below:
initialize loop counter ;
while ( test loop counter using a condition )
{
do this ;
and this ;
increment loop counter ;
}

The for Loop


The for allows us to specify three things about a loop in a single line:
a) Setting a loop counter to an initial value.
b) Testing the loop counter to determine whether its value has reached the number of
repetitions desired.
c) Increasing the value of loop counter each time the program segment within the loop has
been executed.
The general form of for statement is as under:

for ( initialise counter ; test counter ; increment counter )


{
do this ;
and this ;
and this ;
}

Multiple Initialisations in the for Loop


The initialisation expression of the for loop can contain more than one statement separated by a
comma. For example,

for ( i = 1, j = 2 ; j <= 10 ; j++ )

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.

The break Statement


We often come across situations where we want to jump out of a loop instantly, without waiting
to get back to the conditional test. The keyword break allows us to do this. When break is
encountered inside any loop, control automatically passes to the first statement after the loop. A
break is usually associated with an if.
The continue Statement
In some programming situations we want to take the control to the beginning of the loop,
bypassing the statements inside the loop, which have not yet been executed. The keyword
continue allows us to do this. When continue is encountered inside any loop, control
automatically passes to the beginning of the loop.

The do-while Loop


The do-while loop looks like this:

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.

Decisions Using switch


The control statement that allows us to make a decision from the number of choices is called a
switch, or more correctly a switch-case-default, since these three keywords go together to make
up the control statement. They most often appear as follows:

switch ( integer expression )


{
case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;
}

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.

Why Use Functions?


Two reasons:
a) Writing functions avoids rewriting the same code over and over.
b) Using functions it becomes easier to write programs and keep track of what they are
doing. If the operation of a program can be divided into separate activities, and each
activity placed in a different function, then each could be written and checked more or
less independently. Separating the code into modular functions also makes the program
easier to design and understand.

Function Declaration and Prototypes


Any C function by default returns an int value. More specifically, whenever a call is made to a
function, the compiler assumes that this function would return a value of the type int. If we
desire that a function should return a value other than an int, then it is necessary to explicitly
mention so in the calling function as well as in the called function.
We can draw the following conclusions:
a) If we want that the value of an actual argument should not get changed in the function
being called, pass the actual argument by value.
b) If we want that the value of an actual argument should get changed in the function being
called, pass the actual argument by reference.
c) If a function is to be made to return more than one value at a time then return these values
indirectly by using a call by reference.
Recursion
A function is called ‘recursive’ if a statement within the body of a function calls the same
function. Sometimes called ‘circular definition’, recursion is thus the process of defining
something in terms of itself.

You might also like