Introduction to C Language

C was developed by Dennis Ritchie at Bell Laboratories in 1972. Most of its principles and ideas were taken from the earlier language B, BCPL and CPL. CPL was developed jointly between the Mathematical Laboratory at the University of Cambridge and the University of London Computer Unit in 1960s. CPL (Combined Programming Language) was developed with the purpose of creating a language that was capable of both machine independent programming and would allow the programmer to control the behavior of individual bits of information. But the CPL was too large for use in many applications.

Basic Combined Programming Language

In 1967, BCPL (Basic Combined Programming Language) was created as a scaled down version of CPL while still retaining its basic features. This process was continued by Ken Thompson. He made B Language during working at Bell Labs. B Language was a scaled down version of BCPL. B Language was written for the systems programming. In 1972, a co-worker of Ken Thompson, Dennis Ritchie developed C Language by taking some of the generality found in BCPL to the B language.

Unix Kernel Development

The original PDP-11 version of the Unix system was developed in assembly language. In 1973, C language had become powerful enough that most of the Unix kernel was rewritten in C. This was one of the first operating system kernels implemented in a language other than assembly.

System Programming Language

During the rest of the 1970’s, C spread throughout many colleges and universities because of its close ties to UNIX and the availability of C compilers. Soon, many different organizations began using their own versions of C Language. This was causing great compatibility problems. In 1983, the American National Standards Institute (ANSI) formed a committee to establish a standard definition of C Language. That is known as ANSI Standard C. Today C is the most widely used System Programming Language.

First C Program, Hello World

Following C program will simply print “Hello World” on screen. Write carefully because C language is a case sensitive language.

#include < stdio.h>
void main()

{

printf(“Hello World\n”);

}

Press ALT+F9 to compile your program. If you have any error in your program, you will get the message, remove your errors and then execute your program you will got the out put.

Hello World

The printf()

The printf() function prints output to stdout, according to format and other arguments passed to printf(). The string format consists of two types of items – characters that will be printed to the screen, and format commands that define how the other arguments to printf() are displayed.

printf( “Hello World”);

The scanf()

The scanf() function reads input from stdio, according to the given format, and stores the data in the other arguments. It works a lot like printf(). The format string consists of control characters, white-space characters, and non-white-space characters.

void main(void)
{

int i;
scanf(“%d”,&i);
printf(“%d”,i);

}

C Variables & Constants

C Constants

The alphabets, numbers and special symbols when properly combined form constants, variables and keywords. A constant is an entity that does not change.

C Variables

Variables in c are names which refers to a location in memory which is used to holds some value with which we are working. A variable is an entity that may change its value. In any program we typically do lots of calculations. The results of these calculations are stored in computer memory locations. To make the retrieval and usage of these values we give names to the memory locations. These names are called variables.

C Keywords

A keyword is a word that is part of C Language itself. These words have predefined meanings and these words cannot be used as variable names.

char signed break for
auto const sizeof case
if extern double struct
continue goto register enum
typedef default return static
float union do switch
volatile int unsigned else
while long void short

Types of Variables

There are two main types of variables in C: numeric variables that hold only numbers or values, and string variables that hold text, from one to several characters long.

Basic fundamental data types in C language

Name
Description
Size
Range
char Character or small integer. 1byte signed: -128 to 127
unsigned: 0 to 255
short int Short Integer. 2bytes signed: -32768 to 32767
unsigned: 0 to 65535
long int (long) Long integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
bool Boolean value. It can take one of two values: true or false. 1byte true or false
float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits)
double Double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits)
long double Long double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits)

Loops in C

Loops are used to repeat one statement or set statements more than one time. Most real programs contain some construct that loops within the program, performing repetitive actions on a stream of data or a region of memory. There are several ways to loop in C.

for Loop

For loop is a counter loop. The for loop allows automatic initialization of instrumentation of a counter variable. The general form is

for (initialization; condition; increment/decrement)
{

statements block

}

If the statement block is only one statement, the braces are not necessary. Although the for allows a number of variations, generally the initialization is used to set a counter variable to its starting value. The condition is generally a relational statement that checks the counter variable against a termination value, and the increment increments (or decrements) the counter value. The loop repeats until the condition becomes false.

Example

main(){

int i;

for(i = 0; i < count; i++)
{

printf(“%d\n”,i);

}

}

while Loop

The while loop repeats a statement until the test at the top proves false.

The while loop has the general form:

