You are on page 1of 39

‘C’

Hello friends, this is a tutorial for the language ‘C’. This is so


simple to say that we are learning C language however learning
any programming language is as tough as moving in a desert
without any idea of the end point. I am not trying to make you
afraid but its true. There is no end of computers. Lets look at
the basics of any language.

Till now we have worked on the computers without having any


idea of the calculations within the computer. We don’t know
what goes on in the computers when we click the mouse button
or press a key on the keyboard. This is the internal calculation
of the computer.

Here, you are learning a programming language ‘C’. Let me


first introduce you to the programming language.

Language : A language is any medium through which we can


communicate. So, in terms of computers, we can say that we
are needed to communicate with a computer through a
language.

Programming : A programming is not so tough as we thinks


generally. A simple plan of movie with friends is also a
programming in general life. Let us have an example of movie
with friends program. We will need to follow these steps for
that purpose—
1. Be ready to go.
2. Call friends to meet you at theatre at a particular time.
3. Schedule yourself so that you can be at a particular time
at the proposed theatre.
4. Go to the theatre and take tickets.
5. Watch movie.
6. Come back to home.
Now, we can say that a program of movie is in those six
instructions that must be followed to complete the movie
program. In the similar manners, a computer program is
developed to accomplish the particular task.

Let’s see an example of adding two numbers :


1. Take two numbers.
2. Add them.
3. Give the result.

Before going to the programming, I need to explain some other


basic concepts of the computer which are important to be
known by a programmer and these will make the programming
more easier to understand.
Computer Architecture :

The computer is classified in three different parts :


1. Hardware
2. Software
3. User

Hardware : The physical components of the computer are


called as hardware. Example – Monitor, Mouse, Keyboard, CPU
(Central processing Unit) etc.
Software : All of the visual (Imaginary) things are called as
software. In computers, the things thing are seen on the display
device (Monitor generally) are called as software. This also
includes the pointer of mouse and the first screen of system
check when you just power on the computer. Example – Ms
Windows, Ms-Word, avast antivirus software etc.

User : Obviously, a computer cannot be completed without a


operator. We humans are also the part of a complete computer.

HARDWARE
Hardware again is divided into three distinct parts.
1. Input Devices.
2. Output Devices
3. CPU

Input Devices : Input devices are those devices that are used
to send any instruction or command to the CPU. Example –
Keyboard, Mouse, Joystick, Lightpen, Scanner, Thumbwheel,
BCR ( Bar Code Reader) etc.

Output Devices : Output Devices are those devices that are


used to fetch the information from the CPU and give some
results to the user. Example - Monitor, Printer, Plotter,
Speakers, BCW (Bar Code Writer) etc.

CPU : CPU (Central Processing Unit) is the most important part


of the computer. It is also called as the brain of the computer.
Mainly the computer exists in the CPU. All of the work is in the
CPU only. All processes, storage, calculations are done by the
CPU only. All other devices are connected to the CPU and
processes the request through it.
These arrows shows the flow of data as data flows from Input
DevicesCPUOutput Devices.

Example : Suppose we want to add two numbers 4 and 6.


Firstly, we will enter the question 4+6 through the keyboard. It
will be sent to the CPU to process. It will be summed up to give
10 within the CPU but we will see it on the screen of the
monitor when it will be sent to it by the CPU.

It can also be understood by the sugar mill example. When we


want to produce sugar, firstly, the sugar cane is sent to the mill
to process. After processing from the mill, we get the sugar as
the final result.

CPU : We will discuss it after the software as it is a long topic.


SOFTWARE

As we have discussed earlier, software is the visual part of the


computer which do not exists in the physical manners. It can
only be seen through a hardware.

Software is classified in many parts however four major parts


are discussed here.
1. System Software
2. Application Software
3. Utility Software
4. Custom Software

System Software : The software which are necessary for a


computer to work are under system software. These software
provide an environment to the user to work. Without a system
software, no one can use the computer. They also manages the
memory and processing power of the computer. Example – Ms-
Windows, Ms-DOS (Microsoft Disk Operating System), UNIX,
LINUX.

