Latest
Latest
COURSE CONENTS
Sl.No Particulars Page No’s
01 Course Introduction
02 Typing Software
03 Computer Basics
04 Basics Shortcut Keys
05 Number System
06 Algorithms and Flowcharts
07 Types of Programming Languages
08 C-Language History
09 Why Use C , Features and Applications Of C
10 C-Language Software Environment
11 Character Sets
12 Implementation of C Program
13 Basic Programs
14 C-Language Tokens
15 Structure of C Program
16 Built In Functions
17 Escape Sequence Characters
18 C-Language Input/ Output Functions
19 C-Language Control Statements
20 Type Conversions
21 Strings
22 Arrays
23 Structures
24 Unions
25 Typedef ,Enum and Bitfields
26 Functions
27 Storage Classes
28 Pointers
29 Dynamic Memory Allocation
30 Files
31 Command Line Arguments
32 C Preprocessor Directives
33 C-Language Graphics
COMPUTER BASICS
Computer Stands For-
Common Operating Machine particularly used for technical , education and
research
1. Input Unit:- Input unit receive data or instructions through input devices.
The various input devices are: keyboard ,mouse , Scanner ,joystick ,Micro
Phone ,Light Pen , CC Camera etc.
Keyboard and mouse are standard input devices.
2. Output Unit:- output unit display result on output devices.
Monitor is a standard output device.
Various Output Devices:
1.Monitors(Softcopy output device): It is a standard output
device.
2.Printers (Hardcopy output Device)
3.Speakers (Voice output Device)
4.Projectors (Softcopy Output Device)
computer. CPU performs all types of data processing operations. It stores data,
intermediate results and instructions(program). It controls the operation of all
parts of computer.
Components of CPU:
1.Control Unit(CU)
2.Arithmetic & Logical Unit(ALU)
3.Memory Unit(MU)
1.RAM:- RAM(Random Access Memory) is the internal memory of the CPU for
storing data, program and program result. It is read/write memory which stores
data until the machine is working. As soon as the machine is switched off, data is
erased.
2)ROM:-ROM stands for Read Only Memory. The memory from
which we can only read but cannot write on it.The information is stored
permanently in such memories during manufactures. A ROM, stores such
instructions that are required to start a computer.
II. Secondary Memory:-This type of memory is also known as external
memory or non-volatile. It is slower than main memory. These are used for
storing data/Information permanently.
Examples:- Hard Disk, Cd ,DVD ,pen drives etc.
III. Cache Memory:-Cache memory is a very high speed semiconductor
memory which can speed up CPU. It acts as a buffer between the CPU and main
memory.
Advantages:
1.Cache memory is faster than main memory.
2.It consumes less access time as compared to main memory.
3.It stores data for temporary use.
Units of Computer Memory Measurements:
1 Bit = Binary Digit (1,0)
8 Bits = 1 Byte
1024 Bytes = 1 KB (Kilo Byte)
1024 KB = 1 MB (Mega Byte)
1024 MB = 1 GB(Giga Byte)
1024 GB = 1 TB(Terra Byte)
1024 TB = 1 PB(Peta Byte)
1024 PB = 1 EB(Exa Byte)
Payroll Software
Student Record Software
Inventory Management Software
Income Tax Software
Railways Reservation Software
Microsoft Office Suite Software
Microsoft Word , Microsoft Excel , Microsoft PowerPoint etc.
There are basically two types of computer programming languages given below:
1. machine language
2. assembly language
1. Machine dependent
2. Machine level coding
3. Knowledge of hardware required
Easy to learn
Easy to understand
Easy to write program
Easy to detect and remove errors
Built-in library functions
Machine Independence
Low efficiency
Less flexibility
Translators
source code. To convert the source code into machine code, translators are
needed. A translator takes a program written in source language as input and
converts it into a program in target language as output.
1. Compiler
2. Interpreter
3. Assembler
Features of algorithm:
It should be simple.
Algorithms make our work easy in solving the problems.
Every step is clearly described.
Data flow is processed in top-down approach.
Algorithms can easily be converted into flow charts.
Errors can be easily detected.
It is not dependent on any programming language, so it is easy to
understand for any one even without programming knowledge.
By using algorithm the problem is divided into smaller pieces or steps, it
is easier for programmer to convert into actual program.
Limitations of algorithm:
1. Time Consuming
2. Difficult to show branching and looping
3. Al algorithm is not a computer program.
4. Big tasks are difficult to put in algorithm.
Step1: Start
Step2: declare variables a and b
Step3: Read variables a and b
Step4: if a>b
Then print “Big value is a”
Else
Then print “Big value is b”
Step5: Stop
Example3: Write an algorithm to find the largest among three
numbers.
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a>b
If a>c
Display a is the largest number.
Else
Ans:-
Flowcharts: flowchart is a graphical representation of an algorithm. It is an
alternate technique for solving a problem.
Features of flowchart:
1.flowcharts provide a better way of pictorial representation of algorithm.
3.flowchart should be verified for its validity with some test data.
Limitations of flowcharts:
1.flowcharts are difficult to modify.
3.The drawing of flow chart is more than one sheet of paper, the connectors
are used for jointing different pages.
Start Stop
2)Input/ Output Box:- It is used to read data from the user and to display
result on monitor.
Read Write
A,B,C A,B,C
Z=x+y
NUMBER SYSTEM
Number System is used to represent information in quantitative form.
1.Decimal Number System:- The number system that we use in our day-
to-day life is the decimal number system. Decimal number system has base 10
as it uses 10 digits from 0 to 9. In decimal number system, the successive
positions to the left of the decimal point represent units, tens, hundreds,
thousands and so on.
Base Value: 10
Digits : 0 to 9
Base Value:2
Digits : 0,1
Base Value: 8
Digits : 0 to 7
4.Hexa Decimal Number System:- The hexa decimal system has 16-
Digits, which are represent as 0,1,23,4,5,6,7,8,9,A,B,C,D,E,F. The base of
hexadecimal number system is 16.
Base Value: 16
Digits : 0 to 9 and
10-A
11-B
12-C
13-D
14-E
15-F
Ex:- 0xA5 , 0X56
Convert Decimal Number System to Binary Number System
1) 45
2 | 45
2 22-1
2 11-0
2 5-1
2 2-1
1-0
(45)10 = (101101)2
2)120
2| 120
60-0
30-0
15-0
Sirisha Devi -C/o Bright Computer Education ,Sattenapalli Page 19
C-Language Material
7-1
3-1
1-1
(120)10 =(1111000)2
3) 178
2|178
89-0
44-1
22-0
11-0
5-1
2-1
1-0
(178)10=(10110010)2
32 16 8 4 2 1
1 0 1 1 0 1
32+8+4+1=45
2) 1 1001
16 8 4 2 1
1 1 0 0 1
16+8+1=25
3)1111000
64 32 16 8 4 2 1
1 1 1 1 0 0 0
64+32+16+8=120
4) 101010001
256 128 64 32 16 8 4 2 1
1 0 1 0 1 0 0 0 1
256+64+16+1=337
5)10101010
128 64 32 16 8 4 2 1
1 0 1 0 1 0 1 0
= 128+32+8+2=170
2)127
8|127
8 15-7
1-7
(127)10 = (177)8
3) 450
8 |450
8 56-2
7-0
(450)10 =(702)8
4) 615
8 | 615
76-7
9-4
1-1
(615)10=(1147)2
5) 987
8|987
123-3
15-3
1-7
(987)10 = (1733)8
Convert octal number system to decimal Number System
1)106 64 8 1
1 0 6
64+6=70
2)1733
512 64 8 1
1 7 3 3
512+ 448+24+3=987
3)1147
512 64 8 1
1 1 4 7
512+64+32+7=615
Convert Decimal to hexa decimal number system
1)45
16 |45
2-13(D)
45=2D
2)95
16|95
5-15
95= 5F
3) 400
Sirisha Devi -C/o Bright Computer Education ,Sattenapalli Page 22
C-Language Material
16|400
25-0
1-9
400 =190
2) 5F
16 1
5 F
80+15=95
3)256
256 16 1
1 9 0
=256+144=400
4) 315
256 16 1
3 1 5
789
C-LANGUAGE HISTORY
BCPL, etc.
Let's see the programming languages that were developed before C language.
Why Use C?
C was initially used for system development work, particularly the programs
that make-up the operating system. C was adopted as a system development
language because it produces code that runs nearly as fast as the code written
in assembly language. Some examples of the use of C might be −
Operating Systems
Language Compilers
Assemblers
Text Editors
Print Spoolers
Network Drivers
Modern Programs
Databases
Language Interpreters
Utilities
Features of C:-
C is the widely used language. It provides many features that are given below.
1. Simple
2. Machine Independent or Portable
3. Mid-level programming language
4. structured programming language
5. Rich Library
6. Memory Management
7. Fast Speed
8. Pointers
9. Recursion
10. Extensible
File menu:-
1.Load:- It is used to open already existing programs.
2.pick:- It is used to open the recent opened and created programs list.
................
}
c) ( )-> parenthesis
parenthesis are used in conditions and functions
Ex:- if(a>b) here a>b is a condition
conditions are must and should enclosed with parenthesis
main()->here main is a function
add()-> here it is add function
d) # (hash)
in c-language (pre processor directive)
e) =
in c-language "= " is a assignment operator.
x=y+z; (y+z value is assigned to x variable)
i=10; ( 10 is assigned to i variable)
i=j; (j variable value is assigned to i variable )
f) ==("equal to" Operator)
10/2=5
9/3=3
i) *(astrick)
in c-language
1) multiplication(10*3=30)
2) it is also used as a pointer variable in c-language
(*i)-> here i is a pointer variable
j)&(ampersand)
in c-language
1)&&->logical and
Ex:- (i>j)&&(i>k)
2)&-> bitwise and (ex:-10&8)
3)&i-> address of i
k) |(pipe symbol)
in c-language
1) ||->logical OR
Ex:- (i,j)||(i<k)
2) | ->Bitwise OR
Ex:- 10|6
l) !
in c-language it is a "logical not"
!(10<8)
if condition is true then logical not returns false
if condition is false then logical not returns true
m)^(caret)
in c-language it is a "Bitwise exclusive OR"
Sirisha Devi -C/o Bright Computer Education ,Sattenapalli Page 33
C-Language Material
IMPLEMENTATION OF C PROGRAM
Steps for executing the c program:
1. Creation of program
2. Compilation of a program
3. Execution of a program
1. Creation of program:- Programs should be written in c editor. The
file name does not necessarily include extension of C. The default extension
is C. The user can also specify his/her own extension.
{
textcolor(14);
cprintf("welcome to c");
}
after create a program we save a program by using
file menu ->save
c:\turboc2\noname.c
c:\turboc2\s1.c
main()
{
clrscr();
textcolor(10);
cprintf("C-language");
getch();
}
program3:
write a program for display an integer value
main()
{
int i=10;
clrscr();
printf("%d",i);
getch();
}
program4
write a program to display a float value
main()
{
float i=5.67;
clrscr();
printf("float value is:%f",i);
getch();
Sirisha Devi -C/o Bright Computer Education ,Sattenapalli Page 36
C-Language Material
program5:-
W.a.p to display a float value
main()
{
float i=6.8766;
clrscr();
printf("float value=%f\n",i);
printf("3 decimals value:%.3f",i);
getch();
}
Program6:
W.a.p to display a character
main()
{
char ch='l';
clrscr();
printf(" ch = %c",ch);
getch();
}
Program7:
W.a.p to display two integer values
main()
{
int i=90,j=45;
clrscr();
printf("%d\t%d",i,j);
getch();
}
Program 8:
main()
{
int x=10;
float y=3.5;
char z='h';
clrscr();
printf("integer value is: x=%d\n",x);
printf("float value is :y=%f\n",y);
printf("z=%c\n",z);
getch();
}
Program 10:
W.a.p to display square value of given number
main()
{
int i=7;
clrscr();
printf("square value is=%d",i*i);
getch();
}
Program11:
W.a.p to display total, average of given two numbers
Sirisha Devi -C/o Bright Computer Education ,Sattenapalli Page 38
C-Language Material
main()
{
int m1=56,m2=89,m3=45;
int total;
float avg;
clrscr();
total=m1+m2+m3;
avg=total/3;
printf("Total Marks are=%d\n",total);
printf("Average Marks are=%f\n",avg);
}
Program12:
W.a.p to read value in meters and display centimers of the given
meter value.
main()
{
int m=9;
int cm;
clrscr();
cm=m*100;
printf("%d m=%d cm",m,cm);
getch();
}
Program13:
Home Work
......................
1)W.a.p to read value in kilograms and convert to gms
main()
{
int kg=2;
int gms;
clrscr();
gms=kg*1000;
printf("%d kg=%d gms",kg,gms);
getch();
}
2) Grams to Kilograms
3) Celsius to Fahrenheit’s
main()
{
int C=1;
float F;
clrscr();
F=C*1.8+32;
printf("%dC=%.2fF",C,F);
getch();
}
4) Fahrenheit’s to Celsius
T(°C) = (T(°F) - 32) / 1.8
C-LANGUAGE TOKENS
Tokens:- The smallest individual units in a program are known as tokens.
1)variable
2)identifier
3)keyword
4)operators
5)constants
6)data types
1)Variable
Variable:-It is a user defined name. It reserved some space in computer
memory to store a value.
Rules:-
A variable can have alphabets, digits, and underscore.
A variable name can start with the alphabet, and underscore only. It
can't start with a digit.
No commas ,blank spaces are allowed within a variable name.
Keywords should not be used as variable names.
the length of variable cannot exceed up to 8 characters long and some of
the "c" compilers can be recognized upto 31 characters long.
main()
{
int i;
clrscr();
printf("enter a value");
scanf("%d",&i);
printf("the integer value is=%d",i);
getch();
}
2. write a program to display float value at runtime
main()
{
float i;
clrscr();
printf("enter a float value");
scanf("%f",&i);
printf("float value is=%f",i);
getch();
}
3. W.a.p to read and display a character
void main()
{
char ch;
clrscr();
printf("enter a character");
scanf("%c",&ch);
printf("the character is:%c",ch);
getch();
}
float avg;
clrscr();
printf("Enter Three Subject Marks");
scanf("%d%d%d",&sub1,&sub2,&sub3);
total =sub1+sub2+sub3;
avg=total/3;
printf("Total Marks: %d\n",total);
printf("Average :%f",avg);
getch();
}
7. W.a.p to convert Meter value to Centimeters value
main()
{
float m,cm;
clrscr();
printf("Enter Meter value");
scanf("%f",&m);
cm=m*100;
printf("%.2fm=%.2fcm",m,cm);
getch();
}
8. W.a.p to convert Centimeters to Meters
main()
{
float cm,m;
clrscr();
printf("Enter Centimeters value");
scanf("%f",&cm);
m=cm/100;
printf("%.2fcm=%.2fm",cm,m);
getch();
}
variable initilization
The variable receive the value at the time of declaration is called variable
initialization.
syntax:- datatype variable=value;
Ex:- int i=56;
float j=9.0;
program1
main()
{
int i=10;
clrscr();
printf("%d",i);
getch();
}
program2:-
main()
{
int i=67;
float j=8.9;
char k='s';
printf("i=%d\n",i);
printf("j=%f\n",j);
printf("k=%c\n",k);
}
or
main()
{
int i=90;
float j=34.56;
char k='d';
clrscr();
printf("%d %.2f %c",i,j,k);
getch();
}
variable assignment
After initialization again the values are assigned to variables then this is called
variable assignment.
main()
{
int i=10; /*initilization*/
i=20; /*assignement*/
i=30;
clrscr(); /*assignment*/
printf("%d",i);
}
output:- i=30
chained assignment
i=j=k=10;
volatile variable:- volatile variable can accept any changes of the variable.
main()
{
volatile int i=10;
i=20;
i=30;
clrscr();
printf("%d",i);
}
Local variable:- The variable declared inside a function then that variable
is called local variable.local variables can't access by another functions.
local variables are used only that function.
main()
{
int i=10;
float j=20.23;
clrscr();
printf("local variable=%d\n",i);
printf("local variable=%f\n",j);
}
Global variable:- the variables are declared outside the main() then that
variables are called global variables.Global variables are used any where(all
functions)
int i=10; /*global variable*/
main()
{
int j=20; /*local variable*/
clrscr();
printf("local variable=%d\n",j);
printf("global variable=%d",i);
}
problem:-3
int i=10;/*global*/
main()
{
int i=20;/*local*/
printf("%d\n",i);
}
In C-language the local variable and global variable declared with same
name then default the local variable display this means the local variable hide
the global variable.
This problem is solved by using scope resolution operator in C++ language.
Constants
constants:- The constants in C are applicable to the values,which do not
change during the execution of a program. There are several types of constants
in C.
constants are divided into two types
1.numeric constants
1.integer constants:-These are the sequence of numbers from 0 to 9
without decimal points or fractional part or any other symbols.
ex:- int i=10;
int m1=34,m2=45;
++i;
Binary operator:-Which operator operates with two operands are called
binary operators.
Ex:- +,-,*,/,%,&&,|| etc. are binary operators.
c-language operators are
1. arithmetic operators
2. logical operators
3. relational operators
4. conditional operators
5. assignment operators
6. increment or decrement operators
7. bitwise operators
1.Arithmetic operators:-These operators are used to perform fundamental
arithmetic operations. These operators are binary operators.
1.binary arithmetic operators
Operator Meaning
................. .................
+ Addition
- subtraction
* multiplication
/ division
% modulas or remainder
w.a.p to perform arithmetic operations with given two values
main()
{
int i,j;
clrscr();
Sirisha Devi -C/o Bright Computer Education ,Sattenapalli Page 52
C-Language Material
main()
{
int i=-10,j=90;
clrscr();
printf("i=%d\n",-i);
printf("j=%d",-j);
getch();
}
Logical Operators
Sometimes, we have to check more than one conditions at a time then we use
logical operators
Logical operators are
logical and &&
logical OR ||
logical NOT !
Logical AND(&&)
A B A&&B
......... ....
T T T
T F F
F T F
F F F
Logical Or(||)
A B A||B
................
T T T
T F T
F T T
F F F
...................
Logical NOT(!)
A !A
T->F
F->T
Program1:
main()
{
clrscr();
printf("%d\n",(10<100)&&(7!=7));
printf("%d\n",(5==5)||(9>10));
printf("%d",!(6>10));
getch();
}
output:-
0 1 1
W.a.p to read 3 subject marks and to check student is pass or fail
main()
{
int m1,m2,m3;
printf("enter 3 subject marks");
scanf("%d%d%d",&m1,&m2,&m3);
if(m1>=35&&m2>=35&&m3>=35)
{
printf("pass");
}
else
{
printf("fail");
}
getch();
}
w.a.p to check the given charcter is vowel or consonent.
main()
{
char ch;
printf("enter a character");
scanf("%c",&ch);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
printf("vowel");
}
else
{
printf("consonent");
}
}
Relational Operators
Operator Meaning
............... ...............
> Greater Than
< less than
>= Greater than or equal to
<= Less than or equal to
== Equal to
!= Not Equal to
relational operators are used between any two or more operends for check
conditions. if the relation is true then it returns 1 value otherwise it returns
zero value.
main()
{
printf("%d\n",10<100);
printf("%d\n",12!=23);
printf("%d\n",5==5);
printf("%d\n",2>=10);
printf("%d\n",5<=10);
}
Program2:
main()
{
int i=10,j=20,k=30;
i=j=k=5;
clrscr();
k=(i==k);
j=(i>k);
i=(j<=k);
printf("%d\t%d\t%d",i,j,k);
getch();
}
3.Conditional operators (? :)
Conditional operator is also called as 'ternary operator.' It is widely used to
execute condition in true part or in false part.
Syntax:
(exp1)?Exp2: exp3
(condition)?true statement :false statement
here exp1 is a condition if exp1 returns true then exp2 value is displayed
otherwise exp3 value is displayed.
Ex:-
res=(a>b)?a:b;
here the logical or relational operators are used to check condition.
w.a.p to find big value of two numbers by using conditional
operators
main()
{
int a,b,big;
clrscr();
printf("enter two values");
scanf("%d%d",&a,&b);
big=(a>b)?a:b;
printf("big value is=%d",big);
getch();
}
w.a.p to find given no is positive or negitive by using conditional
operator
main()
{
int n;
clrscr();
printf("enter a value");
scanf("%d",&n);
(n>=0)?printf("positive"):printf("Negitive");
getch();
}
main()
{
char ch;
clrscr();
printf("enter a character");
scanf("%c",&ch);
(ch>=65&&ch<=90)?printf("capital") : printf("small");
getch();
}
w.a.p to check given no is even or odd by using conditional
operator
main()
{
int n;
printf("enter a value");
scanf("%d",&n);
(n%2==0)?printf("even") :printf("odd");
getch();
}
Home Work
W.a.p to check given year is leap year or not
main()
{
int y;
printf("enter year");
scanf("%d",&y);
(y%4==0)?printf("leap year"):printf("Non Leap Year");
getch();
}
2)W.a.p to check given two numbers are equal or not by using
conditional operator
main()
{
int x,y;
printf("enter two values");
scanf("%d%d",&x,&y);
(x==y)?printf("same"):printf("different");
getch();
}
Assignment Operator(=):-This operator is used to store value into a
variable.It is used in 3 ways.
1.it is used to store a value into a variable ex:- int x=5;
2.it is used to store the value of a variable into another variable
int x=y;
3.it is used to store the value of the expression into the variable.
x=y+z-4;
short hand assignment operators
operator example
+= i+=10-> i=i+10
-= i-=10 -> i=i-10
*= i*=10 -> i=i*10
/= i/=10-> i=i/10
%= i%=10 ->i=i%10
while using assignment operator (=) some times we may have same variable at
both sides of the operator. In such cases we can eliminate repetition of the
variable and use compact notation
main()
{
int i=10;
clrscr();
printf("Short hand operations are:\n");
printf(" %d\n", (i+=10));
printf("%d\n", (i-=10));
printf("%d\n", (i*=10));
printf("%d\n", (i/=10));
printf( "%d",(i%=10));
getch();
}
W.a.p to read two values and display swapping of given two
variable values
main()
{
int x,y;
clrscr();
printf("enter two values");
scanf("%d%d",&x,&y);
printf("before swapping %d \t %d",x,y);
x=x+y;
y=x-y;
x=x-y;
printf("after swapping %d \t %d",x,y);
getch();
}
W.a.p to read two values and display swaping of given two
variable values
main()
{
int x,y,z;
clrscr();
printf("enter two values");
scanf("%d%d",&x,&y);
printf("before swapping %d \t %d",x,y);
z=x;
x=y;
y=z;
printf("after swapping %d \t %d",x,y);
getch();
}
Comma operator (,)
In c-language instead of " ;" comma(,) is also used as line terminator with some
rules
1.program declartion must and should terminate with semicolon
2.program ending line must terminate with semicolon.
main()
{
int i;
clrscr(),
printf("enter a value"),
scanf("%d",&i),
printf("%d",i),
getch();
}
main()
{
int i=10,j=45,k=67;
i=j<k,
k=i>j,
j=i==k,
clrscr(),
textcolor(12),
cprintf("%d %d %d",i,j,k),
getch();
}
main()
{
int i;
i=10,
i=20,
clrscr(),
textcolor(10),
cprintf("%d",i),
getch();
}