C Language
C Language
C Language
Programming in ‘C’
Computer Engineering Group
Salient Features
• Concise content with complete coverage of revised G-scheme syllabus.
• Simple and Lucid language.
• Illustrative programs with detailed explanation.
• MSBTE Theory Questions and Programming questions from Summer-2011 to
Winter-2015.
• MSBTE Question Papers of Summer, Winter - 2014 and 2015.
• Three Model Question Papers for practice.
• Important Inclusions: Additional Theory Questions, Practice Programs.
No part of this book may be reproduced or transmitted in any form or by any means, C.D. ROM/Audio Video Cassettes or electronic, mechanical
including photocopying; recording or by any information storage and retrieval system without permission in writing from the Publisher.
TEID : 970
PREFACE
In the case of good books, the point is not how many of them you can get through, but rather how many
can get through to you.
Target’s “Programming in C” is a complete and thorough book critically analysed and extensively
drafted to boost the students’ confidence. The book is prepared as per the revised scheme [G-scheme] of
MSBTE curriculum effective from June 2012.
Every chapter is divided into sub-topics as per the requirement of the syllabus. The arrangement of
the sub-topics are such that it helps the students in easy comprehension.
Each chapter includes the following features:
Theory is provided point-wise. Explanatory diagrams have been provided wherever required.
Definitions are in italic representation.
Illustrative Programs are provided with detailed explanation and programming comments which
lead to the smooth and detailed understanding of the program.
MSBTE Theory Questions covered in separate section to give a clear idea of the type of questions
asked. (Reference of answer to each question is provided.)
MSBTE Programming Questions till latest year are included along with its answers.
Additional Theory Questions help the students gain insight on the various levels of theory-based
questions.
Programs for Practice in every chapter for students to improve their programming skills.
MSBTE Question Papers of years 2014 and 2015 are added at the end to make students familiar with the
examination.
Three Model Question Papers are provided to prepare students for examination.
The journey to create a complete book is strewn with triumphs, failures and near misses. If you think
we’ve nearly missed something or want to applaud us for our triumphs, we’d love to hear from you.
Please write to us on : mail@targetpublications.org
A book affects eternity; one can never tell where its influence stops.
Contents:
History of C, where C stands 08 18
C character set, tokens, constants, variables, keywords, identifiers
C operators- arithmetic, logical, assignment, relational, increment and
decrement, conditional, bit wise, special, operator precedence, C
expressions data types.
Problem solving techniques : flowchart and algorithm.
Formatted input, formatted output instructions.
Topic 2 - Decision making
Specific objectives:
Write a simple program using decision making, branching statement,
looping statement.
Describe use of break and continue statement. 10 28
2.1 Decision making and branching if-statement – if, if-else, else-if ladder,
nested if else, switch case statement, break statement [14 M]
2.2 Decision making and looping - while, do, do- while statement, for loop,
continue statement [14 M]
Topic 5 – Pointers
Specific objectives:
State the declaration syntax of pointer, pointer initialization
Write the program using pointer arithmetic 06 12
Understanding pointers, declaring pointer variable, initialization of pointer
variable, accessing address of a variable, pointer expressions, pointers arithmetic
TOTAL 48 100
Contents
Chapter No. Topic Page No.
1 Basics of ‘C’ 1
2 Decision Making 35
5 Pointers 151
Appendix
ASCII Values of characters 186
Target Publications Pvt. Ltd. Basic PhysicsChapter
(F.Y.Dip.Sem.-1) of 'C'
01: BasicsMSBTE
Chapter
1 Basics of 'C'
1.1 History of C
1.2 The C Character Set
1.3 Tokens In C
1.3.(a) Semicolons and Comments
1.3.(b) Identifiers
1.3.(c) C Keywords
1.3.(d) Constants
1.3.(e) Type Qualifiers
1.3.(f) Variables
1.4 Basic Structure of a C Program
1.4.(a) C Header Files and Standard Library Functions
1.5 Input and Output
1.5.(a) Input Functions
1.5.(b) Output Functions
1.6 The First C Program
1.6.(a) Steps to compile and execute a C program
1.6.(b) C Instructions
1.7 C Operators
1.7.(a) Arithmetic Operators
1.7.(b) Relational Operators
1.7.(c) Logical Operators
1.7.(d) Assignment Operators
1.7.(e) Bitwise Operators
1.7.(f) Miscellaneous Operators
1.7.(g) Operators Precedence and Associativity in C
1.8 Formatted Input and Output
1.8.(a) Formatted Input
1.8.(b) Formatted Output
1.8.(c) Format Specifications
1.9 Problem Solving Techniques
1.9.(a) Algorithms
1.9.(b) Flowcharts
1
Target Publications Pvt. Ltd. Prog. in C (F.Y.Dip.Sem.-2) (Comp. Engg.) MSBTE
1.1 History of C
What is C ?
C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was
designed and written by a man named Dennis Ritchie. In the late seventies C began to replace the
more familiar languages of that time like PL/I, ALGOL, etc.
The C language is popular because it is reliable, simple and easy to use.
Historical development of C:
Year Language Developed by Remarks
1960 ALGOL International Committee Too general, too abstract
1963 CPL Cambridge University Hard to learn, difficult implement.
1967 BCPL Martin Richards at Could deal with only specific problems.
Cambridge University
1970 B Ken Thomson at AT & T Could deal with only specific problems.
1972 C Dennis Ritchie at AT & T Lost generality of BCPL and B restored. This is
the C language which we are going to study.
Some important features of C language are given below.
a. C language has several built-in functions and operators which can be used to write any complex
program.
b. The programs written in C language are portable, i.e. they can be run on all the compilers with
least or negligible modifications.
c. The C language also supports user-defined data types and user-defined functions which can be
designed as per the requirement of the programmer. Thus C has the ability to extend itself.
d. The C language is essentially an assembly level language with features of high-level language.
1.3.(d) Constants:
A constant is a value that doesn’t change during the execution of a program. This value
can be stored at a location in the memory of a computer.
Types of C Constants:
C constants can be divided into two major categories:
a. Primary Constants b. Secondary Constants
These constants are further categorized as shown .
C constants
1.3.(f) Variables:
A variable is an entity that may change and take different values at different times during the
execution of the program. It is a name given to a memory location where a data is stored.
5
Target Publications Pvt. Ltd. Prog. in C (F.Y.Dip.Sem.-2) (Comp. Engg.) MSBTE
The following example demonstrates the idea of a variable. Here 3 is stored in a memory
location which is named as v. Then a new value 5 is assigned to the same memory location v.
This would overwrite the earlier value 3, since a memory location can hold only one value at a
time. This is shown below.
v=3 v=5
v 3 v 5
Memory location of v Memory location of v
(after assigning new
value)
i. Data types of C Variables:
The data types in C, specifies what type of data can be stored in a variable. Date types are
of the following three categories:
1. Primary data types 2. User-defined data types
3. Derived data types
The different primary data types are as follows:
a. Character (char) type:
The character (char) data type can be used to define any single character. It usually
takes 1 byte (8 bits) of space. They are of two types, viz.
1. signed 2. unsigned
The default type is signed char type.
E.g.: char a = 'A';
Here the character 'A' is assigned to the char variable a.
b. Integer (int) type:
The integer (int) data type is used to define a variable accepting any integer
number.
The int data type are of following types:
1. short int 2. int 3. long int
Each of the above three int data types can be either signed or unsigned. However
the default type is signed.
E.g.: int a = 20;
Here the integer value 20 is assigned to the int variable a.
The difference between the short int and long int is given below:
Short int Long int
i. Stores smaller value of integers Stores larger value of integer than int
than int data type. data type.
ii. Range is -128 to 127 for signed Range is -2,147,483,648 to
short int and 0 to 255 for 2,147,483,647 for signed long int and 0
unsigned short int to 4,294,967,295 for unsigned long int.
iii. Requires lesser memory space Requires more memory space than int.
than int data type.
iv. Memory space required is 8 bits Memory space required is 32 bits (4
(1 byte) bytes)
v. Syntax: short int a; Syntax: long int a;
6
Target Publications Pvt. Ltd. Basic PhysicsChapter
(F.Y.Dip.Sem.-1) of 'C'
01: BasicsMSBTE
8
Target Publications Pvt. Ltd. Basic PhysicsChapter
(F.Y.Dip.Sem.-1) of 'C'
01: BasicsMSBTE
vii. Executable part: This part consists of minimum one executable statement. The executable part
uses all the global variables and local variables either directly or through the standard library
functions and the user-defined functions.
Both the Declaration part and the Executable parts appear between opening and closing braces
{ } which form the body of the main( ) function. The execution of the program begins at the
opening brace and logically ends at the closing brace of the main function.
viii. Subprogram section: The subprogram section consists of all the user-defined functions that
are called in the main( ) function and may have been declared in the global declaration section.
User-defined functions are generally placed immediately after the main( ) function, although
they may appear in any order.
1.4.(a) C Header Files and Standard Library Functions:
The C library consists of several pre-defined functions grouped under different header files,
which can be accessed and used as per the requirement in a C-program. As discussed in the
previous section, the header files are included in the link section of the program. Some of the
frequently used header files and the type of functions they give access to, are mentioned below.
C Header Files Type of functions under the header file
<stdio.h> Standard input and output functions
<conio.h> Console input output functions
<math.h> Mathematics functions
<string.h> String functions
<ctype.h> Character handling functions
<stdlib.h> General utility functions
i. Functions under <stdio.h>:
The header file stdio.h refers to the standard input/ output header file. It is a standard
library in C which contains constants, macros definitions and declarations of functions. It
includes data types required for all input and output operations.
Some of the frequently used functions under <stdio.h> header file are:
printf(), scanf(), getchar(), putchar(), etc.
Note: The printf() and scanf() functions are discussed in section 1.5.
ii. Functions under <conio.h>:
Some of the frequently used functions under <conio.h> header file and their use are given
below.
clrscr(): Used for clearing the old data from the output window before beginning of
program.
getch() and getche(): Both are used for accepting a character from user. The difference
between these two functions is as follows.
getch() getche()
1. It does not display the character entered The character entered by the user is
by the user on screen, but accepts it and displayed on screen at the cursor and
saves as per the statement. also accepts and saves it as per the
statement.
9
Target Publications Pvt. Ltd. Prog. in C (F.Y.Dip.Sem.-2) (Comp. Engg.) MSBTE
12
Target Publications Pvt. Ltd. Basic PhysicsChapter
(F.Y.Dip.Sem.-1) of 'C'
01: BasicsMSBTE
Illustrative Program
Output:
Hello, World!
Illustrative Programs
# include<stdio.h>
void main()
{
int x = 43, y = 10, z; /* declaration and initialization of variables */
z = x % y; /* calculates remainder of the division of 43 and 10 */
printf("Modulo of the two given numbers is: %d", z);
}
Output:
Modulo of the two given numbers is: 3
ii. Unary Operators:
The arithmetic operators which require only one operand/ variable to operate upon are
called as unary arithmetic operators. The two unary arithmetic operators supported by C
language are;
a. Increment operator:
The increment operator increases (increments) the value of the associated variable
by one. The ++ operator is the increment operator.
Increment operators can either be pre-increment operator or post-increment
operator.
i. Pre increment Operator: This operator is used to increment the value of
variable before using it in the expression. The pre-increment operator first
increments the value and then it is used in the expression.
E.g.: y = ++ x ;
Here, x is first incremented and then the new value is associated to y.
ii. Post increment Operator: This operator is used to increment the value of
variable after executing expression completely in which post increment is
used. The post increment operator first uses the variable in the expression
and then increments it.
E.g.: y = x ++ ;
Here, the value of x is first associated to y and then x is incremented.
15
Target Publications Pvt. Ltd. Prog. in C (F.Y.Dip.Sem.-2) (Comp. Engg.) MSBTE
Illustrative Program
#include<stdio.h>
void main()
{
int x=10,y=10,v1,v2; /* variable declaration */
clrscr();
v1 = x++; /* post-increment */
v2 = ++y; /* pre-increment */
printf("Value of v1 : %d",v1);
printf("Value of v2 : %d",v2);
}
Output:
Value of v1 : 10
Value of v2 : 11
From the above program the difference between pre-increment (++i) operator and post-
increment (i++) operator can be summarized as follows:
Pre - Increment (++i) Operator Post - Increment (i++) Operator
i. It is used as prefix before the variable. It is used as postfix after the variable.
ii. This operator increases the value of the This operator increases the value of the
variable by 1, before execution of the C variable by 1, after the execution of the C
statement containing it. statement containing it.
iii. The C statement containing the operator The C statement containing the operator
and the following statements use the cannot use the incremented value of
incremented value of the variable. variable. It is available for use from the
next logical C statement.
iv. void main() void main()
{ {
int i = 10; int i = 10;
printf("i = %d", ++i); printf("i = %d", i++);
printf("\n Later i = %d", i); printf("\n Later i = %d", i);
} }
Output: Output:
i = 11 i = 10
Later i = 11 Later i = 11
b. Decrement operator:
The decrement operator decreases (decrements) the value of the associated variable
by one. The -- operator is the decrement operator.
16
Target Publications Pvt. Ltd. Basic PhysicsChapter
(F.Y.Dip.Sem.-1) of 'C'
01: BasicsMSBTE
Illustrative Program
#include<stdio.h>
void main()
{
int x=10,y=10,v1,v2; /* variable declaration */
clrscr();
v1 = x--; /* post-decrement */
v2 = --y; /* pre-decrement */
printf("Value of v1 : %d",v1);
printf("Value of v2 : %d",v2);
}
Output:
Value of v1 : 10
Value of v2 : 9
2. Write an algorithm to find largest among three numbers entered by the user.
Ans: The algorithm to read three numbers from user and find the largest among them is given below:
Step-1: Start
Step-2: Declare three variables ‘a’, ‘b’ and ‘c’.
Step-3: Accept values for ‘a’, ‘b’ and ‘c’ from user.
Step-4: Check as per the following:
If a > b
Then if a > c
Display ‘a’ is the largest number.
Else (i.e. if c > a)
Display ‘c’ is the largest number.
Else (i.e. if b > a)
Then if b > c
Display ‘b’ is the largest number.
Else (i.e. if c > b)
Display ‘c’ is the largest number.
Step-5: Stop
1.9.(b) Flowcharts:
Flowcharts are used in C programming due to the following reasons:
i. Visualizing the flow of program graphically is better than describing the same in words.
ii. It helps in understanding the method of coding and improves its efficiency wherever
possible.
iii. It is an easy method of explaining the methodology of a C program through symbols and
text to other programmers.
iv. It also helps in finding errors or debugging the program.
Flowcharts are usually drawn using standard symbols. However, some special symbols can also
be developed when required.
The standard symbols are listed below:
i. Terminator:
An oval flowchart shape indicates the start or end of the
process, usually containing the word “Start” or “End”.
ii. Process:
A rectangular shape indicates a branch in the process
flow step.
For example, "c=a+b", "i=i+1"
iii. Decision:
A diamond shape indicates a branch in the process flow.
This symbol is used when a decision is to be made,
commonly a YES/NO question or True/False test.
iv. Connector:
A small, labelled, circular flowchart used to indicate a
jump in the process flow. Connectors are usually used in
complex or multi-sheet diagrams.
28
Target Publications Pvt. Ltd. Basic PhysicsChapter
(F.Y.Dip.Sem.-1) of 'C'
01: BasicsMSBTE
v. Data:
A parallelogram that indicates data input or output (I/O)
for a process.
vi. Delay:
It is used to indicate a delay or wait in the process.
vii. Arrow:
Used to indicate the flow of control in a process.
Illustrative Program
suma+b
Display sum
Stop
Note: Apart from the above mentioned techniques, some other problem solving techniques are also
used. Few of them are mentioned below:
i. Pseudocode: It is an informal high level description of the operating principle of a
computer program or other algorithm.
ii. Solve by analogy: Analogy is just a broader application of the strategy of looking for
things that are familiar.
iii. Means ends analysis: The strategy behind means and ends analysis is to define the ends
and then to analyze your means of getting between them.
iv. Divide and conquer: The principle behind this is to break up a large problem into
smaller pieces so that we can solve individually.
v. The building block approach: It is a combination of solve by analogy and divide and
conquer. The idea behind this is to see if any solutions for smaller pieces of the problem exist.
vi. Merging solution: The idea behind this is to combine existing solutions and merge them
on a step by step basis.
29
Target Publications Pvt. Ltd. Prog. in C (F.Y.Dip.Sem.-2) (Comp. Engg.) MSBTE
30
Target Publications Pvt. Ltd. Basic PhysicsChapter
(F.Y.Dip.Sem.-1) of 'C'
01: BasicsMSBTE
14. Define following terms: Token 19. What is the difference between short int
Also give examples. [W-11] [4 M] and long int data types? [W-12] [2 M]
OR Ans: Refer 1.3.(f)-i-b (Table of difference
What are C tokens? Give suitable between short int and long int)
examples. [W-13] [4 M] 20. State examples of library functions.
OR [W-12] [2 M]
Enlist types of tokens available in C and Ans: Refer 1.4.(a)
give one example for each.
[S-15] [4 M] 21. State various bit wise operators with
Ans: Refer 1.3 examples. [W-12] [4 M]
OR
15. List bitwise operator used in C. List bit wise operators and explain any
[S-12] [2 M] two. [S-15] [2 M]
Ans: Refer 1.7.(e) (Table of operators and its Ans: Refer 1.7.(e)
meaning) 22. State the operation and difference
16. What are different data types used in between getch(); and getche();
‘C’? [S-12] [4 M] [W-12] [4 M]
OR Ans: Refer1.4.(a)-ii (Table)
List out primary data types and state 23. Explain formatted output statement for
their size in bits. [W-12] [4 M] any eight format codes. [W-12] [4 M]
OR Ans: Refer 1.8.(b)-i and 1.8.(c)-iii Table.
List all basic data types. [S-13] [2 M] 24. State arithmetic and logical operators.
OR [S-13] [2 M]
State various data types along with their Ans: Refer 1.7.(a)-i and ii Table
memory sizes. [W-13] [4 M] (for arithmetic operators) and
OR Refer 1.7.(c) Table (for logical operators)
State different data types in C.
[W-14] [2 M] 25. State the rules for variable declaration.
[S-13] [2 M]
Ans: Refer 1.3.(f)-i-c (Table of Data types,
Ans: Refer 1.3.(f)-iii
Size and Range)
17. Enlist relational operators used in ‘C’ 26. Give the syntax of input and output
also write C program that makes use of statement in C. [S-13] [2 M]
relational operation. [S-12] [4 M] Ans: Refer 1.8.(a) and 1.8.(b) syntax only.
Ans: Refer 1.7.(b) and the Illustrative 27. Draw the structure of C program. Also
Program following it. give one example. [S-13] [4 M]
OR
18. What is meant by increment and Write and explain the structure of a C
decrement operator? [S-12] [4 M] program. [W-13] [4 M]
OR OR
State use of increment and decrement Describe generic structure of 'C'
operators with examples. [W-14] [4 M] program. [S-14] [2 M]
OR OR
What is meant by increment and Explain structure in C with suitable
decrement operator? Explain with example. [W-14] [4 M]
example. [W - 15] [4 M] Ans: Refer 1.4 and Illustrative Program after
Ans: Refer 1.7.(a) - ii.a and ii.b 1.6
31
Target Publications Pvt. Ltd. Prog. in C (F.Y.Dip.Sem.-2) (Comp. Engg.) MSBTE
28. Define the terms: [S-13] [4 M] 35. Enlist different format specifier with its
i. Key word use. [S-14] [4 M]
Ans: Refer 1.3(c) (Only definition) Ans: Refer 1.5(a)-i.d
ii. Variable 36. Explain pre-increment and post
Ans: Refer 1.3.(f) (Only definition) increment operator. [W-14] [2 M]
iii. Identifier Ans: Refer 1.7.(a) - ii.a and ii.b
Ans: Refer 1.3(b) (Only definition) 37. State the four operators in C.
iv. Constant [W-14] [2 M]
Ans: Refer 1.3(d) (Only definition) Ans: Refer 1.7
29. State the use of increment and decrement 38. Distinguish between variable and
operator. Give difference between i++ and constant. [W-14] [4 M]
++i. [S-13, W-13] [4 M]
Ans: Refer 1.3.(f)-v
Ans: Refer 1.7.(a) - ii.a and ii.b
Refer 1.7.(a)-ii.a (Table giving 39. List types of constants. [S-15] [2 M]
difference between Pre-increment and Ans: Refer 1.3(d)
Post-increment Operator)
40. State four rules for choosing variable
30. State four arithmetic and four logical
name. [S-15] [2 M]
operators with their uses. [W-13] [2 M]
Ans: Refer 1.3.(f)-ii
Ans: Refer 1.7.(a) i and ii and 1.7.(c) (Table)
31. With suitable example and syntax, 41. Explain increment and decrement
explain how formatted input can be operator with examples. [S-15] [4 M]
obtained. [W-13] [4 M] Ans: Refer 1.7.(a)-ii
Ans: Refer 1.8.(a)
42. State the importance of flowcharts with
32. State two features of C-language. the symbols used for drawing flowchart.
[S-14] [2 M] [S-15] [4 M]
Ans: Refer 1.1 (List of features) Ans: Refer 1.9.(b)
33. Explain post increment-decrement
operator. [S-14] [4 M] 43. List and explain bitwise operator used in
Ans: Refer 1.7.(a)-ii (a-ii) and ii (b-ii) C. [W-15] [4 M]
Ans: Refer 1.7.(e)
34. State the constants and variables with
examples. [S-14] [4 M] 44. Explain special operator in C with
Ans: Refer 1.3(d) (Examples on the last line of example. [W-15] [4 M]
each i, ii and iii) and 1.3.(f)-i (Examples
Ans: Refer 1.7.(f)-i to iv.
on the last line of each a, b, c)
1. For the following program statement, derive the output generated by printf statement.
int val = 255;
float num = 79.54123;
printf("%d, %4d, %2.3f, %5.3", val, val, num, num); [S-11] [4 M]
Ans. The output of the given program is: 255, 255, 79.541, 79.541
32
Target Publications Pvt. Ltd. Basic PhysicsChapter
(F.Y.Dip.Sem.-1) of 'C'
01: BasicsMSBTE
4. Find out the errors in the following program component and state how it can be rectified.
main()
}
int a = 5, b = 5;
printf("%d", a, b)
} [W-13] [4 M]
Ans. The errors in the given program are mentioned below.
i. main()
}
The main function starts with a closing brace bracket, which will give syntax error.
ii. printf("%d", a, b)
The printf statement is not terminated with semicolon.
34