Application Software : These software are useful to complete


the user needs. These software are not necessary for the
computer but they are required by users to complete the jobs
needed to be performed by them. Example – Ms-Office (Word,
Excel, Power Point, Access, Outlook), Photoshop, Corel Draw,
Pagemaker, Tally, Kundli etc.

Utility Software : The software that are required to manage


the computer are under the section of Utility Software. This
includes the Antivirus software also. Example – Avast, AVG,
Norton, Mc-Afee etc.
Custom Software : These software are made by any
programmer. These software are not marketed by any
company and are available with limited users only. These
software are developed according to the need of the user and
sold to them only. They are not available with any other users.
Example – Bank Software, School Software etc.
CPU (CENTRAL PROCESSING UNIT)

CPU is the most important part of the computer. Without a CPU,


all of the hardware is waste because it is the only device which
processes, stores and manipulates the data.

CPU is divided into three different groups :


1. ALU (Arithmetic Logic Unit)
2. MU (Memory Unit)
3. CU (Control Unit)

ALU : ALU is used for all of the mathematical calculations of


the computer. All of the calculations are performed by the ALU
only.

MU : MU is the memory of the computer. All of the storage is


done in this only.

CU : This unit is used to control all of the activities that goes on


within the computer. It controls all of the flow of data as well as
all of the functions of the computer.

Memory Unit is further divided into three different groups :


1. Primary Memory
2. Secondary Memory

Primary Memory : The memory that is necessary for a


computer to work is under this section. It includes RAM
(Random Access Memory) and ROM (Read Only Memory).

Secondary Memory : Secondary memory is not necessary for


a computer. It can also be removed from the computer and the
computer will work. Example – CD (Compact Disk), VCD (Video
Compact Disk), DVD (Digital Video Disk), HD/HDD (Hard Disk /
Hard Disk Drive), Pan Drive, Floppy, Memory Card etc.

RAM :

RAM is a primary memory used for temporary storage of the


files. This memory is a volatile memory i.e. it loses all of its
data when power is off. This memory is directly attached to the
CPU. Here we are assuming primary memory out of the CPU.
CPU is considered to have the processor, ALU, CU and its
internal registers. We will see this in next section.

ROM :

ROM is a microchip which is programmed once in its lifetime


when it is manufactured by the companies itself. The ROM
performs these operations :
1. Checks whether all of the hardware devices are connected
to CPU or not.
2. Whether all of the hardware devices are connected to
their proper place or not.
3. Give the starting point to start the Operating System
(Windows / UNIX / LINUX).

HIERARCHY OF COMPUTER

Primary
Secondary
Cache
CP
Memory
Memory
(RAM)
U
Cache Memory : Cache memory is a small memory that
contains the recently processed data in it. It is very fast due to
low memory space. It is used to boost up the performance of
the computer.

The speed of the processor is much faster as compared to other


devices. It is 6 to 7 times faster than the speed of RAM. So,
there is enough time for CPU to be in idle state while RAM
fetches the data. The speed of cache is much faster, nearly
equal to the speed of processor hence it boosts the system.

Fetching of Data :
Whenever there is a need of having some data, CPU generates
a request to both cache memory and RAM. If data is found in
cache memory, it is taken from that else RAM is checked. If the
required data resides in RAM, it is fetched else the request of
q33333333333333q333333333333333335the data is sent from
RAM to secondary memory like Hard disk and the data id
fetched from it to RAM and then it is sent to the CPU.
Introduction to ‘C’

C is a programming language for the beginning level


programmers. This is a functional oriented language i.e. all of
the programs are in form of functions. The term function will be
discussed in later chapters. In this tutorial, we will cover the
following things.

