0% found this document useful (0 votes)
289 views

Chapter 5384

Uploaded by

Sweta Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
289 views

Chapter 5384

Uploaded by

Sweta Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

(i)

CRACK JECA
ENTRANCE EXAM BOOK FOR MCA ASPIRANTS
(As per the New Syllabus)

Dreams cannot be true unless you dream it

Dr. Pabitra Pal


Research Associate,
Indian Institute of Technology,
Delhi - 110016, India.

Dr. Partha Chowdhuri


Assistant Professor,
Department of Computer Science,
Vidyasagar University,
Midnapore, West Bengal - 721102, India.

Mr. Prabhash Kumar Singh


Assistant Professor,
Department of Computer Science,
Vidyasagar University,
Midnapore, West Bengal - 721102, India

ISO 9001:2015 CERTIFIED


(ii)
© Authors
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording and/or otherwise without the prior written permission of the publisher.

FIRST EDITION : 2022

Published by : Mrs. Meena Pandey for Himalaya Publishing House Pvt. Ltd.,
“Ramdoot”, Dr. Bhalerao Marg, Girgaon, Mumbai - 400 004.
Phone: 022-23860170, 23863863; Fax: 022-23877178
E-mail: [email protected]; Website: www.himpub.com
Branch Offices :
New Delhi : “Pooja Apartments”, 4-B, Murari Lal Street, Ansari Road, Darya Ganj,
New Delhi - 110 002. Phone: 011-23270392, 23278631; Fax: 011-23256286
Nagpur : Kundanlal Chandak Industrial Estate, Ghat Road, Nagpur - 440 018.
Phone: 0712-2721215, 2721216
Bengaluru : Plot No. 91-33, 2nd Main Road, Seshadripuram, Behind Nataraja Theatre,
Bengaluru - 560 020. Phone: 080-41138821; Mobile: 09379847017, 09379847005
Hyderabad : No. 3-4-184, Lingampally, Besides Raghavendra Swamy Matham, Kachiguda,
Hyderabad - 500 027. Phone: 040-27560041, 27550139
Chennai : No. 34/44, Motilal Street, T. Nagar, Chennai - 600 017. Mobile: 09380460419
Pune : “Laksha” Apartment, First Floor, No. 527, Mehunpura,
Shaniwarpeth (Near Prabhat Theatre), Pune - 411 030.
Phone: 020-24496323, 24496333; Mobile: 09370579333
Cuttack : Plot No. 5F-755/4, Sector-9, CDA Market Nagar, Cuttack - 753 014,
Odisha. Mobile: 09338746007
Kolkata : 3, S.M. Bose Road, Near Gate No. 5, Agarpara Railway Station,
North 24 Parganas, West Bengal - 700109. Mobile: 9674536325
DTP by : Sudhakar Shetty (On behalf of HPH)
Printed at : Geetanjali Press Pvt. Ltd., Nagpur (On behalf of HPH)
(iii)

Dedicated to Our Parents


(iv)
(v)

PREFACE

The field of Computer Science is very vast and so are the syllabi of the MCA entrance
examination. The main aim of this book is to spread the knowledge attained by the authors to the
target aspirants. It caters to the needs of the candidate aspiring for competitive examinations,
especially JECA examination and for the beginners to understand the subject’s intricacies. An
effort has been made to combine all mentioned examination topics under a book. The entire
syllabus is divided into chapters, sections and subsections. A clear and lucid explanation of all the
topics helps understand the subject nicely.
Almost all the competitive examinations are hereby conducted through multiple choice
questions (MCQs). The options provided within the question appear confusing even to the best if
not practiced well. It is intended to enable the students to prioritize and plan their learning
through regular practice. Each chapter is provided with 25 solved questions and 50 unsolved
questions. The questions are formulated in a way that will grow the confidence of the subject
among the aspirants. It will help the students face the competition at any time and any place.
We believe that students seeking compact, significant self-study and up-to-date contents will
find this book to be a valuable resource. Some errors may inevitably appear; we shall be grateful
to the reader for pointing them out and for any recommendations for improving the text.
Authors
(vi)
(vii)

