Learn c programming online

Note: This post is not complete, we are still updating this post.

C Language Introduction & History

C Language is developed by Dennis Richie in 1972. An international committee was formed in 1960 to develop a general purpose language. This committee developed ALGOL 60 in the year of 1960, Later in 1963 Combined programming Language [CPL] was developed but CPL was very big and had many features hence it was difficult to learn and use.

BCPL was developed in 1967 by Martin Richards but it was not powerful, Ken Thompson simplified BCPL and developed B Language in 1970, B represents the first letter of BCPL but this language too was very small.

Dennis Richie developed C Language by combining B Language and BCPL, Some additional features were added by Dennis Richie and C Language was developed in the year 1972. Letter C was taken from the second letter of BCPL.

Characteristics of C Language

C Language is general purpose language, C Language is flexible, powerful and structured programming language. C Language has many inbuilt features , C Language used in many applications it is simple to use and easy to learn Language.

C Language is case sensitive Language , it has many library functions and user can add as many functions as needed. C Language is usually called middle level language.

C Language is simple to use & Easy to learn.

C Language is reliable language.

C Language can be executed on many computers.

C Compiler is very fast , which makes execution of a program very fast.

C Program can be divided into small modules by using functions. C Program is very easy to understand.

C Language has characteristics of both high level language and low level language.

Bit wise manipulation is possible.

C Language has 40 Keywords.

C Language can be used for system programming.

C Language has more datatypes & Operators than any other Language.

Data Types in C Language

Datatype is the type of data which a variable can store, C Language supports following Data types.

Primary Datatype

Derived Datatype

User-Defined Datatype

1. Primary Datatype

Primary datatype further divided into Integer,Float,Double and Void data types.

a) Integer Datatype

Integer datatype is used to store Integer values. It required 2 bytes memory, Int keyword is used to declare integers. Integer range is from -32768 to 32767. Integer data type qualifiers are short,long,unsigned.

Example:

int a,b,c;

short int x,y,z;

long int x,y;

unsigned int length;

b) Float Datatype

Float datatype is used to represent real number in exponential form, It requires 8 bytes of memory. Double data type range is from 1.7e -308 to 1.7e 308. It is declared by using double keyword.

c) Char Datatype

Char datatype is used to store single character or a group of characters. Characters can be either signed or unsigned. Char data type requires one byte memory and range is from -128 to 127.

d) Void Datatype

Void datatype stores nothing, It takes 0 byte and it is declared by using the keyword void.

Keyword

Description

Low

High

Bytes

Format String

char

Single Character

-128

127

1

%c

int

Integer

-32768

32767

2

%d

long int

Long Integer

-2147483648

2147483648

4

%ld

float

Floating

3.4e-38

3.4e38

4

%f

double

Double Floating

1.7e-308

1.7e308

8

%lf

long double

Long Double Floating

3.4e-4932

1.1e4932

10

unsigned char

Char with no sign

0

255

1

%c

unsigned int

Int with no sign

0

65535

2

unsigned long int

Long int no sign

0

4294967295

4

2.Derived Datatype

The datatypes which are derived from primitive data types are called as derived datatypes, They also known as secondary datatypes or structured datatypes. Derived datatypes further divided into arrays, functions and pointers.

a)Arrays

An array is collection of related data items, Which have similar datatype and a common name. Array is used to represent a group of related data items, Data items can be integer,float or char. All data items must have same data type.

Example

int students[60];

b) Function

Function is a block of statements which do some useful work. A function may contain N number of statements.

c)Pointer

A pointer is a variable which points to a location in memory, It contains the address of another variable or array element.

Asterik [*] is used for pointer.

3) User Defined Datatype

Structure,Union and Enum are user defined datatypes, These datatypes are defined by user.

a) Structure

structure is collection of related data items, But data items of different datatypes. A structure may contain integer elements, floating point elements and character elements. The elements are known as structure. structure is similar to record.

Example

struct student

{

char name[25];

int unique_number;

char classname[30];

float fee_amount;

};

struct student s1,s2;

