C AND C++
GUN SHOT IMPORTANT QUESTIONS AND ANSWERS
UNIT-1
Q1. DEFINE C LANGUAGE? EXPLAIN THE FEATURES OF C LANGUAGE?
ANS: C language is a general-purpose, high-level programming language
developed by Dennis Ritchie in 1972 at Bell Labs.
It is mainly used for system programming, application development, and
embedded systems.
FEATURES OF C LANGUAGE
1. Simple and Easy to Learn
C has a simple syntax and uses a small set of keywords.
It is easy for beginners to understand and write programs.
2. Structured Programming Language
C follows a structured approach, where programs are divided into functions
and blocks.
This makes the program easy to read, debug, and maintain.
3. Portable (Machine Independent)
Programs written in C can run on different machines with little or no changes.
This feature makes C highly portable.
4. Efficient and Fast
C programs are fast in execution because they are close to machine-level
language.
It is widely used for performance-critical applications.
5. Rich Library Functions
C provides a large number of built-in functions (like printf, scanf, etc.)
This reduces programming effort.
6. Supports Low-Level Programming
C allows direct memory access using pointers, making it suitable for system
programming like OS and drivers.
7. Modular Programming
Programs can be divided into small functions (modules).
This improves code reusability and reduces complexity.
8. Extensible Language
C allows adding new functions and features as required by the programmer.
Q2. EXPLAIN THE BASIC STRUCTURE OF A C PROGRAM?
ANS: Basic Structure of a C Program
A C program is organized into different sections to make it clear, readable, and
easy to maintain. The standard structure consists of the following parts:
1. Documentation Section
This section contains comments that describe the program. It may include the
program name, author, purpose, and date.
Comments can be written as:
• Single line: // comment
• Multi-line: /* comment */
2. Link (Preprocessor) Section
This section includes header files required for the program. It allows the use of
standard library functions.
Example:
#include <stdio.h> → used for input and output functions.
3. Definition Section
In this section, symbolic constants are defined using #define. It improves
readability and avoids repeated values.
Example:
#define PI 3.14159
4. Global Declaration Section
This section declares global variables and function prototypes that can be
accessed throughout the program.
These variables are declared outside all functions.
Example:
int globalVar;
5. Main Function Section
This is the most important part of a C program. Execution starts from main().
It has two parts:
(i) Variable Declaration
Local variables required for the program are declared.
Example: int a, b;
(ii) Executable Statements
Instructions or logic of the program are written here.
Example: input, calculations, output.
6. Subprogram (User-defined Functions) Section
This section contains user-defined functions written outside the main()
function.
These functions help in modular programming and code reuse.
PROGRAM:
#include <stdio.h> // Link section
int main() // Main function
{
int a = 5, b = 3, sum; // Variable declaration
sum = a + b; // Executable statement
printf("Sum = %d", sum); // Output
return 0;
}
Q3. WHAT IS A DATA TYPE? EXPLAIN THE VARIOUS DATA TYPES IN C
LANGUAGE?
ANS: A data type is used to tell the computer what type of value a variable
will store.
It also decides how much memory is needed and what operations can be
done on that data.
Example:
int a = 10; → int means storing a whole number
CLASSIFICATION OF DATA TYPES IN C LANGUAGE
C data types are broadly classified into four categories:
BASIC (PRIMARY) DATA TYPES
These are the fundamental data types used to store simple values.
• int
Used to store whole numbers (positive or negative).
Example: int a = 100;
• float
Used to store decimal (fractional) numbers with single precision.
Example: float x = 5.75;
• double
Used to store large decimal numbers with double precision.
Example: double y = 123.456789;
• char
Used to store a single character.
Example: char ch = 'A';
2. DERIVED DATA TYPES
These are formed using basic data types.
• Array
Collection of elements of the same data type stored in contiguous
memory locations.
Example: int arr[5];
• Pointer
A variable that stores the memory address of another variable.
Example: int *p;
• Function
A block of code designed to perform a specific task.
Example: int sum(int a, int b);
3. USER-DEFINED DATA TYPES
These are created by the programmer to handle complex data.
• Structure (struct)
Groups variables of different data types under one name.
Example:
• Union
Similar to structure, but all members share the same memory location.
• Enumeration (enum)
Used to assign names to a set of integer constants.
Example:
enum day {Mon, Tue, Wed};
SIMPLE PROGRAM
#include <stdio.h>
int main()
{
int a = 10;
float b = 5.5;
char c = 'A';
printf("%d %f %c", a, b, c);
return 0;
}
Q4. EXPLAIN ABOUT OPERATORS IN C LANGAUGE
ANS: Definition
An operator in C language is a special symbol that is used to perform a specific
operation on one or more operands (variables or values).
Operators help in performing calculations, making comparisons, assigning
values, and controlling program logic.
TYPES OF OPERATORS IN C LANGUAGE
1. Arithmetic Operators
These operators are used to perform mathematical calculations like addition,
subtraction, multiplication, etc.
• + → Addition
• - → Subtraction
• * → Multiplication
2. Relational Operators
These operators are used to compare two values.
The result is always true (1) or false (0).
• == → Equal to
• != → Not equal to
• > → Greater than
• < → Less than
3. Logical Operators
These operators are used to combine multiple conditions and make decisions.
• && → Logical AND (true if both conditions are true)
• || → Logical OR (true if any one condition is true)
• ! → Logical NOT (reverses the result)
4. Assignment Operators
These operators are used to assign values to variables.
They can also combine arithmetic operations with assignment.
5. Increment and Decrement Operators
These operators are used to increase or decrease the value of a variable by 1.
• ++ → Increment
• -- → Decrement
6. Bitwise Operators
These operators work on binary (bit-level) values of data.
• & → AND
• | → OR
• ^ → XOR
• ~ → NOT
7. Conditional (Ternary) Operator
This operator is a short form of if-else used to make decisions.
8. Special Operators
These operators perform special tasks in C.
• sizeof → Finds size of variable
• & → Address of variable
UNIT-2
Q1. EXPLAIN IN DETAIL ABOUT CONDITIONAL STATEMENTS IN C LANGUAGE
ANS: Definition
Conditional statements are used to make decisions in a program.
They execute different blocks of code based on whether a condition is true or
false.
TYPES OF CONDITIONAL STATEMENTS IN C
1. if Statement
The if statement is used to execute a block of code only when the condition is
true.
Syntax:
if (condition)
{
// statements
}
Example:
int a = 10;
if (a > 5)
{
printf("a is greater than 5");
}
2. if-else Statement
This statement is used when there are two possible conditions.
If the condition is true, one block executes; otherwise, another block executes.
Syntax:
if (condition)
{
// true block
}
else
{
// false block
}
Example:
int a = 10;
if (a > 5)
{
printf("Greater");
}
else
{
printf("Smaller");
}
3. else-if Ladder
Used when there are multiple conditions to check.
The program checks conditions one by one and executes the first true block.
Syntax:
if (condition1)
{
// block 1
}
else if (condition2)
{
// block 2
}
else
{
// default block
}
Example:
int marks = 75;
if (marks >= 90)
printf("Grade A");
else if (marks >= 70)
printf("Grade B");
else
printf("Grade C");
4. Nested if Statement
An if statement inside another if statement is called nested if.
Used when conditions depend on previous conditions.
Example:
int a = 10;
if (a > 0)
{
if (a % 2 == 0)
{
printf("Positive Even");
}
}
5. switch Statement (Decision Control)
It is used when a variable is compared with multiple constant values.
It is an alternative to multiple else-if statements.
Syntax:
switch (expression)
{
case value1:
// statements
break;
case value2:
// statements
break;
default:
// statements
}
Example:
int day = 2;
switch (day)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
default:
printf("Invalid");
}
Q2. EXPLAIN IN DETAIL ABOUT LOOPING STATEMENTS.
ANS: Definition
Looping statements are used to execute a block of code repeatedly as long as
a given condition is true.
They help avoid writing the same code multiple times.
TYPES OF LOOPING STATEMENTS IN C
1. for Loop
The for loop is used when the number of iterations is known in advance.
Syntax:
for (initialization; condition; increment/decrement)
{
// statements
}
Working:
• Initialization → executed once
• Condition → checked before each iteration
• Increment/Decrement → updates the variable
Example:
int i;
for (i = 1; i <= 5; i++)
{
printf("%d ", i);
}
2. while Loop
The while loop is used when the number of iterations is not fixed.
The condition is checked before executing the loop.
Syntax:
while (condition)
{
// statements
}
Example:
int i = 1;
while (i <= 5)
{
printf("%d ", i);
i++;
}
3. do-while Loop
The do-while loop executes the block of code at least once, even if the
condition is false.
Syntax:
do
{
// statements
} while (condition);
Example:
int i = 1;
do
{
printf("%d ", i);
i++;
} while (i <= 5);
UNIT-3
Q1. WHAT IS A FUNCTION. EXPLAIN ABOUT VARIOUS TYPES OF FUNCTIONS IN
C LANGUAGE?
ANS: A function in C is a self-contained block of code that performs a specific
task.
It helps in breaking a large program into smaller, manageable parts, making
the program easier to read, test, and maintain.
A function is executed when it is called, and it may take input (arguments) and
return a value.
TYPES OF FUNCTIONS IN C LANGUAGE
Functions in C are mainly classified into two categories:
1. Library Functions (Built-in Functions)
These are predefined functions provided by C standard libraries.
They save time and effort as we do not need to write code for common tasks.
Examples:
• printf() → output
• scanf() → input
• strlen() → string length
• sqrt() → square root
2. User-defined Functions
These are functions created by the programmer to perform specific tasks.
Example:
int add(int a, int b)
{
return a + b;
}
CLASSIFICATION BASED ON ARGUMENTS AND RETURN VALUE
User-defined functions in C can be classified into four types based on whether
they take arguments (inputs) and whether they return a value.
1. Function with No Arguments and No Return Value
This type of function does not take any input and does not return any value.
It simply performs a task when called.
2. Function with Arguments but No Return Value
This function accepts input values (arguments) but does not return any value.
It processes the data and directly displays the result.
3. Function with No Arguments but Return Value
This function does not take input, but it returns a value to the calling function.
4. Function with Arguments and Return Value
This is the most commonly used type.
It takes input values and returns a result.
Q2. BRIEFLY EXPLAIN ABOUT FORMATTED AND UNFORMATTED FUNCTIONS
WITH EXAMPLES
ANS: Formatted Functions
Definition
Formatted functions are used for input and output with a specific format.
They allow us to control how data is displayed or read using format specifiers
like %d, %f, %c, etc.
Common Formatted Functions
• printf() → formatted output
• scanf() → formatted input
EXAMPLE PROGRAM: WRITE UNIT-1 PROGRAM
Unformatted Functions
Definition
Unformatted functions are used for input and output without any format.
They directly read or display data, usually for characters or strings.
Common Unformatted Functions
• getchar() → read a single character
• putchar() → display a single character
• gets() → read string (deprecated but used in exams)
• puts() → display string
Example:
#include <stdio.h>
int main()
{
char ch;
ch = getchar(); // input character
putchar(ch); // output character
return 0;
}
Q3. WHAT IS A STRING? EXPLAIN ABOUT STRING CLASSIFICATION?
ANS: Definition
A string in C is a sequence of characters treated as a single data item and
terminated by a special null character ('\0').
Strings are not a separate data type in C, but are implemented as arrays of
characters.
String Initialization by Individual Characters
In this method, each character is assigned separately, and the null character
must be added manually.
Example:
char sname[5];
sname[0] = 'R';
sname[1] = 'O';
sname[2] = 'B';
sname[3] = 'O';
sname[4] = '\0';
2. String Initialization using String Constant
In this method, the string is assigned using double quotes.
The compiler automatically adds the null character.
Example:
char sname[10] = "Robo";
3. String Declaration + Initialization (Combined)
Declaration and initialization can be done in a single step.
Example:
char name[] = "Hello";
SIMPLE PROGRAM USING STRING
#include <stdio.h>
int main()
{
char name[10] = "Robo";
printf("Name = %s", name);
return 0;
}
Q4. EXPLAIN IN DETAIL ABOUT STRING FUNCTION IN C LANGUAGE?
ANS: Definition
String functions in C are built-in functions provided by the <string.h> library
used to perform operations on strings such as length calculation, copying,
concatenation, comparison, and searching.
1. strlen()
This function is used to calculate the number of characters in a string,
excluding the null character (\0).
It returns an integer value representing the length.
Syntax:
strlen(string);
2. strcpy()
This function is used to copy the contents of one string (source) into another
string (destination), including the null character.
Syntax:
strcpy(destination, source);
3. strcat()
This function is used to Join one string to the end of another string.
Syntax:
strcat(destination, source);
4. strcmp()
This function is used to compare two strings.
Syntax:
strcmp(str1, str2);
5. strrev() – Reverse a String
This function is used to reverse the given string.
Syntax:
strrev(string);
Example program
#include <stdio.h>
#include <string.h>
int main()
{
char s1[20] = "Hi";
char s2[] = "All";
// Length of string
printf("Length = %d\n", strlen(s1));
// Join two strings
strcat(s1, s2);
printf("After join = %s\n", s1);
return 0;
}
Unit-4
Q1. Explain the difference between structure and union?
Ans: Definition
Structure:
A structure is a user-defined data type in C that allows grouping of variables of
different data types under a single name. Each member of a structure is
stored in a separate memory location, so all members can hold values
simultaneously.
Union:
A union is also a user-defined data type that groups variables of different data
types, but all members share the same memory location. At any time, only
one member can store a value, and updating one member affects the others.
KEY DIFFERENCES
Basis Structure Union
Memory Separate memory is allocated Same memory is shared by
Allocation for each member all members
Total size = sum of sizes of all Total size = size of largest
Total Size
members (plus padding) member
Storage of All members can store values Only one member can store
Values at the same time value at a time
Effect of Changing one member does Changing one member
Change not affect others affects all other members
Accessing All members can be accessed Only the last assigned
Members anytime member should be accessed
Memory Usage Requires more memory Saves memory (efficient)
Multiple members can be Only one member can be
Initialization
initialized initialized at a time
When all data is required When only one data item is
Use Case
together needed at a time
Keyword Used Struct union
EXAMPLE OF STRUCTURE
#include <stdio.h>
struct student
{
int id;
float marks;
};
int main()
{
struct student s1;
[Link] = 1;
[Link] = 90.5;
printf("ID = %d, Marks = %.2f", [Link], [Link]);
return 0;
}
Explanation:
• id and marks both have separate memory
• Both values are stored and used together
EXAMPLE OF UNION
#include <stdio.h>
union student
{
int id;
float marks;
};
int main()
{
union student s1;
[Link] = 1;
[Link] = 90.5;
printf("Marks = %.2f", [Link]);
return 0;
}
Explanation:
• id and marks share same memory
• When marks is assigned, id value is overwritten
Q2. Write about enumerated data type in c language?
Ans: Definition
An enumerated data type (enum) in C is a user-defined data type used to
assign names to a set of integer constants.
It improves readability and clarity of the program by using meaningful names
instead of numbers.
Syntax
enum enum_name
{
value1, value2, value3, ...
};
program:
#include <stdio.h>
enum color {Red, Green, Blue};
int main()
{
enum color c = Green;
printf("%d", c); // Output: 1
return 0;
}
Unit-5
Q1. Distinguish between c and c++ language?
Ans:
basis C Language C++ Language
C is a procedural language C++ is an object-oriented
Programming
where programs are written language that uses classes and
Type
using functions step by step. objects along with functions.
Follows a top-down
Follows a bottom-up
approach, breaking the
Approach approach, building programs
program into smaller
using objects.
functions.
Focus is on data and objects,
Focus is mainly on functions
Focus giving importance to data
and procedures.
security.
Provides less security, as Provides better security using
Data Security
data can be accessed easily. encapsulation (data hiding).
Classes & Does not support classes and Supports classes and objects
Objects objects. as core features.
Supported, allowing reuse of
Not supported, so code
Inheritance code from one class to
reuse is limited.
another.
Available, allowing same
Polymorphism Not available.
function to behave differently.
Supported (same function
Function
Not supported. name with different
Overloading
parameters).
Input/Output Uses printf() and scanf(). Uses cin and cout.
Memory
Uses malloc() and free(). Uses new and delete.
Management
basis C Language C++ Language
Code
Limited. High due to OOP features.
Reusability
C++ Program
#include <iostream>
using namespace std;
int main()
{
cout << "Hello C++";
return 0;
}
Q2. Write briefly about Object-Oriented Programming Languages
Ans: Object-Oriented Programming (OOP) is a programming method where
programs are designed using objects and classes instead of only functions.
Key Features of OOP
1. Encapsulation
It means data is hidden and can be accessed only through methods.
It improves security.
2. Polymorphism
It allows one function or object to behave in different ways.
It makes programs flexible.
3. Inheritance
It allows one class to use properties of another class.
It helps in code reuse.
4. Abstraction
It hides unnecessary details and shows only important features.
It makes programs simple.
Uses of OOP Languages
Languages like C++, Java, Python use OOP.
They are used for:
• Software development
• Web applications
• Large systems
Q3. Explain about storage classes?
Ans: Definition
Storage classes in C++ define how a variable is stored, where it is stored, its
scope (visibility), and lifetime (how long it exists) in a program.
TYPES OF STORAGE CLASSES
1. Automatic (auto)
• It is the default storage class for local variables
• Stored in memory (stack)
• Scope is within the block/function only
• Lifetime is only during function execution
Example:
void fun()
{
auto int x = 10;
printf("%d", x);
}
2. Register (register)
• Variable is stored in CPU register for faster access
• Used for frequently used variables (like loop counters)
• Scope and lifetime are same as local variables
Example:
void fun()
{
register int x = 10;
printf("%d", x);
}
3. Static (static)
• Value of variable is retained between function calls
• Initialized only once
• Lifetime is entire program execution
• Scope depends (local/global)
Example:
void fun()
{
static int x = 10;
printf("%d\n", x);
x++;
}
4. External (extern)
• Used to declare global variables
• Variable is defined outside all functions
• Can be accessed by any function
• Lifetime is entire program
Example:
extern int x;
int main()
{
printf("%d", x);
}