ACKNOWLEDGEMENTS

Writing a book is like a major project and is rarely the work of a single individual. We thank
our teachers and seniors for providing a wealth of knowledge, sharing the depth and breadth of
their experience, and answering a barrage of our questions. Our numerous students, whose
valuable discussions are a key constituent of this book.
During the compilation and writing of this work, we have been fortunate enough to receive
valuable suggestions, timely assistance and humble support from our colleagues and friends.
Special thanks to our mentors Dr. Biswapati Jana, Dr. Jaydeb Bhaumik, Dr. Debasis Giri,
Dr. Kakali Datta, Prof. Jyotsna Kumar Mandal and Dr. Dhiman Saha for extending their support
in our academic growth. They were extremely generous to clear our doubts whenever we required.
We also convey our heartfelt thanks to the faculty members of the Department of Computer
Science, Vidyasagar University for their encouragement and help. We want to thank all the
technical and non-teaching staff of the Department as well for supporting us.
We want to bow down to our first Guru, our parents, who led the foundation by generating a
quest for knowledge in our minds and for making us competent enough to write this book. We
would not be able to get our work done without the continual support, love and blessings of our
beloved family members.
We are grateful to the Himalaya Publishing House Pvt. Ltd., Publishing Editor, for initiating
all of us into this venture and Lalita Ji, Publication Response Division, for the continuous
cooperation, assistance and timely feedback. Without their conscientious effort and personal
involvement, this work would not have been possible.
We would also like to thank the production team for their efforts.
We wish to acknowledge the efforts of our reviewer(s) for providing countless suggestions
for improving the content and presentation.
Thank you!
Pabitra Pal
([email protected])
Partha Chowdhuri
([email protected])
Prabhash Kumar Singh
([email protected])
(viii)
(ix)

CONTENTS
1. C PROGRAMMING 1 – 28
1.1 Introduction
1.2 Keyword and Identifier
1.3 Data Types and Variables
1.4 Input Output
1.4.1 Format Specifier
1.5 Operators and Expressions
1.6 Storage Classes
1.7 Function
1.7.1 Recursive Function
1.8 Loops
1.8.1 For loop
1.8.2 While Loop
1.8.3 Do-while Loop
1.8.4 Break Statement
1.8.5 Continue Statement
1.9 Array
1.9.1 What is an Array?
1.9.2 Declare and Initialize an One-dimensional Array
1.9.3 Access the Elements of One-dimensional Array .
1.9.4 Important Properties of Array
1.9.5 Declare and Initialize a Two-dimensional Array
1.10 String
1.10.1 Declaring a String
1.10.2 Initialize a String
1.10.3 Commonly used String Handling Functions
1.11 Pointer
1.11.1 Define a Pointer Variable
1.11.2 Using Pointers in Function
1.12 Structure and Union
1.12.1 Syntax to Define a Structure and Union
1.12.2 Declaring Structure and Union Variable
1.12.3 Initializing and Using Structure Variables
1.12.4 Size of Structure and Union
(x)

1.13 Command-line Argument


1.14 Pre-processor Directives
1.15 typedef
1.16 File Handling
1.17 Solved Questions
1.18 Practice Question Sets
2. OBJECT ORIENTED PROGRAMMING USING C++ 29 – 70
2.1 Introduction
2.2 Data Types
2.3 Input and Output
2.4 Scope of Variables
2.5 Conditional Statement
2.5.1 If
2.5.2 Switch-case
2.6 Loop
2.6.1 For Loop
2.6.2 While Loop
2.6.3 Do-While Loop
2.6.4 Break and Continue
2.7 Function
2.7.1 Function Declaration and Definition
2.8 Array
2.8.1 Declaring an Array
2.8.2 Accessing and Changing the Elements of an Array
2.9 String
2.9.1 Operations on String
2.9.1.1 String Concatenation
2.9.1.2 String Length
2.9.1.3 Accessing and Changing the Characters of a String
2.10 Encapsulation
2.11 Class
2.11.1 Class Members
2.11.2 Constructors
2.11.3 Overloading Constructors
2.11.4 Object
(xi)

