[go: up one dir, main page]

0% found this document useful (0 votes)
139 views189 pages

C Igate Bhilai

C programming notes

Uploaded by

rahulpal12897
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views189 pages

C Igate Bhilai

C programming notes

Uploaded by

rahulpal12897
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 189

C Programming

Study Material

C-Programming
for

Computer Science & Information Technology

By
Siddharth S. Shukla

Website: www.igate.guru
1
Page

1
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Sememster Classes:
1. C-Programming 3rd Semster
2. C++ with OOP’s 4th Semester
3. Java(Core+adv) 5th Semester
4. .Net(VB,C#,ASP)
5. Data Structure 4th Semster
6. Analysis and Design of Algorithm 5th Semester
7. Theory of Computation 5th Semester
8. Compiler Design 6th Semster
Gate Classes:
1. One Year Gate Class Room Program for 6th and 7th Semester
2. Two Year Gate Class Room Program for 5th Semester
Features:
1. Best Gate Result in C.G. in CS/IT
2. Study material
3. Question Bank(Subject wise)
4. Test Series(Subject Wise+Mock Test)
5. Cover all technical and non technical topics(Apti+Maths)

2
Page

2
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Copyright©,i-gate publication
First edition 2021

All right reserved

No part of this book or parts thereof may be reproduced, stored in a retrieval system or
transmitted in any language or by any means, electronic, mechanical, photocopying, recording or
otherwise without the prior written permission of the publisher.

3
Page

3
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Unit-I Elements of C Language


Origin of C, Features & Characteristic of C, C Compiler, Character Set, Keywords,
Identifiers, Constants, Variables, Input/ Output Statements, Basic Data Types, Operators
and Expressions, Tools for Problem Solving: Problem Analysis, Flowchart, Algorithm
Development. Top-Down Program Design, Structured Design Approach, Basic structure of
C programs, A simple C Program.

Question 1: Give brief description of Origin of C and History of C.

Answer:

C99

After the ANSI/ISO standardization process, the C language specification remained


relatively static for some time. In 1995 Normative Amendment 1 to the 1990 C standard
was published, to correct some details and to add more extensive support for international
character sets. The C standard was further revised in the late 1990s, leading to the
publication of ISO/IEC 9899:1999 in 1999, which is commonly referred to as "C99". It
has since been amended three times by Technical Corrigenda. The international C
standard is maintained by the working group ISO/IEC JTC1/SC22WG14.

C99 introduced several new features, including inline functions, several new data types
(including long long int and a complex type to represent complex numbers), variable-
length arrays, support for variadic macros (macros of variable arity) and support for one-
line comments beginning with //, as in BCPL or C++. Many of these had already been
implemented as extensions in several C compilers.

C99 is for the most part backward compatible with C90, but is stricter in some ways; in
particular, a declaration that lacks a type specifier no longer has int implicitly assumed. A
standard macro STDC VERSION is defined with value 199901L to indicate that C99
support is available. GCC, Sun Studio and other C compilers now support many or all of
the new features of C99.

Question 2: Write short notes on Features & Characteristic of C

Answer: Features of c language:-

 Programs written in C are efficient & fast.


 Its strength lies in built-in functions which can be used for developing programs.
 C is highly portable it means C programs written for one computer can be run on
4
Page

4
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

 another with little or no modification.


 It is well suited for structured programming. User think of a problem in terms of
 function modules or blocks. A proper collection of these modules would make a
 complete program. It makes program debugging, testing and maintenance easier.
 C has the ability to extend itself. We can add our own functions to C library.
 C compiler combines the capabilities of an assembly language with the features of
 high-level languages therefore it is well suited for writing both system software and
 business packages.

Characteristics of c language:-

 The C language also exhibits the following more specific characteristics:


 There are a small, fixed number of keywords, including a full set of flow of
control primitives: for, if, while, switch, and do..while. There is basically
one namespace, and user-defined names are not distinguished from keywords by any
kind of sigil.
 There are a large number of arithmetical and logical operators, such
as +, +=, ++, &, ~, etc.
 More than one assignment may be performed in a single statement.
 Function return values can be ignored when not needed.
 Typing is static, but weakly-enforced: all data has a type, but implicit conversions can
be performed; for instance, characters can be used as integers.
 Declaration syntax mimics usage context. C has no "define" keyword; instead, a
statement beginning with the name of a type is taken as a declaration. There is no
"function" keyword; instead, a function is indicated by the parentheses of an
argument list.
 User-defined (typedef) and compound types are possible.
 Heterogeneous aggregate data types (struct) allow related data elements to be
accessed, for example assigned, as a unit.
 Array indexing is a secondary notion, defined in terms of pointer arithmetic. Unlike
structs, arrays are not first-class objects; they cannot be assigned or compared using
single built-in operators. There is no "array" keyword, in use or definition; instead,
square brackets indicate arrays syntactically, e.g. month[11].
 Enumerated types are possible with the enum keyword. They are not tagged, and are
freely interconvertible with integers.
 Strings are not a separate data type, but are conventionally implemented as null-
terminated arrays of characters.
5
Page

5
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

 Low-level access to computer memory is possible by converting machine addresses


to typed pointers.
 Procedures (subroutines not returning values) are a special case of function, with a
dummy return type void.
 Functions may not be defined within the lexical scope of other functions.
 Function and data pointers permit ad hoc run-time polymorphism.
 A preprocessor performs macro definition, source code file inclusion, and conditional
compilation.
 There is a basic form of modularity: files can be compiled separately
and linked together, with control over which functions and data objects are visible to
other files via static and extern attributes.
 Complex functionality such as I/O, string manipulation, and mathematical functions
are consistently delegated to library routines.

Question 3: Write short notes on C Compiler .

Answer: C Compiler

 Visual C++ 2010 includes a C compiler that you can use to create everything from
basic C programs to Windows API applications.
 This walkthrough shows how to create a basic C program by using a text editor, and
then compile it on the command line.
 You can use your own C programs instead of typing the sample programs shown in
this walkthrough. You can also use any C code sample programs that are incle studied
in the Help topics.
 By default, the Visual C++ compiler treats all files that end in .c as C source code,
and all files that end in .cpp as C++ source code. To force the compiler to treat all
files as C regardless of file name extension, use the /Tc compiler option.
 Assuming that you are using a Turbo C or Turbo C++ compiler here are the steps that
you need to follow to compile and execute your first C program…
 start the compiler at C> prompt. The compiler (TC.EXEis usually present in
c:\TC\BIN directory).
 Select New from the File menu.

Type the program.

Save the program using F2 under a proper name 1.c)

User Ctrl+F9 to compile and execute the program.


6
Page

6
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Use Alt+F5 to view the output.

Question 4: Discuss the Character set & keywords in Brief.

Answer: character set:-

In C the characters that can be used to form words, numbers and expressions are grouped
into the following categories.

1.LETTERS :

Uppercase A……….Z

Lowercase a…………z

2. DIGITS :

All decimal digits 0………….9

3. SPECIAL CHARACTERS :

, comma \ backslash ~ tilde

& ampersand _ under score $ dollar sign

. Period % percent sign ^ caret

; semicolon * asterisk - minus sign

: colon + plus sign < opening angle bracket

? Question mark ( left parenthesis > closing angle bracket

‗ apostrophe ) right parenthesis [ left bracket

― quotation mark { left brace ] right bracket

! exclamation } right brace # number sign

| vertical bar / slash

4. WHITE SPACES :

Blank space, Horizontal tab, Carriage sign, New line, Form feed.
7
Page

7
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Keywords:

Keywords are the words whose meaning has already been defined by the C compiler. All
keywords have fixed meanings and these meanings cannot be changed. Keywords serve
as basic building blocks for program statements. All keywords must be written in
lowercase.

For Example:

Char, const, int, float, for, struct, union, switch, goto, while

Question 5: What is Identifiers? Write the rules of Definining identifier.

Answer:Identifiers refer to the names of the variables, functions and arrays.

These are the user-defined names and consist of sequence of letters and digits, with a
letter as a first character.

For Example: Sum, Total, count.

Rules for identifiers :-

 First character must be an alphabet or underscore.


 Must consist of only letters, digits or underscore.
 Only first 31 characters are significant.
 Must not contain white space.

Question 6. Define Constant. Explain their types.

Answer: Constant

The alphabets, numeric and special symbols when properly combined form constant &

variables.

Types of Constant:-

 Numeric constant
 Character constant

1.Numeric constants:-

(i) Integer constants:- An integer constant refers to a sequence of digits.


8
Page

8
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

There are three types of integer constants :

a .Decimal integer:

It consist of a set of digits 0 through 9, preceded by an optional – or + sign

123, -123, 0 65478

Embedded spaces commas and non digit characters are not permitted

Example : 20,000, 15 750 , $1000

b.Octal integer :

Combination 0f digits 0 through 7 with a leading 0.

For example: 037, 0, 0435,0551

c. Hexadecimal integer:

Sequence of digits preceded by 0x or 0X they may include

alphabets A through F or a through f.

For example : 0X2, 0x9F,0Xbcd

(ii).Real constants:- Numbers containing fractional parts are calledreal (floating point)

constants.

For example : 0.0083, -0.75, 435.36

a.Decimal Notation:

eg: 3.45

b. Scientific Notation :

eg. 29.9 E-45

2.Character constants:-

Single character constant:- Single character enclosed within a pair of single quotes.

For example : ‗5‘, ‗X‘


9
Page

9
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

String constant:- Sequence of characters enclosed within a pair of double quotes.

For example : ―hello‖, ―1234‖, ―well done‖, ―X‖

Pointer Constant:

These include the name of array without subscript and the name of a function without
parenthesis. A string literal actually is an instance of a constant pointer, since its value is
apointer to the string.

Question 7: Define Input/ Output Statements in brief.

Answer:

Unlike other high-level languages, C does not have any built-in input/output statements
as part of its syntax. All input/output operations are carried out through function calls
such as printf and scanf. There exist several functions that have more or less become
standard for input and output operations in C. Those functions are collectively known as
the standard I/O library.

Each program that uses a standard input/output function must contain the statement :

#include<stdio.h>

The file name stdio.h is an abbreviation for standard input output header file. This
instruction tells the compiler to search for a file named stdio.h and place its contents at
this point in program. The contents of the header file become part of the source code
when it is compile.

Question 8: Give details of Various Operators and Expressions used in C language.

Answer:

EXPRESSION

An expression is a combination of variables, constants and operators written according to


the syntax of the language.

In C every expression evaluates to a value i.e. every expression results in some value of a
certain type that can be assigned to a variable.

Variable=expression
10
Page

10
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

For example : x=a*b-c

z=a-b/c+d

Rules for evaluation of Expression

1.First, parenthesized sub expression from left to right are evaluated

2. If parentheses are nested, the evaluation begins with the innermost sub-expression.

3. The precedence rule is applied in determining the order of application of operators


in evaluating sub-expressions

4. The associativity rule is applied when two or more operators of the same precedence
level appear in a sub-expression.

5. Arithmetic expressions are evaluated from left to right using the rules of precedence.

6. When parentheses are used, the expressions within parentheses assume highest
priority.

Question 9: Define VariousTools for Problem Solving: Problem Analysis, Flowchart,


Algorithm Development

Answer: Problem Analysis:-

if we are to use the computer as a problem-solving tool, then we must have a good
analysis of the problem given. Here are some suggested steps on how to go about
analyzing a certain problem for computer application:

Review the problem carefully and understand what you are asked to do.

Determine what information is given(input) and what result must be produced(output).

Assign names to each input and output items.

Determine the manner of processing that must be done on the input data to come up with
the desired output(i.e., determine what formulas are needed to manipulate the given data).

Example

Given the scores for the two departmental quizzes, two machine projects, final exam, and
teacher's evaluation, write a program that will compute for the final grade based on the
11

following computation:
Page

11
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

FLOW CHART:-

Flow char is a graphical representation of algorithm. It is used to represent algorithm


steps into graphical format. It also gives us an idea to organize the sequence of steps or
events necessary to solve a problem with the computer. In other words flow charts are
symbolic diagrams of operations and the sequence, data flow, control flow and
processing logic in information processing. These charts are used to understand any
working sequence of the program.

Advantages of flowchart:-

It provides an easy way of communication because any other person besides the
programmer can understand the way they are represented.

It represents the data flow.

It provides a clear overview of the entire program and problem and solution.

It checks the accuracy in logic flow.

It documents the steps followed in an algorithm.

12
Page

12
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

It provides the facility for coding.

It provides the way of modification of running program.

They shows all major elements and their relationship.

Algorithm Development:-

As programming projects get more complicated, it becomes worthwhile to plan before


you start coding. Start by developing a plan, then implement and test it.

Problem-solving phase:

Analyze - understand and define the problem; ask questions, state assumptions.

Develop an algorithm -- determine the steps necessary to solve the problem; use scratch

paper.

Test - follow the exact steps to make sure that the algorithm works. Try specific cases
and see how your algorithm reacts.

Algorithm

A step-by-step procedure for solving a problem.

Implementation phase:

Write code - translate the algorithm into a programming language.

Test -have the computer follow the steps (execute the program).

Debug - check the results and make corrections until the answers are correct.

Quewer 10: Explain Basic structure of C programs.

Answer: A C program can be viewed as a group of building blocks called functions. A function
is a subroutine that may include one or more statements designed to perform a specific
task. To write a C program we first create functions and then put them together. A C
program may contain one or more sections :
13
Page

13
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Documentation Section

Link Section

Definition Section

Global Declaration Section

main ( ) function section

Declaration part

Executable part

} Subprogram Section

Documentation section :

It consist of a set of comment lines giving the name of the program, the author and other

details, which the programmer would like to use later.

Link section :

It provides instructions to the compiler to link functions from the system library.

Definition section : This section defines all symbolic constants.

Global Declaration Section : 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 all the functions. This section also declares all the user-
14

defined functions.
Page

14
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Main function section :

Every C program must have one main function section. This section contains two parts,
declaration part and executable part. The declaration part declares all the variables that
are used in executable part, these two parts must appear between the opening and closing
braces.

Subprogram section : This section contains all the user defined functions that are called in main

function section.

There may be situations where we want to change the order of execution of statements
based on certain decisions or repeat a group of statement until specified conditions are
met. This involves a kind of decision making to see whether a particular condition has
occurred or not and then direct the computer to execute certain statements accordingly.

Example 1 - C hello world program


/* A very simple c program printing a string on screen*/

#include <stdio.h>

main()

printf("Hello World\n");

return 0;

Output of above program: "Hello World"

Question 11: What is Top-Down Program Design??Define Structured Design Approach.

Answer: Top down Program Design :

Begins the design with main or top-level module, and progresses downwards to the
lowest level module.

Structured Design Approach:-

Structured programming is a programming paradigm aimed on improving the clarity,


15

quality, and development time of a computer program by making extensive use


Page

15
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

of subroutines, block structures and for and while loops - in contrast to using simple tests
and jumps such as the gotostatement which could lead to "spaghetti code" which is both
difficult to follow and to maintain.

Structured Programming Constructs

It uses only three constructs -

 Sequence (statements, blocks)


 Selection (if, switch)
 Iteration (loops like while and for)
Sequence

 Any valid expression terminated by a semicolon is a statement.


 Statements may be grouped together by surrounding them with a pair of curly braces.
 Such a group is syntactically equivalent to one statement and can be inserted where
ever
 One statement is legal.
Selection

The selection constructs allow us to follow different paths in different situations. We may
also think of them as enabling us to express decisions.

The main selection construct is:

if (expression)

statement1

else

statement2

statement1 is executed if and only if expression evaluates to some non-zero number. If


expression evaluates to 0, statement1 is not executed. In that case, statement2 is executed.

If and else are independent constructs, in that if can occur without else (but not the
reverse).Any else is paired with the most recent else-less if, unless curly braces enforce a
different scheme. Note that only curly braces, not parentheses, must be used to enforce
the pairing. Parentheses

Iteration
16
Page

16
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Looping is a way by which we can execute any some set of statements more than one
times continuously .In C there are mainly three types of loops are used :

 while Loop
 do while Loop
 For Loop

The control structures are easy to use because of the following reasons:

1) They are easy to recognize


2) They are simple to deal with as they have just one entry and one exit point
3) They are free of the complications of any particular programming language

Question 12: Explain Variable and also degine Local and Global Variable.

Answer: Variable
Variable is a name of memory location where we can store any data. It can store only
single data (Latest data) at a time. In C, a variable must be declared before it can be used.
Variables can be declared at the start of any block of code, but most are found at the start
of each function.

A declaration begins with the type, followed by the name of one or more variables. For

example,

DataType Name_of_Variable_Name;

int a,b,c;

Variable Names

Every variable has a name and a value. The name identifies the variable, the value stores
data. There is a limitation on what these names can be. Every variable name in C must
start with a letter; the rest of the name can consist of letters, numbers and underscore
characters. C recognizes upper and lower case characters as being different. Finally, you
cannot use any of C's keywords like main, while, switch etc as variable names.

Examples of legal variable names include:


17

x result outfile bestyet


Page

17
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

x1 x2 out_file best_yet
power impetus gamma hi_score

It is conventional to avoid the use of capital letters in variable names. These are used for
names of constants. Some old implementations of C only use the first 8 characters of a
variable name.

Local Variables

Local variables are declared within the body of a function, and can only be used within
that function only.

Syntex:

void main( ){

int a,b,c; }

void fun1() {

int x,y,z; }

Here a,b,c are the local variable of void main() function and it can‘t be used within fun1()
Function. And x, y and z are local variable of fun1().

Global Variable

A global variable declaration looks normal, but is located outside any of the program's
functions. This is usually done at the beginning of the program file, but after preprocessor
directives. The variable is not declared again in the body of the functions which access it.

Syntax:

#include<stdio.h>

int a,b,c;

void main()

}
18
Page

18
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

void fun1()

Here a,b,c are global variable .and these variable cab be accessed (used) within a whole
program.

Question 13: List out all the operators with their priority and associativity?

Answer: For operator refer Q.9 unit 1

Priority Associativity Operator Description


1 Left To Right () Function
[] Array
--> Pointer to member
. Structure
2 Right to left - Unary Minus
+ Unary Plus
++/-- Increment/Decrement
~
One‘s Complement
& Address of
(type) Type Casting
sizeof Size(inbytes)
! Logical Not
* Pointer Reference
3 Left To Right * Multiplication
/ Division
% Modulus
4 Left To Right + Addition
- Subtraction
5 Left To Right << Left Shift
>> Right Shift
19
Page

19
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

6 Left To Right < Less Than


<= Less Than Or Equal
> Greater Than
>= Greater Than Or Equal
7 Left To Right == Equlaity
!= Not Equal To
8 Left To Right & Bitwise AND
9 Left To Right ^ Bitwise X-OR
10 Left To Right | Bitwise OR
11 Left To Right && Logical AND
12 Left To Right || Logical OR
13 Left To Right ?: Conditional Operator
14 Right To Left = *= += Assignment

15 Left To Right , Comma

Progrom 1. Write a program to find the roots of a quadratic equation.

Solution:

