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.


  1. C Language is simple to use & Easy to learn.
  2. C Language is reliable language.
  3. C Language can be executed on many computers.
  4. C Compiler is very fast , which makes execution of a program very fast.
  5. C Program can be divided into small modules by using functions. C Program is very easy to understand.
  6. C Language has characteristics of both high level language and low level language.
  7. Bit wise manipulation is possible.
  8. C Language has 40 Keywords.
  9. C Language can be used for system programming.
  10. 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.

  1. Primary Datatype
  2. Derived Datatype
  3. 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.



KeywordDescriptionLowHighBytesFormat String
charSingle Character-1281271%c
intInteger-32768327672%d
long intLong Integer-214748364821474836484%ld
floatFloating3.4e-383.4e384%f
doubleDouble Floating1.7e-3081.7e3088%lf
long doubleLong Double Floating3.4e-49321.1e493210
unsigned charChar with no sign02551%c
unsigned intInt with no sign0655352
unsigned long intLong int no sign042949672954

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.
autobreakcasecharconstantcontinuedefaultdo
doubleelseenumexternfloatfoegotoif
intlongregisterreturnshortsignedsizestatic
structtypedefunionunsignedvoidvolatileswitchwhile

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.
operatormeaningexample
+Additionc = a + b;
-Subscractc = b - a;
*Multiplicationa = b * 10;
/Dividec = a / b;
%Modulec = 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.
OperatorMeaning
<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.
OperatorMeaning
&&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.
OperatorMeaning
$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

  1. if statement
  2. if else statement
  3. nested if else stetements
  4. else if ladder
  5. 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.

syntax

Expression1?Expression2:Expression3

Example

main()
{
int a,b;
clrscr();
printf("Enter the value for x");
scanf("%d",$x);
y = (x>5?3:4);
printf("y=%d",y);
getch();
}

switch case statement 

Switch-Case statement allows  us to filter a choice based on input.

Syntax

Switch (condition))
{
Case 1:
Statement;
break;
Case 2:
Statement;
break;
Case 3:
Statement;
break;
Case n:
statement;
break;
default:
statement;
}
getch();
}

Example

main()
{
int day;
print  (‘enterday=”);
scan f (“%d”,&day);
switch (day)
{
case1:
print f (“monday”);
break;
case2:
print f(“tuesday”);
break;
default:
print f (“enter count day”);
}
getch();
}

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    

for (initialization;condition;increment/decrement)
{
statement 1;
statement n;
}

Example

main ()
{
int i ;
for (i=0 ; i > =5;i++)
print f (“%d”,i);
getch ();
}

While loop 

while loop executes similar to for loop. except the increment  / decrement done in loop block;

Syntax

while (condition)
{
statement 1;
statement n;
increment/decrement;
}

Example

main()
{
int i = 1 ;
while ( i <=5)
{
 print f (“%d”,i);
 i = i +1 ;
 }
 getch ( );
 }

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.

Syntax

do
{
statement 1;
statement n;
}
while (condition);

Example

main()
{
int i=1;
{
print f (“%d”,i);
i =i+1;
}
while (i<=5);
getch ();
}

Go to statement  

Go to statement used to jump from one statement to another statement in a program based on a condition change sequence of program.

syntax

main()
{
int a,b,result;
clrscr ();
go to input block;
result=a+b;
print f (“result =%d”,result);
input block:
print f (“enter a,b “);
scan f (“%d%d”,$a,$b);
getch();
}

Nesting loop

nesting one loop in other loop is possible.

Example

main ()
{
int i,j;
for (j=1;j<=5;j++)
{
print f (“\n”);
for (i=1;1’<=j;i++)
{
print f (“%d\t”,i);
}
}
getch();
}

Break statement

break statement is used to exit from a loop.
break statement can be used with for loop,do while loop ,if statement & switch statement.

Continue statement

continue statement automatically takes control to the begining of the loop.

Array

Array is collection of similar data items ,which have similar data type and have a common name to represent a group of related data items.