2.11.5 Member Initialization in Constructors


2.11.5.1 Member Initializer List
2.11.6 ‘this’ Keyword
2.11.7 Static Members
2.11.8 Const Member Functions
2.12 Inheritance
2.12.1 Access Control and Inheritance
2.12.2 Type of Inheritance
2.13 Polymorphism
2.13.1 Function Overloading
2.13.2 Operator Overloading
2.13.2.1 Operator Overloading as a Member/Non-member Function
2.13.2.2 Some Operators that Cannot be Overloaded
2.13.2.3 Complex Class with Overloaded Operators
2.13.3 Function Overriding
2.13.3.1 Virtual Function
2.13.3.2 Pure Virtual Function and Abstract Class
2.13.3.3 Virtual Base Class
2.14 Namespace
2.14.1 Defining a Namespace
2.14.2 Use of Namespace
2.14.3 The ‘using directive’ and ‘using declaration’
2.15 Templates
2.16 Friend Function and Friend Class
2.16.1 Friend Function
2.16.2 Friend Class
2.17 Pointers
2.18 Solved Questions
2.19 Practice Question Sets
3. UNIX 71 – 93
3.1 Shell
3.2 Shell Script
3.3 Vi Editor
3.4 Wildcard
3.5 PATH and CLASSPATH
(xii)

3.6 Essential UNIX Commands


3.6.1 echo
3.6.2 cd
3.6.3 pwd
3.6.4 ls
3.6.5 chmod
3.6.6 chown
3.6.7 chgrp
3.6.8 bc
3.6.9 cal
3.6.10 cat
3.6.11 cp
3.6.12 mv
3.6.13 grep
3.6.14 touch
3.6.15 ps
3.6.16 comm
3.6.17 diff
3.6.18 cut
3.6.19 du
3.6.20 env
3.6.21 file
3.6.22 ln
3.6.23 kill
3.6.24 nice
3.6.25 renice
3.6.26 paste
3.6.27 pwd
3.6.28 sleep
3.6.29 sort
3.6.30 time
3.6.31 top
3.6.32 whereis
3.6.33 which
(xiii)

3.7 Solved Questions


3.8 Practice Question Sets
4. DATA STRUCTURE 94 – 120
4.1 Concept of Data Structure
4.1.1 Types of Data Structures
4.1.2 Terms Related to Data Structure
4.2 Stack and Queue
4.2.1 Stack
4.2.2 Operations on Stack
4.2.3 Applications
4.2.4 Queue
4.2.5 Operations on Queue
4.2.6 Types of Queues
4.2.6.1 Circular Queue
4.2.6.2 Priority Queue
4.2.6.3 Deque
4.2.6.4 Applications
4.3 Linked List
4.3.1 Introduction
4.3.2 Why Linked List is Used over Array?
4.3.3 Drawbacks of Linked List
4.3.4 Types of Linked Lists
4.3.4.1 Singly Linked Lists
4.3.4.2 Doubly Linked Lists
4.3.4.3 Circular Linked Lists
4.3.5 Applications
4.3.6 Comparison of Sequential and Linked Data Structures
4.4 Tree
4.4.1 Introduction
4.4.2 Tree Terminologies
4.4.3 Binary Trees
4.4.3.1 Types of Binary Trees
4.4.3.2 Binary Search Tree
4.4.4 Binary Trees Traversals
4.4.4.1 Pre-order Traversal
(xiv)

4.4.4.2 In-order Traversal