1. Basic C programming
2. Conditional statements
a. If statement
b. Switch Statement
3. Jump statements
a. Go to statement
b. Break statement
c. Continue statement
4. Looping
a. While loop
b. Do...While loop
c. For loop
5. Pointers
6. Arrays
a. Single dimensional array
b. Multi dimensional array
c. Pointer to array
7. Strings
a. In form of array of characters
b. In form of pointers
8. Functions
a. Call by Value
i.Without return, No parameters
ii.Without return, with Parameters
iii.With return, No parameters
iv.With return, with parameters
b. Call by Reference
c. Recursion
d. Array in functions
9. Structure / Union
a. Simple structures
b. Array of structures
c. Pointer to structures

All of the given topics are fundamentals of language ‘C’. After


this, a programmer may proceed to any language like C++,
Java, .NET etc. or he/she may chose to be in advanced
programming in C.

Before going to the programming I would like to give some


introduction of work of programming languages, their use etc.

Till now, we have worked on the computer without knowing the


internal calculations. Now, let us understand some good things.

There are two type of screens we can use; the system program
screen and a user screen. The system program screen contains
all of the programs which are needed to be combined to form a
complete program and a user screen will have the output
/outcome result from that program. When we write a program
in any language, it is needed to be processed by someone so
that a result can be obtained. The compiler is the software
which processes the program written in C. Here I am saying it
in C only. Interpreter is also a software which performs the
same operation as of compiler. Every language have its own
compiler/interpreter. Several languages like java contains both
of them. C contains the compiler only and named as Turbo.
The difference in both is as:
Suppose we have a program of four lines, the compiler reads
whole program at once and compiles it as a whole however an
Interpreter reads the program line by line. So the whole process
can be shown by the following diagram.
Output
Source
of the
Compiled by Compiler
Program
program.
in C
Output/User
language.
Screen
Program
Screen

Memory :

When we talk about memory, the unit of measuring of the


memory must be well known. As we know, the computer a
electronic machine. Generally we uses English language while
working on the computer but it is not so that computer have
the knowledge of English language. Actually, the computer is
known about only two letters: 0 and 1. Just like an electric wire,
it may either contain some power in it or no power, in the same
manners computer may have 0 or 1. The language of 0’s and
1’s is called as Binary Language. The question arises that
how we are able to work in English if computer is not known
about English????

Ans: let us assume two persons of two different countries with


different languages like English and Chinese. None of them is
known about any other language and they need to
communicate with each other. They can use a third person who
is known about both the languages. Through this third person
they can communicate.
Assume a fruit Apple. In English it is made up of five different
letters A,p,p,l and e. In Chinese it may be of a single letter
suppose (chiu). So to form a chiu of single letter of Chinese,
we used five letters but both points to the same thing. Like this
computer also works. It have a dictionary of all letters we use in
English and their equivalent binary code.
ASCII Codes :

ASCII (American standard code for information interchange) is a


standard made for the computer world. In this standard, a
particular ASCII number is given to every alphabet in English.
The binary code of any letter can be found by converting that
number to its equivalent binary number. This is acceptable by
every computer of the world. There are total 256 codes. Some
of them are written here.

Some of the binary codes are as follows:


Letter ASCII Binary Letter ASCII Binary
Code Code Code Code

A 65 01000001 a 97 0110000
1
B 66 01000010 b 98 0110001
0
C 67 01000011 c 99 0110001
1
D 68 01000100 d 100 0110010
0
E 69 01000101 e 101 0110010
1
F 70 01000110 f 102 0110011
0
G 71 01000111 g 103 0110011
1
H 72 01001000 h 104 0110100
0
I 73 01001001 i 105 0110100
1
J 74 01001010 j 106 0110101
0
K 75 01001011 k 107 0110101
1
L 76 01001100 l 108 0110110
0
M 77 01001101 m 109 0110110
1
N 78 01001110 n 110 0110111
0
O 79 01001111 o 111 0110111
1
P 80 01010000 p 112 0111000
0
Q 81 01010001 q 113 0111000
1
R 82 01010010 r 114 0111001
0
S 83 01010011 s 115 0111001
1
T 84 01010100 t 116 0111010
0
U 85 01010101 u 117 0111010
1
V 86 01010110 v 118 0111011
0
W 87 01010111 w 119 0111011
1
X 88 01011000 x 120 0111100
0
Y 89 01011001 y 121 0111100
1
Z 90 01011010 z 122 0111101
0