/*---------------------------------------------------------------------------
PROGRAM TO FIND THE ROOTS OF A QUADRATIC EQUATION
---------------------------------------------------------------------------*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int A,B,C;
float disc,deno,x1,x2;
printf("\n-------------------------------------------------------");
printf("\n\n PROGRAM TO FIND THE ROOTS OF A QUADRATIC EQUATION ");
20

printf("\n\n-------------------------------------------------------");
Page

20
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

printf("\n\n\t ENTER THE VALUES OF A,B,C...");


scanf("%d,%d,%d",&A,&B,&C);
disc = (B*B)-(4*A*C);
deno = 2*A;
if(disc > 0)
{
printf("\n\t THE ROOTS ARE REAL ROOTS");
x1 = (-B/deno)+(sqrt(disc)/deno);
x2 = (-B/deno)-(sqrt(disc)/deno);
printf("\n\n\t THE ROOTS ARE...: %f and %f\n",x1,x2);
}
else if(disc == 0)
{
printf("\n\t THE ROOTS ARE REPEATED ROOTS");
x1 = -B/deno;
printf("\n\n\t THE ROOT IS...: %f\n",x1);
}
else
printf("\n\t THE ROOTS ARE IMAGINARY ROOTS\n");
printf("\n-------------------------------------------------------");
getch();
}

Progrom 2. Write a program to input distance between two cities in kilometers & convert it
into meters & centimeters, feet & inches.

Solution: #include<stdio.h>

int main()

{int km,meter,cm;

float inch,feet;

printf("Enter the distance between two cities in KM : ");

scanf("%d",&km);

//Conversion of km in meter
21

meter = km * 1000;
Page

21
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

printf("\nDistance between two cities is : %d meter",meter);

//converion of meter in centi meter

cm = meter * 100;

printf("\nDistance between two cities is : %d c.m.",cm);

//Conversion of c.m. in inch

inch = cm / 2.54f;

printf("\nDistance between two cities is : %f inch",inch);

//Conversion of inch in feet

feet = inch / 12;

printf("\nDistance between two cities is : %f feet",feet);

return 0;

Progrom 3: Wrote a program to Convert Celsius Into Farenheit.

Solution: #include<stdio.h>
#include<conio.h>
void main ()
{ float cel, faren;
clrscr ();
printf ("Enter The Temperature In Degree Celsius: ");
scanf ("%f", &cel);
faren=(9*cel/5)+32;
printf ("The Equivalent Temperature In Farenheit is : %f", faren);
getch ();
}

OUTPUT
Enter The Temperature In Degree Celsius: -40
The Equivalent Temperature In Fahrenheit is : -40
22

Progrom 4: Write a program to perform various arithmetic operations


Page

22
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Solution: #include<stdio.h>
#include<conio.h>
void main()
{ int a, b, sum, diff, pro, div;
clrscr ();
printf ("Enter The Two Numbers : ");
scanf ("%d%d", &a, &b);
sum=a+b;
diff=a-b;
pro=a*b;
div=a/b;
printf ("Sum=%d\n", sum);
printf ("Difference=%d\n", diff);
printf ("Product=%d\n", pro);
printf ("Quotient=%d\n", div);
getch ();
}

OUTPUT
Enter The Two Numbers : 15 23
Sum = 38
Difference = -8
Product = 345
Quotient = 0

Progrom 5: Write a program to swap two numbers without using third variable

Solution: #include<stdio.h>
#include<conio.h>
void main()
{ int a,b;
clrscr();
printf("Enter The Value Of a and b respectively : ");
scanf ("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("\nThe Values Of a and b After Swapping is %d & %d",a,b);
getch();
}
23

OUTPUT
Page

23
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Enter The Values Of a and b respectively : 15 65


The Values Of a and b After Swapping is 65 & 15

Progrom 6: Write a program to swap two numbers using third variable

Solution: #include<stdio.h>
#include<conio.h>
void main()
{ int a,b,c;
clrscr();
printf("Enter The Value Of a and b respectively : ");
scanf ("%d%d",&a,&b);
c=a+b;
b=c-b;
a=c-b;
printf("\nThe Values Of a and b After Swapping is %d %d",a,b);
getch();
}

OUTPUT
Enter The Values Of a and b respectively : 15 65
The Values Of a and b After Swapping is 65 & 15

Progrom 7: Write a program to print ASCII value of entered character

Solution: #include<stdio.h>
#include<conio.h>
void main()
{ char a;
clrscr();
printf("Enter The Character Whose ASCII value You Want To Print \t ");
a = getchar();
printf("\n The ASCII Value of Entered Character Is %d", a);
getch();
}

OUTPUT
Enter The Character Whose ASCII value You Want To
Print a

The ASCII Value of Entered Character Is 97


24
Page

24
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Progrom 8:Write a programto calculate the sum & percentage of marks obtained in 5 subject

Solution: #include<stdio.h>
#include<conio.h>
void main()
{ int a,b,c,d,e;
float sum, per;
clrscr();
printf("Enter The Marks In Five Subjects Respectively\n");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
sum = a+b+c+d+e;
per = (sum / 5);
printf("\n\nThe Sum Of The Marks Obtained Is \t%f",sum);
printf("\n\nThe Percentage is \t%f",per);
getch();
}

OUTPUT
Enter The Marks In Five Subjects Respectively
56 65 98 78 74
The Sum Of The Marks Obtained Is 371.000000
The Percentage Is 74.20000

Progrom 9: Write a prigrom to reverse four digit number.

Solution: #include<stdio.h>
#include<conio.h>
void main ()
{ int num, a, b, c, num1;
clrscr ();
printf ("Enter The Four Digit Number : ");
scanf ("%d", &num);
a=num%10;
num=num/10;
b=num%10;
num=num/10;
c=num%10;
num=num/10;
num1=a*1000+b*100+c*10+num;
printf ("The Reversed Four Digit Number Is : %d", num1);
getch ();
}
25

OUTPUT
Page

25
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Enter The Four Digit Number : 1256


The Reversed Four Digit Number Is: 6521

Program 10: Write a program to compare two numbers and print the largest number without

Using if .

Solution:

#include<stdio.h>
#include<conio.h>
void main ()
{ int a, b, c;
clrscr ();
printf ("Enter The Two Numbers Respectively: ");
scanf ("%d%d", &a, &b);
c=(a>b)?a:b;
printf ("The Largest Among The Two Number Is : %d", c);
getch ();
}
OUTPUT
Enter The Two Numbers Respectively : 15 98
The Largest Among Two Number Is : 98

26
Page

26
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Unit-2 Control Flow Construction


Decision making and branching: Simple if statement, if elsestatement, Nesting of if.. else
statement, else.. if Ladder, switch statement, ?: operator, goto statement. Decision making
and looping: while statement, do .. while statement, for statement, jumps in loops, break
and continue statement.

Question 1: Discuss Decision making If Statement and Swich case Statement with proper
syntax..

Answer:Various decision control statement are

 if statement
 switch statement

IF- Statement:

It is the basic form where the if statement evaluate a test condition and direct program
execution depending on the result of that evaluation.

Syntax:

If (Expression)

Statement 1;

Statement 2;

Where a statement may consist of a single statement, a compound statement or nothing as


an empty statement. The Expression also referred so as test condition must be enclosed in
parenthesis, which cause the expression to be evaluated first, if it evaluate to true (a non
zero value), then the statement associated with it will be executed otherwise ignored and
the control will pass to the next statement.

Example:

if (a>b)
27

{
Page

27
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

printf (―a is larger than b‖);

IF-ELSE Statement:

An if statement may also optionally contain a second statement, the ``else clause,'' which
is to be executed if the condition is not met. Here is an example:

if(n > 0)
average = sum / n;
else
{
printf("can't compute average\n");
average = 0;
}
NESTED-IF- Statement:

It's also possible to nest one if statement inside another. (For that matter, it's in general
possible to nest any kind of statement or control flow construct within another.) For
example, here is a little piece of code which decides roughly which quadrant of the
compass you're walking into, based on an x value which is positive if you're walking east,
and a y value which is positive if you're walking north:

if(x > 0)
{
if(y > 0)
printf("Northeast.\n");
else printf("Southeast.\n");
}
else {
if(y > 0)
printf("Northwest.\n");
else printf("Southwest.\n");
}

Switch Case

This is another form of the multi way decision. It is well structured, but can only be used
in certain cases where;

 Only one variable is tested, all branches must depend on the value of that variable.
28

The variable must be an integral type. (int, long, short or char).


Page

28
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

 Each possible value of the variable can control a single branch. A final, catch all,
default branch may optionally be used to trap all unspecified cases.

Hopefully an example will clarify things. This is a function which converts an integer
into a vague description. It is useful where we are only concerned in measuring a quantity
when it is quite small.

Syntax:

switch(expression)

case item1: statement1; break;

case item2: statement2; break;

case itemn: statementn; break;

default: statement;

In each case the value of itemi must be a constant, variables are not allowed.

29
Page

29
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

int number;
/* Estimate a number as none, one, two, several, many */
{ switch(number) {
case 0 :
printf("None\n");
break;
case 1 :
printf("One\n");
break;
case 2 :
printf("Two\n");
break;
case 3 :
case 4 :
case 5 :
printf("Several\n");
break;
default :
printf("Many\n");
break;
}
}

Question 2: Write menu driven program for calculator using Switch case.

Solution: #include<stdio.h>

#include<conio.h>

void main()

{char ch='y';

int a,b,c,choice;

clrscr();

printf("Enter two values:");

scanf("%d%d",&a,&b);
30

while(ch=='y')
Page

30
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

printf("Operations:\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division");

printf("\nEnter your choice:");

scanf("%d",&choice);

switch(choice)

ase 1: c=a+b;

printf("\nResult of addition is :%d",c);

break;

case 2: c=a-b;

printf("\nResult of subtraction is :%d",c);

break;

case 3 :c=a*b;

printf("\nResult of multiplication is :%d",c);

break;

case 4: c=(float)(a)/b;

printf("\nResult of division is :%d",c);

break;

default : printf("Invalid choice");

break;

printf("\nDo you want to continue(y/n):");

scanf("%c",&ch);
31
Page

31
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

getch();

OUTPUT :

Enter two values:2 5

Operations:

1.Addition

2.Subtraction

3.Multiplication

4.Division

Enter your choice:1

Result of addition is :7

Question 3: Explain ? : operator and goto statement.

Answer: ? : operator

The C language has an unusual operator, useful for making two-way decisions. This
operator is a combination of ? And : and takes three operands. This operator is popularly
known as conditional operator.The general form of conditional operator is :

conditional expression ? expression1 : expression2

The conditional expression is evaluated first. If the result is nonzero, expression1 is


evaluated and is returned as the value of the conditional expression. Otherwise,
expression2 is evaluated and its value is returned.

SALARY= 4x + 100 for x < 40

300 for x = 40

4.5x + 150 for x> 40


32

Can be written as :
Page

32
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Salary=(x<40) ? (4*x+100):(x>40)?(4.5x+150):300;

goto statement

C supports the goto statement to branch unconditionally from one point to another in the
program.

The goto requires a label in order to identify the place where the branch is to be made. A
label is any valid variable name, and must be followed by a colon. The label is placed
immediately before the statement where the control is to be transferred.

label:
goto label;

……………. statement;

……………. …………….

label: …………….

statement;
goto label;

The label can be anywhere in the program either before or after the goto label: statement.

If the label is before the statement goto label; a loop will be formed and some statement
will be executed repeatedly. Such a jump is known as a backward jump.

If the label: is placed after the goto label; some statements will be skipped and the jump
is known as a forward jump.

Question 4: Define Looping and various looping techniques with example.

Answer: Looping

In looping a sequence of statements are executed until some conditions for the
termination of the loop are satisfied. A program loop consist of two segments , one
known as body of loop and the other known as control statement .

The control statement test certain conditions and then directs the repeated execution of
the statements contained in the body of the loop.
33
Page

33
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

A looping process includes the following steps :

 Setting and initialization of a condition variable.


 Execution of the statements in the loop
 Test for a specified value of the condition variable for the
 Execution of the loop
 Increment or updating the condition variable
 The test may be either to determine whether the loop has been repeated the specified
number of times or to determine whether a particular condition has been met.
 Based on the nature of control variable and the kind of value assigned to it for testing
the loop may be classified as :

Counter-controlled loops or Definite repetition loop :

It is used when we know in advance exactly how many times the loop will be executed

Sentinel-controlled loops or Indefinite repetition loop :

In this type of loop a special value called a sentinel value is used to change the loop
expression from true to false

Loops

Looping is a way by which we can execute any some set of statements more than one
times continuously .In c there are mainly three types of loops are use :

 while Loop
 do while Loop
 For Loop

While Loop

Loops generally consist of two parts: one or more control expressions which (not
surprisingly) control the execution of the loop, and the body, which is the statement or set
of statements which is executed over and over.

The general syntax of a while loop is

while( expression )
{ Statement1
34

Statement2
Page

34
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Statement3
}

The most basic loop in C is the while loop. A while loop has one control expression, and
executes as long as that expression is true. This example repeatedly doubles the number 2
(2, 4, 8, 16, ...) and prints the resulting numbers as long as they are less than 1000:

int x = 2;

while(x < 1000)


{
printf("%d\n", x);
x = x * 2;
}
(Once again, we've used braces {} to enclose the group of statements which are to be
executed together as the body of the loop.)

For Loop

Our second loop, which we've seen at least one example of already, is the for loop. The
general syntax of a while loop is

for( Initialization;expression;Increments/decrements )

{
Statement1
Statement2
Statement3

The first one we saw was:

for (i = 0; i < 10; i = i + 1)


printf ("i is %d\n", i);
(Here we see that the for loop has three control expressions. As always, the statement
can be a brace-enclosed block.)

Do while Loop

This is very similar to the while loop except that the test occurs at the end of the loop
35

body. This guarantees that the loop is executed at least once before continuing. Such a
Page

35
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

setup is frequently used where data is to be read. The test then verifies the data, and loops
back to read again if it was unacceptable.

do
{
printf("Enter 1 for yes, 0 for no :");
scanf("%d", &input_value);
} while (input_value != 1 && input_value != 0)

Question 5: Write a program to print the following pattern

12345

1234

123

12

Answer: #include<stdio.h>

main()

int n, c, k, space;

scanf("%d", &n);

space = 0;

for ( k = n ; k >= 1 ; k-- )

for ( c = 1 ; c <= space ; c++ )

printf(" ");

space++;
36

for ( c = 1 ; c <= k ; c++)


Page

36
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

printf("%d", c);

printf("\n");

return 0;

Question 6: Define Break and Continue in Detail.

Answer: The break Statement

We have already met break in the discussion of the switch statement. It is used to exit
from a loop or a switch, control passing to the first statement beyond the loop or a switch.

With loops, break can be used to force an early exit from the loop, or to implement a loop
with a test to exit in the middle of the loop body. A break within a loop should always be
protected within an if statement which provides the test to control the exit condition.

The continue Statement

This is similar to break but is encountered less frequently. It only works within loops
where its effect is to force an immediate jump to the loop control statement.

 In a while loop, jump to the test statement.


 In a do while loop, jump to the test statement.
 In a for loop, jump to the test, and perform the iteration.

Like a break, continue should be protected by an if statement. You are unlikely to use it

very often.

Take the following example:

int i;
for (i=0;i<10;i++)
{

if (i==5)
37

continue;
Page

37
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

printf("%d",i);
if (i==8)
break;
}

This code will print 1 to 8 except 5.

Continue means, whatever code that follows the continue statement WITHIN the loop
code block will not be exectued and the program will go to the next iteration, in this
case, when the program reaches i=5 it checks the condition in the if statement and
executes 'continue', everything after continue, which are the printf statement, the next
if statement, will not be executed.

Break statement will just stop execution of the look and go to the next statement after
the loop if any. In this case when i=8 the program will jump out of the loop. Meaning,
it wont continue till i=9, 10.

Question 7:Write difference among

(i) break, continue & goto

Answer:

The break statement will immediately jump to the end of the current block of code.

The continue statement will skip the rest of the code in the current loop block and will
return to the evaluation part of the loop. In a do or while loop, the condition will be tested
and the loop will keep executing or exit as necessary. In a for loop, the counting
expression (rightmost part of the for loop declaration) will be evaluated and then the
condition will be tested.

Goto allows to make an absolute jump to another point in the program. You should use
this feature with caution since its execution causes an unconditional jump ignoring any
type of nesting limitations.

The destination point is identified by a label, which is then used as an argument for the
goto statement. A label is made of a valid identifier followed by a colon (:).
38

(ii) while & do while


Page

38
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Answer:

The difference between "do while" and "do until" is that a "do while" loops while the test
case is true, whereas "do until" loops UNTIL the test case is true (which is equivalent to
looping while the test case is false).

The difference between a "do ...while" loop and a "while {} " loop is that the while loop
tests its condition before execution of the contents of the loop begins; the "do" loop tests
its condition after it's been executed at least once. As noted above, if the test condition is
false as the while loop is entered the block of code is never executed. Since the condition
is tested at the bottom of a do loop, its block of code is always executed at least once.

Program 1: Write a program to take a number as input and calculate the sum of its digits.

Solution: #include<conio.h>

#include<stdio.h>

void main()

int n,a,sum=0;

clrscr();

printf("\nEnter a Number : ");

scanf("%d",&n);

while(n>0)

a=n%10 ;

sum=sum+a;

n=n/10;

printf("\nsum of the digits of given number is : %d",sum);


39
Page

39
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

getch( );

OUTPUT :

Enter a Number : 1256

sum of the digits of given number is : 14

Program 2: Write a program to find smallest and largest among four numbers.

Solution: #include<stdio.h>
#include<conio.h>
void main ()
{
int a, b, c, d;
clrscr ();
printf ("Enter The Four Numbers Respectively : ");
scanf ("%d %d %d %d", &a, &b,&c, &d);
if (a>b && a>c && a>d)
printf ("%d Is The Largest Number\n", a);
else if (b>a && b>c && b>d)
printf ("%d Is The Largest Number\n", b);
else if (c>a && b<c && c>d)
printf ("%d Is The Largest Number\n", c);
else
printf ("%d Is The Largest Number\n", d);
if (a<b && a<c && a<d)
printf ("%d Is The Smallest Number\n", a);
else if (a>b && b<c && b<d)
printf ("%d Is The Smallest Number\n", b);
else if (c<b && a<c && c<d)
printf ("%d Is The Smallest Number\n", c);
else
printf ("%d Is The Smallest Number\n", d);
getch ();
}
OUTPUT
Enter The Four Numbers Respectively : 12 52 65 78
78 Is The Largest Number
12 Is The Smallest Number
40
Page

40
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Program 3: WAP To Calculate Gross Salary of MR based on Sales.

Solution: #include<stdio.h>
#include<conio.h>
void main ()
{
float bs, hra, da, ta, inc, bonus, gs, sales;
clrscr ();
printf ("Enter The Sales Earned : ");
scanf ("%f", &sales);
if (sales < 100000)
{
bs = 3000;
hra = 0.2 * 3000;
da = 1.10 * 3000;
ta = 500;
inc=0.1*sales;
bonus=1000;
}
else
{
bs = 3000;
hra = 0.2 * 3000;
da = 1.10 * 3000;
ta = 500;
inc=0.1*sales;
bonus=2000;
}
gs=bs+hra+da+ta+inc+bonus;
printf ("The Gross Salary of MR is : %f \n", gs);
getch ();
}

OUTPUT

Enter The Sales Earned : 110000, The Gross Salary Of MR Is : 20400

Program 4: Write a program to convert year in to months, days, hours, minutes and seconds.

Solution: #include<stdio.h>
#include<conio.h>
void main ()
41

{
Page

41
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

int year, choice;


clrscr ();
printf ("Enter the Number of Years : ");
scanf ("%d", &year);
printf ("1.Months\n 2.Days\n 3.Hours\n 4.Minutes\n 5.Seconds\n");
printf ("Enter Your Choice For Conversion : ");
scanf ("%d", &choice);
switch (choice)
{
case 1:printf ("No. of Months Are : %d\n", year*12);
break;
case 2:printf ("No. of Days Are : %lf\n", year*365.0);
break;
case 3:printf ("Total No. of Hours Are : %lf\n", year*365.0*24.0);
break;
case 4:printf ("Total No. of Minutes Are : %lf\n", year*365.0*24.0*60.0);
break;
case 5:printf ("Total No. of Seconds Are : %lf\n", year*365.0*24.0*3600.0);
break;
default:printf("Invalid Choice\n");
}
getch ();
}
OUTPUT

Enter the Number of Years : 5


1.Months

2.Days

3.Hours

4.Minutes

5.Seconds

Enter Your Choice For Conversion : 1


No. of Months Are : 60

Program 5: Wap to find all pythagorian triplet for all odd number in range 1 to10.

Solution: #include<stdio.h>
#include<conio.h>
42
Page

42
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

#include<math.h>
void main()
{
int i,x;
clrscr();
for(i=1;i<10;i=i+2)
{
x=(pow(i,2)-1)/2;
printf("\nThe Pythagorian triplet for %d are \t%d %d %d", i, i, x, x+1);
}
getch();
}
OUTPUT
The Pythagorean Triplet for 1 are 1 0 1
The Pythagorean Triplet for 3 are 3 4 5
The Pythagorean Triplet for 5 are 5 12 13
The Pythagorean Triplet for 7 are 7 24 25
The Pythagorean Triplet for 9 are 9 40 41

Program 6: Write a program to evaluate the following series


1+X+X2+X3……..Xn
Solution: #include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
double x, n, s=0;
int i;
clrscr();
printf("Enter The Value Of x: ");
scanf("%lf",&x);
printf("Enter The Value Of n: ");
scanf("%lf",&n);
for(i=0;i<=n;i++)
s=s+pow(x,i);
printf("The Sum Of The Series Is : %lf",s);
getch();
}
OUTPUT
Enter The Value Of x : 5
Enter The Value Of n : 6
The Sum Of The Series Is : 19531.000000
43
Page

43
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Program 7: Write a program to evaluate the following series


1+2+3+…….+n
Solution: #include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long int i, n, s=0;
clrscr();
printf("Enter The Value Of n :");
scanf("%ld",&n);
for(i=1;i<=n;i++)
s=s+i;
printf("The Sum Of The Series Is : %ld",s);
getch();
}
OUTPUT
Enter The Value Of n : 6
The Sum Of The Series Is : 21

Program 8: Write a program to evaluate the following series


1+2/2!+3/3!+…….+n/n!

Solution: #include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
double i, n, s=0, j, fact=1;
clrscr();
printf("Enter The Value Of n : ");
scanf("%lf",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
fact=fact*j;
s=s+(i/fact);
}
printf("The Sum Of The Series Is : %lf",s);
getch();}
OUTPUT
Enter The Value Of n : 5
44

The Sum Of The Series Is : 2.708333


Page

44
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Unit-3 Defining and Manipulating Arrays


Dimensional Arrays: Declaration of Arrays, Initialization of Arrays, Reading and Writing
of integer, real and character arrays, Sorting and Searching in Arrays, Multi-Dimensional
Arrays, Handling of Character Strings.

Question 1: what is Arrays ? Define types of Array.

Answer: One dimensional arrays

Array:

An array is a fixed sized sequenced collection of related data items of same data type.

In its simplest form an array can be used to represent a list of numbers or a list of names
using a single name.

We can use an array name salary to represent a set of salaries of a group employees in an
organization. We can refer to the individual salaries by writing a number called index or
subscript in brackets after the array name. for example salary[10] represents the salary of
10th employee.

While the complete set of values referred to as an array, individual values are called
elements.

C supports the following types of arrays :-

 One-dimensional arrays
 Two-dimensional arrays
 multidimensional arrays

One-dimensional arrays :-

A list of items can be given one variable name using only one subscript and such a
variable is called a single-subscripted variable or one-dimensional array.

Declaration of one-dimensional arrays:

Like any other variable, arrays must be declared before they are used.

The general form of array declaration is :


45

type variable_name[size];
Page

45
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

type specifies the type of elements of array, such as int, float or char and the size
indicates the maximum number of elements that can be stored inside the array.

For example :

float height[10];

declares the height to be an array containing 10 real elements.

Multidimensional arrays

Two-dimensional arrays

One-diemensional array can store a list of values. There could be situations where a table
of values will have to be stored.

We can think of a table as a matrix consist of rows and columns.

C allows to define such table of items by using two-dimensional array such as v[3][4].

The general form of declaration of a two-dimensional array is :

type array_name[row_size][column_size];

Each dimension of array is indexed from zero to its maximum size minus one, the first
index selects the row and the second index selects the column within that row.

e.g. int a[3][4];

46
Page

46
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Multi-dimensional array

C allows arrays of three or more dimensions. The exact limit is determined by the
complier.

The general form of multi-dimensional array is :