4.4.4.3 Post-order Traversal
4.4.4.4 Threaded Binary Trees
4.5 Searching and Sorting
4.5.1 Linear Search
4.5.2 Binary Search
4.5.3 Bubble Sort
4.5.4 Selection Sort
4.5.5 Insertion Sort
4.5.6 Merge Sort
4.5.7 Quicksort
4.6 Graphs
4.6.1 Introduction
4.6.2 Types of Graphs
4.6.3 Graph Representation
4.6.3.1 Adjacency Matrix
4.6.3.2 Incidence Matrix
4.6.3.3 Adjacency List
4.6.3.4 Traversal of a Graph
4.6.3.5 Spanning Trees
4.6.4 Complexity of Data Structures, Sorting and Searching Algorithms
4.7 Solved Questions
4.8 Practice Question Sets
5. INTRODUCTION OF COMPUTERS 121 – 136
5.1 Bus Structure
5.2 Basic I/O
5.2.1 Peripheral Devices
5.2.2 Modes of I/O Data Transfer
5.3 Subroutines
5.4 Interrupt
5.4.1 Types of Interrupt
5.4.2 Interrupt Nesting
5.5 DMA
5.5.1 Types of DMA Transfer
5.5.2 Advantages
(xv)

5.5.3 Disadvantages
5.6 RAM and ROM
5.7 Pipeline
5.7.1 Types of Pipeline
5.7.2 Advantages
5.7.3 Disadvantages
5.8 System Calls
5.8.1 How it Works?
5.8.2 Need for System Calls
5.8.3 Types of System Calls
5.8.4 Some Examples of System Calls
5.9 Solved Questions
5.10 Practice Question Sets
6. OPERATING SYSTEM 137 – 174
6.1 Introduction
6.1.1 Goal of an Operating System
6.1.2 Functions of an Operating System
6.2 Important Terminologies
6.2.1 Kernel
6.2.2 System Call
6.2.3 Batch System
6.2.4 Spooling
6.2.5 Multi-programming
6.2.6 Multi-processing
6.2.7 Multi-tasking
6.2.8 Time-sharing System .
6.3 Process
6.3.1 Process State
6.4 Process Scheduling
6.4.1 Process Scheduler
6.4.2 Types of Scheduler
6.4.3 Context Switching
6.4.4 Scheduling Performance Criteria
6.4.5 Scheduling Algorithms
6.5 Process Synchronization
(xvi)

6.5.1 Critical Section


6.5.2 Critical Section Problem
6.5.3 Solutions to Critical Section Problem
6.5.4 Peterson’s Solution
6.5.5 Semaphore
6.5.5.1 Implementation of Semaphore
6.5.5.2 Types of Semaphore
6.5.5.3 Disadvantages of Semaphore
6.6 Deadlocks
6.6.1 Necessary Conditions for Deadlocks
6.6.2 Deadlock Prevention
6.6.3 Deadlock Avoidance
6.6.3.1 Safe State
6.6.3.2 Resource Allocation Graph Algorithm
6.6.3.3 Banker’s Algorithm
6.6.4 Deadlock Detection and Recovery
6.6.5 Deadlock Ignorance
6.7 Memory Management
6.7.1 Contiguous Memory Management Schemes
6.7.1.1 Partition Allocation Strategy
6.7.2 Non-contiguous Memory Management Schemes
6.7.3 Fragmentation
6.7.3.1 Internal Fragmentation
6.7.3.2 External Fragmentation
6.7.4 Paging
6.7.4.1 Memory Management Unit
6.7.5 Segmentation
6.7.6 Virtual Memory
6.8.6.1 Demand Paging
6.8.6.2 Page Fault
6.8.6.3 Page Replacement Algorithms
6.7.7 Thrashing
6.8 Disk Management
6.8.1 Time Terminology
6.8.2 Disk Scheduling Algorithms
(xvii)

6.8.2.1 FCFS Scheduling Algorithm