0 48 00110000 1 49 0011000
1
2 50 00110010 3 51 0011001
1
4 52 00110100 5 53 0011010
1
6 54 00110110 7 55 0011011
1
8 56 00111000 9 57 0011100
1
NOTE : All of the binary codes are in 8 digits. It is true for all of
the letters of English.

A single 0 or 1 is called as bit. Hence we can say that 8 bits are


combined to form a character/letter. The 8 bit combination is
called as a byte. So, a byte contains a character.

8 bits = 1 byte / 1 character


10
2 bytes = 1024 bytes = 1 KB (Kilo Byte)
220 bytes = 1024 KB = 1 MB (Mega Byte)
230 bytes = 1024 MB = 1 GB (Giga Byte)
240 bytes = 1024 GB = 1 TB (Terra Byte)

So, we can estimate the memory by these units of


measurement.

Computer Languages :

There are four type of languages in computers.


1. Low level language / Machine Language : Binary
Language
2. Assembly Language : Assembly
Language
3. High Level Language :
Programming C etc.
4. 4GL (4th generation language) : SQL etc.

The difficulty in understanding the language decreases from


top to bottom. The low level is most difficult. The assembly
language is somewhat simple than binary. HLL is more easier
and 4GL are as simple as our normal writing language.

The programs written in HLL or 4GL are firstly translated to


Assembly Language and then it works on machine/computer. It
is translated from assembly language to LLL by assembler.
Hence the complete hierarchy is as follows.

Compiler/Interp
Assembl
Assembl
Machine
4GL
HLL
er
reter
Languag
y
Languag
e
e

Now, let us move to the basic programming part of C.


BASIC ‘C’ PROGRAMMING

The most simplest program is written in C is :

#include<stdio.h>
void main()
{
printf(“Hello, welcome to C”);
}

Now, let us see all of the parts of the program.

#
Its a pre-processor used for including header files or defining.

Include
it is used to call the header files into the program. As all of the
functions of C are defined in header files. Example : printf is a
function defined in stdio.h file. The operation that must be
performed by calling printf is described in stdio.h file. So if we
want to use printf in the program, stdio.h must be a part of the
program. Hence it is included in the program by include
statement.

<>
These works like a bracket to keep the complete name for the
header file.

stdio
this is a header file in the library of C and contains many
functions like printf , scanf, getc, putc, gets, puts etc.

.h
Indicates that it is a header file.

void
void means for blank. This indicates that main will not return
anything. This will be described in functions chapter.

main
main is the main function. Whenever a compiler compiles the
program, only main function is compiled. A program without
main function is always incomplete. It can not be compiled.

()
These brackets contains the parameters that are passed to the
functions. Even this will be described in functions topic.