type array_name[s1][s2][s3[]……….[sm];

Where si is the size of ith dimension .

For example :

int a[3][4][12];

float table[5][4][5][3];

survey is a three-dimensional array, whereas table is a four-dimensional array.

Question 2: Write a program to find the sum of the elements of an array.

Solution: #include<stdio.h>

#include<conio.h>

void main( )

int a[10],sum=0,i;

printf(―Enter the number of elements in array:‖);

scanf(―%d‖,&n);

printf(―\nEnter the elements of array :\n‖);

for(i=0;i<n;i++)

scanf(―%d‖,&a[i]);

for(i=0;i<n;i++)

sum=sum+a[i];
47
Page

47
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

printf(―Sum of the elements of array is :%d‖,sum);

getch();

OUTPUT :

Enter the number of elements in array:5

Enter the elements of array :

1 2 3 4 5 Sum of the elements of array is :15

Question 3: What is CHARACTER ARRAYS or STRINGS iilustrate with example.

Answer: Strings

A string is sequence of characters that is treated as a single data item. Any group of
characters defined between double quotation marks is a string constant.

Declaring strings :

C does not support strings as a data type. However, it allows us to represent strings as
character arrays. In C therefore, a string variable is any valid C vaiable name and is
always declared as an array of characters.

char string_name[size];

The size determines the number of characters in string.

For example :

char city[10];

when compiler assigns a character string to a character array, it automatically supplies a


null character (‗\0‘) at the end of the string. Therefore, the size should be equal to the
maximum number of characters in the string plus one.

Consider the following program,

#include <stdio.h>

main()
48
Page

48
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

static char name1[] = {'H','e','l','l','o'};

static char name2[] = "Hello";

printf("%s\n", name1);

printf("%s\n", name2);

Sample Program Output

Helloxghifghjkloqw30-=kl`'

Hello

The difference between the two arrays is that name2 has a null placed at the end of the
string, ie, in name2[5], whilst name1 has not. This can often result in garbage characters
being printed on the end. To insert a null at the end of the name1 array, the initialization
can be changed to,

static char name1[] = {'H','e','l','l','o','\0'};

Reading strings from terminal :

Using scanf function :

The familiar input function scanf can be used with %s format specification to read in a
string of characters.

For example :

char address[15];

scanf(―%s‖,address);

the problem with the scanf function is that it terminates its input on the first white space it
finds.

Reading with getchar and gets functions :


49
Page

49
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

A single character can be read from the keyboard using getchar. We can use this function
repeatedly to read successive single characters from the input and place them into a
character array. The reading is terminated when newline character (‗\n‘) is entered and
the null character is then inserted at the end of the string.

The getchar function call takes the form :

char ch;

ch=getchar( );

Another and more convenient method of reading a string of text containing whitespaces
is to use the library function gets available in the <stdio.h> header file. This is simple
function with one string parameter and called as under:

gets(str);

str is a string variable declared properly. It reads characters into str from the keyboard
until a newline character is encountered and then appends a null character to the string.
Unlike scanf, it does not skip whitespaces.

Writing strings to screen :

Using printf function :

printf function with %s format specifier can be used to print strings to the screen. The
format %s can be used to display an array of characters that is terminated by the null
character.

For example :

char name[10];

printf(―%s‖,name);

can be used to display the entire contents of the array.

Question 4: Write a program to read and display a line of text using getchar and putchar

Answer:#include <stdio.h>

#include <conio.h>
50

void main( )
Page

50
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

char str[ 30 ], c;

int i = 0;

printf( "Enter a string:" );

while( ( c = getchar( ) ) != '\n' )

{ str[ i ] = c;

i++; }

str[ i ] = '\0';

printf(―\nThe string is :‖);

for(i = 0; str[ i ] != '\0'; i++)

putchar( str [ i ] );

getch( ); }

OUTPUT :

Enter a string:this is a character array

The string is :this is a character array

Question 5: Write a program to copy one string into another without using any string

functions.

Answer:#include<stdio.h>

#include<conio.h>

void main( )

{char s1[15],s2[15];

int i;

clrscr();
51
Page

51
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

printf("Enter a string :");

gets(s1);

for(i=0;s1[i]!='\0';i++)

s2[i]=s1[i];

s2[i]='\0';

printf("Copy of given string :%s",s2);

getch();

OUTPUT :

Enter a string :C Programming

Copy of given string : C Programming

Question 6: Explain Arithmetic Operation on Strings and String handling and

manipulation functions.

Answer: Arithmetic Operation on Strings

C allows us to manipulate characters the same way we do with numbers.

Whenever a character constant or character variable is used in an expression, it is


automatically converted into integer value by the system.

To write a character in its integer representation, we may write it as an integer.

For example :

char x=‗a‘;

printf(―%d‖,x);

Will display 97 ASCII value of a on the screen.

It is also possible to perform arithmetic operations on the characters and variables


52
Page

52
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

For example :

int x;

x=‗z‘-1;

is a valid statement. The ASCII value of z is 122 and therefore the statement will assign
121 to x.

String-Handling Functions:

C library supports a large number of string-handling functions that can be used to carry
out many of the string manipulations.

Most commonly used string functions are :

strcat( ) – concatenates two strings

strcmp( ) – compares two strings

strcpy – copies one string into another

strlen – finds the length of a string

These functions are defined in string.h header file.

Strcat() function

The strcat function joins two strings together.

It takes the following form :

strcat(string1,string2);

Where string1 & string two are character arrays.

When the strcat function is excuted, string two is appended to string1.

It does so by removing the null character at the end of string1 and placing string2 from
there.

The string at string2 remains unchanged.

Size of string1 should be enough to accommodate the final string.


53
Page

53
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

strcat function may also append a string constant to a string variable.

strcmp() function

The strcmp compares two functions identified by the arguments and has a value of 0 if
they are equal.

If they are not, it has numeric difference between the first nonmatching characters in the
strings.

It takes the form :

strcmp(string1,string2);

string1 and string2 may be string variables or string constants.

strcpy() function

strcpy copies one string into another.

It takes the form :

strcpy(string1,string2);

It assigns the contents of string2 to string1.

string2 may be a character array variable or a string constant.

Strlen() function

This function counts and returns the number of characters in a string.

It takes the form :

n=strlen(string);

Where n is an integer variable, which receives the value of the length of the string.

The argument may be a string constant.

The counting ends at the first null character.

Other string handling functions


54

strncpy(): it copies the left-most n characters of the sourse string to the target string variable.
Page

54
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

This is a three parameter function & has the following form :

strncpy(string1,string2,n);

strncmp – this fuction has three parameter and has the following form :

strncmp(string1,string2,n);

This compares the left-most n characters of string1 to string2

and returns :

O if they are equal.

Negative number, if string1 is less than string2.

Positive number, otherwise.

strncat(): This function concatenates the left-most n characters of second string to the end of

first string.

It takes the following form :

strncat(string1,string2,n);

strstr(): It is a two-parameter function that can be used to locate a sub-string in a string.

It takes the form :

strstr(string1,string2);

The function strstr searches the string1 to see whether the string2 is contained in string1.

if yes function returns the position of the first occurrence of the sub-string. Otherwise, it
returns a NULL pointer.

Question 7: Write a C program to check whether the string is palindrome or not?

Answer: #include<stdio.h>

main()
{
int i=0,n[5],s=0,m[5],r=0;
55
Page

55
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

for(i=0; i<5; i++)


{
printf("Enter Any number:");
scanf("%d",&n[i]);
m[i]=n[i];
}
for(i=0; i<5; i++)
{
while(n[i]!=0)
{
r=n[i]%10;
s=(s*10)+r;

n[i]=n[i]/10;
}
if(m[i]==s)
{
printf("\nPalindrome :");
}
else
{
printf("\nNot palindrome :");
}
printf("%d",s);
s=0,r=0;
}

getch();

Question 8: Write a program in c language to find greatest and smallest element in one

dimensional array.

Answer:

#include<stdio.h>
56

int main()
Page

56
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

int a[]= {4,3,6,7};

int l.n = 0;

int s.n = 9;

int i=0;

while(a[i] != '\0')

if(a[i]>= l.n)

l.n = a[i];

else if(a[i]<=s.n)

s.n = a[i]

i++;

printf("%d %d",s.n,l.n) ;

return(0);

Question 9: WAP to find largest number and smallest number from 10 integer number in

array.

Answer:#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,b,s;
clrscr();
for(i=0;i<10;i++)
{
57

printf("Enter %d Element In An Array: ",i+1);


Page

57
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

scanf("%d",&a[i]);
}
b=a[0];
s=a[0];
for(i=1;i<10;i++)
{
if(a[i]>b)
b=a[i];
if(a[i]<s)
s=a[i];
}
printf("\nLargest Value In An Array Is : %d\nSmallest Value In An Array Is : %d",b,s);
getch();
}
OUTPUT
Enter 1 Element In An Array: 15 Enter 2 Element In An Array: 36
Enter 3 Element In An Array: 95 Enter 4 Element In An Array: 40
Enter 5 Element In An Array: 12 Enter 6 Element In An Array: 85
Enter 7 Element In An Array: 74 Enter 8 Element In An Array: 65
Enter 9 Element In An Array: 92 Enter 10 Element In An Array: 57

Largest Value In An Array Is : 95


Smallest Value In An Array Is : 12

Question 10: Write a program to enter five number using array and rearrange it in reverse
order.

Solution: #include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
for(i=0;i<5;i++)
{
printf("Enter %d Element : ",i+1);
scanf("%d",&a[i]);
}
printf("The Elements In Reverse Order Is: ");
for(i=4;i>=0;i--)
printf("%d, ",a[i]);
getch();
58

}
Page

58
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

OUTPUT
Enter 1 Element : 12
Enter 2 Element : 35
Enter 3 Element : 62
Enter 4 Element : 45
Enter 5 Element : 54
The Elements In Reverse Order Is: 54, 45, 62, 35, 12,

Question 11: Write a program to transpose a matrix of 4*4 matrix.

Answer: #include<stdio.h>
#include<conio.h>
void main()
{
int matrix[4][4],i,j,temp;
clrscr();
printf("Enter Elements of the 4*4 Matrix:");
for(i=0;i<4;i++)
for(j=0;j<4;j++)
scanf("%d",&matrix[i][j]);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(j<i)
{
temp=matrix[i][j];
matrix[i][j]=matrix[j][i];
matrix[j][i]=temp;
}
printf("\nTransposed Matrix :\n\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf("%d ",matrix[i][j]);
printf("\n");
}
getch();
}

OUTPUT
Enter Elements of the 4*4 Matrix:15 65 87 98 87 15 32 96 74 85 50 41 65 32
145 65 98
59

Transposed Matrix :
Page

59
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

15 87 74 65
65 15 85 32
87 32 50 145
98 96 41 65

Question 12: Write a prigram to add, substarct, multiply the two 3*3 matrix.

Answer: #include<stdio.h>
#include<conio.h>
void main()
{
int matrix1[3][3],matrix2[3][3],multi[3][3],i,j,k;
clrscr();
printf("Enter Elements of The First Matrix :");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&matrix1[i][j]);
printf("Enter Elements of The Second Matrix :");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&matrix2[i][j]);
printf("\n\nMatrix Formed By Addition :\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d ",matrix1[i][j]+matrix2[i][j]);
printf("\n");
}
printf("\n\nMatrix Formed By Substraction :\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d ",matrix1[i][j]-matrix2[i][j]);
printf("\n");
}
printf("\n\nMatrix Formed By Multiplication :\n\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
multi[i][j]=0;
for(k=0;k<3;k++)
60

multi[i][j]+=matrix1[i][k]*matrix2[k][j]; }
Page

60
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d ",multi[i][j]);
printf("\n");
}
getch();
}
OUTPUT
Enter Elements of The First Matrix :15 15 32 9875 51 25 32 32
12 32
Enter Elements of The Second Matrix :15 47 89 65 45 12 32 21 10 25 35

Matrix Formed By Addition :

47 47 47
9922 140 90
77 44 44

Matrix Formed By Substraction :

-17 -17 17
9828 -38 -40
-13 20 -20

Matrix Formed By Multiplication :

2625 2199 2224


-8158 -6841 21168
3068 4016 2944

Question 13: Write a program to perform following sorting method in array with five element
by using
(a) Selection sort
(b) Bubble sort

Answer: Selection Sort:


#include<stdio.h>
#include<conio.h>
void main()
{
int array[5],i,j,temp;
61

clrscr();
Page

61
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

printf("Enter Elements of the Array:");


for(i=0;i<5;i++)
scanf("%d",&array[i]);
for(i=0;i<5;i++)
for(j=i;j<5;j++)
if(array[i]>array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
printf("\nSorted Array Is : ");
for(i=0;i<5;i++)
printf("%d ",array[i]);
getch();
}
OUTPUT
Enter Elements of the Array:45 65 98 78 10

Sorted Array Is : 10 45 65 78 98

Bubble Sort:
#include<stdio.h>
#include<conio.h>
void main()
{
int array[5],i,j,temp;
clrscr();
printf("Enter Elements of the Array:");
for(i=0;i<5;i++)
scanf("%d",&array[i]);
for(i=0;i<5;i++)
for(j=0;j<5-i-1;j++)
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
printf("\nSorted Array Is : ");
for(i=0;i<5;i++)
printf("%d ",array[i]);
62

getch(); }
Page

62
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

OUTPUT
Enter Elements of the Array:45 65 98 78 10

Sorted Array Is : 10 45 65 78 98

Question 14: Write a program to perform following searching method in array with 5 element
Linear search & binary search.

Answer: linear search

#include<stdio.h>
#include<conio.h>
void main()
{
int array[5],i,j,element;
clrscr();
printf("Enter Elements of the Array :");
for(i=0;i<5;i++)
scanf("%d",&array[i]);
printf("Enter Element to search: ");
scanf("%d",&element);
for(i=0;i<5;i++)
if(array[i]==element)
{
printf("\nElement found at Array index %d ",i);
break;
}
if(i==5)
printf("\nElement not found");
getch();
}
OUTPUT
Enter Elements of the Array :5 9 8 2 1
Enter Element to search: 1

Element found at array index 4

Answer: binary search


include<stdio.h>
#include<conio.h>
void main()
{
63

int x[5],y[5],s,m=0,e=4,b=0,i,mid,j,t;
Page

63
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

clrscr();
for(i=0;i<5;i++)
{
printf("Enter value %d:",i+1);
scanf("%d",&y[i]);
x[i]=y[i];
}
for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
if(x[j]>x[j+1])
{
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
}
}
printf("\nEnter number to search:");
scanf("%d",&s);
while(b<5)
{
mid=(b+e)/2;
if(x[mid]<s)
b=mid+1;
else if(x[mid]>s)
e=mid-1;
else
{
m=1;
break;
}
}
for(i=0;i<5;i++)
{if(x[mid]==y[i])
break; }
if(m==1)
printf("\nIt exists in position %d",i+1);
else
printf("\nIt does not exists");
getch();
}
OUTPUT
64
Page

64
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Enter value 1:20


Enter value 2:25
Enter value 3:26
Enter value 4:29
Enter value 5:30

Enter number to search:30

It exists in position 5

Question 15: Write a program to check string is palindrome or not using loop.

Answer: #include<stdio.h>
#include<conio.h>
void main()
{
char a[100],b[100];
int i=0,k=0,j=0,len=0;
clrscr();
printf("Enter The String : ");
scanf("%[^\n]s",a);
while(a[i]!='\0')
{
len++;
i++;
}
for(k=i-1;k>=0;k--)
{
b[i-(k+1)]=a[k];
}
i=0;
while(a[i]!='\0')
{
if(a[i]==b[i])
j++;
i++;
}
if(len==j)
printf("Palindrome\n");
else
printf("Not Palindrome\n");
getch();
65

}
Page

65
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

OUTPUT
Enter The String : RADAR
Palindrome

Question 16: Write a program to compare two string. (ignore case) using loop.

Answer: #include<stdio.h>
#include<conio.h>
void main()
{
char a[25],b[25];
int i=0,j=0,len=0,len1=0,k=0;
clrscr();
printf("Enter The First String: ");
gets(a);
printf("Enter The Second String: ");
gets(b);
while(a[i]!='\0')
{
len++;
i++;
}
i=0;
while(b[i]!='\0')
{
len1++;
i++;
}
if(len==len1)
{
while(a[j]!='\0')
{
if(a[j]==b[j]||a[j]==b[j]+32||a[j]==b[j]-32)
k++;
j++;
}
if(k==len)
printf("Both Strings Are Same\n");
}
else
printf("\nBoth Strings Are Not Same");
getch();
66

}
Page

66
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

OUTPUT

Enter The First String: i-GATEMentor


Enter The Second String: i-gatementor
Both Strings Are Same

Question 17: Write a program to compare two string.(don’t ignore case)

Answer: #include<stdio.h>
#include<conio.h>
void main()
{
char a[25],b[25];
int i=0,j=0,len=0,len1=0,k=0;
clrscr();
printf("Enter The First String: ");
gets(a);
printf("Enter The Second String: ");
gets(b);
while(a[i]!='\0')
{
len++;
i++;
}
i=0;
while(b[i]!='\0')
{
len1++;
i++;
}
if(len==len1)
{
while(a[j]!='\0')
{
if(a[j]==b[j]&&(a[j]==b[j]+32||a[j]==b[j]-32))
k++;
j++;
}
if(k==len)
printf("Both Strings Are Same\n");
else
printf("Both Strings Are Not Same\n");
67

}
Page

67
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

else
printf("\nBoth Strings Are Not Same");
getch(); }

OUTPUT
Enter The First String: i-GATE
Enter The Second String: I-GATE
Both Strings Are Not Same

Question 18: Write a program that will read a line and delete all vowel from sentence.Assume
that sentences is not more than 80 char long.

Answer: #include<stdio.h>
#include<conio.h>
void main()
{
char a[100];
int i=0,len=0;
clrscr();
printf("Enter The String: ");
gets(a);
while(a[i]!='\0')
{
len++;
i++;
}
printf("The String After Deletion Of Vowel: ");
i=0;
while(i<=len)
{
if(a[i]!='a')
if(a[i]!='e')
if(a[i]!='i')
if(a[i]!='o')
if(a[i]!='u')
if(a[i]!='A')
if(a[i]!='E')
if(a[i]!='I')
if(a[i]!='O')
if(a[i]!='U')
printf("%c",a[i]);
i++;
68

}
Page

68
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

getch();
}

OUTPUT
Enter The String: PRESENTLY WE ARE DOING THE C LABWORK
The String After Deletion Of Vowel: PRSNTLY W R DNG TH C LBWRK

Question 19: Write a program to count frequency of all character in input string.

Answer: #include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string[100], ch;
int c = 0, count[26] = {0};
clrscr();

printf("Enter a string\n");
gets(string);

while ( string[c] != '\0' )


{
if ( string[c] >= 'a' && string[c] <= 'z' )
count[string[c]-'a']++;

c++;
}

for ( c = 0 ; c < 26 ; c++ )


{
if( count[c] != 0 )
printf("%c occurs %d times in the entered string.\n",c+'a',count[c]);
}

getch();
}

OUTPUT
Enter a string :=> It Is My First Practical Class On C Lab
a occurs 4 times in the entered string.
b occurs 1 times in the entered string.
69

c occurs 2 times in the entered string.


Page

69
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

i occurs 2 times in the entered string.


l occurs 2 times in the entered string.
n occurs 1 times in the entered string.
r occurs 2 times in the entered string.
s occurs 4 times in the entered string.
t occurs 3 times in the entered string.
y occurs 1 times in the entered string.

Question 20: Write a program to count no. of vowels , consonant , blank in input string.

Answer: #include <stdio.h>


#include <string.h>
#include<conio.h>
void main()
{
char a[100];
int VowelCount=0, ConsonantCount=0;
int whitespace=0, digit;
int i;
clrscr();
printf( "Enter a String :=> ");
gets(a);
digit=strlen(a);

for (i=0; i < digit; i++) {


switch(a[i]) {

case 'a' : case 'A' :


case 'e' : case 'E' :
case 'i' : case 'I' :
case 'o' : case 'O' :
case 'u' : case 'U' : VowelCount++;
break;
case 'b' : case 'B' :
case 'c' : case 'C' :
case 'd' : case 'D' :
case 'f' : case 'F' :
case 'g' : case 'G' :
case 'h' : case 'H' :

case 'j' : case 'J' :


70

case 'k' : case 'K' :


Page

70
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

case 'l' : case 'L' :


case 'm' : case 'M' :
case 'n' : case 'N' :
case 'p' : case 'P' :

case 'q' : case 'Q' :


case 'r' : case 'R' :
case 's' : case 'S' :
case 't' : case 'T' :
case 'w' : case 'W' :
case 'v' : case 'V' :
case 'x' : case 'X' :
case 'y' : case 'Y' :
case 'z' : case 'Z' : ConsonantCount++;
break;
case ' ' : whitespace++;
break;
}
}
printf("Vowels = %d",VowelCount);
printf("\nConsonant = %d\nWhitspace = %d",ConsonantCount,whitespace);
getch();
}

OUTPUT
Enter a String :=> Saturday Is The Last Day For Submission Of Soft Copy Of C
Lab

Vowels = 17
Consonant = 32
Whitspace = 12

71
Page

71
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Unit-4 User Defined Functions


Syntax of Function, Calling functions, Actual & Formal Arguments, Categories of
Functions, Function prototype, Scope Rules: Local & Global variables, Recursion,
Recursion vs. iteration, Passing Arguments: call by values & call by reference, Passing
array to function.Structures: Declaration and initialization of structures, Array of
structures, Array within structure, structure within structure, Structures and functions,
Introduction to unions.

Question 1: What is Functions? Explain need for function.

Answer:

A function is a self-contained block of code that performs a particular task.

Once a function has been designed and packed, it can be treated as a black box that takes
some data from the main program and returns a value. The inner details of the operation
are invisible to the rest of the program. All that the program knows about a function is :
What goes in and what comes out.

Every C program can be designed using a collection of these black boxes known as
functions.

Prototypes:- are syntactically distinguished from the old style of function declaration.
The two styles can be mixed for any single function, but this is not recommended. The
following is a comparison of the old and the prototype styles of declaration:

Old style:

Functions can be declared implicitly by their appearance in a call.

Arguments to functions undergo the default conversions before the call.

The number and type of arguments are not checked.

Prototype style:

Functions are declared explicitly with a prototype before they are called. Multiple
declarations must be compatible; parameter types must agree exactly.

Arguments to functions are converted to the declared types of the parameters.


72
Page

72
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

The number and type of arguments are checked against the prototype and must agree with
or be convertible to the declared types. Empty parameter lists are designated using
the void keyword.

Ellipses are used in the parameter list of a prototype to indicate that a variable number of
parameters are expected.

Prototype Syntax

A function prototype has the following syntax:

function-prototype-declaration:

C functions can be classified into two categories :

Library functions :

This functions are built-in functions and not required to be written by user.

Example : sqrt, printf, scanf, exp( ).

Used-Defined functions :

It has to be developed by the user at the time of writing a program. However it can later
become a part of

the C program library.

example : main( ).

Need for functions:-

It makes program easier to understand, debug and test.

It saves both time and space.

It facilitates top-down modular programming.

In this programming style, the high level logic of the overall problem is solved first
while the details of each lower-level functions are addressed later.

The length of a source program can be reduced by using functions at appropriate places.

It is easy to locate and isolate a faulty function for further investigation.


73
Page

73
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

A function may be used by many other programs. In order to make use of a user-defined
function, we need to establish three elements that are related to functions :

 Function definition
 Function call
 Function declaration

The function definition is an independent program module that is specially written to


implement the requirements of the function.

In order to use this function we need to invoke it at a required place in the program. This
is known as function call. The program (or a function) that calls the function is
referred to as the calling program or calling function.

The calling program should declare any function (like declaration of a variable) that is to
be used later in the program. This is known as function declaration or function prototype.

Function declaration :

Like variables, all functions in C program must be declared, before they are invoked. A
function declaration also known as function prototype consist of four parts :

Function type

Function name

Parameter list

Terminating semicolon

They are coded in the following format :

Function-type function-name (parameter list);

Definition of a function

A function definition, also known as function implementation shall include the following
elements :

Function name

Function type
74
Page

74
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

List of parameters

Local variable declaration

Function statements

A return statement

All six elements are grouped into two parts :

Function header

Function body

Function_type function_name(parameter list)

local variable declaration;

executable statement1;

executable statement2;

……………………….

………………………

return statement;

Function call

A function can be called by simply using the function name followed by a list of actual
parameters (or arguments), if any, enclosed in parentheses.

Function_name(parameter list);

Question 2: Define value return by functions and their Different categories of functions.

Answer: Return values and their types

A function may or may not send back any value to the calling function.
75
Page

75
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

If it does, it is done through the return statement.

A called function can return only one value per call at the most.

The return statement can take one of the following form:

return;

OR

return(expession);

The plane return does not return any value to the calling function, it acts much as the
closing brace of the function.

When a return is encountered the control is immediately passed back to the calling
function.

For example :

mul(x,y)

int x,y;

int p;

p=x*y;

return(p);

If we do not specify any return type then by default compiler assumes it as int.

Question 3: Write short note on

(i) Nesting of functions

(ii) Call by value

(iii) Call by reference


76

Answer:
Page

76
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

(i)Nesting of functions

C permits nesting of functions freely. main can call function1, which calls function2,
which calls function3,………… and so on. There is in principle no limit as to how deeply
functions can be nested.

Fun1( )

………….

……….

Fun2( )

……….

……….

Fun2( )

…………

……….

Fun3( )

…………..

(ii) call by value :

Call by value means programmer send some value coping from one function to
another.At the time of function calling aprogrammer can send a copy of variable of value.

call by value method:

passing the value of variable to the function.


77
Page

77
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

void swap(int ,int ); // function declaration

void main()

int x=10,y=20;

printf("%d%d',x,y);

swap(x,y); // function calling

void swap(int a,int b) // function definition

int c;

c=a;//changes here do not affect in values

a=b;//of x and y in main function..

b=c;

(iii)call by referance :

it means sending sending the address of variable to the called function means a user can
send the address of variable.

call by reference method:

passing the address of variable to the function.

swap(&a,&b)

&c=&a;

&a=&b;

&b=&c;
78
Page

78
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

changes made in subfunction causes changes in address of

variables n thus in main () also..

Question 4: What is Recursion ? Explain with small example.

Answer:

When a called function in turn calls another function a process of chaining occurs.
Recursion is a special case of this process where a function calls itself.

For example :

void main( )

printf(―this is an example of recursion\n‖);

main( );

Recursive functions can be effectively used to solve problems where solution is


expressed in terms of successive applying the same solution to subsets of the problem.

When we write recursive functions, we must have an if statement to force the function to
return without the recursive call being executed. Otherwise, the function will never
return.

Question 5: Write a program to print the following Fibonacci series using recursive
function :

1 1 2 3 5 8 13 ……………………………………..

Answer:

#include<stdio.h>

#include<conio.h>

int count=0,a=1,b=1,n,c,f;

void main()
79
Page

79
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

int sum;

int febo(int,int);

clrscr();

printf(―Enter the number of terms:‖);

scanf("%d",&n);

printf("%d\t%d\t",a,b);

c=febo(a,b);

getch();

febo(int a,int b)

if(count==n-2)

return c;

else {

count++;

c=a+b;

printf("%d\t",c);

febo(b,c);

}}

OUTPUT :

Enter the number of terms:6

1 1 2 3 5 8
80
Page

80
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Question 6: Explain method of Passing arrays to a function.

Answer:

Passing One dimensional arrays :

To pass a one-dimensional array to a called function, it is sufficient to list the name of the
array, without any subscripts and the size of the array as arguments.

For example :

Declaration : void largest(int [ ],int);

Calling : largest(a,n)

Definition : void largest(int a[ ],int n)

Three rules to pass an array to a funcion

The function must be called by passing only the name of the array.

for example : largest(a);

2. In the function definition, the formal parameter must be an array type, the size of the
array does not need to be specified.

for example : void largest(int a[ ])

The function prototype must show that the argument is an array.

for example : void largest(int [ ]);

passing Two dimensional arrays:-

The rules are :


81

The function must be called by passing only the array name.


Page

81
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

for example : average(matrix,m,n);

In the function definition, we must indicate that the array has two dimensions by
including two sets of brackets.

for example : void average(int matrix[ ][n],int m,int n)

The size of the second dimension must be specified.

The prototype declaration should be similar to function header.

for example : void average(int matrix[ ][n],int,int);

Question 7:

(i)What is structure ?

(ii) Write a program to define a structure student that will contain the roll number, name
and total marks of a student the program will ask the user to input details of 5 students
and print the details of all the students whose total marks is greater then a given value.

(iii) Define Array of structure.

Answer:

(i) Structure:

A structure is a collection of data items of different types. A structure is a convenient


tool for handling a group of logically related data items.

For example it can be used to represent a set of attributes, such as student_name,


roll_number and marks.

Defining a structure

Structures must be defined first for their format that may be used later to declare structure
variable. The general format of a structure definition is as follows :

struct tag_name
82
Page

82
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

data_type member1;

data_type member2;

……………………

……………………};

In defining a structure you may note the following syntax :

The template is terminated with semicolon.

While the entire definition is considered as a statement, each member is declared


independently for its name and type in a separate statement inside the template.

The tag_name can be used to declare structure variables of its type, later in program.

Declaring structure variables

After defining a structure format we can declare variables of that type.

A structure variable declaration is similar to the declaration of variables of any other data
types.

It includes the following elements :

The keyword struct.

The structure tag name.

List of variable names separated by commas.

A terminating semicolon.

For example :

Struct Student{

Char name[10];

int rno;
83

char sec; };
Page

83
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

struct student s1,s2;//s1, s2 are structure variable;

The members of a structure themselves are not variables. They do not occupy any
memory until they are associated with the structure variables such as student.

When the complier comes across a declaration statement, it reserves memory space for
the structure variables.

It is also allowed to combine both the structure definition and variables in one statement.

Accessing structure members

The members themselves are not variables, they should be linked to the structure
variables in order to make them meaningful members.

The link between a member and a variable is established using member operator ‗ . ‗,
which is also known as dot operator or period operator.

For example : s1.rno;

Structure initialization

Initializer lists

Example:

struct student s1={―RAm‖, 100, ‗A‘};

Assignment statements

Example:

s2=s1;

Could also declare and initialize as follows:

s2.rno=20;

Structure Comparison

We can compare values of members of same structure just like as ordinary variables.

For example :
84

struct student
Page

84
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

int roll_no;

char name[20];

float per;

};

struct student s1, s2;

We can compare percentage as :

if(s1.per>=s2.per)

…………………..

………………….

(ii) Prog #include<stdio.h>

#include<conio.h>

void main( )

struct student

int roll;

char name[10];

float marks;

}st[5];
85
Page

85
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

int i;

printf(―Enter details of students their roll no, name & marks:‖);

for(i=0;i<=5;i++)

scanf(―%d%s%f‖,&st[i].roll,st[i].name,&st[i].marks);

for(i=0;i<=5;i++)

if(st[i].marks>65)

printf(―%d%s%f‖,st[i].roll,st[i].name,st[i].marks);

getch( );

(iii) ARRAYS OF STRUCTURES:-

Suppose we have to maintain details for all the students of a class.

In such cases we may declare an array of structures, each element of the array
representing a structure variable.

For example :

struct student std[50];

Defines an array called std that consist of 50 elements.

Each element is defined to be of type struct list.

Consider the following declaration:

struct student
86
Page

86
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

int roll_no;

char name[20];

};

struct student std[3]={{101,‖abc‖},{102,‖xyz‖},{103,‖deg‖}};

This declares std as an array of three elements std[0], std[1], std[2].

And initializes their roll_no as:

std[0].roll_no=101 std[1].roll_no=102 std[2].roll_no=103

Question 8: What is Union :? Write difference between Union And structure.

Answer : Union

Unions are a concept borrowed from structures and therefore follow the same syntax as
structure. However, there is a major difference between them in terms of storage. In
structures, each member has its own storage location, whereas all the members of the
union use the same location. This implies that although a union may contain many
members of different types, it can handle only one member at a time.

Union

 Memory that contains a variety of objects over time


 Only contains one data member at a time
 Members of a union share space
 Conserves storage
 Only the last data member defined can be accessed
 union declarations
 Same as struct

union Number

int x; float y;
87

};
Page

87
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

union Number value;

Valid union operations:-

Assignment to union of same type: =

Taking address: &

Accessing union members: .

Accessing members using pointers: ->

The main difference between structure and union is:-

The size of the structure is sum of the size of each member in the struchture.but sizeof the
union is size of Largest member in the union because union members are overlaps on
each other in memory.

If we declare two structure variables,Both variables are stored in different location


but union stored in same location.

General format of structure : -

struct tag_name
{
data type member1;
data type member2;


}

Example of Structure:
struct lib_booker
{
char titles[20];
char authors[15];
int page;
float prices;
88

};
Page

88
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

General format of union : -


union item
{
int m;
float p;
char c;
}

code;

Question 9: Discuss Method of Defining structure inside another structure and give proper

example.

Answer: This is a discussion on Defining structure inside another structure within the C

Programming forums, part of the General Programming Boards category; for example:

struct IDType

char zone;

char id[10];

struct Record

IDType id; //nested stucture

char x[30];

int y,z,f;

}
89
Page

89
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Unit-5 Pointer Data type and its Application


Pointer Operator, Pointer Expression, Initializating pointers, Pointer Arithmetic, Pointer
and Function Arguments, Pointer to function, Pointer and Arrays, Pointers and String,
Arrays of Pointers, Pointers to Pointers.Files in C: Defining and opening a file, closing a
file, input/ Output operations on files, error handling during I/O operations, random access
to files.

Question 1: What is Pointers? How is it initialized ? Define various pointer Arithmeitc

operation.

Answer: Pointer:

Pointer is a derived data type in C. it is built from one of the fundamental data types
available in C. A pointer variable is a variable that contains an address which is the
location of another variable in memory. Since these memory addresses are the locations
in the computer memory where program instructions and data are stored, pointers can be
used to access and manipulate data stored in the memory.

Declaring pointer variables

In C every variable must be declared by its type. Since pointer variables contain
addresses that belong to a separate type, they must be declared as pointers before we use
them. The declaration of a pointer variable takes the following form :

data_type *pt_name;

This tells the compiler three things about the variable pt_name : The asterisk ( * ) tells
that the variable pt_name is a pointer variable. pt_name needs a memory location.
pt_name points to a variable of type data type.

Initialization of pointers

Initialize pointers to 0, NULL, or an address

0 or NULL – points to nothing (NULL preferred)

Accessing the address of a variable

The actual location of a variable in the memory is system dependent and therefore, the
90

address of a variable is not known to us immediately.


Page

90
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Then how can we determine the address of a variable?

This can be done with the help of the operator & (address of) available in C.

The operator & immediately preceding a variable returns the address of the variable
associated with it.

For example the statement :

P=&quantity;

would assign the address of quantity to variable p.

Accessing variable through its pointer

Once a pointer has been assigned the address of a variable, the question remains as to
how to access the value of the variable using the pointer?

This is done by using a unary operator asterisk ( * ), usually known as the indirection
operator, another name for this operator is the dereferencing operator or it can be
remembered as value at address.

Pointer Arithmetic:-

 Arithmetic operations can be performed on pointers


 Increment/decrement pointer (++ or --)
 Add an integer to a pointer( + or += , - or -=)

Operations meaningless unless performed on an array Five element int array on machine
with 4 byte ints vPtr points to first element v[ 0 ] at location 3000 (vPtr = 3000)

vPtr += 2; sets vPtr to 3008

vPtr points to v[ 2 ] (incremented by 2), but the machine has 4 byte ints, so it points to
address 3008

Subtracting pointers

Returns number of elements from one to the other. If

vPtr2 = v[ 2 ];

vPtr = v[ 0 ];
91
Page

91
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

vPtr2 - vPtr would produce 2

Pointer comparison ( <, == , > )

See which pointer points to the higher numbered array element Also, see if a pointer

points to 0

Pointers of the same type can be assigned to each other

If not the same type, a cast operator must be used

Exception: pointer to void (type void *)

Generic pointer, represents any type .

No casting needed to convert a pointer to void pointer.

void pointers cannot be dereferenced.

Question 2: What is pointer to pointer? Explain with suitable Example?

Answer: Pointer to Pointer

It is declare as

<data type> **<ptr-to-ptr>;

Example:

char ch=‘c‘;

char **p1, *p2;


As the definition of pointer says that its a special variable that can store the address of an
other variable. Then the other variable can very well be a pointer. This means that its
perfectly legal for a pointer to be pointing to another pointer.

Lets suppose we have a pointer ‗p1′ that points to yet another pointer ‗p2′ that points to a
92

character ‗ch‘. In memory, the three variables can be visualized as :


Page

92
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

p2=& c;

p1=& p2;

So we can see that in memory, pointer p1 holds the address of pointer p2. Pointer p2
holds the address of character ‗ch‘.

So ‗p2′ is pointer to character ‗ch‘, while ‗p1′ is pointer to ‗p2′ or we can also say that
‗p2′ is a pointer to pointer to character ‗ch‘.

Now, in code ‗p2′ can be declared as :

p2=&ch;

But ‗p1′ is declared as :

char **p1=&p2;

So we see that ‗p1′ is a double pointer (ie pointer to a pointer to a character) and hence
the two *s in declaration.

Now,

 ‗p1′ is the address of ‗p2′ ie 5000


 ‗*p1′ is the value held by ‗p2′ ie 8000
 ‗**p1′ is the value at 8000 ie ‗c‘
Code:
#include<stdio.h>
int main(void)
93

{
Page

93
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

char **ptr = NULL;


char *p = NULL;
char c = ‗d‘;
p=&c;
printf(―\n %c‖, c);
printf(―\n %c‖, *p);
printf(―\n %c‖, **ptr);
getch();
}

Question 3: Explain the array of pointers.

Answer:

Its used for isolated variable.

An array of pointers can be declared as

<type> *<name>[<number-of-elements>];

Example:

Char *ptr[3];

#include<stdio.h>

void main()

{
94

char *p1=‖i-Gate ‖;
Page

94
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

char *p2=‖Bhilai‖;

char *p3=‖SSS ‖;

char *arr[3];

int i;

for(i=0;i<3;i++)

printf(―\n %s‖, arr[i]);

getch();

Question 4: Explain the function of pointers.


Answer: A function pointer can be declared as :

<return type of function> (*<name of poiter>)(type of function arguments);

For example :

int (*fptr) (int, int);

The above line declares a function pointer ‗fptr‘ that can point to a function whose return
type is ‗int‘ and takes two integers as arguments.

Lets take a working example :

#include<stdio.h>

int func(int a, int b)

{
95
Page

95
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

printf(\n %d, %d‖, a, b);

void main()

int (*fptr) (int, int);// function pointer

fptr=func; //Assign address to function pointer

func(2, 3);

fptr(2, 3);

getch();

Output: 2 3

2 3

Question 5: What is dynamic memory Allocation? Explain with example. Write down

difference between malloc() and calloc().

Answer:

The exact size of array is unknown untill the compile time,i.e., time when a
compier compiles code written in a programming language into a executable form.
The size of array you have declared initially can be sometimes insufficient and
sometimes more than required.
Dynamic memory allocation allows a program to obtain more memory space,
while running or to release space when no space is required.
Although, C language inherently does not has any technique to allocated memory
dynamically, there are 4 library functions under "stdlib.h" for dynamic memory
96

allocation.
Page

96
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Function Use of Function


Allocates requested size of bytes and returns a pointer first
malloc() byte of allocated space
Allocates space for an array elements, initializes to zero and
calloc() then returns a pointer to memory
free() dellocate the previously allocated space
realloc() Change the size of previously allocated space

malloc()
It is used to allocate single block of memory of specified size and return a pointer of type
void which can be type casted into pointer of any type.

Syntax of malloc()
ptr=(cast-type*) malloc(byte-sze);
Exaple:
int *p;
p=(int *)malloc(10* sizeof(int));
This statement will allocate 20 bytes respectively and the pointer points to the
address of first byte of memory.
calloc()
It is used to allocate multi block of memory of specified size and return a pointer of type
void which can be type casted into pointer of any type.

Syntax of calloc()
ptr=(cast-type*) calloc(n, element-sze);
Exaple:
int *p;
p=(int *)calloc(10, sizeof(int));
This statement will allocate 20 bytes respectively and the pointer points to the
address of first byte of memory.
free()
Dynamically allocated memory with either calloc() or malloc() does not get return
on its own. The programmer must use free() explicitly to release space.
97

syntax of free()
Page

97
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

free(ptr);
This statement cause the space in memory pointer by ptr to be deallocated.
Examples of calloc() and malloc()
Write a C program to find sum of n elements entered by user. To perform this
program, allocate memory dynamically using malloc() function.
#include <stdio.h>
#include <stdlib.h>
int main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc
if(ptr==NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}

Write a C program to find sum of n elements entered by user. To perform this


program, allocate memory dynamically using calloc() function.
#include <stdio.h>
#include <stdlib.h>
int main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int));
if(ptr==NULL)
{
98

printf("Error! memory not allocated.");


Page

98
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}
realloc()
If the previously allocated memory is insufficient or more than sufficient. Then,
you can change memory size previously allocated using realloc().
Syntax of realloc()

ptr=realloc(ptr,newsize);

Here, ptr is reallocated with size of newsize.


#include <stdio.h>
#include <stdlib.h>
int main(){
int *ptr,i,n1,n2;
printf("Enter size of array: ");
scanf("%d",&n1);
ptr=(int*)malloc(n1*sizeof(int));
printf("Address of previously allocated memory: ");
for(i=0;i<n1;++i)
printf("%u\t",ptr+i);
printf("\nEnter new size of array: ");
scanf("%d",&n2);
ptr=realloc(ptr,n2);
for(i=0;i<n2;++i)
printf("%u\t",ptr+i);
return 0;
}
99
Page

99
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Difference between calloc() and malloc():

Characteristic Calloc() Malloc()


Function allocates a region of memory allocates "size" bytes of memory.
large enough to hold "n
elements" of "size" bytes each.
Also initializes contents of
memory to zeroes.
Number of Arguments 2 1
Syntax void *calloc (number_of_blocks, void *malloc (size_in_bytes);
size_of_each_block_in_bytes);
Contents of allocated The allocated region is initialized The contents of allocated
to zero. memory are not changed. i.e., the
memory contains unpredictable
or garbage values. This presents a
risk.
Retuen Type void pointer (void *). If the void pointer (void *). If the
allocation succeeds, a pointer to allocation succeeds, a pointer to
the block of memory is returned. the block of memory is returned.
If the allocation of memory fails, If the allocation of memory fails,
a NULL pointer is returned. a NULL pointer is returned.

Question 6: What is FILE? Define various file handling function fopen(), fclose() , fprintf()
, fscanf() .

Answer:

A file is a place on disk where the group of related data is stored .Like most other
language, C supports number of functions that have the ability to perform basic file
operations, which includes:

 naming a file.
 Opening a file.
 Reading data from a file.
 Writing data to a file .
 Closing a file.

There are two distinct way to perform file operation in C. The first one is known as low
level I/O and uses UNIX system calls. The second method is referred to as the high level
I/O operation and uses functions in C‘s standard I/O library.

High Level I/O functions


100
Page

100
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Function Name Operation

fopen() Creates new file for use. Opens an existing file for use.

fclose() Closes a file which has been opened for use.

getc() Reads a character from a file.

putc() Writes a character to a file.

fprintf() Writes set of data values to a file.

fscanf() Reads set of data values from a file.

getw() Reads an integer from a file.

putw() Writes an integer to a file.

fseek() Sets the position to a desired location in the file.

ftell() Gives the current position in the file.

rewind() Sets the position to the beginning of the file.

DEFINING AND OPENING A FILE :

Data structure of a file is defined as FILE in the library of standard I/O function

definitions. Therefore all the file should be declared as type FILE before they are used.

FILE is defined data type.

When we open a file, we must specify what we want to do with the file. For example we

may write data to the file or read the already existing data.

General format for declaring and opening a file:

FILE *fp;

Fp = fopen(“filename”, “mode”);
101

The first statement declares the variable fp as a ―pointer to the data type FILE‖
Page

101
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

The second statement opens the file named filename and assigns an identifier to the

FILE type pointer fp.

Mode can be one of the following :

r Open the file for reading only.

w Open the file for writing only.

a Open the file for aapending only.

Many recent compilers includes additional modes of operation. They include:

r+ The existing file is opened to the beginning for both reading and writing

w+ Open the file for both reading and writing.

a+ Open the file for both reading and appending.

When trying to open a file, one of the following things may happen:

When the mode is ‗writing‘, a file with the specified name is created if the file does not
exist. The content are deleted if the file already exists.

When the purpose is ‗appending‘, the file is opened with the current contents safe. A file
with the specified name is created if the file does not exist.

If the purpose is ‗reading‘ , and if it exists, then the file is opened with the current
contents safe; otherwise an error occurs.

CLOSING A FILE:

A file must be closed as soon as all operations on it have been completed. This ensures
that all outstanding information associated with the file is flushed out from the buffers
and all links to the file are broken.

fclose(file_pointer);

This would close the file associated with the FILE pointer file_pointer.
102

The following segment of a program:


Page

102
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

…………

…………

FILE *p1, *p2;

p1 = fopen(―INPUT‖, ―w‖);

p2 = fopen(―OUTPUT‖, ―r‖);

…………

………….

fclose(p1);

fclose(p2);

………..

This program opens two files and closes them after all operations on them are completed.

INPUT/OUTPUT OPERATIONS ON FILES:

Once a file is opened , reading out or writing to it is accomplished by using the standard
I/O routines.

The getc() and putc() Functions:

The simplest file I/O function are getc() and putc(). These are analogous to getchar and
putchar function and handle one character at a time. Assume that a file is opened with
mode wand file pointer fp1. Then statement

putc(c,fp1);

Writes the character contained in character variable c to file associated with FILE

pointer fp1.

Similarly getc() is used to read a character from a file that has been opened in read mode.
103

The statement
Page

103
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

c = getc(fp2);

Would read a character from the file whose file pointer is fp2.

The getw() and putw() Functions:

The getw and putw are integer oriented functions. They are similar to the getc and putc
functions and are used to read and write integer values. These function would be useful
when we deal with only integer data. The general forms of getw and putw are:

putw(integer, fp);

getw(fp);

The fprintf() and fscanf() Functions:

Most complier support two other functions, namely fprintf and fscanf that can handle a
group of mixed data simultaneously.

The function fprintf and fscanf performsI/O operation that are similar to printf and scanf
function, except they work on files.

The general form of fprintf is:


fprintf(fp, “control string”, list);

Where fp is the file pointer associated with the file that has been opened for writing.

The control string contains output specification for the items in the list

The list may include variables, constants and strings.

For example:

fprintf(f1, ―%s%d%f‖ ,name,age,7.5);

The general form of fscanf is:

fscanf(fp, “control string”, list);


104

For example:
Page

104
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

fscanf(f2, ―%s%d‖ ,item, &quqntity);

Like scanf , fscanf also returns the number of items that are successfully read. When the
end of file is reached, it returns the value EOF.

/* file.c: Display contents of a file on screen */

#include <stdio.h>

void main()

FILE *fopen(), *fp;

int c ;

fp = fopen( ―prog.c‖, ―r‖ );

c = getc( fp ) ;

while ( c != EOF )

putchar( c );

c = getc ( fp );

fclose( fp );

Question 7: How ERROR HANDLING DURING I/O OPERATIONS is performed?

Answer: Errors can occur during file handling are

 Trying to read beyond the end-of-file.


 Device overflow.
105

 Trying to use a file that has not been opened.


Page

105
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

 Trying to perform an operation on a file, when the file is opened for another type
of information.
 Opening a file with an invalid file name.
 Attempting to write to a write protected file.
 If we fail to check such read and write errors, a program may behave abnormally
when an error occurs.
 An unchecked error may result in a premature termination of program or incorrect
output.
 C has two library functions feof and ferror that can help us to detect I/O errors.
 If fp is a pointer to a file that has just been opened for reading :

if(feof(fp))

printf(―End of data‖);

The ferror function reports the status of the file indicated.

It returns a nonzero integer if an error has been detected upto that point, during
processing.

It returns zero otherwise.

tf(ferror(fp)!=0)

printf(―An error has occurred‖);

When we open a file using fopen function, a file pointer is returned .

If the file cannot be opened for some reason, then the function returns null pointer.

This facility can be used to test whether a file has been opened or not.

if(fp= =NULL)

printf(―file cannot be opened‖);

Question 8: Define fseek(), ftell and rewind.

Answer:
106
Page

106
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

ftell : Takes a file pointer and returns a number of type long, that corresponds to the
current position. This function is useful in saving the current position of the file,
which can be used later in program.

It takes the following form:

n = ftell(fp);

n would give the relative offset(in bytes) of the current position.

rewind : This function is used to take the file pointer and resets the position to the
start of the .The statement:

rewind(fp);

n = ftell(fp);

fseek: This function is used to move the file position to a desired location within the
file. It takes the following form.

fseek(fp, offset, position);

fp is a file pointer to the file concerned offset is a number or variable of type long.

offset may be positive, meaning move upward or negative meaning backward.

position is an interger no.The position can take one of the following three values.

Value Meaning

0 Beginning of the file

1 Current position

2 End of the file.


107
Page

107
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Question 9: How command line argument is used explain with example.

Answer:

It is a parameter supplied to a program when the program is invoked. This parameter may
represent filename the program should process. For example, if we want to execute a
program to copy the contents of file named X_FILE to another one named Y_FILE , then
we may use command line like-

C>PROGRAM X_FILE Y_FILE

PROGRAM is the filename where the executable code of the program is stored. This
eliminates the need for the to request the user to enter the filenames during execution.
main can take two arguments called argc and argv and the information contained in the
command line is passed on to the program through these arguments, when main is called
up by the system.

The variable argc is an argument counter that counts the number of arguments on the
command line. The argv is an argument vector and represent s an array of character
pointers that point to the command line arguments. The size of this array will be equal to
the value of argc.

argv[0] PROGRAM

argv[1] X_FILE

argv[2] Y_FILE

In order to access the command line arguments, we must declare the main function and
its parameter as follows:

main(argc, argv)

int argc;

char *argv[];

{
108

………….
Page

108
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

………….

/*prompt user for filename and display file on screen */

#include <stdio.h>

void main()

FILE *fopen(), *fp;

int c ;

char filename[40] ;

printf(―Enter file to be displayed: ―);

gets( filename ) ;

fp = fopen( filename, ―r‖);

c = getc( fp ) ;

while ( c != EOF )

putchar(c);

c = getc ( fp ); }

fclose( fp );

Question 10: Write a program to count the number of lines and characters in a file.

Answer: Each line of input from a file or keyboard will be terminated by the newline character

‗\n‘. Thus by counting newlines we know how many lines there are in our input.
109

/*count.c : Count characters in a file*/


Page

109
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

#include <stdio.h>

void main()

/* Prompt user for file and count number of characters

and lines in it*/

FILE *fopen(), *fp;

int c , nc, nlines;

char filename[40] ;

nlines = 0 ;

nc = 0;

printf(―Enter file name: ―);

gets( filename );

fp = fopen( filename, ―r‖ );

if ( fp == NULL )

printf(―Cannot open %s for reading \n‖, filename );

exit(1); /* terminate program */

c = getc( fp ) ;

while ( c != EOF )

if ( c == ‗\n‘ )
110

nlines++ ;
Page

110
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

nc++ ;

c = getc ( fp );

fclose( fp );

if ( nc != 0 )

printf(―There are %d characters in %s \n‖, nc, filename );

printf(―There are %d lines \n‖, nlines );

else

printf(―File: %s is empty \n‖, filename );

Question 11: Write a program to display file contents 20 lines at a time. The program

pauses after displaying 20 lines until the user presses either Q to quit or Return to

display the next 20 lines.

Answer: /* display.c: File display program */

/* Prompt user for file and display it 20 lines at a time*/

#include <stdio.h>

void main()

FILE *fopen(), *fp;

int c , linecount;
111

char filename[40], reply[40];


Page

111
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

printf(―Enter file name: ―);

gets( filename );

fp = fopen( filename, ―r‖ ); /* open for reading */

if ( fp == NULL ) /* check does file exist etc */

printf(―Cannot open %s for reading \n‖, filename );

exit(); /* terminate program */

linecount = 1 ;

reply[0] = ‗\0‘ ;

c = getc( fp ) ; /* Read 1st character if any */

while ( c != EOF && reply[0] != ‗Q‘ && reply[0] != ‗q‘)

putchar( c ) ; /* Display character */

if ( c == ‗\n‘ )

linecount = linecount+ 1 ;

if ( linecount == 20 )

linecount = 1 ;

printf(―[Press Return to continue, Q to quit]‖);

gets( reply ) ;

}
112

c = getc ( fp );
Page

112
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

fclose( fp );

Question 12: Write a program to compare two files specified by the user, displaying a

message indicating whether the files are identical or different.

Answer: /* compare.c : compare two files */

#include <stdio.h>

void main()

FILE *fp1, *fp2, *fopen();

int ca, cb;

char fname1[40], fname2[40] ;

printf(―Enter first filename:‖) ;

gets(fname1);

printf(―Enter second filename:‖);

gets(fname2);

fp1 = fopen( fname1, ―r‖ ); /* open for reading */

fp2 = fopen( fname2, ―r‖ ) ; /* open for writing */

if ( fp1 == NULL ) /* check does file exist etc */

printf(―Cannot open %s for reading \n‖, fname1 );


113

exit(1); /* terminate program */


Page

113
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

else if ( fp2 == NULL )

printf(―Cannot open %s for reading \n‖, fname2 );

exit(1); /* terminate program */

else /* both files opened successfully */

ca = getc( fp1 ) ;

cb = getc( fp2 ) ;

while ( ca != EOF && cb != EOF && ca == cb )

ca = getc( fp1 ) ;

cb = getc( fp2 ) ;

if ( ca == cb )

printf(―Files are identical \n‖);

else if ( ca != cb )

printf(―Files differ \n‖ );

fclose ( fp1 );

fclose ( fp2 );
114

}
Page

114
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Question 13: Write a program to copy the content of a file to another.

Answer:

The first parameter in the command line is always the program name and therefore

argv[0] always represent the program nam

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<ctype.h>

void main()

FILE *fs,*ft;

char ch;

printf("copying contents of one file to another file\n");

fs=fopen("al.cpp","r");

if(fs==NULL)

puts("canot open the siurce file\n");

exit(0);

ft=fopen("a2.cpp","w");

if(ft==NULL)
115

puts("\ncannot open target file\n");


Page

115
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

fclose(fs);

exit(0);

while(1)

ch=fgetc(fs);

if(ch==EOF)

break;

else

fputc(ch,ft);

fclose(fs);

fclose(ft);

getch();

116
Page

116
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Question n Answer

Aim – Write a program to print value one less than & One More Than The Given Value & Print

their Sum .
Code –

#include<stdio.h>
#include<conio.h>
void main ()
{
int a,b,c,sum;
clrscr();
printf("Enter The Value \t");
scanf("%d",&a);
b= a-1;
c= a+1;
sum=a+b+c;
printf("\nThe Value One less Than The Given Value %d",b);
printf("\nThe Value One Greater Than The Given Value %d",c);
printf("\nThe Value Of Their Sum Is %d",sum);
getch();
}
OUTPUT
Enter The Value 8
The Value One Less Than The Given Value 7
The Value One Greater Than The Given Value 9
The Value Of Their Sum Is 24

Aim - Write a program to reverse four digit number


Code-

#include<stdio.h>
#include<conio.h>
void main ()
{
int num, a, b, c, num1;
clrscr ();
printf ("Enter The Four Digit Number : ");
scanf ("%d", &num);
a=num%10;
117

num=num/10;
b=num%10;
Page

117
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

num=num/10;
c=num%10;
num=num/10;
num1=a*1000+b*100+c*10+num;
printf ("The Reversed Four Digit Number Is : %d", num1);
getch ();
}
OUTPUT
Enter The Four Digit Number : 1256
The Reversed Four Digit Number Is: 6521

Aim – Write a program to find the first day of the any year.
Code -

#include<stdio.h>
#include<conio.h>
void main()
{
int year, leapdays, days;
clrscr ();
printf ("Enter The Year To Find First Day (Year>1900) : ");
scanf ("%d", &year);
leapdays=(year-1900)/4;
days=leapdays+(year-1900);
days=days%7;
switch (days)
{ case 0:printf ("Monday\n");
break;
case 1:printf ("Tuesday\n");
break;
case 2:printf ("Wednesday\n");
break;
case 3:printf ("Thursday\n");
break;
case 4:printf ("Friday\n");
break;
case 5:printf ("Saturday\n");
break;
case 6:printf ("Sunday\n");
break;
}
118

getch ();
}
Page

118
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

OUTPUT

Enter The Year To Find First Day (Year>1900): 2012


Sunday

Aim – Write a program to reverse a 5-digit number and check whether the reversed and original
numbers are Equal
Code -

#include<stdio.h>
#include<conio.h>
void main ()
{
long int num, a, b, c, d, e, num1;
clrscr ();
printf ("Enter The 5 Digit Number : ");
scanf ("%ld", &num);
e=num;
a=num%10;
num=num/10;
b=num%10;
num=num/10;
c=num%10;
num=num/10;
d=num%10;
num=num/10;
num1=a*10000+b*1000+c*100+d*10+num;
printf ("The Reversed Number Is : %ld \n", num1);
if (e==num1)
printf ("The Entered and Reversed Numbers Are Equal\n");
else
printf ("The Entered and Reversed Numbers Are Not equal\n");
getch ();
}
OUTPUT

Enter The 5 Digit Number : 12321


The Reversed Number Is : 12321
The Entered and Reversed Number Are Equal
119
Page

119
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Aim – Write a program to calculate gross salary of MR based on Sales


Code -
#include<stdio.h>
#include<conio.h>
void main ()
{
float bs, hra, da, ta, inc, bonus, gs, sales;
clrscr ();
printf ("Enter The Sales Earned : ");
scanf ("%f", &sales);
if (sales < 100000)
{
bs = 3000;
hra = 0.2 * 3000;
da = 1.10 * 3000;
ta = 500;
inc=0.1*sales;
bonus=1000;
}
else
{
bs = 3000;
hra = 0.2 * 3000;
da = 1.10 * 3000;
ta = 500;
inc=0.1*sales;
bonus=2000;
}
gs=bs+hra+da+ta+inc+bonus;
printf ("The Gross Salary of MR is : %f \n", gs);
getch ();
}

OUTPUT

Enter The Sales Earned : 110000


The Gross Salary Of MR Is : 20400 120
Page

120
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Aim – Write a program to find which type of character is entered?


Code -

#include<stdio.h>
#include<conio.h>
void main()
{
char a;
clrscr ();
printf ("Enter The Character : ");
a=getchar ();
if (a>=65 && a<=90)
printf ("Entered Character Is a Upper Case Letter\n");
else if (a>=97 && a<=122)
printf ("Entered Character Is a Lower Case Letter\n");
else if (a>=48 && a<=57)
printf ("Entered Charcter Is a Number\n");
else
printf ("Entered Character Is a Special Symbol\n");
getch ();
}
OUTPUT
Enter The Character : #
Entered Character Is a Special Symbol

Aim – Write a program to find whether a person is insured or not?


Code-

#include<stdio.h>
#include<conio.h>
void main()
{
char sex, health, place;
int age;
clrscr ();
printf ("Enter Gender (M \ F) : ");
scanf("%c",&sex);
printf("Enter Health Condition (E \ P) : ");
scanf("%c",&health);
printf("Enter Place C for City or V for Village : ");
scanf ("%c", &place);
121

printf ("Enter The Age Of Person : ");


scanf ("%d", &age);
Page

121
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

if (age>=25 && age<=35 && sex=='M' && health=='E' && place=='C')


printf ("Premium is Rs: 4 Per Thousand And His Policy Cannot Exceed Rs 2,00,000\n");
else if (age>=25 && age<=35 && sex=='F' && health=='E' && place=='C')
printf ("Premium is Rs: 3 Per Thousand And Her Policy Cannot Exceed Rs 1,00,000\n");
else if (age>=25 && age<=35 && sex=='M' && health=='P' && place=='V')
printf ("Premium is Rs: 6 Per Thousand And His Policy Cannot Exceed Rs 10,000\n");
else
printf ("The Person Is Not Insured\n");
getch ();
}
OUTPUT
Enter Gender (M \ F) : M
Enter Health Condition (E \ P) : E
Enter Place C for City or V for Village : C
Enter The Age Of Person : 30
Premium is Rs: 4 Per Thousand And His Policy Cannot
Exceed Rs 2,00,000

Aim – Write a program to check leap year using conditional operators.


Code -
#include<stdio.h>
#include<conio.h>
void main ()
{
int year;
printf ("Enter The Year To Check It Is A Leap Year Or Not : ");
scanf ("%d", &year);
((year%4==0 && year%100!=0)||(year%400==0))?printf ("%d Is A Leap Year
\n",year):printf ("%d Is Not A Leap Year\n",year);
getch ();
}
OUTPUT

Enter The Year To Check It Is A Leap Year Or Not : 2016


2016 Is A Leap Year

Aim – Write a program to display the n natural number.


Code -

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

void main()
{
Page

122
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

int i, n;
clrscr();
printf(―\n Enter n= ‖);
scenf(―%d‖, &n);
for(i=1;i<=50;i++)
printf("%d \t",i);
getch();
}

OUTPUT

1 2 3 4 5 6 7 8 9 10 11 12
13 14 15 16 17 18 19 20 21 22 23 24
…..

Aim - Write a program to count number between 1-100 not Divisible by 2,3,5
Code

#include<stdio.h>
#include<conio.h>
void main()
{
int i, x=0;
clrscr();
printf("The Numbers From 1 To 100 Not Divisible By 2 , 3 & 5 Are : \n");
for(i=1;i<=100;i++)
{
if(i%2!=0 && i%3!=0 && i%5!=0)
{
printf(" %d\n",i);
x++;
}
}
printf("\nTotal No Which Are Not Divisible By 2 , 3 & 5 Are : %d",x);
getch();
}

OUTPUT

The Numbers From 1 To 100 Not Divisible By 2 , 3 & 5 Are : 1 7 11 13 17 19 23 2


9 31 37 41 43 47 49 53 59 61 67 71 73 77 79 83 89 91 97
123

Total No Which Are Not Divisible By 2 , 3 & 5 Are : 26


Page

123
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Aim – Write a program to print Even number upto n.


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

void main()
{
int i, n;
clrscr();
printf("Enter The Number Upto Which The Even Numbers To Print : ");
scanf("%d",&n);
printf("The Even Numbers Upto %d Is : \n",n);
for(i=1;i<=n;i++)
{
if(i%2==0)
printf("%d\t",i);
}
getch();
}

OUTPUT

Enter The Number Up to Which The Even Numbers To Print : 50


The Even Numbers Up to 50 Is :
2 4 6 8 10 12 14 16 18 20
22 24 26 28 30 32 34 36 38 40
42 44 46 48 50

Aim- Write a program to display armstrong number from 1 - 500


Code-

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,r,s,a;
clrscr();
printf("The Armstrong Number From 1 - 500 : \n");
for(i=1;i<=500;i++)
{
124

a=i;
r=a%10;
Page

124
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

a=a/10;
s=a%10;
a=a/10;
if(pow(r,3)+pow(s,3)+pow(a,3)==i)
printf("%d\t",i);
}
getch();
}

OUTPUT
The Armstrong Number From 1 – 500:
1 153 370 371 407

Aim - wap for a matchstick game being played between the computer and auser. Your

program should ensure that the computer always wins.


Code-

#include<stdio.h>
#include<conio.h>
void main()
{
int matchstick =21,user,computer;
clrscr();
printf("\nDont enter invalid number 1,2,3,4");
for(;matchstick>=1;)
{
printf("\n no .of avail matchstick %d ",matchstick);
printf("\n your turn pick ");
scanf("%d",&user);
if(user>4)
{
printf("invalid");
break;
}
computer = 5 - user;
printf("\ncomputer turn");
if(matchstick==1)
break;
printf("\n computer choose %d",computer);
matchstick=matchstick-user-computer;
125

continue;
}
Page

125
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

matchstick--;
printf("computer win");
getch();
}

OUTPUT

Dont enter invalid number 1,2,3,4


no .of avail matchstick 21
your turn pick 2

computer turn
computer choose 3
no .of avail matchstick 16
your turn pick 1

computer turn
computer choose 4
no .of avail matchstick 11
your turn pick 3

computer turn
computer choose 2
no .of avail matchstick 6
your turn pick 1

computer turn
computer choose 4
no .of avail matchstick 1
your turn pick 1

computer turn
computer win

Aim - Write a program to find all pythagorian triplet for all odd number in range 1 to10.
Code –
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
126

int i,x;
clrscr();
Page

126
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

for(i=1;i<10;i=i+2)
{
x=(pow(i,2)-1)/2;
printf("\nThe Pythagorian triplet for %d are \t%d %d %d",i,i,x,x+1);
}
getch();
}

OUTPUT

The Pythagorean Triplet for 1 are 1 0 1


The Pythagorean Triplet for 3 are 3 4 5
The Pythagorean Triplet for 5 are 5 12 13
The Pythagorean Triplet for 7 are 7 24 25
The Pythagorean Triplet for 9 are 9 40 41

Aim –Write a program to stimulate digital clock.


Code –
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
int h,m,s;
h=0;
m=0;
s=0;
clrscr();
while(1)
{
if(s>59)
{
m=m+1;
s=0;
}
if(m>59)
{
h=h+1;
m=0;
}
if(h>11)
127

{
h=0;
Page

127
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

m=0;
s=0;
}
delay(1000);
s=s+1;
clrscr();
printf("\n DIGITAL CLOCK");
printf("\n HOUR:MINUTE:SECOND");
printf("\n%d:%d:%d",h,m,s);
}
getch();
}

OUTPUT

DIGITAL CLOCK
HOUR:MINUTE:SECOND
0:05:35

Aim - To Print Series


*
**
***
**** (up to n)
code

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
clrscr();
printf("Enter The Length Of The Series : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("*");
printf("\n");
}
getch();
128

}
Page

128
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

OUTPUT
Enter The Length Of The Series : 6
*
**
***
****
*****
*******
Aim- To Print Series
1
12
123
1 2 3 4( up to n)
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
clrscr();
printf("Enter The Length Of The Series : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("%d ",j);
printf("\n");
}
getch();
}

OUTPUT

Enter The Length Of The Series : 6


1
12
123
1234
12345
129
Page

129
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Aim- To Print Series


1
ab
234
a b c d( up to n)
code-

#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n,x=1;
char a='a';
clrscr();
printf("Enter The Length Of The Series : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==0)
{
for(j=1;j<=i;j++)
{
printf("%c ",a);
a++;
if(a>122)
a=97;
}
}
else
{
for(j=1;j<=i;j++)
{
printf("%d ",x);
x++;
}
}
printf("\n");
}
getch();
}
130

OUTPUT
Page

130
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Enter The Length Of The Series : 6

1
ab
234
abcd
56789
ef g hi j

Aim- To Print Series


******
*****
****
***
**
*

Code-

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

void main()
{
int i,j,n;
clrscr();
printf("Enter The Length Of The Series : ");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=i;j>=1;j--)
printf("* ");
printf("\n");
}
getch();
}
OUTPUT Enter The Length Of The Series : 6
******
*****
****
***
131

**
*
Page

131
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Aim- To Print Series


654321
54321
4321
321
21
1
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,x=7;
clrscr();
for(i=6;i>=1;i--)
{
for(j=i;j>=1;j--)
{
printf("%d",j);
}
printf("\n");
}
getch();}
OUTPUT
654321
54321
4321
321
21
1
Aim- To Print Series
1
12
123
1234
4321
321
32
1
Code-
#include<stdio.h>
132

#include<conio.h>
void main()
Page

132
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

{
int i, j, n;
clrscr();
printf("Enter The Length Of The Series : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("%d ",j);
printf("\n");
}

for(i=n;i>=1;i--)
{
for(j=i;j>=1;j--)
printf("%d ",j);
printf("\n");
}
getch();
}

OUTPUT

1
12
123
1234
4321
321
32
1

Aim - To Print Series


0
101
21012
3210123

Code -
#include<stdio.h>
#include<conio.h>
133

void main()
{
Page

133
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

int i, j, k, l, n;
clrscr();
printf("Enter The Length Of The Series : ");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=n-1;j>=i;j--)
printf(" ");

for(k=i;k>=0;k--)
printf("%d ",k);

for(l=i-1;l>=0;l--)
printf("%d ",i-l);

printf("\n");
}
getch();
}

Aim – To Print Series


1
2 3
4 5 6
7 8 9 10

Code –
#include<stdio.h>
#include<conio.h>
void main ()
{
int i, j, a=1, k;
clrscr();
for(i=1;i<=4;i++)
{
for(j=4;j>=i;j--)
printf(" ");
for(k=1;k<=i;k++)
{
printf("%d ",a);
a++;
134

}
printf("\n");
Page

134
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

}
getch();
}

Aim- WAP to take 10 integer number in array and display it with their address.
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i;
clrscr();
for(i=0;i<10;i++)
{
printf("Enter %d Element In An Array:",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
printf("\nElement=%d Address=%u",a[i],&a[i]);
getch();
}
OUTPUT
Enter 1 Element In An Array:2
Enter 2 Element In An Array:4
Enter 3 Element In An Array:5
Enter 4 Element In An Array:6
Enter 5 Element In An Array:7
Enter 6 Element In An Array:8
Enter 7 Element In An Array:9
Enter 8 Element In An Array:10
Enter 9 Element In An Array:15
Enter 10 Element In An Array:12

Element=2 Address=65506
Element=4 Address=65508
Element=5 Address=65510
Element=6 Address=65512
Element=7 Address=65514
Element=8 Address=65516
Element=9 Address=65518
Element=10 Address=65520
135

Element=15 Address=65522
Element=12 Address=65524
Page

135
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Aim- Write a program to find largest number and smallest number from 10 integer
number in array.
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,b,s;
clrscr();
for(i=0;i<10;i++)
{
printf("Enter %d Element In An Array: ",i+1);
scanf("%d",&a[i]);
}
b=a[0];
s=a[0];
for(i=1;i<10;i++)
{
if(a[i]>b)
b=a[i];
if(a[i]<s)
s=a[i];
}
printf("\nLargest Value In An Array Is : %d\nSmallest Value In An Array Is : %d",b,s);
getch();
}

OUTPUT

Enter 1 Element In An Array: 15


Enter 2 Element In An Array: 36
Enter 3 Element In An Array: 95
Enter 4 Element In An Array: 40
Enter 5 Element In An Array: 12
Enter 6 Element In An Array: 85
Enter 7 Element In An Array: 74
Enter 8 Element In An Array: 65
Enter 9 Element In An Array: 92
Enter 10 Element In An Array: 57

Largest Value In An Array Is : 95


136

Smallest Value In An Array Is : 12


Page

136
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Aim – Write a program to merge two array ,with 5 element each, in to third array.
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],b[5],i,c[10];
clrscr();
printf("Enter Element In First Array: \n");
for(i=0;i<5;i++)
{
printf("%d Elements Is :",i+1);
scanf("%d",&a[i]);
}
printf("Enter Element In Second Array: \n");
for(i=0;i<5;i++)
{
printf("%d Element Is :",i+1);
scanf("%d",&b[i]);
}
for(i=0;i<10;i=i+2)
{
c[i]=a[i/2];
c[i+1]=b[i/2];
}
printf("\nElements of Merged Array Are: ");
for(i=0;i<10;i++)
printf("%d ",c[i]);
getch();
}

OUTPUT

Enter Element In First Array:


1 Elements Is :15
2 Elements Is :13
3 Elements Is :11
4 Elements Is :7
5 Elements Is :9
Enter Element In Second Array:
1 Element Is :14
137

2 Element Is :12
3 Element Is :10
Page

137
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

4 Element Is :6
5 Element Is :8

Elements of Merged Array Are: 15 14 13 12 11 10 7 6 9 8

Aim- Write a program to read three digit number and generate its possible permutation
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3],x,i,m=1,c,j;
clrscr();
printf("Enter Three Digit Number : ");
scanf("%d",&x);
for(i=0;i<3;i++)
{
a[i]=x%10;
x/=10;
}
if(a[0]==a[1]&&a[1]==a[2])
m=6;
if(a[0]==a[1]||a[1]==a[2]||a[0]==a[2])
m=2;
printf("Possible Permutations Are: ");
for(i=0;i<6/m;i++)
{
for(j=0;j<3;j++)
{
if(i>2)
c=i-j;
else
c=i+j;
if(c<0)
c=-1*c;
printf("%d",a[c%3]);
}
printf(" ");
}
getch();
}
138

OUTPUT
Page

138
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Enter Three Digit Number : 123


Possible Permutations Are: 321 213 132 312 231 123

Aim- Write a program to enter five number using array and rearrange it in reverse order.
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
for(i=0;i<5;i++)
{
printf("Enter %d Element : ",i+1);
scanf("%d",&a[i]);
}
printf("The Elements In Reverse Order Is: ");
for(i=4;i>=0;i--)
printf("%d, ",a[i]);
getch();
}
OUTPUT
Enter 1 Element : 12
Enter 2 Element : 35
Enter 3 Element : 62
Enter 4 Element : 45
Enter 5 Element : 54
The Elements In Reverse Order Is: 54, 45, 62, 35, 12,

Aim – Write a program to count the number of student belonging to each of the following
group of marks: obtained by 50 students
0-9, 10-19, 20-29,………….,80-89,90-99,10
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int m[10],i,j,c[10],k=0,d=0;
clrscr();
for(i=0;i<10;i++)
{
139

printf("Enter Marks:");
scanf("%d",&m[i]);
Page

139
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

if(m[i]>99)
d++;
}
for(j=0;j<=90;j=j+10)
{
c[k]=0;
for(i=0;i<10;i++)
{
if(m[i]>=j&&m[i]<=j+9)
c[k]++;
}
k++;
}
for(j=0,k=0;k<10;j=j+10,k++)
printf("\nNo Of Students Belonging To %d to %d Is : %d",j,j+9,c[k]);
printf("\nNo of Students Belonging To 100 Is : %d",d);
getch();
}

OUTPUT
Enter Marks:95
Enter Marks:65
Enter Marks:12
Enter Marks:32
Enter Marks:15
Enter Marks:32
Enter Marks:65
Enter Marks:02
Enter Marks:32
Enter Marks:36

No Of Students Belonging To 0 to 9 Is : 1
No Of Students Belonging To 10 to 19 Is : 2
No Of Students Belonging To 20 to 29 Is : 0
No Of Students Belonging To 30 to 39 Is : 4
No Of Students Belonging To 40 to 49 Is : 0
No Of Students Belonging To 50 to 59 Is : 0
No Of Students Belonging To 60 to 69 Is : 2
No Of Students Belonging To 70 to 79 Is : 0
No Of Students Belonging To 80 to 89 Is : 0
No Of Students Belonging To 90 to 99 Is : 1
140

No of Students Belonging To 100 Is : 0


Page

140
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Aim- WAP to input 20 number and separate it in even and odd array.
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],e[20],o[20],i,j=0,k=0;
clrscr();
for(i=0;i<20;i++)
{
printf("Enter %d Element : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<20;i++)
{
if(a[i]%2==0)
{
e[k]=a[i];
k++;
}
else
{
o[j]=a[i];
j++;
}
}
printf("\nEven Array Element Is: ");
for(i=0;i<k;i++)
printf("%d ",e[i]);
printf("\nOdd Array Element Is: ");
for(i=0;i<j;i++)
printf("%d ",o[i]);
getch();
}
OUTPUT
Enter 1 Element : 15
Enter 2 Element : 32
Enter 3 Element : 65
Enter 4 Element : 48
Enter 5 Element : 958
Enter 6 Element : 75
141

Enter 7 Element : 52
Enter 8 Element : 51
Page

141
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Enter 9 Element : 36
Enter 10 Element : 39
Enter 11 Element : 82
Enter 12 Element : 10
Enter 13 Element : 32
Enter 14 Element : 02
Enter 15 Element : 01
Enter 16 Element : 30
Enter 17 Element : 37
Enter 18 Element : 39
Enter 19 Element : 34
Enter 20 Element : 36

Even Array Element Is: 32 48 958 52 36 82 10 32 2 30 34 36


Odd Array Element Is: 15 65 75 51 39 1 37 39

Aim - WAP a program to count occurrence of digit 0 to 9 digit between 1 and given decimal
number
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,i,j,c[10];
clrscr();
printf("Enter Number (<100): ");
scanf("%d",&a);
for(i=0;i<=a;i++)
{
for(j=0;j<10;j++)
{
c[j]=0;
if((i/10)%10==j||i%10==j)
c[j]++;
}
}
for(i=0;i<10;i++)
printf("\nNumber of %d Is : %d",i,c[i]);
getch();
}
142

OUTPUT
Page

142
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Enter Number (<100): 52

Number of 0 Is : 0
Number of 1 Is : 0
Number of 2 Is : 1
Number of 3 Is : 0
Number of 4 Is : 0
Number of 5 Is : 1
Number of 6 Is : 0
Number of 7 Is : 0
Number of 8 Is : 0
Number of 9 Is : 0

Aim- Write a program to find largest number from 5 rows and 5 column matrix
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int matrix[5][5],i,j,big;
clrscr();
printf("Enter Elements of the 5*5 Matrix: ");
for(i=0;i<5;i++)
for(j=0;j<5;j++)
scanf("%d",&matrix[i][j]);
big=matrix[0][0];
for(i=0;i<5;i++)
for(j=0;j<5;j++)
{
if(matrix[i][j]>big)
big=matrix[i][j];
}
printf("\nBiggest Element in 5*5 Matrix Is : %d",big);
getch();
}

OUTPUT
Enter Elements of the 5*5 Matrix: 15
51 65 98 45 87 98 65
45 65 98 41 20 32 32 65
654 456 45 465 68
143

645 643 564 45


Biggest Element in 5*5 Matrix Is : 654
Page

143
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Aim – Write a program to find String Length.


Code-
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char a[100];
int i=0,k=0;
clrscr();
printf("Enter The String: ");
scanf("%[^\n]s",a);
while(a[i]!='\0')
{
k++;
i++;
}
printf("The Length Of The String Is : %d ",k);
getch();
}

OUTPUT

Enter The String: BHILAI NAGAR


The Length Of The String Is : 12

Aim – Write a program to Reverse the string.


Code-

#include<stdio.h>
#include<conio.h>
void main()
{
char a[100], t;
int l=0,k=0;
clrscr();
printf("Enter The String : ");
scanf("%[^\n]s",a);
while(a[l]!='\0')
{
l++;
144

}
Page

144
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

for(int i=1;i<l/2;i++)
{
t=a[i];
a[i]=a[l-i-1];
a[l-i-1]=t;
}
printf ("The reverse Of The Entered String Is: %s", a);

getch();
}

OUTPUT
Enter The String : KAUSHAL
The reverse Of The Entered String Is: LAHSUAK

Aim – Write a program to check string is palindrome or not.

Code-
#include<stdio.h>
#include<conio.h>
void main()
{
char a[100],b[100];
int i=0,k=0,j=0,len=0;
clrscr();
printf("Enter The String : ");
scanf("%[^\n]s",a);
while(a[i]!='\0')
{
len++;
i++;
}
for(k=i-1;k>=0;k--)
{
b[i-(k+1)]=a[k];
}
i=0;
while(a[i]!='\0')
{
if(a[i]==b[i])
j++;
145

i++;
}
Page

145
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

if(len==j)
printf("Palindrome\n");
else
printf("Not Palindrome\n");
getch();
}

OUTPUT

Enter The String : RADAR


Palindrome

Aim – Write a program to compare two string. (ignore case)


Code-
#include<stdio.h>
#include<conio.h>
void main()
{
char a[25],b[25];
int i=0,j=0,len=0,len1=0,k=0;
clrscr();
printf("Enter The First String: ");
gets(a);
printf("Enter The Second String: ");
gets(b);
while(a[i]!='\0')
{
len++;
i++;
}
i=0;
while(b[i]!='\0')
{
len1++;
i++;
}
if(len==len1)
{
while(a[j]!='\0')
{
if(a[j]==b[j]||a[j]==b[j]+32||a[j]==b[j]-32)
146

k++;
j++;
Page

146
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

}
if(k==len)
printf("Both Strings Are Same\n");
}
else
printf("\nBoth Strings Are Not Same");
getch();
}

OUTPUT

Enter The First String: SIDDHARTH HUKLA


Enter The Second String: siddharth shukla
Both Strings Are Same

Aim – Write a program to compare two string.(don’t ignore case)


Code-
#include<stdio.h>
#include<conio.h>
void main()
{
char a[25],b[25];
int i=0,j=0,len=0,len1=0,k=0;
clrscr();
printf("Enter The First String: ");
gets(a);
printf("Enter The Second String: ");
gets(b);
while(a[i]!='\0')
{
len++;
i++;
}
i=0;
while(b[i]!='\0')
{
len1++;
i++;
}
if(len==len1)
{
147

while(a[j]!='\0')
{
Page

147
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

if(a[j]==b[j]&&(a[j]==b[j]+32||a[j]==b[j]-32))
k++;
j++;
}
if(k==len)
printf("Both Strings Are Same\n");
else
printf("Both Strings Are Not Same\n");
}
else
printf("\nBoth Strings Are Not Same");
getch();
}

OUTPUT

Enter The First String: SIDDHARTH SHUKLA


Enter The Second String: siddharth shukla
Both Strings Are Not Same

Aim – Write a program to copy one string in to another string


Code-
#include<stdio.h>
#include<conio.h>
void main()
{
char a[100],b[100];
int i=0;
clrscr();
printf("Enter The String: ");
gets(a);
while(a[i]!='\0')
{
b[i]=a[i];
i++;
}
b[i+1]='\0';
printf("\n\nThe Copy String Is : ");
puts(b);
getch();
}
148

OUTPUT
Page

148
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Enter The String: ZEBRONICS

The Copy String Is : ZEBRONICS

Aim - WAP to append the first n character of string in to another string, and print.
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
char a[100],b[100];
int i=0,len=0,n,k=1;
clrscr();
printf("Enter The String: ");
gets(a);
while(a[i]!='\0')
{
len++;
i++;
}
printf("The Length Of The String Is : %d\n\n",len);
printf("Enter The Length Of Character To Append less Than %d: ",len);
scanf("%d",&n);
while(k<n)
{
b[k-1]=a[k-1];
k++;
}
b[k]='\0';
printf("The String Appended is: ");
puts(b);
getch();
}

OUTPUT

Enter The String: ARTIFICIAL INTELLIGENCE


The Length Of The String Is : 23

Enter The Length Of Character To Append less Than 23: 15


The String Appended is: ARTIFICIAL INTE
149
Page

149
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Aim – WAP that will read a line and delete all vowel from sentence. Assume that sentences
is not more than 80 char long.
Code –
#include<stdio.h>
#include<conio.h>
void main()
{
char a[100];
int i=0,len=0;
clrscr();
printf("Enter The String: ");
gets(a);
while(a[i]!='\0')
{
len++;
i++;
}
printf("The String After Deletion Of Vowel: ");
i=0;
while(i<=len)
{
if(a[i]!='a')
if(a[i]!='e')
if(a[i]!='i')
if(a[i]!='o')
if(a[i]!='u')
if(a[i]!='A')
if(a[i]!='E')
if(a[i]!='I')
if(a[i]!='O')
if(a[i]!='U')
printf("%c",a[i]);
i++;
}
getch();
}

OUTPUT

Enter The String: PRESENTLY WE ARE DOING THE C LABWORK


The String After Deletion Of Vowel: PRSNTLY W R DNG TH C LBWRK
150

Aim - Write a program that convert all character in string in to capital letter.
Page

150
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Code -
#include<stdio.h>
#include<conio.h>
void main()
{
char a[100];
int i=0;
clrscr();
printf("Enter The String: ");
gets(a);
while(a[i]!='\0')
{
if(a[i]>=97 && a[i]<=122)
a[i]=a[i]-32;
i++;
}
a[i]='\0';
printf("The String In Capital Is: ");
puts(a);
getch();
}

OUTPUT

Enter The String: it is an example to do


The String In Capital Is: IT IS AN EXAMPLE TO DO

Aim – Write a program to take set of individual full name and print abbreviate the first,
middle except the last name.
Code –

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char name[40];
char abname[30];
int i,len,m=1,j=0;
clrscr();
puts("Enter Name: ");
151

gets(name);
len=strlen(name);
Page

151
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

abname[j++]=name[0];
abname[j++]='.';
abname[j++]=' ';
for (i=0;i<len;i++)
{
if(name[i]==' ')
if(m==1)
{
abname[j++]=name[i+1];
abname[j++]='.';
abname[j++]=' ';
m=0;
}
else
{
while(name[i]!='\0')
{
abname[j++]=name[++i];
}
}
}
abname[j++]='\0';
puts("Abbreviated Name: ");
puts(abname);
getch();
}

OUTPUT

Enter Name: Siddharth Shanker Shukla


Abbreviated Name: S. S. Shukla

Aim – Write a program to take set of 5 name, and find Employee “abc” is there or not.
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
char name[5][25],sa[4]={"ABC"};
int i , c=1,j;
152

clrscr();
Page

152
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

puts("Enter The Employee Names : ");


for(i=0;i<=4;i++)
gets(name[i]);
j=0;
while(sa[j]!='\0')
{
for(i=0;i<=4;i++)
{
for(j=0;j<=2;j++)
{
if(sa[j]==name[i][j])
c=0;
}
}
}
if(c==0)
printf("Employee ABC Is Found\n");
else
puts("Employee ABC Not Found");
getch();
}

OUTPUT
ASD
AFG
ABC
QWE
RST
Employee ABC Is Found

Aim – Write a program to take set of 5 name, and find largest name.
Code-

#include<stdio.h>
#include<conio.h>
void main()
153

{
char name[5][25];
Page

153
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

int i , c[5]={0,0,0,0,0},j,k,max;
clrscr();
puts("Enter The Names : ");
for(i=0;i<=4;i++)
scanf("%s",name[i]);
for(i=0;i<=4;i++)
{
j=0;
while(name[i][j]!='\0')
{
c[i]++;
j++;
}
}
max=c[0];
for(i=0;i<5;i++)
{
if(c[i]>max && c[i]!=max)
{
max = c[i];
k=i;
}
k=0;
}
printf("The Largest Name Is : %s",name[k]);
getch();
}

OUTPUT
Enter The Names :
VIMAL
MRINAL
BHUDEV
ABHILASH
LOKESH
The Largest Name Is : ABHILASH
154
Page

154
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

AIM – Write a program to count frequency of all character in input string.


CODE
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string[100], ch;
int c = 0, count[26] = {0};
clrscr();

printf("Enter a string\n");
gets(string);

while ( string[c] != '\0' )


{
if ( string[c] >= 'a' && string[c] <= 'z' )
count[string[c]-'a']++;

c++;
}

for ( c = 0 ; c < 26 ; c++ )


{
if( count[c] != 0 )
printf("%c occurs %d times in the entered string.\n",c+'a',count[c]);
}

getch();
}

OUTPUT
Enter a string :=> It Is My First Practical Class On C Lab
a occurs 4 times in the entered string.
b occurs 1 times in the entered string.
c occurs 2 times in the entered string.
i occurs 2 times in the entered string.
l occurs 2 times in the entered string.
n occurs 1 times in the entered string.
r occurs 2 times in the entered string.
s occurs 4 times in the entered string.
155

t occurs 3 times in the entered string.


y occurs 1 times in the entered string.
Page

155
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Aim – Write a program to count no. of vowels , consonant , blank in input string.
Code –
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char a[100];
int VowelCount=0, ConsonantCount=0;
int whitespace=0, digit;
int i;
clrscr();
printf( "Enter a String :=> ");
gets(a);
digit=strlen(a);

for (i=0; i < digit; i++) {


switch(a[i]) {

case 'a' : case 'A' :


case 'e' : case 'E' :
case 'i' : case 'I' :
case 'o' : case 'O' :
case 'u' : case 'U' : VowelCount++;
break;
case 'b' : case 'B' :
case 'c' : case 'C' :
case 'd' : case 'D' :
case 'f' : case 'F' :
case 'g' : case 'G' :
case 'h' : case 'H' :

case 'j' : case 'J' :


case 'k' : case 'K' :
case 'l' : case 'L' :
case 'm' : case 'M' :
case 'n' : case 'N' :
case 'p' : case 'P' :

case 'q' : case 'Q' :


156

case 'r' : case 'R' :


case 's' : case 'S' :
Page

156
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

case 't' : case 'T' :


case 'w' : case 'W' :
case 'v' : case 'V' :
case 'x' : case 'X' :
case 'y' : case 'Y' :
case 'z' : case 'Z' : ConsonantCount++;
break;
case ' ' : whitespace++;
break;
}
}
printf("Vowels = %d",VowelCount);
printf("\nConsonant = %d\nWhitspace = %d",ConsonantCount,whitespace);
getch();
}

OUTPUT

Enter a String :=> Saturday Is The Last Day For Submission Of Soft Copy Of C
Lab
Vowels = 17
Consonant = 32
Whitspace = 12

157
Page

157
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Determine The Output of The Following Programme Module

1. main() 2. main()
{ {
int i = –1, j = –1, k = 0, l = 2, m; printf("%x",–1 << 4);
m = i++&&j++&&k++||l++; }
printf("%d %d %d %d %d",i,j,k,l,m);
} (a) 3ffc (b) FFF0
(a) –1 –1 0 2 (b) 0 0 1 3 1 (c) fff0 (d) 3FFC
(c) 0 0 1 3 0 (d) 1113
3. #define int char 4. main()
main() {
{ int i=10;
int i = 65; i=!i>14;
printf("sizeof(i)=%d", sizeof(i)); Printf ("i=%d",i);
} }
(a) 8 (b) 2 (a) 0 (b) 1
(c) 4 (d)1 (c) 2 (d) 0 & 1

5. main() 6. main()
{ {
printf("\nab"); int i=5;
printf("\bsi"); printf("%d%d%d%d%d",i++,i--
printf("\rha"); ,++i,--i,i);
} }
(a) ash (b) absiha (a) 56665 (b) 564544
(c) hai (d) has (c) 45545 (d) 55545

7. void main(){ char *url="c:\tc\bin\rw.c"; 8. main()


printf("%s",url); } {
(a)c:\tc\bin\rw.c printf("%p",main);
(b)c:/tc/bin/rw.c }
(c)c: c inw.c (d) w.c in (a) some address will be print
(b) linking error
(c) compiling error
(d) loading error

9. main() 10. void main()


{ {
int i=400, j=300; int i=5;
printf("%d..%d"); printf("%d", i++ + ++i);
158

} }
(a) 400..300 (b) 300..400 (a) 13 (b) 11
Page

158
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

(c) 400..garbage (d) garbage..300 (c) 14 (d) 12

11. void main() 12. main()


{ {
int i=5; int i;
printf("%d", i+++++i); printf("%d",scanf("%d",&i));} //
} value 10 is given as input here
(a) compiling error (b) linking error (a) 0 (b) 1
(c) loading error (d) 12 (c) 2 (d) 1

13. main() 14. main()


{ {
char not; int i=-1;
not=!2; -i; printf("i = %d, -i = %d \n",i,-
printf("%d",not); i); }
} (a) i= -1 , i= 1 (b) i= 1 , i= -1
(a) 2 (b) 1 (c) 0 (d) Error (c) i= -1 , i= -1 (d) i= 1 , i= 1

15. main() 16. void main()


{ {
int i=5,j=6,z; int i=i++,j=j++,k=k++;
printf("%d",i+++j); printf(―%d%d%d‖,i,j,k);
} }
(a) 10 (b) 12 (c) 11 (d) (a) garbage value (b) 0000
13 (c) 1111 (d) 0101

17. Minimum number of temporary 18. main()


variables needed to swap the contents { int i=5; printf("%d",++i++);
of two variables is }
(a) compile time error
(a) 0 (b) 1 (c) 2 (d) 3 (b) run time error
(c) linking time error
(d) loading time error

19. main() 20. main()


{ {
int i=5; printf(―%d‖,i=++i ==6); int i=10,j=20;
} j = i, j?(i,j)?i:j:j;
(a) 0 (b) error printf("%d %d",i,j);
159

(c) 1 (d) 5 }
(a) 10 10 (b) 20 10
Page

159
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

(c) 20 20 (d) 10 20

21. main() 22. main()


{ {
int i=5,j=10; int i=4,j=7;
i=i&=j&&10; printf("%d %d",i,j); j = j || i++ && printf("YOU
} CAN");
(a) 1 10 (b) 10 1 printf("%d %d", i, j);
(c) 5 10 (d) 1 0 }
(a) 11 4 (b) 1 4
(c) 4 7 (d) 4 1

23. main() 24. void main()


{ {
register int a=2; int a=-1;
printf("Address of a = %d",&a); printf(―%d‖,a>>4);
printf("Value of a = %d",a); getch();
} }
(a) compile time error (a) 1 (b) 0 (c) –1 (d) –2
(b) some address will be printed
(c) linking time error (d) 2

25. void main() 26. void main()


{ {
int a=0xab,b=0x78,c; int I=2;
c=a&b; printf(―%d,%d,%d,%d,%d‖,++I,I++,++I,
printf(―%p‖,c); I++,I);
} }
(a) 28 (b) 0028 (c) 028 (d) error (a) 33556 (b) 64422
(c) 33566 (d) 65432

27. void main() 28. void main()


{ {
const char x= ‗Z‘; int i=2,j=3,k=0,p;
printf(―%c‖,x); p =(i , k , j);
x=‘A‘; printf(―%c‖,x); printf(―%d‖,p);
} }
(a) Z (b) A (c) Z & A (d) (a) 2 (b) 3 (c) 0 (d) none
error

29. void main() 30. void main()


160

{ {
int ii = 10; printf(―%d‖,printf(―computer‖));
Page

160
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

ii<<=1; printf(―%d‖,ii); } }
(a) 10 (b) 15 (c) 20 (d) error (a) computer 8 (b) computer8
(c) 8computer (d) 8 computer

31. void main() 32. void main()


{ {
int i,j,k; int var = - -3;
scanf(―%d%d‖,&k,scanf(―%d%d‖, printf(―var= %d‖,var);
&i,&j); }
printf(―i= %d j= %d k= %d‖,i,j,k); (a) 3 (b) –3
getch(); (c) 2 (d) error
}
if input is 11,12,13,14
(a) i= 12,j=13,k=11
(b) i= 11,j=12,k=13
(c) i= 11,j=13,k=14
(d) i= 13,j=14,k=11

33. What will be the output 34. void main()


void main() {
{ int x; char *p; p="%d\n";
x=-3+4*5-6; printf(―%d ‖,x); p++; p++;
x=3+4%5-6; printf(―%d ‖,x); printf(p-2,300); }
x=-3*4% -6/5; printf(―%d ‖,x); (a) 300
x=(7+6)%5/2; printf(―%d ‖,x); (b) 400
} (c) 500
(d) None
(a) 10 -2 0 1 (b) 11 -3 0 1
(c) 11 3 0 1 (d) 11 1 0 1

35. main() 36. main()


{ int (*functable[2])(char *format, ...) { char s[]={'a','b','c','\n','c','\0'};
={printf, scanf}; char *p,*str,*str1;
int i = 100; p=&s[3]; str=p; str1=s;
(*functable[0])("%d", i);
printf("%d",++*p + ++*str1-32); }
(*functable[1])("%d", i);
(a) 77 (b) 76
(*functable[1])("%d", i);
161

(*functable[0])("%d", &i); }
Page

161
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

(A) 100, Runtime error. (c) 75 (d) 78


(B) 100, Random number, Random

number, Random number.


(C) Compile error
(D) 100, Random number

37. main() 38. main()


{ {
int var=5; int p=10,q=20,r=20;
printf(―%d‖,var++ * var++); p<<2>>1;
} r<<=2>>1;
(a) 30 (b) 36 printf(―%d %d
(c) 42 (d) 49 %d‖,p,q<<2>>1,r); }
(a) 10 20 10 (b) 10 20 20
(c) 10 40 40 (d) 10 10 10

39. main() 40. main()


{ {
int i=4; int i=5,j=8;
printf(―%d‖,i++*i++); j=j || (i++ && printf(―windows‖));
printf(―%d‖,++i*++i); printf(―%d %d‖,i,j);
} }
(a) 20 42 (b) 20 56 (a) 6 1 (b) 6 8
(c) 16 56 (d) 24 42 (c) 5 1 (d) 5 5

41. main() 42. main()


{ {
int x; int x;
x=~!printf(); x= – ~ ! ! ~ printf(―‖);
printf(―%x‖,x); printf(―%d‖, x);
} }
(a) –1 (b) 0 (c) ffff (d) (a) 1 (b) 2
Error (c) –2 (d) –1

43. void main(main) 44. void main()


{ {
2||printf(―hi‖)&&printf(―%s‖,‖Hello‖);
printf(―%d**‖,main+=pow(++main/**/
162

}
,++main)); } (a) Hihello (b) hi hello
Page

162
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

(a) 11 (b) 12** (c) 13 (d) 12 (c) 1 (d) print


nothing

45. void main() 46. void main()


{ {
printf(―%d‖,3|printf(―hi\n‖)||printf(―% int k;
s‖,hello\n)); k=(~2|3)>~0?7:8<<2;
} printf(―%o‖,k);
(a) hi hello 3 (b) hi 1 }
(c) hello hi 3 (d) hi hello 1 (a) 34 (b) 40 (c) 20 (d) 30

47. #define call(x,y) x##y 48. void main()


void main(){ {
int x=5,y=10,xy=20; int i=15,j=4,m,n;
printf("%d",xy+call(x,y); } m=i>9;
n=j>2 && j!=2;
(a) 35 (b) 510 (c) 15 (d) 40 printf(―m= %d,n= %d‖,m,n);
}
(a) m= 1 n= 0 (b) m=1 n=1
(c) m=0 n=0 (d) m=1 n=1

49. void main() 50. void main()


{ {
printf(―%d %d %d‖ , sizeof(3.14f) int i= –1;
,sizeof(3.14) , sizeof(3.141) ); i>>=5;
} printf(―%x‖,i);
(a) 4,8,10 (b) 4, 8, 8 }
(c) 4,8,4 (d) 4,4,4 (a) ffff (b) FFF
(c) 07ff (d) 07FF

51. #define FALSE –1 52. main()


#define TRUE 1 { int x = 4, y, z, m;
#define NULL 0
main() { y = -- x; z = x --;
if(NULL) m = y – z; printf(―\n %d‖, m);
puts("NULL"); }
else if(FALSE)
puts("TRUE"); (a) 0 (b) 1 (c) 2 (d) 3
else
puts("FALSE");
}
163

(a) FALSE (b) TRUE


(c) NULL (d) ERROR
Page

163
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

53. int f(int x) 54. main()


{
{if (x>0) return g(x) + f(x – 1);
int i =0;j=0;
return 1;} if(i && j++)
int g(int x) printf("%d..%d",i++,j);
printf("%d..%d,i,j);
{if(x > 0) return g(x-1) + f(x-1)-1; }
return 1;} (a) 0..0 (b) 1..1 (c) 1..0 (d)
What is the value of f(3)? 0..1
(A) 10 (B) 9 (C) 11 (D)12

55. void main() { 56. what is the value an integer value an


double far* p, q; integer can hold in an ANSI C complier?
printf("%d", sizeof (p) + sizeof (q);
} (a) 65536 (b) 2147483647
(a)12 (b)8 (c)4 (d)1 (c) INT_MAX (d) 1<<INT_BITS

57. void main() 58. void main()


{ int i; {
char a[]="\0"; if(printf(―%d%d%d‖))
if(printf("%s\n",a)) printf(―hello‖);
printf("Ok here \n"); printf(―Sir‖);
else }
printf("Forget it\n"); } (a) 03440helloSir (b) helloSir
(a) Ok here (b) Forget it (c)Sir (d) 03440
(c) Error (d) Nonthe above

59. main() 60. void main()


{ {
int x,y=2,z,a; if(~0 == (unsigned int)-1)
if(x=y%2) z=2; printf(―You can answer this if you
a=2; know how values are represented in
printf("%d %d ",z,x); memory‖);
} }
(a) 0 (b) 2
(c) garbage value 0 (d) Error (a) 0 (b) Print printf msg
(c) –2 (d) Error

61. void main() 62. main()


164

{ printf(―%d‖, {
strcat(((!0)|6|0xf)>=20?0x10:2,8)); } float i=1.5;
Page

164
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

(a) 1 (b) 0 switch(i)


(c) 2 (d) None { case 1: printf("1");
case 2: printf("2");
default : printf("0"); } }
(a) run time error
(b) compiler Error
(c) linking error
(d) loading error

63. main() 64. main()


{ int i=3; {
switch(i ) { int I;
default:printf("zero"); for(I=1;I<2;printf(―By
case 1: printf("one"); break; iitian‖),I++)
case 2:printf("two"); break; printf(― i-Gate‖); }
case 3: printf("three"); break; } (a) By iitian i-Gate (b) i-Gate
} (c) i-Gate by iitian (d) Error
(a) three (b) one (c) two (d) zero

65. #include<stdio.h> 66. main()


main() {
{ int i=0;
int i=1,j=2; for(;i++;printf("%d",i)) ;
switch(i) printf("%d",i);
{ }
case 1: printf("GOOD"); break; (a) 1 (b) 0
case j: printf("BAD"); break; } (c) error (d) garbage
}
(a) Compiler Error
(b) Run time error
(c) GOOD (d) BAD

67. void main() 68. main()


{ {
while(1) unsigned int i=10;
{ printf(―jay‖);} while(i-->=0)
} printf("%u ",i);
(a) jay +INF time (b) jay }
(c) true (d) Error (a) 9 8 7 6 5 4 3 2 1 0 65535
65534…..
165

(b) 10 9 8…………2 1 0
(c) 9 8 7…………..2 1 0
Page

165
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

(d) garbage

69. #include<conio.h> 70. void main()


main() {
{ char ch;
int a, b;
a=10,20,30; for(ch=0;ch<=127;ch++)
b=(10,20,30); printf(―%c %d \n―, ch, ch);
printf("%d %d ",a,b); }
} (a) Implementaion dependent
(a) 30,30 (b) 10,10
(b) 0…127
(c) 10,20 (d) 10,30
(c) 0….127…128
(d) None

71. void main() 72. void main()


{ {
int I=0; int I=0;
while(++I<=10) while(I++<=10)
printf(―%d‖,I); printf(―%d‖,I);
} }
(a) 1---10 (b) 1—9 (a) 0---10 (b) 0---11
(c) 1---11 (d) 1---12 (c) 1---11 (d) 1---12

73. void main() 74. wahat is the data type used in switch
{ statement?
int i , j; (a) int char float (b) char float
for(i=0,j=0;i<5,j<25;i++,j++); (c) int char (d) char string
printf(―i=%d,j=%d‖,i,j);
}
(a) i=25 j=25 (b) i= 5 j=5
(c) i=4 j=24 (d) Error
75. void main() 76. void main()
{ int i=1; {
for(;i;) i++;printf(―%d‖,i); } int var=0;
(a) 1 (b) 0 for(;++var;printf(―%d‖,var));
(c) +inf (d) none printf(―%d‖,var);
}
(a) 0 (b) 1 to 0
(c) 1 to +INF (d) 0 to +INF
166

77. void main() 78. void main(int)


{ {
Page

166
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

int i=1,j=1; int x=1;


while(++i<=10){ j++; } if(―%d=hello‖,x);
printf(―%d%d‖,i,j); }
} (a) Error (b) No Error
(a) 10 11 (b) 11 10 (c) 1 (d) hello
(c) 10 9 (d) 9 10

79. void main() 80. main()


{ {
char i=250; int k=1;
for(i<0;i++;i=0,printf(―%d‖,i)) ; } switch(k)
(a) 1 (b) +INF {
(c) 0 (d) Error case 1: goto label1;
case 2:
printf(―\nHello…‖);break;
}
printf(―\n Hi….‖);
label1:
}
(a) Hello Hi (b) Print nothing
(c) Hi (d) Error
81. void main() 82. void main()
{ {
int k=4; int i=1,j=2;
switch(k) switch(i,j)
{ {
default: printf(―LAST\n‖); default: printf(―LAST\n‖);
case 4: printf(―FOUR\n‖); case 3: printf(―THREE\n‖);break;
case 1: break; case 2: printf(―TWO\n ‖);break;
printf(―ONE\n‖); case 1: printf(―ONE\n‖); break;
case 2: printf(―TWO\n‖); break; }
} } }
(a) LAST FOUR (b) FOUR (a) LAST TWO (b) LAST
(c) ONE (d) Error (c) TWO (d) Error

83. What is the value assigned to the 84. What is the value of the expression
variable X if b is 7? (3^6) + (a^a)?
X = b>8? b <<3: b>4? b>>1: b; (a)3 (b) 5
(a) 5 (b) 3 (c) 6
(c) 7 (d) 0
(d) a+18 ( e) None
167
Page

167
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

85. oid main () 86. main()


{ {
int a=1; int i;
while(a<=32767) for(i=0;i<5;i++)
{ {
printf(― %d\n‖ , a) ; a++; printf("%d\n", 1L << i);
} }
(a) +INF (b) 1----32767 }
(c) 1------------32767----------0 (a) 1 2 4 8 16 (b) 0 1 2 4 8
(d) Error (c) Error (d) 2 4 8 16 32

87. main() 88. void main()


{
{ int I=0;
int i=0;
for(;I++; printf("%d" , I )) for(;i<5;i++);
printf("%d" ,I ); printf(―%d‖,i);
}
} (a) 0,1,2,3,4 (b) 5
(a) 0 (b) 1 (c) 1,2,3,4 (d) +INF
(c) 0 , 1 (d) blank

89. void main() 90. void main()


{ {
int i=1; int suite=1;
for(;;) switch(suite);
{printf(―%d‖,i++); switch(suite);
if(i>10) {
break; } case 0:printf(―club‖);
} case 1:printf(―Diamond‖);
(a) 1 to 10 (b) +INF }
(c) 1 (d) Error }
(a) club (b) Diamond
(c) 1 (d) Error

91. void main() 92. void main()


{ {
int i=1; int a=10;
while() switch(a){}
{ printf(―i-Gate Academy‖);
168

printf(―%d‖,i++); }
if(I>10) (a) Error (b) 10
Page

168
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

break; (c) i-Gate Academy (d) +INF


}
}

(a) 1 (b) +INF


(c) 1 to 10 (d) Error

93. void main() 94. void main()


{ {
int i=1; char i=‘1‘;
switch(i) switch(i)
{ {
printf(―Hello‖); case7:printf(―7‖);
case 1:printf(―Yes‖); break; case1:printf(―1‖);
case 2:printf(―No‖);break; case21:printf(―21‖);break;
} case0:printf(―0‖);
} default:printf(―Jai‖);
(a) No (b) 1 (c) Hello (d) Yes }
}
(a) 7 (b) 21 (c)0 (d) Jai

95. void main() 96. void main() {


{ struct student
int i=3; { sex : 15; status:6; };
switch(i) printf(―\n size of= %d‖,sizeof(struct
{ student )); }
case 1:printf(―1‖);break; (a) 2 (b) 1 (c) 0 (d) 3
case 2:printf(―2‖);
case 3:continue;
default:printf(―Bye‖);
}
}
(a) 1 (b) 2 (c) Bye (d) Error

97. main() { 98. void main()


char * strA=‖jay‖; {
char * strB = ―I am OK‖; if(‗A‘ < ‗a‘)
memcpy( strA, strB, 6); printf(―Hello….‖);
puts(strA); } else
(a) Runtime error (b) ―I am OK‖ printf(―Hi…….‖);
}
169

(c) Compile error (d) ―I am O‖ (a) Hello (b) Hi


(c) Hello……. (d) Hi……..
Page

169
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

99. void main() 100. void main()


{ {
int i=1; char ch=‘A‘;
if(--i=0) while(ch<=‘Z‘)
printf(―Executed\n‖); printf(―\n%d‖,ch);
} }
(a) Executed (b) 0 (a) A (b) A…to...Z
(c) 1 (d) Error (c) Z (d) A & Z

101. main() 102.The declaration float a[][3] = { {1.0} ,


{ char s[ ]="man"; {2.0} , {3.0}}; represents
int i; (a) a one-by-three array
for(i=0;s[ i ];i++) (b) a three-by-one array
printf("\n%c%c%c%c",s[i],*(s+i),*(i+s (c) a three-by-three array
) , i[s] ); } (d) a one-by-one array
(a) mmmm (b) aaaa
(c) nnnn (d) mmmm
aaaa
nnnn

103. main() 104. Function fun(x:integer):integer,


{ begin
char *str1="abcd"; if x>100 then fun:=x-10;
char str2[]="abcd"; else fun(fun(x+11));
printf("%d%d%d",sizeof(str1),sizeof(str2) end;
, sizeof("abcd")); (a) 89 (b) 90 (c) 91 (d) 92
}
(a) 2 5 5 (b) 1 5 5
(c) 2 4 4 (d) 1 2 2

105. long fun(char *s) 106. main()


{ {
long r = 0; int x=3,z; z=x-- -111;
for(;*s;r=(r<<1)|(*s++ - ‗0‘)); printf(x,z); }
return r; } (a)2,101 (b) 2,111
What does fun(―000001010‖) return?
(a) 1024 (b) 10 (c) 100 (d) 512 (c)2,109 (d) 2, -108

107. main() 108. main()


170

{ {
char p[ ]="%d\n"; char str1[] = {‗s‘,‘o‘,‘m‘,‘e‘};
Page

170
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

p[1] = 'c'; char str2[] = {‗s‘,‘o‘,‘m‘,‘e‘,‘\0‘};


printf(p,65); while (strcmp(str1,str2))
} printf(―Strings are not equal\n‖);
(a) A (b) B (c) C (d) C }
(a) Strings are not equal
(b) Strings are not equal +INF time
(c) Strings are equal
(d) Error

109. #define DIM(array,type) sizeof(array) 110. void main()


{
/ sizeof(type)
char arr[]=‘\0‘;
main() if(printf(―%s\n‖,arr))
{int arr[10]; printf(―Nothing\n‖);
else
printf(―The dimension of the array is printf(―Something‖);
%d‖, DIM(arr, int)); }
} (a) Nothing (b) Something
(c) Nothing\n (d) Error
(a) 12 (b) 20 (c) 10 (d) Error

111. void main() 112. void main(main) {


{ char printf(―%d‖,main = getche(printf (
n[5][6]={―Zero‖,‖One‖,‖Two‖,‖Three ―%d‖ , main+
‖ , ‖Four‖}; =pow(++main/**/,++main)))); }
printf(―%s is %c‖,&n[4][0],n[0][0]); if i/p is 0
}
(a) 1 0 49 (b) 12 48
(a) Four is Z (b) Four is E
(c) Four is R (d) Four is O (c) 14 0 (d) 12 0 48

113. main() 114. Missing elements of partly initialized


{ char s1[]=‖Live‖; arrays are :
char s2[]=‖Life‖; (a) set to 0 (b) set to 1
puts(s1,s2); } (c) not define (d) invalid
(a) LiveLife (b) LifeLive
(c) Live (d) Error

115. void main() 116. void main()


{ {
char *s1=‖Science‖; char s1[]=‖Hello‖;
171

int num=5; char s2[]=‖Welcome‖;


printf(num>10?‖%s‖:‖computer %s‖,s1); char s3[20];
Page

171
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

} s3=s1+s2;
(a) science (b) computer printf(―%s‖,s3);
science }
(c) computer (d) Error (a) HelloWelcome (b) Hello
(c) Welcome (d) Error

117. void main() 118. void main()


{ {
char s1[]=‖Hello‖; int a[10];
char s2[10]; printf(―%d‖,*a+5 - *a+3);
s2=s1; }
printf(―%s‖,s2); (a) 5 (b) 3
} (c) 8 (d) 0
(a) Hello (b) Error
(c) H (d) O

119. void main() 120. void main()


{ {
char a[]=‖hello\0\0\0\0\0‖; int a[4]={3,4,5,6};
char b[]=‖hello\n\n\n\n\n‖; printf(―%d,%d‖,3[a],a[3]);
printf(―%d,%d‖,sizeof(a),strlen(a)); printf(―%d‖,*a);
printf(―%d,%d‖,sizeof(b),strlen(b)); }
} (a) 5 5 3 (b) 6 6 3
(a) 11 5, 11 10 (b) 11 5 (c) 4 4 3 (d) 3 3 3
(c) 11 10 (d) 11 11

121. void main() 122. void main()


{ int a[10]; { int a[][2]={{2},{3}};
printf(―%d‖,((a+9)+(a+1))); printf(―%d‖,a[0][0]);
} printf(―%d‖,a[0][1]);
(a) Invalid pointer arithmetic (b) 10 printf(―%d‖,a[1][0]);
(c) 20089 (d) 0 printf(―%d‖,a[1][1]);
}
(a) 2 0 3 0 (b) 3 0 2 0
(c) 0 0 2 2 (d) 3 3 2 2

123. void main() 124. void main()


{ {
int a[]={15,16,17,18,19}; char a[]={‗7‘,‘8‘,‘9‘,0};
printf(―%d‖,a[2.99999]); char
} b[]={‗9‘,‘8‘,‘7‘,‘\0‘,‘7‘,‘8‘,‘9‘};
172

(a) 18 (b) 19 (c) 16 (d) 17 printf(―%s‖,a);


printf(―%s‖,b);
Page

172
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

}
(a) 789 , 987 (b) 987 , 789
(c) 789 , 789 (d) 987 ,987
125. void main() 126. void main()
{ {
char s[]={{‗H‘},{‗E‘}}; char str[]=‖RAVI‖;
printf(―%s‖,s); int i=0,n=strlen(str);
printf(―%c‖,*s-1); while(n)
printf(―%c‖,*s); { n--;
printf(―%c‖,*s+1); str[i]=str[n]; i++;
printf(―%c‖,*s+2); }
} printf(―%s‖,str); }
(a) H,E,I,J,K (b) HE,G,H,I,J (a) RAVI (b) IVI
(c) HE,J,I,H (d) HE (c) IVAR (d) IVVI

127. void main() 128. void main()


{ {
int a[10]; char str1[]=‖HELLO\r‖;
printf(―%d\n‖,sizeof(a)/sizeof(a[0])); printf(―5%5s5‖,strcat(str1,‖HI‖));}
} (a) HI5LLO (b) HELLOHI
(a) 20 (b) 10 (c) HELLO (d) HILL
(c) 12 (d) 14

129. void main() 130. what is the allowed data type for
{ subscript?
int a[]={0,0x4,4,9}; (a) int (b) float
int I=2; (c) double (d) all
printf(―%d,%d\n‖,a[I],I[a]); }
(a) 4 (b) 04 04
(c) 0x4 0x4 (d) o4 o4

131. void main() 132. void main()


{ {
int a[]={10,20,30,40,50}; int
printf(―%u,%u‖,a+1,&a+1); a[3][4]={1,2,3,4,5,6,7,8,9,10,11};
} printf(―%u,%u‖,a+1,&a+1);
if base address is 1000. }
(a) 1002,1010 (b) 1002,1012 if base address is 1000.
(c) 1000,1008 (d) 1002,10014 (a) 1008,1022 (b) 1008,1024
(c) 1008,1026 (d) 1008,1028
173

133. void main() 134. void main()


{ {
Page

173
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

char s1[]=‖computer‖; int a[20];


char s2[]=‖computer‖; a[0]=10; a[19]=20;
if(s1==s2) printf(―%d,%d‖,*a,*(a+19)+*(a+
printf(―\n\t equal‖); 0));
else }
printf(―\n\tunequal‖); (a) 10 30 (b) 20 30
} (c) 30 30 (d) 20 20
(a) unequal (b) equal
(c) Error (d) Computer

135. void main() 136. void main()


{ {
static int a[]={10,11,12,13,14}; int a[]={0,1,2,3,4};
int i,*var; int *ptr,i;
var=&a[4]-4; for(ptr=a+4;ptr>=a;ptr--)
for(i=0;i<=4;i++) printf(―%d‖,a[ptr-a]);
{ }
printf(―%d‖,var); (a) 0,1,2,3,4 (b) 4,3,2,1,0
var++; (c) 1,2,3,4 (d) 0,1,2,3
}
}
(a) 10 11 12 13 14;
(b) 14 13 12 11 10
(c) 11 12 13
(d) 10 12 13 14
137. void main() 138. void main()
{ {
static char s[]=‖World of computer static char s[]={86,‘V‘,86,86,86};
science‖; char *v;
printf(―%c\n‖,*(&s[9])); int i;
printf(―%s\n‖,s+9); v=s;
} for(i=0;i<=4;i++)
(a) C {
computer if(*v)
(b) C printf(―%c‖,*v);
science var++;
(c) C }}
Computer science (a) V,V,V,V,V (b) V,V,V,V
(d) C (c) V,V,V (d) V,V
World of Computer science
174

139. void main() 140. void main()


{ static char a[]=‖University‖; {
Page

174
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

printf(―%d‖,*(a+strlen(a)));} printf(―i-GateAcademy‖+6);}
(a) 0 (b) 1 (a) cademy (b) Academy
(c) 2 (d) 3 (c) i-Gate (d) Error

141. void main() 142. void main()


{ {
int l; int a[10]={};
char s[]=‖KIDS‖; int I;
for(l=0;s[l];l++); for(I=0;I<=5;I++)
printf(―%d‖,l); printf(―%d‖,a[I]);
} }
(a) 5 (b) 8 (c) 4 (d) 0 (a) 0 (b) 1
(c) Error (d) 0,1,2,3,4,5

143. void main() 144. void main()


{ {
int a[10]; const int I=5;
for(I=0;I<10;I++) int a[I]={1,2,3,4,5};
{ printf(―%d‖,a[3]);
a[I]=++I; }
printf(―%d‖,a[I]); } (a) Error (b) 1,2,3,4,5
} (c) 4 (d) 3
(a) 1,3,5,7,9 (b) 2,4,6,9
(c) 0,2,4,6,8 (d) Garbage

145. #define max 4 146. The array elements are represented by


void main() (a) index value
{ (b) subscripted variable
int a[max]={1,2,3,4}; (c) array name
printf(―%d‖,a[5]); (d) size of an array
}
(a) Garbage (b) 1,2,3,4
(c) 0 (d) 1

147. One-Dimensional array is known as 148. ANSI C recommends a complier to


(a) vector (b) table support at least ______ dimensions of
(c) matrix (d) an array of an array.
array (a) 4 (b) 5 (c) 6 (d) 7

149. Identify the wrong expression given int 150. void main()
a[10]? {int a[3]={1,2,3,4,5};
175

(a) a[-1] (b) a[10] (c) a[0] (d) ++a printf(―%d‖,a[3]);}


(a) Error (b) 1,2,3,4,5
Page

175
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

(c) 3 (d) 0

151. main() 152. #define clrscr() 100


{ main()
char string[]="Hello World"; {
display(string); clrscr();
} printf("%d\n",clrscr());
void display(char *string) }
{printf("%s",string); } (a) 100 (b) Compile
(a) Compile Error (b) Hello Error
(c) Hello World (d) None of the (c) Linking Error (d) 6
above

154. void junk(int *a,int *b) 155. int i,j;


{ for(i=0;i<=10;i++)
*a=*a * *a; {
*b=*b * *b; j+=5;
} assert(i<5);
void main() }
{ (a) 0,5,10 (b) 5,10
int a=2,b=4; (c) 10 (d) Abnormal
junk(&a,&b); Program
printf(―\n\t %d,%d‖,a,b);
}
(a) 2, 4 (b) 2,2
(c) 4,4 (d) 4,16

156. main() 157. main()


{ { int i=_l_abc(10);
main(); printf("%d\n",--i); }
} int _l_abc(int i)
(a) Abnormal program (b) +INF time { return(i++); }
execute (a) 10 (b) 9 (c) 8 (c) none
(c) 0 (d)None

158. void junk(int a,int *b) 159. void main()


{ {
a=a * a; int a=4,b=2;
*b=*b * *b; int p=&a,q=&b;
} int k=*p/*q;
176

void main() printf(―%d‖,k);


{ }
Page

176
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

int a=2,b=4;
junk(a,&b); (A) 2
printf(―\n\t %d,%d‖,a,b); (B) 0
} (C)Compiler error
(a) 2, 16 (b) 2, 2 (D) Runtime error
(c) 4, 4 (d) 4,16

160. abc(); 161. What are the following notations of


main() defining functions known as?
{ i. int abc(int a,float b)
int i; {
i = abc(); /* some code */
printf("%d",i); }
} ii. int abc(a,b)
abc() int a; float b;
{ _AX = 1000; { /* some
} code*/ }
(a) 100 (b) 1000 (a)i.ISO ii. Kernighan & Ritche
(c) Some address will be printed notation
(d) Compile Error (b) i. ANSI C notation
ii. Kernighan & Ritche notation
(c) i. Venugopal
ii. Kernighan & Ritche notation
(d) i. ANSI C notation
ii. ISO

162. abc(char a[]); 163. void main()


main() {
{ int k=ret(sizeof(float));
char a[100]; printf("\n here value is %d",++k);
a[0]='a';a[1]]='b';a[2]='c';a[4]='d'; }
abc(a); int ret(int ret)
} { ret += 2.5; return(ret); }
abc(char a[]) (a) Here value is 7
{ (b) Here value is 6
a++; (c) here value is 7
printf("%c",*a); (d) None of the above
a++;
printf("%c",*a);
}
(a) ad (b) bc (c) ab (d) ac
177
Page

177
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

164. int cat(int); 165. void main()


main() {
{ fun(); fun();
int n;n=cap(6); printf(―%d‖,n); }
} fun()
int cap(int n) { static int i=10;
{ printf(―%d‖,i);
if(n<=1) i++;
return 1; }
else (a) 10 11 (b) 1 1 10
return(cap(n-3)+cap(n-1)); (c) 10 12 (d) 10 10
}
(a) 7 (b) 8 (c) 9 (d) 10

166. void main() 167. int newval(int);


{ void main()
int n=10; {
printf(―%d‖,fun(n)); int ia[]={12,24,45,0};
} int i;
int fun(int n) int sum=0;
{ for(i=0;ia[i];i++)
if(n>0) return(n+fun(n-2)); {
else return 0; sum+=newval(ia[i]);
} }
(a) 28 (b) 30 (c) 22 (d) 31 printf(―sum= %d‖,sum);
}
int newval(int x)
{
static int div=1;
return(x/div++);
}
(a) 39 (b) 40 (c) 38 (d) 37

168. aaa() { printf("hi"); } 169. main() {


bbb() { printf("hello"); } printf(―/n i-GATEMentor‖);
ccc() { printf("TechPreparation.com"); }
main(); }
main() { int (*ptr[3])(); (a) Infinite number of times
ptr[0]=aaa; ptr[1]=bbb;
ptr[2]=ccc; ptr[2](); } (b) Give compile time error
(a) TechPreparation.com (c) Till the stack does not overflow
178

(b) hi TechPreparation.com
(c) hi hi TechPreparation.com (d) Give runtime error
Page

178
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

(d) hello TechPreparation.com

170. main( ) 171. int a=7;


{ char *q; int j; void fun()
for(j=0; j<3; j++) {
scanf(―%s‖ ,(q+j)); ++a;
for (j=0; j<3; j++) printf(―%d‖,a);
printf(―%c‖ ,*(q+j)); }
for (j=0; j<3; j++) void main()
printf(―%s‖ ,(q+j)); } {
If inputs are MOUSE, TRACK and int a=10;
VIRTUAL. printf(―%d‖,a);
(a) MTV, MTVIRTUAL, TVIRTUAL and fun();
VIRTUAL. printf(―%d‖,a);
(b) MTVIRTUAL, MVIRTUAL and }
VIRTUAL. (a) 10 8 8 (b) 10 11 11
(c) MVIRTUAL, TIRTUAL and VIRTUAL. (c) 10 8 10 (d) None of the above
(d) Error

172.The number of tokens in the following C 173. A function with no action


statement (a) is an invalid function
printf(―i=%d;&i=%x‖,I,&i); is (b) produces syntax error
(a) 3 (b) 26 (c) 10 (d) 21 (c) is allowed and is know as dummy
function
(d) return zero

174. The default return data type in function 175. Recursive call results when
definition is : (a) a function calls itself
(a) void (b) int (b) a function1 call another function,
(c) float (d) char which in turn calls the function1
(c) options a and b
(d) a function calls another function

176. Functions are assigned by default 177. Function have


(a) auto storage class (a) file scope (b) local scope
(b) static storage class (c) block scope (d) function scope
(c) extern storage class
(d) register storage class

178. void main() 179. The function ceil(x) defined in


179

{ math.h
printf(―sizeof (void *) = %d \n―, sizeof( void (a) return the value rounded down to
Page

179
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

*)); the next lower integer


printf(―sizeof (int *) = %d \n‖, sizeof(int *)); (b) return the value rounded up to the
printf(―sizeof(double*)=%d\n‖, sizeof(double next lower integer
*)); (c) the next lower value
printf(―sizeof(structunknown*)=%d\n‖, (d) the next higher value
sizeof(struct unknown *)); }
(a) 0, 2 , 4, unknown (b) 0 2 4 4 8
(c) 2 2 2 2 (d) Error

180. The function floor(x) defined in math.h 181. The parameter passing mechanism
(a) return the value rounded down to used in C is
the next lower integer (a) call by reference
(b) return the value rounded up to the (b) call by value
next lower integer (c) option a & b
(c) the next lower value (d) call by name
(d) the next higher value

182. main() 183. All standard C library <math.h>


functions returns what data type?
{
(a) decimal (b) float
int y, s = 2, t = 5; (c) double (d) int
y = fun(s + t);
printf(―%d%d%d‖, s, t, y); } }
int t = 8;
fun(s)
{ s++; t++; return (s + t); } }
(a) 2, 9, 15 (b) 2, 9, 17
(c)3, 5, 15 (d) 2, 5, 17

184. struct A{ 185. void display(int x)


{
if(x!=0)
unsigned a:5; {
display(x/16);
unsigned a:5; putchar(―0123456789ABCDEF‖[x
%16]);
}
unsigned a:5; unsigned a:5;
else putchar(‗\n‘);
180

}
Page

180
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

}v={1,2,3,4}; v occupies What will be o/p if the above function


is called with x=1234?
(a) 1234
(a) 4 word (b) 2 words (b) 4321
(c) 4D2
(c) 1 words (d) None (d) It will not compile

186. void show() 187. main()


{ { int x=10,y,z;
putchar(―GATELEADER‖[2%4]); z=y=x;
} y-=x--;
void main() z-=--x;
{ clrscr(); x-=--x-x--;
show(); printf(x,y,z);}
getch();}
(a) GATE (b) A (a) 6 0 0 (b) 6 0 2
(c) T (d) E (c) 5 0 2 (d) 6 0 1

188. long factorial(long x) 189. if( x ? y : z ) doSomething();


{ Referring to the sample above, which
???? statement correctly identifies when y
return x * factorial(x-1); is evaluated:
} (a) y is evaluated only x= =1.
What would you replace the ???? with, (b) y is evaluated only x>=1
to make the function shown above, (c) y is evaluated only x!=0.
return the correct answer? (d) y is evaluated only x= =0.
(a) if(= =0) return 0;
(b) if(x= =0) return 1;
(c) if(x< =1) return 1;
(d) return 1;

190. void fun(int x) 191. if j=0, k=2 and m=14, consider the
{ expression,
if(x>0) func(--x); N=(k&&m)+(j<k/m)+(j||(!m))+(m/k);
printf(―%d,‖,x);
} What value is stored in N?
int main()
{ fun(5); (a) 0 (b) 8 (c) 7 (d) 9
181

return 0;
}
Page

181
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

(a) 0,1,2,3,4,5 (b) 0,0,1,2,3,4


(c) 1,2,3,4,0.0 (d) 5,4,3,2,1,1

192. int incr(int i) 193. When a function is recursively called


{ all automatic variable are :
static int count =0; (a) Stored in stack
count =count+i; (b) Stored in queue
return(count); (c) Stored in array
} (d) Stored in linked list
main()
{
int i, j;
for(i=0;i<=4;i++)
j=incr(i);
}

(a) 10 (b) 6 (c) 4 (d) 7

194. void main(){ 195 void main() {


int a=sizeof(a); int a[]={1,2,3,4,5},j,*p;
a=modify(a); for(j=0;j<5;j++)
printf("%d",a); {
} printf(―%d‖,*a);
int modify(int x){ a++; }
int y=3; _AX=x+y; return; p=a;
} for(j=0;j<5;j++)
{
(a)2 (b)3 printf(―%d‖,*p); p++; } }
(a) 1 2 3 4 5 1 2 3 4 5
(c)5 (d)Garbage value (b) 1 2 3 4 5
(c) Compile Error
(d) Memory Allocation problem

196. main() 197. int n=-24;


{ main()
int a[]={1,2,3,4,5,6,7}; { printd(n); }
char c[]={‗a‘, ‘x‘ ,‘h‘ ,‘o‘ ,‘k‘}; printd(int n)
printf(―%d %d‖,(&a[3]-&a[0]),(&c[3]- {
&c[0])); if(n<0) { printf(―-‖); n=-n;
} }
if(n%10) printf(―%d‖,n);
182

(a) 12 3 (b) 8 3 else printf(―%d‖,n/10);


(c) 3 3 (d) None printf(―%d‖,n);}
Page

182
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

(a) –24 (b) 24


(c) –2424 (d) –224

198. If yhe binary equivalent of 5.375 in 199. void main()


normalized form is 0100 0000 1010 1100 {
0000 0000 0000 0000, what is the o/p of the int num=1;
following program? while(num<=5)
void main() {
{ printf(―%d‖,num);
float a=5.375; if(num>2)
char *p; goto here;
int I; num++;
p=(char*)&a; }
for(I=0;I<=3;I++) }
printf(―%02x‖,(unsigned char)p[I]); fun()
} {
here:
(a)40AC0000 printf(―PP‖);
(b)00CA0040 }
(c)0000AC40 (a) 5 (b) 5,6
(d)0000CA04 (c) Compile Time Error
(d) None of the above

200. Let A be 2D array declared as follows 201. What does the following algorithm
A: array[1..10][1..15] approximates?(Assume m>1, e>0)
Assuming that each integer takes one x=m;
memory locations the array is stored I y=1
row-major order and the first element of while(x-y> e)
the array is stored at location 100, what is {
the address of the element a[i][j]? x=(x+y)/2;
y=m/x;}
(a) 15i+j+84 (b) 15j+i+84 print(x);
(c) 10i+j+89 (d) 10j+i+89 (a) logm (b) m2 (c) m1/2(d) m1/3

202. float f(float x,float y) Common Data Questions 203 & 204
{
float p,s;int i; Consider the following code
for(s=1,p=1,i=1;i<y;i++) void main(){
{ enum color{
183

P*=x/i; RED,GREEN=-20 , BLUE , YELLOW };


s+=p; enum color x;
Page

183
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

} x=YELLOW;
return s; printf("%d",x);
} }
For large values of y, the return value Q203- What will be output if you will
of the function f best approximates compile and execute the above c code?
(a)Xy (b) ex (c) ln(1+x) (d) Xx
(a) -22 (b) -18 (c) 1 (d)
Compiler error

Q204- What will be output if you will


assign the GREEN=32767?

(a) 0 (b) 1 (c) 32766(d) Compiler error

Answer Key
1-B 2-C 3-D 4-A 5-C 6-C 7-D 8-A 9-A 10-D
11-A 12-B 13-C 14-A 15-C 16-A 17-A 18-A 19-C 20-A
21-A 22-D 23-A 24-C 25-B 26-B 27-D 28-B 29-C 30-B
31-B 32-A 33-D 34-A 35-B 36-A 37-A 38-C 39-B 40-C
41-D 42-B 43-B 44-D 45-B 46-B 47-D 48-B 49-B 50-A
51-B 52-A 53-B 54-A 55-A 56-C 57-A 58-A 59-C 60-B
61-C 62-B 63-A 64-C 65-A 66-A 67-A 68-A 69-D 70-A
71-A 72-C 73-A 74-C 75-B 76-B 77-B 78-B 79-C 80-B
81-B 82-C 83-B 84-B 85-A 86-A 87-B 88-B 89-A 90-B
91-D 92-C 93-D 94-D 95-D 96-D 97-D 98-A 99-D 100-B
101-D 102-C 103-A 104-C 105-B 106-D 107-A 108-B 109-C 110-D
111-A 112-B 113-C 114-A 115-B 116-D 117-B 118-C 119-A 120-B
121-A 122-A 123-D 124-A 125-B 126-D 127-B 128-A 129-D 130-A
131-B 132-B 133-A 134-A 135-A 136-B 137-C 138-A 139-A 140-B
141-C 142-C 143-A 144-C 145-A 146-B 147-A 148-C 149-D 150-A
151-A 152-A 153-B 154-D 155-D 156-D 157-B 158-A 159-C 160-B
161-B 162-B 163-C 164-C 165-A 166-B 167-A 168-A 169-C 170-A
171-C 172-C 173-C 174-B 175-C 176-C 177-A 178-C 179-B 180-A
181-B 182-D 183-C 184-D 185-C 186-D 187-B 188-B 189-C 190-B
191-B 192-A 193-A 194-C 195-C 196-C 197-C 198-C 199-C 200-A
201-C 202-B 203-B 204-D
184
Page

184
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

Test Paper 1:

1. What is the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. int c = 2 ^ 3;
5. printf("%d\n", c);
6. }

2. What is the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. unsigned int a = 10;
5. a = ~a;
6. printf("%d\n", a);
7. }

3. What is the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. if (7 & 8)
5. printf("Honesty");
6. if ((~7 & 0x000f) == 8)
7. printf("is the best policy\n");
8.
9. }

4. What is the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. int a = 2;
5. if (a >> 1)
6. printf("%d\n", a);
185

7. }
Page

185
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

5. Comment on the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i, n, a = 4;
5. scanf("%d", &n);
6. for (i = 0; i < n; i++)
7. a = a * 2;
8. }

6. What is the output of this C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 97;
5. int y = sizeof(x++);
6. printf("x is %d", x);
7. }

7. What is the output of this C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 4, y, z;
5. y = --x;
6. z = x--;
7. printf("%d%d%d", x, y, z);
8.
9. }

8. What is the output of this C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 4;
5. int *p = &x;
6. int *k = p++;
186

7. int r = p - k;
8. printf("%d", r);
Page

186
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

9.
10. }

9. What is the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = -3;
5. int k = i % 2;
6. printf("%d\n", k);
7. }

10. What is the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 3;
5. int l = i / -2;
6. int k = i % -2;
7. printf("%d %d\n", l, k);
8. return 0;
9. }

11. What is the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. int i = 5;
5. i = i / 3;
6. printf("%d\n", i);
7. return 0;
8. }

12. What is the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
187

4. int i = -5;
5. i = i / 3;
Page

187
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

6. printf("%d\n", i);
7. return 0;
8. }

13. What is the value of x in this C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 5 * 9 / 3 + 9;
5. }

14. What is the output of this C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 5.3 % 2;
5. printf("Value of x is %d", x);
6. }

15. What is the output of this C code?

1. #include <stdio.h>
2. void main()
3. {
4. int y = 3;
5. int x = 5 % 2 * 3 / 2;
6. printf("Value of x is %d", x);
7. }

16. What is the output of this C code?

1. #include <stdio.h>
2. void main()
3. {
4. char *s= "hello";
5. char *p = s;
6. printf("%c\t%c", p[0], s[1]);
7. }
188

17. What is the output of this C code?


Page

188
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming

1. #include <stdio.h>
2. void main()
3. {
4. char *s= "hello";
5. char *p = s;
6. printf("%c\t%c", 1[p], s[1]);
7. }

18. What is the output of the code given below?

1. #include <stdio.h>
2. void foo( int[] );
3. int main()
4. {
5. int ary[4] = {1, 2, 3, 4};
6. foo(ary);
7. printf("%d ", ary[0]);
8. }
9. void foo(int p[4])
10. {
11. int i = 10;
12. p = &i;
13. printf("%d ", p[0]);
14. }

19. What is the output of the code given below?

1. #include <stdio.h>
2. int main()
3. {
4. int ary[4] = {1, 2, 3, 4};
5. int *p = ary + 3;
6. printf("%d\n", p[-2]);
7. }

20. What is the output of the code given below?

1. int main()
2. {
3. int ary[4] = {1, 2, 3, 4};
4. int *p = ary + 3;
189

5. printf("%d %d\n", p[-2], ary[*p]);


6. }
Page

189
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.

You might also like