6.8.2.2 SSTF Scheduling Algorithm
6.8.2.3 SCAN Scheduling Algorithm
6.8.2.4 C-SCAN Scheduling Algorithm
6.8.2.5 LOOK Scheduling Algorithm
6.8.2.6 C-LOOK Scheduling Algorithm
6.9 Solved Questions
6.10 Practice Question Sets
7. COMPUTER NETWORK 175 – 207
7.1 Introduction
7.2 Concept of Networking
7.2.1 Network Topology
7.2.1.1 Bus Topology
7.2.1.2 Ring Topology
7.2.1.3 Star Topology
7.2.1.4 Mesh Topology
7.2.1.5 Tree Topology
7.3 LAN Technologies
7.4 Ethernet
7.4.1 Token Bus
7.4.2 Token Ring
7.5 ISO/OSI Stack
7.5.1 ISO/OSI Model
7.5.1.1 Layer 1: Physical Layer
7.5.1.2 Layer 2: Data Link Layer
7.5.1.3 Layer 3: Network Layer
7.6 Routing Algorithm
7.7 Network Layer Protocols
7.7.1 Internet Protocol (IPv4)
7.7.1.1 IPv4 Addresses
7.7.2 ICMP
7.8 Transport Layer
7.9 Congestion
7.9.1 Congestion versus Flow Control
7.10 UDP and TCP
(xviii)

7.10.1 User Datagram Protocol


7.10.2 Transmission Control Protocol
7.11 Sockets
7.12 Session Layer
7.13 Presentation Layer
7.14 Application Layer
7.14.1 Internet Control Message Protocol (ICMP)
7.14.2 Domain Name System (DNS)
7.14.3 Simple Mail Transfer Protocol (SMTP)
7.14.4 Post Office Protocol (POP)
7.14.5 File Transfer Protocol (FTP)
7.14.6 Hypertext Transfer Protocol (HTTP)
7.15 Devices
7.16 Network Security
7.16.1 Cryptography
7.16.1.1 Symmetric Cryptography
7.16.1.2 Asymmetric Cryptography
7.16.2 Digital Signature
7.16.3 Firewall
7.17 Solved Questions
7.18 Practice Question Sets
8. DATABASE MANAGEMENT SYSTEM 208 – 236
8.1 Introduction
8.1.1 Traditional File Processing Approach
8.1.2 Database Management System
8.2 Components of Database Systems
8.3 DBMS Architecture
8.3.1 3-tier Architecture
8.3.2 Data Independence
8.3.3 Data Models
8.3.4 Relational Model
8.3.4.1 Constraints in Relational Mode
8.3.4.2 Relational Algebra
8.3.4.3 Tuple Calculus
8.3.5 ER Model
(xix)

8.3.5.1 Entity
8.3.5.2 Attributes
8.3.5.3 Relationships
8.4 Database Design
8.4.1 Integrity Constraints
8.4.2 Normal Forms
8.4.3 Attribute Closure
8.4.4 Key
8.4.4.1 Super Key
8.4.4.2 Candidate Key
8.4.4.3 Primary Key
8.4.5 Decomposition
8.4.5.1 Relation between Two Functional Dependency (FD) Sets
8.5 Transactions and Concurrency Control
8.5.1 Transactions
8.5.2 Schedule
8.5.2.1 Serial schedule
8.5.2.2 Concurrent Schedule
8.6. File Structures
8.6.1 Sequential Files
8.6.2 Indexing
8.6.3 B-tree
8.7 Structured Query Language (SQL)
8.7.1 SQL Commands
8.8 Query Optimization
8.8.1 Purpose of Query Optimization
8.8.2 Various Query Optimization Strategies
8.8.3 Important Points on Optimization
8.9 Solved Questions
8.10 Practice Question Sets
9. SOFTWARE ENGINEERING 237 – 270
9.1 Introduction
9.2 Software
9.2.1 Characteristics of Software
9.2.2 Software Engineering
(xx)

9.2.2.1 Support
9.2.2.2 Software Process .
9.3 Process Models
9.3.1 Conventional Process Model
9.3.1.1 Waterfall Model
9.3.1.2 Prototype Model
9.3.1.3 Rapid Application Development
9.3.2 Evolutionary Process Model
9.3.2.1 Evolutionary Process Model
9.3.2.2 Spiral Model
9.3.2.3 Component-based Development Model
9.4 Measurement of Metrics
9.4.1 Size-oriented Metrics
9.4.1.1 Line of Code
9.4.1.2 Function Point Analysis
9.4.2 Effort and Schedule (Duration) Estimation
9.4.2.1 Constructive Cost Model
9.4.2.2 Defect Rate
9.4.2.3 Defect Removal Efficiency
9.4.2.4 Halstead Size-oriented Metric
9.5 Risk Analysis
9.5.1 Risk Identification
9.5.1.1 Risk Strategy
9.5.1.2 Types of Risk
9.5.1.3 Risk Components
9.5.2 Risk Projection or Risk Estimation
9.6 Software Development Life Cycle
9.6.1 Requirement
9.6.1.1 External Interfaces
9.6.1.2 Requirement Engineering
9.6.1.3 Elicitation
9.6.1.4 Analysis
9.6.2 Design
9.6.2.1 Concept of Design
9.6.2.2 Cohesion
(xxi)