{}
These brackets forms a block indicating that all of the things
written between them are part of the statement just above {. In
this program, printf statement is in the block of main function.

NOTE : All of the statements needed to be compiled must be


within the block of main function.

printf
printf is the statement which shows some output on the user
screen. The things written within double quotes(“”) are printed
as it is on the user screen except some cases.

;
Semicolon (;) indicates the end of the line. It is also called as
termination symbol.

GETTING OUTPUT :
To get the output of the program, we will first compile it by
pressing key F9. Then compiler will compile the program. If the
program is correct. Success will be shown else error will be
displayed.
Now, we will run the program by pressing Ctrl+F9. Now, the
program is finished running. If we want to see the output
screen, we will press Alt+F5.

Step 1: Compile F9
Step 2: Run Ctrl + F9
Step 3: See output Screen (If required) Alt + F5

NOTE :
Instead of writing void main() we can also write

void main(void) No error No warning


int main() No error 1 warning (Function should return
a value)
int main(void) No error 1 warning (Function should return
a value)

PROGRAM 2 :
OUTPUT:
#include<stdio.h>
void main()
Hello,
{
Welcome
printf(“\nHello, \n\nwelcome \t\tto C”); to C
}
There is a very little difference in the previous program and this
one. There is a \n and \t in printf statement. It means for the
next line and tab space respectively. They both are used within
double quotes only. When we use \n, the cursor goes to the
next line and \t gives tab space. We can use them any number
of times according to our need and format of writing. They help
us to adjust the things in the display.

Now, try the following program.

#include<stdio.h>
void main()
{
printf(“\n\nName\t\t:\tHimanshu Verma”);
printf(“\nCourse\t\t:\tB.Tech.\n”);
printf(“Branch\t\t:\tComputer Science”);
}

TOPIC 2: getch() and clrscr()

getch() and clrscr() are two functions defined in conio.h header


file. So, to use them, we will need to include a extra header file.

#include<conio.h>
This line will be below inclusion of stdio.h. getch() is the
function that holds the output screen till we press any key on
the keyboard. This stands for “get character”. The main
purpose of getch well be discussed later but here we will use it
to hold the screen. The benefit of it will be – we will not need to
press alt+F5 to see the screen after running the program.
When we will run the program, the screen will be holding for
any key from keyboard. We will use getch at the end of the
main function.

clrscr() is a function which clears the screen every time. Have a


experiment : compile the program many times without using
the clrscr function. The output will be displayed as many times
as you have compiled the program. To prevent this, we will use
clrscr at the first line of the program (here program means for
the program within the main function).

NOTE : This is not an hard and fast rule to use the clrscr at first
and getch at the last of the program. We can use them in
between also as our convenience.

Now, our simplest program becomes –

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr()
printf(“\nHello, Welcome to C.”);
getch()
}

Now, when we will run the program, it will hold for any key.
Also, the screen will be cleared at every compilation.
VARIABLES / CONSTANTS :

A variable is a quantity that can change its value and a


constant is a quantity that have a fixed value. Example : If x is
2 then y is 4 else y is 6. In this case, we can not say the value
of y because it is changing. Hence y is a variable. If we are able
to give the exact value, it becomes constant.

DATA TYPES :

There are many different data types in C. Data type means for
any variable that can store anything in it. A variable may
contain any type of value – it may be an integer type, it can
store values in decimal form i.e. floating type or a character.
Depending upon the nature of the variable, we uses them in
our programs. Generally, there are three basic data types
namely :

1. int (can store integer type of values)


2. float (can store floating type values i.e. decimal values)
3. char (can store a character in it)

There are many other data types also like long, double etc. but
the basic data types are three only.

There is the table of data type, memory space and range:

Data Type Memory Range


(Bytes)

unsigned 1 0 to 255
char
char 1 -128 to +127
unsigned 2 0 to 65,535
int
short int 2 -32,768 to +32,767
int 2 -32,768 to +32,767
unsigned 4 0 to 4,294,967,295
long
long 4 -2,147,483,638 to +2,147,483,637
float 4 3.4 * 10-38 to 3.4* 1038
double 8 1.7* 10-308 to 1.7* 10308
long double 10 3.4* 10-4932 to 3.4* 104932

The declaration of variables is done after starting the main


function. The declaration is done according to our need of
program. Let us understand this with the help of a short
example :
OUTPUT :
#include<stdio.h>
#include<conio.h>
The sum of 10 and 20 is 30
void main()
{
int a,b,c;
clrscr();

a=10;
b=20;
c=a+b;
printf(“\nThe sum of %d and %d is %d”,a,b,c);

getch();
}
In this program, two constants a and b area added in a variable
c. The value of a and b are fixed as 10 and 20. Hence they are
constants. The value of c depends upon the value of a and b so
a variable. Total memory consumption by this program is
2*3=6 bytes. To print the value of a variable, printf includes
some special symbols such as %d for integers. The table is
given as :

Data Type Symbol to be used

Int %d
Float %f
Char %c
Double %e
long int %ld