struct is a keyword, Student is name of structure and name,unique_number,classname and fee_amount are elements of structure. s1,s2 are structure variables.

Union

union is similar to structure data type, Like the structure union also contains members of different datatypes however union can hold one member.

Difference between structure and union is union can contain only one member at one time.

Example

union student

{

char name[25];

int unique_number;

char classname[30];

float fee_amount;

};

student s;

now s contains either name or unique_number or classname or fee_amount at one time.

Enumarated

Enumarated datatype is used to define the values of user defined datatypes.

Example

enum day

{

Sunday,

Monday,

Tuesday,

Wednusday,

Thursday,

Friday,

Saturday

};

enum is keyword, day is name of the type.

in the above example,

Sunday will have value of 0
Monday will have value of 1
Tuesday will have value of 2 etc...

Variables in C Language

A Variable is quantity which can change during program execution, It is the name of the location in the memory of computer in which constant can be stored. These locations can store integer, float and char data types.

Variable must be declared before it can be used in a program, Variable declaration helps the compiler to know about the name of the variable and type of the data which the variable holds.

When a Variable is declared, Compiler create space in memory and give the variable name to the space created.

Syntax

[datatype] [space] [variable name]

If variable is more than one, Variable name's should be separated by comma.

Example

int a,b;

float c,d;

int e=26;

Rules for declaring Variables

variable name should only contain numbers from 1-9, Alphabets and Underscores.

First letter of variable name should not be a number but it can be a Alphabet or Underscore.

Comma and blank space not allowed in a variable name.

No special symbol other than Underscore is allowed in variable name.

Keywords In C Language

Keyword is a predefined words, Whose meaning is already known to compiler so it can not be used as a variable name. Keywords are always written in lower case and they also known as reserved words.

C Language has 40 keywords out of which 32 keywords are used for High level programming and 8 are used for Low level programming, These 8 keywords are optional keywords.

auto

break

case

char

constant

continue

default

do

double

else

enum

extern

float

foe

goto

if

int

long

register

return

short

signed

size

static

struct

typedef

union

unsigned

void

volatile

switch

while

Identifier

Identifiers are two types

1) User defined

2) Keyword

Identifiers are nothing but names of variables,functions and arrays. They consist of letters, digits and underscore characters.

Rules for Identifiers

First character must be a letter, It must be from the character set.

Keywords can not be used as identifiers.

Identifier length must not be more than 8 characters [Note: some compilers allow 32 character identifiers]

Except underscore any other special symbols are not allowed.

Operators

An operator is a symbol, Which tells the compiler to perform certain mathematical operation or logical operation.

C Language supports following operators,

Arithmetic operators

Assignment operators

Increment / decrement operators

Relational operators

Logical operators

Conditional operators

Special operators

Bit wise operators

Arithmetic operators

Arithmetic operators are used for arithmetic operations, These are the basic operators these operations require 2 variables and called as binary operation.

operator

meaning

example

+

Addition

c = a + b;

-

Subscract

c = b - a;

*

Multiplication

a = b * 10;

/

Divide

c = a / b;

%

Module

c = a % 5;

Assignment operators

Assignment operators are used to assign the result of an expression to a variable, = [equals to] is the most common assignment operator.

Syntax

Identifier = Expression.

Example

int c = 2; [Assigns Right to left value, c now have value of 2]

c = a+b; [ Assigns Right to left value, a+b operation executed first and then the value assigned to c]

Increment / Decrement operators

These operators are used to increase or decrease value of a variable.

Increment => ++

Decrement => --

Syntax

x = 2;

y = ++x;

Relational Operators

Relational operators are used to compare two values depending on their relation we can make a decision, which one is greater or lesser value.

Operator

Meaning

<

Less than

>

Greater than

<=

Less than or equal

>=

Greater than or equal

==

Equal

!=

Not equal

Example

10 > 20 always returns false, because 10 is less than 20.

Logical Operators

Logical operators are boolean operators, Logical operators are used to test the value of a condition which is either true or false.

Operator

Meaning

&&

And

||

Or