9.6.2.3 Coupling
9.6.2.4 Software Architecture
9.6.2.4.1 Control Hierarchy
9.6.2.4.2 Data Structure
9.6.2.4.3 Software Procedure
9.6.3 Coding
9.6.4 Testing
9.6.4.1 Structural Testing
9.6.4.1.1 Basis Path Testing
9.6.4.1.2 Cyclomatic Complexity
9.6.4.1.3 Graph Matrices
9.6.4.1.4 Control Structure Testing
9.6.4.2 Black-box Testing
9.6.4.3 Life Cycle Testing
9.6.4.4 Validation Testing
9.6.4.5 Integration Testing
9.6.4.5.1 Top-down Integration
9.6.4.5.2 Bottom-up Integration
9.6.4.6 Regression Testing
9.6.4.7 Smoke Testing
9.6.4.8 Unit Testing
9.6.4.9 Alpha Testing
9.6.4.10 Beta Testing
9.6.4.11 System Testing
9.7 Solved Questions
9.8 Practice Question Sets
10. MACHINE LEARNING 271 – 290
10.1 Introduction
10.1.1 What is ML?
10.1.2 ML Life Cycle
10.1.3 ML in Current Scenario
10.2 Classification
10.2.1 Supervised Learning
10.2.1.1 Classification
10.2.1.2 Regression
(xxii)

10.2.2 Unsupervised Learning


10.2.2.1 Clustering
10.2.2.2 Association
10.2.3 Reinforcement Learning
10.3 Decision Tree Learning
10.3.1 Terminologies
10.3.2 Attribute Selection Measures
10.3.3 Pruning
10.4 Artificial Neural Networks
10.4.1 Architecture of an ANN
10.4.2 Types of ANN
10.5 Support Vector Machines
10.5.1 Types of SVM
10.6 Bayesian Learning
10.6.1 Bayes’ Theorem
10.6.2 Types of Naive Bayes’ Model
10.6.3 Applications of Naive Bayes’ Algorithm
10.7 Clustering
10.7.1 Types of Clustering Algorithms
10.7.2 Applications of Clustering
10.8 Hidden Markov Models
10.8.1 Concept
10.8.2 Applications
10.9 Solved Questions
10.10 Practice Question Sets
CHAPTER
1
C PROGRAMMING
PREFACE

SYLLABUS
Variables and Data Types, I/O Operations, Operators and Expressions,
Control Flow Statements, Functions, Array, Pointers, String Handling,
Structures and Unions, Files Handling, Pre-processor Directives,
Command Line Arguments.

1.1 INTRODUCTION
C is a general-purpose, procedural programming language. It was developed at Bell Labs by
Dennis Ritchie, between 1972 and 1973. During the 1980s, C gradually gained popularity. C has
been standardized by the American National Standards Institute (ANSI) since 1989 and
subsequently standardized by the International Organization for Standardization (ISO).
A procedural language is a type of programming language that consists of a series of well
structured steps and procedures within its programming context to write a program. It contains a
systematic order of statements, functions and commands to complete a program. That means, it
consists of a set of instructions and divides these instructions into smaller parts known as
functions for the computer to perform.

1.2 KEYWORD AND IDENTIFIER