The symbols are used within the double quotes and the name
of the variables are given after the quotes after a separation by
comma (,). If we need to use more than one variables to
display, more number of % symbols can be used and the
variable names can be given in respective order.
We can use more than one number of data types in a single
program if required. We can also reduce number of variables in
the program. Let us see this from the previous example itself :

OUTPUT :
#include<stdio.h>
#include<conio.h>
10 + 20 = 30

void main()
{
int a=10, b=20;
clrscr();
printf(“\n%d + %d = %d”,a,b,(a+b));
getch();
}

Here, I have declared the value of a and b in declaration only. It


is a technique to short up the program. Now in printf
statement, I have used (a+b) instead of writing c. In fact, the c
is not even required to show the sum of a and b that’s why it is
not even declared. If we write any calculation in printf
statement, the calculation is solved first and then its value is
given. There is one more advantage of removing the number of
variables, memory. The memory consumption is also less. Now,
after reducing the variable c, the total memory consumption of
the program is 2*2=4 bytes only.
TOPIC 3 : scanf () function

scanf() function is defined in the stdio.h file. This method is


useful when we want the user to give the value of the variable
at the run time. This is used when we need to change the value
of the variable with each compilation.

Assume that we have a variable a of integer type and we want


to scan the variable. Then the statement will be as follows :

scanf(“%d”,&a);

when this line comes for compilation, the screen waits for a
integer type value to enter. We then gives the value and press
enter key. The value given is then stored in a. The value must
be in the range of -32768 to +32767 as it is a integer.

Let us understand this with the help of an example :


OUTPUT :
#include<stdio.h>
#include<conio.h>
Enter two integer values : 25 15
25 + 15 = 40
void main()
{
int a, b, c;
clrscr();
printf(“Enter two integer values : ”);
scanf(“%d %d”, &a, &b);
c = a + b;
printf(“\n%d + %d = %d”,a,b,c);
getch();
}

Here, the underlined numbers are entered through keyboard


and then enter is pressed. Note that in scanf statement, two
variables are scanned hence two values are required at the run
time. These values stores in a and b respectively and the
program continues to run after that.

OPERATORS :

There are many different type of operators in C.


1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators

Operator Name

= Assignment Operator
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo
++ Increment
-- Decrement

Arithmetic Operators

Operator Name

== Comparative Equal
< Less Than
<= Less than equals
> Greater Than
>= Greater than equals
!= Not equals

Relational Operators

Operator Name

&& And operator


|| Or operator
! Not operator

Logical Operators

Operator Name

& Bitwise And


| Bitwise Or
<< Bitwise Left Shift
>> Bitwise Right Shift
^ Bitwise XOR

Bitwise Operators

Now, let’s have an program in which the roll number, marks in


4 subjects of a student will be asked and then there will be auto
calculation of total and percentage for the student.
OUTPUT :
#include<stdio.h>
#include<conio.h>
Enter the roll number : 20
Enter the marks of four subjects : 80 76 86 78
void main()
{
int RN, s1, s2, s3, s4, total;
Roll Number
float per; : 20
Subject
clrscr();
1 : 80
Subject 2 : 76
Subject
printf(“\nEnter
3 : 86 the roll number : “);
Subject
scanf(“%d”,&RN);
4 : 78
printf(“Enter the marks of four subjects : “);
Percentage
total ==s1+s2+s3+s4;
80.000000
per = (float)total/4;

printf(“\n\n\n\nRoll Number\t:\t%d”,RN);
printf(“\nSubject 1\t:\t%d\nSubject 2\t:\t%d”, s1, s2);
printf(“\nSubject 3\t:\t%d\nSubject 4\t:\t%d”, s3, s4);
printf(“\n\nTotal = %d\nPercentage = %f”, total, per);

getch();
}
Now, as we can see that the percentage is displayed up to six
points of decimal. It is not required. So to limit the floating point
variables, we can use some extra things in printf statement.
Suppose if we want to limit the floating point variable to 2
decimal points then we can use %0.2f instead of using %f in
printf statement. This is also true for the numbers with more
than 6 decimal points. If we need the number up to 20 decimal
points, we can use %0.20f. Try this.

