Language
Set of programs are called as language
Computer languages
1. Low level languages
2. Assembly languages (Nuemonics)
3. High level languages
Compiler:
It checks the entire source program and if it is error free, produces a
complete object program.
Source program- Retained for modifications and corrections
Object program- Loaded into the computer’s memory for execution.
Interpreter:
It translates one statement at a time and if it is error free executes the
instructions and so on.
Difference between compiler & interpreter
Compiler Interpreter
1. Error correction (called debugging) is here error correction is simpler
Somewhat tedious. Since it produces an error .because it is done in stages
list for the entire program before execution of
even the first instruction can begin.
2. Programs are executed fastly. Here, it takes more time for
execution, because a statement has to be
translated every time the
program is
executed
Execution of a program:
This is basically a two step process.
1. The source program is first compiled.
2. Then it is converted into object program and loaded into memory
Programmer
#include<stdio.h>
main()
TEST {
EDITOR ……….
……….
} Source
00110 100 Complier
10101 010
……..
10110100
Object
000011110 1001
01010110 00110
Linker ……
…….
00101 01101 01
Executable
Library
Runner
Result
Programming Techniques
Three types of techniques:
1. Top down approach->Ex: C-language
2. Bottom Up approach -> Ex: C++
3. Modular Programming
History of C:
Year Language Developed By Remarks
1960 ALGOL International committee too general,
Too abstract
1963 CPL Cambridge University Hard to Learn
Difficult to
implement
1967 BCPL Martin Lichards at Too specific
Cambridge University could
deal with
only
specific problems
1970 B Ken Thampson at Too specific could
AT&T’S Bell Labs deal with only
specific
problems
1972 C Dennis Ritchie at Host generality of
AT&T’s Bell labs BCPL & B
Restored.
C is a Middle Level Language:
Reason:
Basically programming Languages are classified into two categories
1. Problem oriented language: [HLL-High Level Language]
i) Design to give a better program efficiency.
ii) Faster Program development.
2. Machine Oriented Language: [LLL-Low Level Language]
i) Design to give a better Machine efficiency.
ii) Faster Program execution
C is designed to satisfy these two conditions. Hence, it is called as Middle
level Language.
C is a Structured Programming Language:
Reason:
Structured programs used only 3 basic constructs.
1. Sequential structure- Steps are performs one after another in succession. (Step
by step action)
2. Selection Structure – Implemented with an If then else (or) case statement.
3. Repetition Structure – Implemented with a for, while (or) Repeated statements.
Features of C:
1. Economy of expressions
2. Modern control flow and structures
3. Rich set of operators.
Characteristics of C:
1. Size of Language
2. Modern Control structures [if statement, if… else statement, case statement,
while loop, for loop, do…while loop]
3. Bitwise control
4. Pointer Implementation
Uses of C:
1. System Applications:
a) Operating System
b) Interpreters
c) Editors
d) Assembly Programs
e) Compilers
2. Database Systems
3. Graphics Package
4. Spread sheet
5. Word processors
6. CAD/CAM applications
7. Office Automations
8. Scientific and Engineering applications
C-Character sets:
A to Z
1 to 9
a to z
Special characters:
+ - * /(division) %(Modulus) #(hash) $(dollar) ~(tilde) {}(curly
brackets) ()(Open and Close brackets) [](square brackets) , . ; : ’ ” _ @(at) ? <
> ! & (ambersand) ^ (exponent) \ (backslash) | (Pipe symbol)
Data Types:
Data Types
Primary Data Types Secondary Data Types
(Or) (Or)
Built in data Types User defined Data Types
1. Integer 1. Arrays
2. Float 2. Pointers
3. Character 3. Structures
4. Double 4. Unions
5. Void (meaning less data) 5. Enums
Primary Data Types:
1. Integer:
A Positive or Negative whole Number
2. Float:
A number having decimal point (either positive or negative)
3. Character:
A single character represented within single quote.
4. Double:
Floating Point Number having exponent
5. Void:
Meaning less data.
Secondary Data types:
1. Arrays:
It is a collection of similar / like data types, which occupies the adjacent
memory location.
2. Pointers:
It is a special type of variable which is used to store the address of another
variable
3. Structures:
It is a collection of unlike data types which occupies the adjacent memory
location.
4. Unions:
It is same as structure but uses the same memory location.
5. Enums
This gives us an opportunity to define our own data types and determine
what values it can take.
Constants:
A value that does not change.
C Constant
Character type Numeric type
Character constant string integer real
Constant constant constant
Unsigned long short integer Float Double
Integer Constant
This is an integer quantity
Rules for constructing Integer constant
1. It must have at least one digit
2. It must not have a decimal point
3. It could be either positive or negative.
4. If no sign precedes an integer constant it is assumed positive.
5. No commas or blanks are allowed within an integer constant.
6. Range -32768 to +32767
7. It occupies two bytes
e.g.:
425,-68,78
Long Integer
1. It occupies 4 bytes
2. Range -2,14,74,83,648 to +2,14,74,83,647
e.g.:
3456789,-2345897
Short integers
Ordinary integers
Unsigned Integer
If we declare an integer as unsigned, the range will change to 0 to 65535.
Declaring this will free the 16th bit and is used to store the number. It occupies two
bytes in memory
E.g.:
89, 67.
Long unsigned Integer
1. Range 0 to 4294967295
2. It occupies two bytes.
E.g.:
567897, 5683452
Note
By default, a short int is an unsigned short integer and a long integer is an
unsigned long integer.
Real constants
It is often called as floating point constant. It occupies four bytes in memory
Rules for constructing real constant expressed in fractional form
1. It must have at least one digital
2. It must have a decimal point.
3. It could be either positive or negative.
4. Default sign is positive.
5. No commas are blanks are allowed within a real constant.
E.g.:
+325.34,-35.78
Rules for constructing real constant expressed in exponential form
1. Mantissa part and exponent part should be separated by a letter e.
2. Mantissa part may have a Positive or negative sign.
3. Default sign of mantissa part is positive.
4. The exponent must have at least one digit, which must be a positive or a
negative integer. Default sign is positive.
5. Range -3.4e38 to +3.4e38
Double
Real constant with a precision of 16 digits are called as double constant. It
occupies 8 bytes in memory.
Character constant
It is a single character enclosed within single quotes.
Range -128 to +127
It occupies one byte in the memory.
E.g.:
‘a’ ,’7’
String constant
Collection of characters enclosed within double quotes is called as string
constant. String constant are terminated by a null (‘\0’) character.
E.g.:
“hello” , “4567”
Variable:
A name given to the memory location where the constant value is stored in the
memory.
Rules for constructing variable Names
1. Variable name is any combination of 1 to 8 alphabets, digits or underscores.
2. First character must be an alphabet
3. No commas or blank spaces are allowed within a variable name.
4. No special symbols other than an underscore can be used in a variable name.
Variable Declaration:
Syntax:
data type variable name;
Eg:
int a;
float b;
char c;
char name[30];
int a, b;
Scope of variables
Local – Available only to certain selected statements in the program. It is defined
inside a function
Global – It is accessible to all the statements in the function. It is declared outside
the function.
C keywords
Keywords are the words whose meaning has already been explained to the C
compiler. It cannot be used as variable names. There are 32 keywords available in
C.
They 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
C-PROGRAM
It is collection of functions. Every C program must have a main ( ). The
program execution starts from the main function. A function name is always
followed by a pair of parantheses ().Apart from these each C-Compiler provides a
library of around 200 predefined functions and macros designed for use in C
Programs.
For using these functions, certain files must be included in the program
which makes call to these functions. These files are known as header files. It
contains macro definitions and function declarations. It has an extension .h. The
statements within a function are always enclosed within a pair of curly brackets
{ }.
Some Common Header files:
1. stdio.h –Contains standard Input and output functions
2. conio.h – Contains console Input and output functions
3. math.h – Contains Mathematical functions
4. string.h – Contains string Input and output functions
5. graphics.h – Contains graphical functions
Escape sequences
Certain ASCII characters are unprintable, which means they are not
displayed on the screen or printer. Those characters perform other functions aside
from displaying text. Escape sequences usually consist of a backslash and a letter
or a combination of a digit. An escape sequence is regarded as a single character
and is therefore valid as a character constant. Escape sequences are typically used
to specify actions such as carriage return and tab movements on terminals and
printers. The commonly used escape sequences are listed below.
Escape sequences Represents
\a Bell (alert)
\b Backspace
\f Form feed
\n new line
\r Carriage Return
\t Horizontal tab
\v Vertical tab
\’ Single quotation Mark
\’’ Double Quotation Mark
\? Literal Quotation Mark
\\ Backslash
\0 Null
Format of a C Program:
#include<header file>
main()
{
Variable declarations;
clrscr();
Program statements;
getch();
}
Ex:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,sum;
clrscr();
printf(“Enter the values for a and b:”);
scanf(“%d%d”,&a,&b);
sum=a+b;
printf(“Sum=%d”,sum);
getch();
}
Standard Input and Output Functions:
Standard Input function:
scanf( )
Syntax:
scanf(“format string”, list of address variable);
Definition:
This function accepts the input value from the keyboard and stores it into the
corresponding memory location.
Ex:
scanf(“%d”,&a);
scanf(“%f%f,&a,&b);
Conversion character:
%d Data item is a decimal integer
%f Data item is a floating point value
%s Data item is a string
%c Data item is a character
%lf Data item is a double
%ld Data item is an long integer.
& Address Operator
2. Standard output function:
printf( )
Syntax:
printf(“format string”, list of variables);
Description:
This function prints the given output on the screen.
Ex:
printf(“%d”,sum);
printf(“Enter the values for a and b”);
printf(“Sum=%d”,sum);
Ex 1:
Program for Addition:
#include<stdio.h>
#include<conio.h>
main( )
{
int a,b,sum;
clrscr();
printf(“Enter the values for a&b:”);
scanf(“%d%d”,&a,&b);
sum=a+b;
printf(“Sum=%d”,sum);
getch( );
}
Character Input Output function:
Single character Input function:
getchar( );
Syntax:
Variable name=getchar( );
Description:
This function inputs a single character from the keyboard and converts it
into the equivalent ASCII value.
Ex:
C=’A’
C=getchar( );
Single character Output function:
putchar( );
Description:
This function converts the ASCII value of a single character and displays the
result on the monitor.
Syntax:
Putchar(variable name);
Ex:
putchar(a);
Ex: 2
Program for converting uppercase character into lowercase letter:
#include<stdio.h>
#include<conio.h>
main()
{
int c;
clrscr();
printf(“Enter an upper case letter”);
c=getchar();
printf(“Lowercase=”);
putchar(c+32);
getch();
}
Standard string Input output functions:
Standard String Input Function:
gets( );
Syntax:
Variable name=gets( );
Description:
This function inputs the string given in the keyboard and stores it into the
corresponding memory location.
Ex:
Name=”Kavitha”
Name=gets( );
Standard String Output function:
puts( );
Syntax:
puts(variable name);
Description:
This function prints the string on the monitor.
Ex: 3
#include<stdio.h>
#include<conio.h>
main( )
{
char name[30];
clrscr( );
puts(“Enter your name”);
gets(name);
puts(“Your Name is:”);
puts(name);
getch();
}
Operators:
Five types:
1. Arithmetic operators
2. Unary Operators
3. Relational and Logical Operators
4. Assignment Operators
5. Conditional Operators
1. Arithmetic Operators:
To do arithmetical calculations.
1. + Addition
2. - Subtraction
3. * Multiplication
4. / Division (Quotient)
5. % Division (Reminder)
2. Unary Operators:
1. -- Unary Minus (To represent Negative Number)
2. ++ Increment Operator
Two types
1. Preincrement operatorEx: ++i
2. Post increment OperatorEx: i++
3. -- Decrement Operator
Two Types
1. Predecrement OperatorEx:--i
2. Post decrement OperatorEx:i--
4. sizeof To print the size of data types.
Ex: 4
#include<stdio.h>
#include<conio.h>
main()
{
int i=1;
clrscr();
printf(“i=%d\n”,i);
printf(“i=%d\n”,++i);
printf(“i=%d\n”,i);
getch();
}
Similarly do this program by changing i++,--i,i--)
Ex:4
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf(“Integer=%d\n”,sizeof(int));
printf(“Float=%d\n”,sizeof(float));
printf(“Character=%d\n”,sizeof(char));
printf(“Double=%d”,sizeof(double));
getch();
}
3.Relational Operators
> greater than
< less than
>= greater than or equal to
<= less than or equal to
== perfectly equal to
!= not equal to
Logical Operators
&& logiacl and
|| logical or
! logical not
4. Assignment operator
=
It is used to assign values to a variable.
Syntax:
Identifier=expression;
Eg:
A=2
B=5.6
5. Conditional Operator
It is also called as ternary operator.
Syntax:
expression 1? expression 2 : expression 3;
Here expression 1 is evaluated first and if it is true, expression 2 is evaluated and
assign the value to expression 3.
If expression is false, then expression 2 is not evaluated, and the initial value
is assigned to expression 3.
Eg: 5
#include<stdio.h>
#include<conio.h>
main()
{
int num,absolute;
clrscr();
printf(“Enter any no:”);
scanf(“%d”,&num);
absolute=(num<0)?-num:num;
printf(“Absolute value=%d”,absolute);
getch();
}
Hierarchy of Operators
1.Parantheses
2. Unary Operators
3. Multiplication and division
4. Subtraction and addition
5. Relational operator
6...Equality checking
7. Logical operations
8. Conditional checking
9. Assignment operator
Type casting
Converting one data type to another is called as casting. For sometimes
certain variables declared as float might be required as int for some expressions.
For that purpose the cast is used.
General form
(data type) expression
Eg:
Let a, b are float variables and c integer variable
If we want to use a and b as int in some expression then we can change this
as follows
((int)(a+b))+c;
Here a+b is converted to int and then added with c. After the execution of this
expression and b will be returned as float variables.
Control Statements
It determines the flow of control in a program.
Control statements
Conditional control statements Unconditional control
statements
1. simple if statement 1. go to statement
2. if else statement 2. continue
3. Nested if statement 3. break
4. Switch case statement
Looping statements
1.for statement
2. while statement
3. do while statement
Conditional control statements
1. simple if statement
Syntax:ll
If (condition)
Statements;
E.g.:
#include<stdio.h>
#include<conio.h>
main()
{
int pos;
clrscr();
printf(“Enter a no:”);
scanf(“%d”,&pos);
if (pos>0)
printf(“The given no is a positive number”);
getch();
}
2. if else if statement
syntax:
if (condition)
statements;
else
statements;
eg:
#include<stdio.h>
#include<conio.h>
main()
{
int pos;
clrscr();
printf(“Enter any no:”);
scanf(“%d”,&pos);
if (pos>0)
printf(“The given no is a positive no”);
else
printf(“The given no is a negative no”);
getch();
}
3.Nested if statement
syntax:
if (condition)
statements;
else if (condition)
statements;
else
statements;
eg:
#include<stdio.h>
#include<conio.h>
main()
{
int pos;
clrscr();
printf(“Enter any no:”);
scanf(“%d”,&pos);
if(pos>0&&pos!=0)
printf(“The given no is a positive no”);
else if(pos<0&&pos!=0)
printf(“The given no is a negative no”);
else
printf(“The given no is a zero);
getch();
}
4. switch case statement
It allows us to make a decision from the number of choices .The keyword
case is followed by an integer constant or a character constant.
First, the expression is evaluated and if the value matches any one of the
case statement then the corresponding statement is evaluated. If there is no match
then the statements below, default is evaluated.
Syntax:
switch(expression)
{
case 1:
statements;
break;
case 2:
statements;
break;
case 3:
statements;
break;
default:
statements;
}
eg:
#include<stdio.h>
#include<conio.h>
main()
{
char code;
clrscr();
printf(“Enter a code[a/m/p]:”);
scanf(“%c”,&code);
switch(code)
{
case ‘a’ :
printf(“Accounts staff”);
break;
case ‘m’ :
printf (“Management staff”);
break;
case ‘p’ :
printf (Purchase staff”);
break;
default :
printf (“Invalid choice ! Try again”);
}
getch();
}
Looping statements
1. for loop
syntax:
for (expression 1 ; expression 2; expression 3)
statements;
where
expression 1 initialization statement
expression 2 condition checking statement
expression 3 increment or decrement operator
eg:
#include<stdio.h>
#include<conio.h>
main()
{
int i,n;
clrscr();
printf(“Enter the range:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
printf(“i=%d\n”,i);
getch();
}
2. while loop
syntax:
while ( condition)
{
statements;
}
eg:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i=0;
clrscr();
printf(“Enter the range:”);
scanf(“%d”,&n);
while(i<n)
{
printf(“i=%d\n”,i);
i++;
}
getch();
}
3. do……. While loop
The do while statement test the condition after having executed the
statements within the loop.
This means it will executes the statements at least once even if the condition
fails for the first time itself.
syntax:
do
{
statements;
} while(condition);
eg:
#include<stdio.h>
#include<conio.h>
main()
{
int i=0,n;
clrscr();
printf(“Enter the range:”);
scanf(“%d”,&n);
do
{
printf(‘i=%d\n”,i);
i++;
}while(i<n);
getch();
}
Unconditional control statements
1. break statement
It is used to terminate from the loop.This statement is used with for,, do
while,switch case statements.
Eg:
#include<stdio.h>
#include<conio.h>
main()
{
int x=1;
clrscr();
while (x<=10)
{
printf(“x=%d\n”,x);
if(x==5)
break;
x++;
}
getch();
}
2. continue statement
This statement will take the control to the beginning of the loop.It can be
included only in a while, do while, for statement.
3. goto statement
It transfers the control to anywhere in the program. Avoid using this
statement.
Some important programs
1. Program for Factorial
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,f=1;
clrscr();
printf("Enter any no:");
scanf("%d",&n);
for(i=1;i<=n;i++)
f=f*i;
printf("Factorial value=%d\n",f);
getch();
}
2. Program to count the number of digits
#include<stdio.h>
#include<conio.h>
main()
{
int n,c=0;
clrscr();
printf("Enter a no:");
scanf("%d",&n);
while(n!=0)
{
n=n/10;
c++;
}
printf("The total no of digits are:%d",c);
getch();
}
3. Program to reverse a given no
#include<stdio.h>
#include<conio.h>
main()
{
int x,rev=0,rem;
clrscr();
printf("Enter the value to be reversed:");
scanf("%d",&x);
while(x>0)
{
rem=x%10;
rev=rev*10+rem;
x/=10;
}
printf("The reversed no is %d",rev);
getch();
}
4. Program to check the given no is prime or not
#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
clrscr();
printf("Enter the no:");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i!=0)
continue;
else
break;
}
if(i==n)
printf("The given no is a prime number");
else
printf("The given number is not a prime number");
getch();
}
5. Program to generate Fibonacci series
#include<stdio.h>
#include<conio.h>
main()
{
int a=0,b=1,c,i,n;
clrscr();
printf("Enter the range:");
scanf("%d",&n);
printf("%d\t",a);
printf("%d\t",b);
for(i=3;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("%d\t",c);
}
getch();
}
6. Program to find the sum of n natural numbers
#include<stdio.h>
#include<conio.h>
main()
{
int i,n,sum=0;
clrscr();
printf("Enter the range:");
scanf("%d",&n);
for(i=1;i<=n;i++)
sum=sum+i;
printf("Sum of %d natural numbers are %d",n,sum);
getch();
}
7. Program to find the sum of digits
#include<stdio.h>
#include<conio.h>
main()
{
int n,sum=0,a;
clrscr();
printf("Enter the value to sum:");
scanf("%d",&n);
while(n>0)
{
a=n%10;
sum=sum+a;
n=n/10;
}
printf("The summed digit is %d",sum);
getch();
}
8. Program to check the given no is Armstrong or not
#include<stdio.h>
#include<conio.h>
main()
{
int n,j,s=0,a;
clrscr();
printf("Enter a no:");
scanf("%d",&n);
j=n;
while(n>0)
{
a=n%10;
s=s+a*a*a;
n=n/10;
}
if(s==j)
printf("The given no is an armstrong no");
else
printf("The given no is not an armstrong no");
getch();
}
9. Program to generate prime numbers
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
clrscr();
printf("Enter the range:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=2;j<=i;j++)
{
if(i%j!=0)
continue;
else
break;
}
if(j==i)
printf("%d\t",i);
else
continue;
}
getch();
}