The keywords are reserved words. They have some special meanings to the compiler. The C
programming language has 32 such keywords. The keywords are given in the Table 1.1. In C, an
identifier is a name given to a variable, function, etc. Identifiers must be unique in a program.
They are used to identify a particular entity in a program. For example:
int roll_number;
char name[10]
Here, roll_number and name are identifiers. While declaring any identifier, we need to
keep in mind that a valid identifier can have letters, digits and underscores only. The first letter
of an identifier should not be a digit. A keyword cannot be used as an identifier.
2 CRACK JECA
Table 1.1: Keywords in C Programming
auto break case char
const continue default do
double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while

1.3 DATA TYPES AND VARIABLES


In C, data type of a variable determines the type and size of data associated with that
variable. For example
int marks;
Here, marks is a variable of integer (int) type with size 2 bytes or 4 bytes depending on the
platform used. The size of operator is used to know the actual size of any data type in bytes. ‘int’,
‘char’, ‘float’, and ‘double’ are the basic data types in C. The ‘short’ and ‘long’ are quantifiers
that are used to alter the size of char, int or double data types. Whereas, ‘signed’ and ‘unsigned’
are sign qualifiers which are used to specify the signed nature of integer types.

1.4 INPUT OUTPUT


The printf() function is one of the main output functions in the C programming language.
The function is used to send formatted output to the standard output screen.
#include<stdio.h>
int main(){
printf("My First Program");
return 0;
}
A C program must contain the main() function. The execution of the source code begins
from the main() function. Here, printf() is a library function that formats the string inside the
double quotes and sends the formatted string as an output to the screen. The printf() function is
declared in stdio.h file. So, the program should add the stdio.h file in the program using
#include<stdio.h> to successfully run. This is generally added at the beginning of the source
code. That is why it is called a header file. The statement return 0; inside the main() function is
the “exit status” of the program.
C Programming 3

In C programming, scanf() is one of the commonly used functions to take input from the
user. The scanf() function reads formatted input from the standard input such as keyboard.
#include <stdio.h>
int main()
{
int roll;
printf(“Enter a roll number:”);
scanf(“%d”, &roll);
printf("Roll Number=%d", roll);
return 0;
}
In this program, %d is a format specifier which is used in the scanf() function to take the
input (roll) from the keyboard as an integer (int). When an integer is entered through the keyboard,
it is stored in the roll variable.
1.4.1 Format Specifier
To format different data types inside the printf or scanf function, different format specifiers
are used. For example, if we want to print the value of an integer variable using the printf
function, we have to use %d as the format specifier.
int i=10;
printf("The value of i = %d", i);
The format specifiers for other data types are given in the Table 1.2.
Note that, both %d and %i format specifiers can be used for an integer type. Here, %i
specifies integer whereas %d specifies signed decimal integer. For printf function, both these
format specifiers can be used interchangeably. But, in scanf function %d and %i behaves
differently. In scanf, the %d always takes integer value as signed decimal integer. Whereas, the
%i takes an integer value as signed decimal, hexadecimal or octal type. So, the scanf function
can take octal or hexadecimal input from the keyboard using %i only.
Table 1.2: Format Specifiers for Different Data Types
Data Type Format Specifier
int %d, %i
char %c
float %f
double %lf
short int %hd
long int %ld, %li
4 CRACK JECA

unsigned int %u
long long int %lld, %lli
unsigned long int %lu
unsigned long long %llu
int
long double %Lf

1.5 OPERATORS AND EXPRESSIONS


The C programming language supports different types of operators. The operators are used
to perform operations on values and variables. The type of operators and their descriptions are
given in the table below. These operators join different variables, functions and constants together
to form expressions. For an example, consider the expression fun() + x * 10. Here, fun() is a
function which may returns an integer, + and * are operators, x is a variable and 10 is a constant.
Table 1.3 shows a list of operators used in C language.
Table 1.3: Type of Operators
Operator Type Example Description
Arithmetic Operators +, -, *, /, ++, --, % perform common mathematical operations
Assignment Operators =, +=, -=, *=, /= assign a value to a variable
Relational Operators ==, <, >, >=, compare two values
<=, !=
Logical Operators &&, ∥, ! return true/false comparing variables or values
Bitwise Operators &, |, ∧, ˜ perform bitwise operator between variables or
values
Increment/Decrement ++, - - increment or decrement the value of a variable
Operator
Misc Operators &, ?: address of a variable and conditional expression