INCREMENTING / DECREMENTING PROGRAMS :

In these programs we will see how the values of a variable


increases or decreases or changes by some quantity.

A=5;
A++;

In this line A++, the variable A is incremented by 1 unit. Now,


the value of A will be 6. In the similar manner, -- can be used.

The second type is :


A = A op X;
In this form, op is any operator (+,-,*,/,%) and X is any value,
constant or variable. We will understand this by an example.

A=8;
A = A* 2;

Here, the value of A is multiplied by 2 and then stored in A


itself. So, the value of A after the second statement will be 16.

Instead of using this method, we can use another method


which is more easy to write.
A = A op X can be replaced by
A op=X;

These both are same. A=A*2 can also be written as A*=2;

SWAPPING PROGRAM :

Swapping is a good concept in computer field. Swapping means


for the interchange of the values of two different variables. To
do so, we can adopt two methods : with use of third variable &
without using third variable.

= operator is used to swap the variable values. Let us


understand the assignment operator more deeply.

A=10;
B=20;
B=A;

So, what you guess the values of A and B after these lines ???
Ans : B=10 and A=10

If I use
A=B
At the place of B=A, then the answer will change. Then the
result will be
A=20 and B=20.

So, the conclusion is that by use of assignment operator, the


left side value is assigned to the right side. This is the only
reason that we always uses
A=10 not 10=A.

This property will be used to swap.


OUTPUT
// Swapping
: with use of third variable.
Enter two integers to swap : 20 10
#include<stdio.h>
#include<conio.h>
Before swapping,
a = 20 b = 10
void main()
After swapping,
{ = 10
a b = 20
int a, b, temp;
clrscr();

printf(“\nEnter two integers to swap : “);


scanf(“%d %d”, &a, &b);
printf (\n\nBefore swapping,\na = %d\tb = %d”, a, b);

temp = a;
a = b;
b = temp;
printf(“\nAfter swapping,\na = %d\tb = %d”, a, b);
getch();
}
Here, the value 20 is first given to temp, then a is assigned the
value of b i.e. 10 and then at the end, the value of temp (10) is
given to b to complete swapping process.
OUTPUT
// Swapping
: without using third variable.
Enter two integers to swap : 20 10
#include<stdio.h>
#include<conio.h>
Before swapping,
a = 20 b = 10
void main()
After swapping,
{ = 10
a b = 20
int a, b;
clrscr();

printf(“\nEnter two integers to swap : “);


scanf(“%d %d”, &a, &b);
printf (\n\nBefore swapping,\na = %d\tb = %d”, a, b);

a = a + b;
b = a - b;
a = a - b;
printf(“\nAfter swapping,\na = %d\tb = %d”, a, b);
getch();
}
CHARACTER PROGRAM :

When we are using characters in our program, it is a little


complex to handle than integers and floating numbers. There is
a game of ASCII codes in characters and helps in somewhat
difficult programs. The assignment in characters is also little
different.

char ch;
ch = ‘H’;

here ch will contain H in it. We can also write it as –

char ch = ‘H’;
instead of giving the character itself, we can also give the ASCII
code of any letter to the variable. The variable will
automatically fetch the corresponding character and take it.

char ch = ‘H’;
or
char ch = 72;

we can have an experimental program also in which we can see


the ASCII value or the character corresponding to any integer
value. Try this program :

// ASCII game.

#include<stdio.h>
#include<conio.h>

void main()
{
int a= 78;
char ch = ‘+’;
clrscr();
OUTPUT
printf(
: “%d %c\n%d %c”, a, a, ch, ch);
78 N getch();
43 +
}

Before swapping,
a = 20 b = 10
After swapping,
a = 10 b = 20
So, by this program we can say that the 78 contains the
character N and + has an ASCII code as 43. By this method, we
can have the A code of any character or find the character at
any ASCII code.

You might also like