while(condition)
{

statement block

}

The while tests its condition at the top of the loops. Therefore, if the condition is false to begin with, the loop will not execute at all. The condition may be any expression. An example of a while follows. It reads characters until end-of-file is encountered.

Example

main(){

int t = 0;
while(t<=10)
{

printf(“%d\n”,t);

t=t+1;

}

}

do-while Loop

This is very similar to the while loop except that the test occurs at the end of the loop body. This guarantees that the loop is executed at least once before continuing. Such a setup is frequently used where data is to be read. The test then verifies the data, and loops back to read again if it was unacceptable.

void main(void) {

int val;
do
{

printf(“Enter 1 to continue and 0 to exit :”);
scanf(“%d\n”, &val);

} while (val!= 1 && val!= 0);

}

C Control Structure

C language possesses such decision making capabilities and supports the following  statements known as c control structure or decision-making statements.

  • if statement
  • switch statement
  • Conditional operator statement
  • goto statement

if Statement

The if statement is a powerful decision making statement and is used to control
the flow of execution of statements. It is basically a two-way decision statement
and is used in conjunction with an expression.

if Statement Syntax

if (conditional)

{

block of statements executed if conditional is true;

}
else

{

block of statements if condition false;

}

if Statement Example

main()
{

int x=5

if (x > 1)
{

x=x+10;

}

printf(“%d”, x);

}

if…else statement

The if….else statement is an extension of the simple if statement. The general form is

if (condition)
{

True-block statement(s)

}
else
{

False-block statement(s)

}

If the condition is true, then the true-block statement(s), immediately following the if
statement are executed; otherwise the false-block statement(s) are executed.

void main(void)
{

int a, b;
char ch;

printf(“Choice:\n”);
printf(“(A) Add, (S) Subtract, (M) Multiply, or (D) Divide?\n”);
ch = getchar();
printf(“\n”);

printf(“Enter a: “);
scanf(“%d”, &a);
printf(“Enter b: “);
scanf(“%d”, &b);

if(ch==’A’) printf(“%d”, a+b);
else if(ch==’S’) printf(“%d”, a-b);
else if(ch==’M’) printf(“%d”, a*b);
else if(ch==’D’ && b!=0) printf(“%d”, a/b);

}

if-else-if statement

void main(void)
{

int numb;

printf(“Type any Number : “);
scanf(“%d”, &numb);

if(numb > 0) {

printf(“%d is the positive number”, numb);

} else if(numb < 0) {

printf(“%d is the Negative number”, numb);

} else {

printf(“%d is zero”,numb);

}

}

Switch Statement

The switch and case statements help control complex conditional and branching operations.
The switch statement transfers control to a statement within its body.

Switch Statement Syntax

switch (expression) {

case item:

statements;
break;

case item:

statements;
break;

case item:

statements;
break;

default:

statement;
break;

}

Switch Statement Example

main(){

int numb;
printf(“Type any Number”);
scanf(“%d”,&numb);

switch(numb %2)
{

case 0 :

printf(“the number %d is even \n”, numb);
break;

case 1 :

printf(“the number %d is odd \n”, numb);
break;

}

}

Ternary condition

The ? (ternary condition) operator is a more efficient form for expressing simple if
statements. It has the following form:

expression1 ? expression2: expression3

Ternary condition Example

res = (a>b) ? a : b;

if a is greater than b than res has the value a else the res has value b.

break statement

break statement is used to exit from a loop or a switch, control passing to the first
statement beyond the loop or a switch.

With loops, break can be used to force an early exit from the loop, or to implement a
loop with a test to exit in the middle of the loop body. A break within a loop should
always be protected within an if statement which provides the test to control the exit condition.

break statement example

For(i=0;i<=10;i++)

{

if(i==5){

break;

}

printf(“\n%d”,i);

}

Output:

0
1
2
3
4

continue statement

continue is similar to the break statement but it only works within loops where its effect
is to force an immediate jump to the loop control statement.

Like a break, continue should be protected by an if statement.

continue statement example

for(i=0;i<10;i++)

{

if(i==5){

continue;

}

printf(“\n%d”,i);

}

Output:

0
1
2
3
4
6
7
8
9

The goto statement

The goto is a unconditional branching statement used to transfer control of the program from one statement to another.

One must ensure not to use too much of goto statement in their program because its
functionality is limited. It is only recommended as a last resort if structured solutions are
much more complicated.