Date post: | 30-May-2018 |
Category: |
Documents |
Upload: | himanshu-verma |
View: | 217 times |
Download: | 0 times |
of 39
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
1/39
C
Hello friends, this is a tutorial for the language C. This is sosimple 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 dont knowwhat 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 cancommunicate. 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 forthat 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.
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
2/39
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.
Lets 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.
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
3/39
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 aoperator. We humans are also the part of a complete computer.
HARDWAREHardware 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 areused to fetch the information from the CPU and give some
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
4/39
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.
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
5/39
These arrows shows the flow of data as data flows from Input
DevicesCPUOutput 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.
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
6/39
SOFTWARE
As we have discussed earlier, software is the visual part of thecomputer 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 Software4. 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 thememory 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.
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
7/39
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.
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
8/39
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 isdone 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 Memory2. 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 thecomputer will work. Example CD (Compact Disk), VCD (Video
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
9/39
CP
U
Cache
Memory
Primary Memory
(RAM)
Secondary
Memory
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 itsinternal 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 connectedto 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
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
10/39
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 ofthe 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.
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
11/39
Introduction to C
C is a programming language for the beginning levelprogrammers. 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 statementb. Switch Statement
3. Jump statements
a. Go to statement
b. Break statement
c. Continue statement
4. Looping
a. While loop
b. Do...While loopc. 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 charactersb. 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 Referencec. Recursion
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
12/39
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.
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
13/39
Source
Program in C
language.
Program
Screen
Output of the
program.
Output/User
Screen
Memory :
When we talk about memory, the unit of measuring of thememory 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 0s and
1s is called as Binary Language. The question arises thathow 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 personthey 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.
Compiled by Compiler
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
14/39
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
Code
Binary
Code
Letter ASCII
Code
Binary
Code
A 65 01000001 a 97 0110000
1B 66 01000010 b 98 0110001
0C 67 01000011 c 99 0110001
1D 68 01000100 d 100 0110010
0E 69 01000101 e 101 0110010
1F 70 01000110 f 102 0110011
0G 71 01000111 g 103 0110011
1H 72 01001000 h 104 0110100
0I 73 01001001 i 105 0110100
1J 74 01001010 j 106 0110101
0K 75 01001011 k 107 0110101
1
L 76 01001100 l 108 01101100
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
15/39
M 77 01001101 m 109 0110110
1N 78 01001110 n 110 0110111
0
O 79 01001111 o 111 01101111
P 80 01010000 p 112 0111000
0Q 81 01010001 q 113 0111000
1R 82 01010010 r 114 0111001
0
S 83 01010011 s 115 01110011T 84 01010100 t 116 0111010
0U 85 01010101 u 117 0111010
1V 86 01010110 v 118 0111011
0W 87 01010111 w 119 0111011
1X 88 01011000 x 120 0111100
0Y 89 01011001 y 121 0111100
1Z 90 01011010 z 122 0111101
0
0 48 00110000 1 49 001100012 50 00110010 3 51 0011001
14 52 00110100 5 53 0011010
16 54 00110110 7 55 0011011
18 56 00111000 9 57 0011100
1
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
16/39
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 character210 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
Language3. 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
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
17/39
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.
Now, let us move to the basic programming part of C.
Assembl
er
Compiler/Interp
reterHLL4GLAssembl
y
Languag
e
Machine
Languag
e
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
18/39
BASIC C PROGRAMMING
The most simplest program is written in C is :
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 thefunctions 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
#include
void main()
{
printf(Hello, welcome to C);
}
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
19/39
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 bypressing key F9. Then compiler will compile the program. If the
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
20/39
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 :
#include
void main()
{
printf(\nHello, \n\nwelcome \t\tto C);
}
OUTPUT:
Hello,
Welcome to C
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
21/39
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.
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
#include
void main()
{
printf(\n\nName\t\t:\tHimanshu Verma);
printf(\nCourse\t\t:\tB.Tech.\n);
printf(Branch\t\t:\tComputer Science);
}
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
22/39
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
Now, when we will run the program, it will hold for any key.
Also, the screen will be cleared at every compilation.
#include
#include
void main()
{
clrscr()
printf(\nHello, Welcome to C.);
getch()
}
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
23/39
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 is2 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
(Bytes)
Range
unsigned
char
1 0 to 255
char 1 -128 to +127unsigned 2 0 to 65,535
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
24/39
intshort int 2 -32,768 to +32,767
int 2 -32,768 to +32,767unsigned
long
4 0 to 4,294,967,295
long 4 -2,147,483,638 to +2,147,483,637float 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 :
#include#include
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();
}
OUTPUT :
The sum of 10 and 20 is 30
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
25/39
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 %dFloat %f Char %cDouble %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.
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
26/39
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 :
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 cis not even required to show the sum of a and b thats why it is
#include
#include
void main()
{
int a=10, b=20;
clrscr();
printf(\n%d + %d = %d,a,b,(a+b));
getch();
}
OUTPUT :
10 + 20 = 30
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
27/39
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 pressenter 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 :
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
28/39
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
#include
#include
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();
}
OUTPUT :
Enter two integer values : 25 15
25 + 15 = 40
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
29/39
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
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
30/39
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 Right Shift^ Bitwise XOR
Bitwise Operators
Now, lets have an program in which the roll number, marks in
4 subjects of a student will be asked and then there will be autocalculation of total and percentage for the student.
#include
#include
void main()
{
int RN, s1, s2, s3, s4, total;
float per;clrscr();
printf(\nEnter the roll number : );
scanf(%d,&RN);
printf(Enter the marks of four subjects : );
OUTPUT :
Enter the roll number : 20
Enter the marks of four subjects : 80 76 86 78
Roll Number : 20Subject 1 : 80
Subject 2 : 76
Subject 3 : 86
Subject 4 : 78
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
31/39
total = s1+s2+s3+s4;
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();
}
Percentage = 80.000000
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
32/39
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
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
33/39
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.
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
34/39
// Swapping with use of third variable.
#include
#include
void main()
{
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();
}
OUTPUT :
Enter two integers to swap : 20 10
Before swapping,
a = 20 b = 10
After swapping,
a = 10 b = 20
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
35/39
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.
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
36/39
// Swapping without using third variable.
#include
#include
void main()
{
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();
}
OUTPUT :
Enter two integers to swap : 20 10
Before swapping,
a = 20 b = 10
After swapping,
a = 10 b = 20
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
37/39
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;
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
38/39
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 :
OUTPUT :
78 N
43 +
Before swapping,
a = 20 b = 10After swapping,
a = 10 b = 20
printf( %d %c\n%d %c, a, a, ch, ch);
getch();
}
// ASCII game.
#include
#include
void main()
{
int a= 78;
char ch = +;
clrscr();
8/14/2019 Hello Friends, This is a Tutorial for the Language C.
39/39
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.