1.6 STORAGE CLASSES


The storage class of a variable determines whether the variable is stored in memory or CPU
registers. It also determines the scope and initial value of the variable. There are four storage
classes in C: automatic, register, static and external.
(a) Automatic: All variables defined within a function or block by default (or with auto
specification) belong to the automatic storage class. The automatic variables are local
to the block in which they are defined, and get destroyed on exit from the block.
(b) Register: The register specifier declares a variable of the register storage class.
Variables declared as register are local to the block in which they are defined, and get
destroyed on exit from the block. Register variables are placed in CPU registers, not in
memory.
C Programming 5

(c) Static: The static specifier gives the declared variable static storage class. Static
variables can be used within a function. Static variables are not visible outside their
function. But, static variables maintain their values between calls.
(d) External: The extern specifier gives the declared variable external storage class. No
storage is allocated to an extern variable. It is assumed that the variable has already
been defined elsewhere in the program. When we use an extern specifier the variable
cannot be initialized because an extern variable is only declared, not defined.

1.7 FUNCTION
A function is a block of statements that perform a particular task. Every C program has at
least one function which is main() function. We can divide our code into separate functions. A
function declaration tells the compiler about a function’s name, parameters, and return type. A
function definition provides the body of the function.
Every function has four parts: Function Name, Function Parameter, Function Body and
Return Type.
//int: return type
//sum: function name
//int m, int n: function parameters
int sum(int n, int m){
// function body
return m + n;
}

1.7.1 Recursive Function


In the C programming language, a program allows calling a function inside the same
function. The function that calls itself is known as a recursive function and the technique is
known as recursion. Every recursive function has at least one base case and one recursive case.
The condition which returns from the function is called a base case and the condition which calls
the function again is called the recursive case. An example of a recursive function is given below.
int fact(int n)
{
if (n < = 1)
return 1; // base case
else
return n*fact(n-1); // recursive case
}
6 CRACK JECA

1.8 LOOPS
Loops are used to execute a block of code repeatedly according to a condition given in the
loop. There are 3 loops: for loop, while loop and do-while loop.
1.8.1 For loop
The syntax of for loop is:
for (initialization; condition; update){
// code block
}
The initialization statement is executed only once. The condition expression is evaluated
every time before the loop is executed. The update expression is executed at the end of each
iteration. The loop is repeatedly executed until the condition expression is false. For loop is
generally used when the number of iterations is known.
1.8.2 While Loop
The syntax of a while loop is:
while (condition){
//code block
}
The while loop evaluates the condition before each iteration. If the condition is true, i.e.,
non-zero, codes inside the body of the while loop are executed. When the condition is false or
zero, the while loop is terminated.
1.8.3 Do-while Loop
The syntax of a do-while loop is:
do{
// code block
}while(condition);
The code inside the loop is executed once. Then, the condition is evaluated. If the condition
is true or non-zero, the code inside the loop is executed again. When the condition is false or zero,
the do-while loop is terminated.
The do-while loop is similar to the while loop. But there is an important difference. The
body of the do-while loop is executed once before checking the condition. So, unlike while loop,
a do-while loop is executed at least once.
C Programming 7

1.8.4 Break Statement


The break statement terminates the loop immediately when it is encountered. The break
statement is almost always used with some conditional statement inside the loop.
The following loop terminates whenever the number 5 is encountered.
# include <stdio.h>
int main(){
int number;
for(number=1; number <= 10; number++) {
printf("%d", number);
// If the number is 5, the loop terminates
if(number == 5){
break;
}
}
}

1.8.5 Continue Statement


The continue statement skips all the statements declared after it inside the loop and continue
with the next iteration. The following loop skip printf statement whenever the number 5 is
encountered.
#include<stdio.h>
int main(){
int number;
for(number=1; number <= 10; number++) {
// If the number is 5, the loop continue with the next iteration,
// without executing the next printf statement
if(number == 5){
continue;
}
printf(“%d”, number);
}
}

You might also like