!!

Not

Example

if(a==2 && b==2)
{

}

In the above condition if both a=2 and b=2 are true then only the if condition executed.

Conditional Operators

Conditional operator is used to check the validity of a statement.

Syntax

Expression1 ? Expression2 : Expression3

Expression1 is evaluted first, If it is true it will become value for condition, Else Expression2 is evaluated and it will become value for condition.

Special Operators

Special operators furthur divided into two categories.

1) Size of operator.

2) Comma operator.

Size of operator

Size of operator returns the number of bytes required to represent a data type or variable.

Syntax

sizeof(datatype)

Comma operator

A set of expressions are separated by commas, Expression linked using comma operator are evaluated from left to right, And the value of the right most expression is the result.

Example

a = (a=4, a+5)

The above expression is evaluated left to right, First a value will become 4 [a=4] , then a value will become 9 [4+5] and 9 will be the result, so finally a value will become 9 [a=9]

Bitwise Operator

Bitwise operatos are used for manipulation of data at bit level.

Operator

Meaning

$

Bitwise AND

|

Bitwise OR

^

Bitwise exclusive OR

<<

Bitwise shift left

>>

Bitwise shift right

Decision Control Statement

Decision control statements are used for decision making.

Example

Assume if students marks are above 35, To know if a student passed a exam or not we can know that using decision control statements using students marks.

if (marks >= 35) { Student pass }

else { Student Fail }

C Language supports following Decision control statements

if statement

if else statement

nested if else stetements

else if ladder

conditional operators

If Statement

if statement has a condition in brackets, If the condition is true the block following if statement will be executed.

Example

main()

{

int marks;

printf("Enter Marks");

scanf("%d",marks);

if(marks >= 35) {

printf("You passed this exam");

getch();

}

If else Statement

if statement has a condition in brackets, If the condition is true the block following if statement will be executed, If the condition is false control transferred to next instruction [else block].

Example

main()

{

int marks;

printf("Enter Marks");

scanf("%d",marks);

if(marks >= 35)

printf("You passed this exam");

else

printf("Sorry, Better luck next time!");

getch();

Nested if else statement

if else statements contain another if condition in brackets with a default else condition.

Syntax

if (condition) {

if (condition)

statement;

else

statement;

}

else {

if (condition)

statement;

else

statement;

}

Example

main()

{

int a,b,c;

clrscr();

printf("Enter a,b,c values");

scanf("%d%d%d",$a,$b,$c);

if(a>b) {

if(a>c)

printf("largest=%d",a);

else

printf("largest=%d",c);

}

else {

if(b>c)

printf("largest=%d",b);

else

printf("largest=%d",c);

}

getch();

}

if else ladder

if else ladder contains more than one if condition with a default else condition.

syntax

if (condition)

{

statement;

}

else

{

if (condition)

{

statement;

}

else

{

if (condition)

{

statement;

}

else

{

statement

}

}

Example

main()

{

int marks;

clrscr();

printf("Enter marks");

scanf("%d",$marks);

if(marks >= 60)

{

printf(" A Grade");

}

else

{

if (marks >= 50)

{

printf(" B Grade");

}

else

{

if (marks >=35 )

{

printf(" C Grade");

}

else

{

printf(" Better luck next time");

}

}

getch();

}

Conditional Operators

In C Language ? and : are conditional operators, These operators also called as ternary operators because they need three arguments to function.

Example

Loop control statements

In some cases we need to repeat execution of a particular block in a programming” the concept of repeating execution of a block of statements in a program is called loop.The loop entry (or) exit is controlled by a condition.
[loop initialization]
[condition]
[increment/decrement]

Loops in C Language

for loop

while loop

do while loop

For loop

for loop executes until a condition is satisfied for loop contains there parameters, to enter into for block ,first the condition is checked ,if the condition is true ,the statement in the loop executer.

Syntax

Example

Do-while loop

Do-while loop is different than for loop & while loop in Do-while loop the loop condition will be checked at the end of the loop block.
where as in for loop & while loop condition will be checked at the loop block.