Dr. Shyam N. Chawda, C Language Tutorial, 78 74 39 11 91 1.1 Concepts of Programming Methodology
Dr. Shyam N. Chawda, C Language Tutorial, 78 74 39 11 91 1.1 Concepts of Programming Methodology
The most common and basic languages are C, C++, Java, Python, Php , C# , VB ,
SQL and Swift etc. These are programming languages. Instructions or programs
are written in these programming languages.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Programming languages are used to communicate with the computer through a set
of code and instruction which are collectively known as Programs.
Machine Language is the lowest level of programming language and was the first
type of programming language to be developed. Machine Language only consists
of 0’s and 1’s.
Machine language uses strings of 1's (ones) and 0’s (zeros) codes as a character
sets for writing programs. Therefore, when the computer is given the sequence of
binary code, it identifies the code and executes the program.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
High-Level languages were introduced in the 1950’s. High level Languages also
abbreviated as HLL are computer language which are developed independent of a
computer i.e. that they are not limited by the computer and are designed for a
specific job.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
• The code and instructions are very similar and use well known English
vocabulary and symbols.
• High-Level languages are easier to learn and maintain.
• High-Level languages are problem oriented rather than
machine based and are designed to solve specific problems.
• High-Level languages can be translated into many Machine
languages and can run on any computer, if the computer
has an appropriate translator.
Compiler
Compiler is a software.
While a compiling a code, compiler will report any error it finds in the code. If it
does not find any error it translates the code into Machine language.
High level languages, such as COBOL, FORTRAN, Pascal, and C, are known as
procedural languages because they use a sequence of instructions to specify on
how to solve a problem.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
1.2 Flowchart
A flowchart is a graphical representation of the plan.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Input / Output Operation (parallelogram)
Connector
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
1.2.3.1 Sequential
The sequential process is just a series of processes carried out one by one.
Following is the sequential structure of the flowchart.
Process1
Process2
Process3
START
READ A, B
C=A+B
PRINT C
STOP
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
1.2.3.2 Selection
START
READ A,
B
NO YES
IF
A>B
PRINT C PRINT A
STOP
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
1.2.3.3 Repetition
START
SUM=0
I=1
READ N
False IF True
I<=N
SUM=SUM+I
PRINT
I = I+1
SUM
STOP
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
1.3 Algorithm
Algorithm is a problem-solving technique.
Properties of an Algorithm
• Every instruction must be clear and have single clearly defined meaning.
An algorithm has following parts and these parts are always in below shown
order:
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
1.3.2 How to write algorithm
1 To read a data.
Use Read or Get instructions
For example, Read name, no1
2 To print the data
Use Print or Write instructions
For example, print "The average is", average
3. To perform arithmetic operation
Add number to sum or
Sum = sum + Number
4. To assign some value to a variable
Variable=value or set variable to value
For example, Total =0
sum=0
5. To compare the value
Use selection type instructions if...else …endif
For example,
If number a< b then
Print “maximum is “,a
else
Print “maximum is “,b
end-if
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
6. To perform repetitions operation
Use looping type instructions repeat…until or while..do
For example,
Repeat until I = 10
read number
write number
I=I+1
end-repeat
OR
while I < 10 do:
read number
write number
I=I+1
end-while
1. Start
2. Declare two variables a,b
Declar no1,no2 as Number
3. Print “Enter value of a & b
->”
4. Input a and b Input no1,no2,ans
stop
1. Start
2. Declare two variables r,ans
Declare area,p,r as Number
3. Print “Enter value of r ->”
4. Input r
5. ans=3.14*r*r
Input r
6. Print “Area of circle = “ &
ans
7. End p = 3.14
area = p * r * r
Stop
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Step Start
1. Start
2. Declare variables l,b,ans
Declare the variables l ,b, ans
3. Print “Enter value of l ->”
4. Input l
5. Print “Enter value of b ->” Input l,b
6. Input b
7. ans=l*b Ans=l*b
8. Print “Area of Rectangle = “
& ans
Print ”Area Of Rectangle:
9. End ” & ans
Stop
1. Start
2. Declare variables l,b,ans Declare the variables l ,b, ans
3. Print “Enter value of l ->”
4. Input l
5. Print “Enter value of b ->” Input l,b
6. Input b
7. ans=2*(l+b)
8. Print “perimeter of Rectangle Ans=2*(l+b)
= “ & ans
9. End Print”Perimeter of
rectangle”&ans
Stop
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Step Start
1. Start
2. Declare variables
a,b,c Dealer no1, no2, no3 as Number
11. End
Stop
1. Start
2. Declare variables p,r,n,ans
Declare the variables p,r,n,ans
3. Print “Enter value of p ->”
4. Input p
5. Print “Enter value of r ->” Input p,r,n
6. Input r
7. Print “Enter value of n ->”
8. Input n ans=p*r*n/100
9. ans=p*r*n/100
10. Print “Simple interest = “ &
ans Print”Simple Interest
11. Stop ”&ans
Stop
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Start
Step
1. Start
2. Declare variables p,r,n,t,ans Declare the variables
p,r,n,t,ans
3. Print “Enter value of p ->”
4. Input p
5. Print “Enter value of r ->” Input p,r,n,t
6. Input r
7. Print “Enter value of n ->”
8. Input n Ans=p*[1+(r/
9. Print “Enter value of t ->” n)^t]
10. Input t
11. ans=p*[1+(r/n)t] Print”Compound Interest
“&ans
12. Print “Compound Interest is
= “ & ans
13. Stop
Stop
Stop
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Step Start
1. Start
2. Declare variables f,ans Declare the variables f , ans
3. Print “Enter value of
Fahrenheit: ->”
4. Input f Input f
5. ans=(f-32)*9/5
6. Print “Celsius to Fahrenheit
= “ & ans Ans=(f-32)*9/5
7. Stop
Print”Celsius to
Fahrenheit”&ans
Stop
1. Start
2. Declare variables c,ans Declare the variables c, ans
3. Print “Enter value of celsius: -
>”
4. Input c Input c
5. ans=(c*5/9)+32
6. Print “Fahrenheit to Celsius =
“ & ans
7. Stop
Ans=(c*5/9)+32
Print” Fahrenheit to
Celsius”&ans
Stop
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Step Start
1. Start
2. Declare variables b,h,ans
3. Print “Enter value of b->” Declare the variables b,h, ans
4. Input b
5. Print “Enter value of h->” Input b,h
6. Input h
7. ans=1/2*(b*h)
Ans=½(b*h)
8. Print “Area of triangle = “ &
ans
9. Stop
Print”Area of
triangle”&ans
Stop
1. Start
2. Declare variables r,h,ans
Declare the variables r ,h, ans
3. Print “Enter value of r->”
4. Input r
5. Print “Enter value of h->” Input r,h
6. Input h
7. ans=3.14*r*r*h
8. Print “volume of a cylinder = Ans=3.14*r*r*h
“ & ans
9. Stop
Print”Volume of
Cylinder”&ans
Stop
1. Start
2. Declare variables r,ans
Declare the variables r , ans
3. Print “Enter value of r->”
4. Input r
5. ans=3.14*2*r Input r
6. Print “circumference of a
circle = “& ans
7. Stop Ans=3.14*2*r
Print”Circumference of
Circle”&ans
Stop
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Start
Step
1. Start
Declare the variables a,b,c,d,e,ans
2. Declare variables
a,b,c,d,e,ans
3. Print “Enter value of a->” Input
4. Input a a,b,c,d,e
12. Input e
13. ans=(a+b+c)/(d+e)
14. Print “Ans is = “& ans
Stop
15. Stop
1. Start
2. Declare
two Declare variables
a,b
variable
s a,b
3. Print
Input a,b
“Enter
value a
and b -
>” Y A>b No B>A Y
4. Input a
&b
5. If a>b Print “ A is Print” B is
Maximum no.”&b
then maximum no.”&a
No
6. Print “a Print “A and
is B Both are
Equal”
maximu
m”
7. Else if
b>a
then
8. Print “b
is Stop
maximu
m”
9. Else
10. Print
“Both
are
same”
11. Stop
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
1. Start
2. Declare two
variables a,b,c Declare variables a,b,c
then N
8. Print “b is
maximum” Y B>C N A>c
9. Else if c>a && c>b B=C
then
10. Print “c is maximum” Print “B” N Y
11. Else
12. Print “All are same” Print”A
Print”C
13. Stop Y
Y Stop
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Write an algorithm and flowchart to find if the given number is odd or
even
Step Start
1. Start
2. Declare Declare varibles no
variable
no
3. Print Print”enter
no”
“Enter
value no Input no
->”
4. Input no
5. If Y Print”No is Even”
no%2== no%2==0
0 then
6. Print “no
N
is even”
7. Else
8. Print “no Print”No is Odd”
is odd”
9. Stop
Stop
1. Start
2. Declare
variable no Declare varibles no
3. Print
“Enter
value no - Print”enter no”
>”
4. Input no Print”No is Positive“
Input no
5. If no>0
then Y
6. Print “no is
No>0
positive”
Print”No is
7. Else if N N Zero.”
no<0 then
8. Print “no is No<0
negative”
9. Else Y
10. Print “no is
0” Print”No is Negative“&no
11. Stop
Stop
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Step Start
1. Start
2. Declare variables
maths,ss,eng,tot Declare variable maths,ss,eng,total,
al
3. Print “Enter value
maths,ss,eng ->” Print”Enter marks of
subject”
4. Input
maths,ss,eng
5. Total=maths+ss Input maths,ss,eng
+eng
6. If total<45 then
7. Print “Fail” Total=maths+ss+eng
8. Else if total>=45
and total<=60
then Total<45 Y Print”fail”
9. Print “Pass class”
10. Else if total>60
N
and total<=70
then
total>=45 &&
11. Print “First class” total<=60
Y Print”Pass class”
Stop
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Step Start
1. Start
2. Declare variable ch
3. Print “Enter value ch -
Declare variable ch
>”
4. Input ch
5. If ch==’a’ or ch==’e’ Print”Enter ch”
or ch==’I’ or ch==’o’
or ch==’u’ then
6. Print “it’s a vowel” Input ch
7. Else
8. Print “it’s a consonant”
9. stop
ch==’a’ || ch==’e’|| ch==’I’ ||
ch==’o’ || ch==’u’ Y
Stop
Sum = Sum +I
i=i+1
Stop
Write an algorithm and flowchart to find the sum of first 10 odd and even
numbers
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Step Start
1. Start
2. Declare variables
sumo,sume,i
3. Initialize sumo Declare variables sumo,sume,i
=0,sume=0,i=1
4. If i<21 then
If I % 2 ==0
then sumo =0,sume=0,i=1
sume=sume+i
Else
sumo=sumeo+i
i=i+1 i<21
go to step 4
Y
5. else
print “Sume = “ &
Y I % 2 ==0 N
sume &” Sumo = “ &
sumo N
6. stop sume=sume+i
Sumo=sume+1
i=i+1
Stop
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Step Start
1. Start
2. Declare variables
sum,n1,n2 Declare variables sum,n1,n2
3. Initialize sum =0
4. Print “Enter n1 and
n2”
sum =0
5. Input n1 & n2
6. If n1<=n2 then
sum=sum+n1
n1=n1+1
go to step 6 Print “Enter n1 and n2”
7. else
print “Sum = “ &
sum
8. stop Input n1 & n2
n1<=n2 N Print”sum=”&sum
sum=sum+n1
stop
n1=n1+1
Write an algorithm and flowchart to count total number of odd and even
in Range
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Step Start
1. Start
2. Declare variables
cnte,cnto,N,i Declare variables cnte,cnto,N,i
3. Initialize
cnte=0,cnto=0,i
=1 cnte=0,cnto=0,i=1
4. Print “Enter N”
5. Input N
6. If i<N then Print “Enter N”
If I % 2
==0 then
Input N
cnte=cnte
+1
Else
Y i<N N
cnto=cnto
+1
i=i+1 I % 2 ==0 Y cnte=cnte+1
go to step 6
7. else N
Print”cnte=
print “cnte = “ & cnto=cnto+1 “&cnte&”cnto=”&cnto&
cnte &” cnto = “ i=i+1
& cnto
8. End
Stop
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Step Start
1. Start
2. Declare
variables
p,n,z,I,n1,n2 Declare variables p,n,z,I,n1,n2
3. Initialize
p=0,n=0,z=0
,i=1
P=0,n=0,z=0,i=1
4. Print “Enter
n1 and n2”
5. Input n1 & n2
6. If n1<=n2 Input n1,n2
then
If
n1>0
n1<=n2 Y n1<0
then
N
Y
p=p+ N n1>0 n=n+1
1
else if Y N
n1<0
z=1
then p=p+1
n1=n1+1
Print”Positive = “ & p
&”Negative = “&n&”Zero=”&z
n=n+
1
else
Stop
z=1n1
=n1+
1
go to step 6
7. else
print “Positive
= “ & p &”
Negative = “
& n &” Zero =
“&z
8. End
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
1. Start
2. Declare
variables Declare variables
M,N,value, M,N,value,Max
Max
3. Initialize
M=1,Max= .M=1,Max=0
0
4. Print Input N
“Enter N”
5. Input N
6. If M<=N
then N M<=N
Print
Y
“Enter Y
Input
value
value” Print”Enter
Value>max
Value”
Input Print”Maxim
um = “&max
value
If
Y
value>Max
then Stop
Max=value
M=M+1
Max
=value
M=M+1
go to step
6
7. else
Print
“Maximum
= “ & Max
8. End
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Start
Step
1. Start
2. Declare
variables Declare variables
M,N.value,avg
M,N,value,
Avg
3. Initialize M=1,avg=0
M=1,Avg=
0
4. Print Print “enter N”
“Enter N”
5. Input N
6. If M<=N
Input N
then
Print
“Enter
value”
Input M<=N
value Y
Print”enter
Input value
value”
Avg=avg+value
N
Avg=Avg+ M=M+1
value
Print”Maximum=”&(Avg/N)
M=M+1
go to step
6
7. else
Stop
Print
“Maximum
=“&
(Avg/N)
8. End
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Step Start
1. Start
2. Declare variables
R,N,value,pow Declare variables
3. Initialize M=1,pow=1 R,N,value,pow
7. else
Print “Ans = “ & pow
8. End Input N,R
Pow=pow*N
M<=R Y
M=M+1
Print”ans=”
&pow
Stop
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Step Start
1. Start
2. Declare variables N,
value, fact Declare variables
3. Initialize fact=1 N,value,fact
4. Print “Enter N”
5. Input N fact=1
6. If N>0 then
fact=fact*N
N=N-1 Print “Enter N”
go to step 6
7. else
Print “Ans = “ & fact Input N
8. End
Fact=fact*N
N N>0 Y
N=N-1
Print”Ans=”
&fact
Stop
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Step Start
1. Start
2. Declare variables N, Sum
Declare variables
3. Initialize Sum=0 N,sum
4. Print “Enter N”
5. Input N
6. If N>0 then Sum=0
Sum=Sum+N%10
N=N/10
go to step 6 Print “Enter N”
7. else
Print “Sum = “& sum
8. End Input N
Sum=sum+N%10
N>0 Y
N=N/10
Print”Sum=
”&sum
Stop
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Start
Step
1. Start
2. Declare variables N,rev Declare variables
3. Initialize rev=0 N,rev
4. Print “Enter N”
5. Input N rev=0
6. If N>0 then
N>0 Y rev=(rev*10)+N%10
Print”Reverse = “rev
Stop
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
5. Input N
6. If i<N then
If N%i==0 then
flag=1
Go to step 7
Else
Go to step 6
7. If flag==1 then
Print “No is Not prime “
Else
Print “No is prime”
8. End
1. Start
2. Declare variables N, sum,i
3. Initialize sum=0 Declare variables N,sum,i
4. Print “Enter N”
5. Input N
6. If i<=N then
sum=sum+(i*i) sum=0
go to step 6
7. Print sum
8. End Input N
i<=N
sum=sum+(i*i)
Print”sum”
Stop
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Step Start
1. Start
2. Declare variables N, sum,i
3. Initialize sum=0 Declare variables N,sum,i
4. Print “Enter N”
5. Input N
6. If i<=N then
sum=sum+(i*i) sum=0
go to step 6
7. Print sum
8. End Input N
i<=N
Sum=sum+(i*i)
Stop
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Keywords
Libraries
• C Compiler comes with list of header files which consist of many built in
functions which can be used to develop program.
Modularity
• As a middle level language C combines both the advantages of low level and
high-level languages.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Operating Systems are written in C language
• Major operating systems (OS) like Windows, Linux and Unix are all written
in C language. So, if we want to modify programs in Windows or Linux, basic
understanding of C is inevitable.
Portability
Easy to learn
Speed
• It is Close to hardware make many ‘C’ programs run at speed close to their
assembly language.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
The American National Standards Institute (ANSI) has developed a standard and
formed a committee for the C language in 1983.
In this book, all the examples are tested using DOS based Borland turbo C++
Version 3.0 compiler.
Step 1: Open a turbo C++ editor or any editor and write a program.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Step 2: Save the program and give the file name with extension.
For C program, the extension name must be. C. Filename can be any name but
should not be more than 8 characters and does not contain space or any symbols.
Compile the program. If you are using turbo C than from the compile menu and
select compile option. Compiler perform two operations, first find out errors in your
program, if no errors then it perform second operation, converts program into
binary file also known as object file(.obj) which is understood by the computer only.
If there are any errors during compilation process, then debug them and compile
the program again. During compilation process, compiler make an object file which
is specific to the operating system (on which we have compiled the program) and
hardware dependent, this means compiled code may not work on another operating
system.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Link the object file (obj) generated by the compiler with other library files(lib) to
make a executable file(exe).This process is done by selecting link option from the
compile menu. If any errors occur during linking process, correct them and compile
the program again otherwise we can run the program by selecting run option from
the run menu in turbo C.
The preprocessor
These are the various Helpful files which gets executed on compiling. These
Preprocessor Files must be included in any program to run it smoothly, creating no
any errors. These commands tell the compiler to do preprocessing before doing
actual compilation. These are readymade standard library files with C provides the
facilities that are not defined within C, which we will se later on… Soo Cheers!!!
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Comments
Comments are used for commenting on Source code which will not get compiled
but it will be very helpful in finding errors, functioning of Various Commands.
Functions
1. User-Defined Functions
2. Derived Functions
There is only one compulsory function called - the main function. We can also write
our own functions.
Example#
/*Here, our all main contain to be executed, starts with a main() function which is
compulsory in every program*/
void main()
{
clrscr();
printf(“HELLO WORLD…!!”);
getch();
}
Output:
Hello World
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
The Basic structure of a ‘C’ Program…
1. Documentation section
2. Link section
The link section provides instructions to the compiler to link functions from
the system library such as using the #include directive.
3. Definition section
The definition section defines all symbolic constants such using the #define
directive.
There are some variables that are used in more than one function. Such
variables are called global variables and are declared in the global
declaration section that is outside of all the functions. This section also
declares all the user-defined functions.
Every C program must have one main function section. This section contains
two parts; declaration part and executable part
1. Declaration part
The declaration part declares all the variables used in the executable
part.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
2. Executable part
6. Subprogram section
All section, except the main () function section may be absent when they are not
required.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
1.7 Executing C Program
Step I: Edit
1. During Compilation Compiler will check for error, If compiler finds any
error then it will report it.
1. If run time error occurs then “Run-time” errors are reported to user.
2. Again, programmers have to review code and check for the solution.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
C Language has its own character set as given below. The only characters required
by the C Programming Language are as follows:
1.8.1 Letters
LowerCase a-z
UpperCase A-Z
1.8.2 Digits
0-9
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
1.8.3 Special Characters
These characters are also known as back slash or escape characters. Some white
space characters are non-printable. They are new line, form feed, blank space,
horizontal tab, carriage return.
These characters are used to print some character or to format the output of the
program. In C, these white space characters are also identifying as character
constant.
They start with back slash (\) and then following is the white space character.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
1.9 Tokens
C tokens are the basic buildings blocks in C language which are constructed
together to write a C program.
Token Example
Keywords int, for,if
Identifiers main, result ,add
Constants 11, 3.14
Strings "total", "hello"
Special symbols (), {},"'",;
Operators +, /,-,*
Example# Identifiers
void main()
{
int a,b,sum;
a = 10;
b = 20;
sum=a+b;
printf ("\nSum = %d", sum);
getch();
}
main – identifier
{,}, (,) – delimiter
+ - operator
10 20 - constant
int – keyword
a,b,sum – identifier
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
1.10.1 Keywords
A word in C is named as keyword or identifier, consisting the set of rules for Case-
sensitivity, reserved word that are mainly used in writing a C program.
Since keywords are referred names for compiler, they can’t be used as variable
name.
1.10.2 Identifiers
Identifiers are used to give name to the variables, functions, arrays, structure, and
union. This name is given by the programmer. Name of the identifier should be
meaningful.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Following are the rules when declaring identifiers.
Example#
1.11 Constants
Constants refer to fixed values that the program may not alter
during its execution. These fixed values are also called boot
literals or just literals.
There are enumeration constants as well. Constants are treated just like regular
variables except that their values cannot be modified after their definition.
212 /* Legal */
215u /* Legal */
0xFeeL /* Legal */
078 /* Illegal: 8 is not an octal digit */
032UU /* Illegal: cannot repeat a suffix */
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
✓ An integer literal can also have a suffix that is a combination of U and L,
for unsigned and long, respectively. The suffix can be uppercase or
lowercase and can be in any order. Following are other examples of various
types of integer literals –
85 /* decimal */
0213 /* octal */
0x4b /* hexadecimal */
30 /* int */
30u /* unsigned int */
30l /* long */
30ul /* unsigned long */
✓ While representing decimal form, you must include the decimal point, the
exponent, or both; and while representing exponential form, you must
include the integer part, the fractional part, or both. The signed exponent
is introduced by e or E.
3.14159 /* Legal */
314159E-5L /* Legal */
510E /* Illegal: incomplete exponent */
210f /* Illegal: no decimal or exponent */
.e55 /* Illegal: missing integer or fraction */
✓ Character literals are enclosed in single quotes, e.g., 'x' can be stored in a
simple variable of char type.
✓ You can break a long line into multiple lines using string literals and
separating them using white spaces.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
✓ Here are some examples of string literals. All the three forms are identical
strings.
1.12 Variables
Variables are used to store a data. A variable is a container (storage area) to hold
data.
We can think of a variable like a bucket and values are like a water in bucket which
can easily change.
Variable is an entity where user can store digits, characters and strings. It is similar
to a box or a container.
Constant store a value that cannot be change but storing a value in a variable value
can be change.
These cells are called the location or address that store the data. Variables are used
to give a name to these locations.
A name given to the memory location is known as variable name. There are rules
that must be followed before giving a variable name in C. They are as under.
• In C, it must start with an alphabet or underscore (_) and following
characters can be letter, underscores and digits only.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
• Both uppercase and lowercase letters can be
used.
• Spaces are not allowed in variable name.
• Certain keywords (these are C language reserved
words) cannot be used in a variable name.
• The variable names should not be very long.
(maximum 32 characters)
• The variable names should be meaningful.
Example#
Valid A, a1, a_ ,_ a
Invalid If, 1a, a#, a bc
Syntax#
Data-type variableName1, variableName2 … variableNameN;
Example# Name
int sum, rollno;
Variable
float average;
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
1.12.4 Data types in C
For example, in Our secular nation we can generally identify a person simply looking
at their costumes, their identity belonging, their language, their religion, their daily
eating etc. Similarly, in C language we can get idea about variable category from
its data type.
Data types represent the type of data to be stored in a variable. We store a data
in computer memory through the variables. So variables type must be specified
before Storing data into the computer memory.
Computer memory is organized in bytes, and byte stores the smallest amount of
data. C language supports different types of data (integer, float, character etc.)
and need to store the values in the program. In C, there is fixed amount of memory
assigned to each data type.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Example#
int num1,num2;
long int num3;
There are various integer data types each has different memory assignment. This
gives you the flexibility of choosing the right type of variable in the program.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
The float data type is used to store fractional numbers (real numbers) with up to 6
digits of precision.
When the accuracy of the floating-point number is insufficient, we can use the
double type instead of float.
The double is same as float but provide more memory space for storing large value
with longer precision.
Example#
float percentage;
double meterreading;
There are various float data types each has different memory assignment. this
gives you the flexibility of choosing the right type of variable in the program.
Character type variable can hold a single character. There are signed and unsigned
chars; both occupy 1 byte each, but having different ranges.
Example#
char ch = ‘a’;
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
1.12.4.1.4 void Type
This data type is used return type of the function and it means function does not
return any value.
Another use with the as an argument of the function and meaning is that function
does not has any arguments.
Example#
1.12.4.2.1 Arrays
• Arrays can be any of the C data-types int, float, and char. An integer array
can only hold integer values and cannot hold values other than integer
similarly for others data types.
Like an Excel spreadsheet, arrays have a position number for each row.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Each object of the array can be accessed
by using its positions number - index.
The positions-index in an array start at 0
and go up sequentially. Each position in
the array can hold a value. When we
declare an array, we need to set its size.
Types of Arrays in C
One dimensional
Syntax# Example#
type arrayName [ arraySize ]; int a[10];
Multi-dimensional
Array having more than one subscript variable is called Multi-Dimensional array.
Syntax# Example#
type arrayName [ arraySize ] [ arraySize ]; int matrix[3][3];
1.12.4.2.2 Structures
Syntax# Example#
struct structure_name struct stu
{ {
data type member1; int sno;
data type member2; char sname[20];
… int sem;
… }; }
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
1.12.4.2.3 Pointers
By using the pointer, we can change the content of the memory location.
Pointer allows accessing the memory location directly. This is the most important
feature of the C language.
Syntax# Example#
type * identifier; int *ptr;
1.12.4.3.1 typedef
In C language, a user can define another name for the data-type also known as
alias. This alias can later be used to declare variables.
Syntax#
typedef data-type identifier;
here data-type represent built in data type and ‘identifier’ denotes name given to
the data type.
Example#
Now we can use usint as a data-type name for unsigned short int and shyam in the
program
i.e. usint a; shyam b;
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
1.12.4.3.2 enum
Syntax#
The “identifier” is a user defined enumerated data type which can be used to declare
variables that have one of the values enclosed within the braces.
The enumerated variables V1, V2,…..Vn can have only one of the values value1,
value2 ….. value n
Example#
enum colour {Red,Green,Blue};
enum colour c1;
c1 = Red;
Example#
enum day {
Monday = 0,
Tuesday = 10,
Wednesday = 20,
Thursday = 3,
}
Example#
#include<stdio.h>
#include<conio.h>
main()
{
int a;
clrscr();
printf("A = %d",a);
getch();
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
}
Output
A = -28762
Syntax#
Data-type variableName=value;
Example#
int rollno=5, total=300;
float pi=3.14;
char name[]=”Shyam”;
Syntax#
VariableName3=VariableName1/VariableName2
Example#
int a=20,b=5,c,x;
c=a/b;
printf(“\nEnter no =>”);
scanf(“%d”,&x);
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
1.13 Type Conversion
Converting an expression of a given type into another type is known as type-
casting. typecasting is more use in c language programming.
Here, it is best practice to convert lower data type to higher data type to avoid data
loss.
Data will be truncated when higher data type is converted to lower. For example,
if float is converted to int, data which is present after decimal point will be lost.
Here, the value of a has been promoted from int to double and we have not had to
specify any type-casting operator. This is known as a standard conversion.
Example#
#include<stdio.h>
#include<conio.h>
void main()
{
int i=20;
double p;
clrscr();
p=i; // implicit conversion
printf(“implicit value is %d”,p);
getch();
}
Output
implicit value is 20.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
They are not automatically performed when a value is copied to a compatible type
in program.
Syntax#
variableName =(DataType)VariableName;
Example#
#include<stdio.h>
#include<conio.h>
void main()
{
int i=20;
short p;
clrscr();
p = (short) i; // Explicit conversion
printf(“Explicit value is %d”,p);
getch();
}
Output
Explicit value is 20.
1.14 Comments
In the C Programming Language, you can place comments in our source code that
are not executed as part of the program.
Comments provide clarity to the C source code allowing others to better understand
what the code was intended to accomplish and greatly helping in debugging the
code.
A comment starts with a slash asterisk /* and ends with a asterisk slash */ and can
be anywhere in your program. Comments can span several lines within your C
program.
Comments are typically added directly above the related C source code.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Adding source code comments to your C source code is a highly recommended
practice. In general, it is always better to over comment C source code than to not
add enough.
Syntax#
// Author: Shyam
Syntax#
/*
comment goes here
*/
Example#
/*
* Author: Aspiration Institute- Shyam Sir
* Purpose: To show a comment that spans multiple lines.
* Language: C
*/
The compiler will assume that everything after the /* symbol is a comment until it
reaches the */ symbol, even if it spans multiple lines within the C program.
www.shyamsir.com 78 74 39 11 91