A variable in C is a name given to a memory location where data is stored. It allows the programmer to store values that can change during program execution. Each variable has a data type, which decides what kind of data it can hold (like int, float, char, etc.). Variables make programs flexible because they store values and can be used in expressions and calculations.
5Define a string in ‘C’. How do you declare it?
A String is a sequence of characters terminated with a null character '\0'. The C String is stored as an array of characters.
It is declared as:
char string_name[size];
6What is a function in ‘C’?
A function is a set of statements which perform some specific tasks when called. It is the basic building block of a C program that provides modularity and code reusability.
7What is a self-referential structure?
Self-Referential structures are those structures that have one or more pointers which point to the same type of structure, as their member.
8What is a pointer to a function?
Function pointers point to the functions. They are different from the rest of the pointers in the sense that instead of pointing to the data, they point to the code.
9Differentiate between a structure and a union.
Feature
Structure (struct)
Union (union)
Size
Equal to or greater than the sum of all members.
Equal to the size of the largest member.
Storage
Stores distinct values for all members simultaneously.
Stores value for only one member at a time.
10What is file? Define file handling in C.
A file is a collection of data stored on secondary memory. File handling is the process of handling file operations such as create, open, read, write, and close on a file using different functions such as fopen(), fwrite(), fread(), fseek(), and fprintf().
Part B
(5 Marks)
11Describe the different categories of software.
1. System Software
System software directly operates the computer hardware and provides basic functionality to users and other software.
Global Declaration: Global variables and functions.
Main() Function: The entry point of execution.
Sub Programs: User-defined functions.
// Documentation Section/*
* file: sum.c
* author: you
* description: program to find sum.
*/// Link (Preprocessor Section)
#include <stdio.h>
// Definition
#define X 20
// Global Declarationint sum(int y);
// Main() Functionint main(void)
{
int y = 55;
printf("Sum: %d", sum(y));
return 0;
}
// Subprogramint sum(int y)
{
return y + X;
}
13Write a program to check if a string is a palindrome or not without using function.
A palindrome reads the same backwards as forwards (e.g., "MADAM").
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, length = 0, flag = 0;
printf("Enter a string: ");
scanf("%s", str);
// Calculate length manuallywhile (str[length] != '\0') {
length++;
}
// Compare characters from start and endfor (i = 0; i < length / 2; i++) {
if (str[i] != str[length - i - 1]) {
flag = 1; // Flag raised if mismatch foundbreak;
}
}
if (flag == 0) {
printf("%s is a Palindrome\n", str);
} else {
printf("%s is Not a Palindrome\n", str);
}
return 0;
}
14Write a program to create employee payroll using nested structure.
This program demonstrates a structure (Salary) inside another structure (Employee).
#include <stdio.h>
// Child Structure for Salary Detailsstruct Salary {
int basic;
int hra;
int da;
};
// Parent Structure for Employeestruct Employee {
int id;
char name[20];
struct Salary pay; // Nested structure variable
};
int main() {
struct Employee emp;
int gross_salary;
printf("Enter Employee ID: ");
scanf("%d", &emp.id);
printf("Enter Employee Name: ");
scanf("%s", emp.name);
printf("Enter Basic Pay: ");
scanf("%d", &emp.pay.basic); // Accessing nested member
printf("Enter HRA: ");
scanf("%d", &emp.pay.hra);
printf("Enter DA: ");
scanf("%d", &emp.pay.da);
// Calculate Gross Salary
gross_salary = emp.pay.basic + emp.pay.hra + emp.pay.da;
// Display Payroll Info
printf("\n--- Employee Payroll ---\n");
printf("ID: %d\n", emp.id);
printf("Name: %s\n", emp.name);
printf("Gross Salary: %d\n", gross_salary);
return 0;
}
15Discuss the various storage classes in C with an example for each.
Storage Classes
auto: Default for local variables. Scope is within the block.
extern: Declares a global variable defined elsewhere. Visible across files.
static: Preserves value between function calls. Lifetime is entire program execution.
register: Requests storage in CPU register for faster access.
#include <stdio.h>
// Declare an extern variableexternint externVar;
int externVar = 10;
void staticDemo()
{
staticint staticVar = 0;
// Static variable retains its value across function calls
staticVar++;
printf("Static variable value: %d\n", staticVar);
}
int main()
{
// Example of auto storage classautoint autoVar = 5;
// Example of register storage classregisterint registerVar = 7;
staticDemo(); // Prints 1
staticDemo(); // Prints 2return 0;
}
Part C
(10 Marks)
Note: The answers provided below serve as a structured summary. For 10-mark questions, please expand on the context, elaborate on definitions to ensure the answer is comprehensive enough for the full marks.
16Explain the function of various components of a computer with its block diagram.
The computer organization consists of Input, CPU (Control Unit, ALU, Internal Memory), Output, and Storage.
Data Path
Control Path
Input Unit
Keyboard, Mouse
CPU
Control Unit
ALU
Internal Memory
Main Memory
(RAM)
Secondary Storage
HDD / SSD
Output Unit
Monitor, Printer
Functions of Various Components
1. Input Unit
This unit accepts data and instructions from the outside world.
It converts these instructions and data into a form (binary form) that the computer can understand.
It supplies the converted data to the computer system for further processing.
Examples: Keyboards, Mouse, Joystick, OCR, MICR, etc.
2. Central Processing Unit (CPU)
The CPU is the brain of the computer. It performs all calculations, decisions, and controls/coordinates all units. It is subdivided into:
(i) Control Unit:
It instructs the computer how to carry out program instructions.
It directs the flow of data between memory and the Arithmetic Logical Unit (ALU).
It controls the flow of data from the input unit to the storage unit and from the storage unit to the output unit.
It fetches instructions from memory, decodes them, and sets up instruction execution.
(ii) Arithmetic and Logical Unit (ALU):
It performs all arithmetic operations (addition, subtraction, multiplication).
It performs logical operations (comparisons like less than, greater than).
It executes the calculations and takes decisions based on instructions provided.
(iii) Memory Unit:
It stores data temporarily and monitors external requests. It holds data for processing.
Primary Storage (Main Memory/RAM): Used to hold the program currently being executed, data received from the input unit, and intermediate results. It is fast but volatile.
Secondary Storage (Auxiliary Storage): Used to store several programs, documents, and databases permanently. Examples include Hard disks, Floppy disks, and Tapes.
3. Output Unit
Devices used to get the response or result of a process from the computer.
It provides the information and results of a computation to the outside world.
It converts the binary data produced by the computer into a form that users can understand (human-readable form).
Examples: Visual Display Units (VDU/Monitor), Printers, and Plotters.
(Source: 1_Introduction.pdf, Pages 8-11)
17Explain decision making and branching statements with examples.
‘C’ language provides conditional statements to execute code blocks based on whether a condition is true or false.
1. The if Statement
The simplest form. The block executes only if the condition is true.
if(condition)
{
// True statements
}
2. The if...else Statement
Executes one block if true, and a different block if false.
if(i < j) {
big = j;
} else {
big = i;
}
3. Nested if...else Statement
An if...else statement inside another if...else statement.
if(condition1) {
if(condition2) {
// executed if both true
}
}
4. The if...else Ladder
Used to check a series of conditions sequentially.
if (mark > 90) grade='S';
else if (mark > 80) grade='A';
else if (mark > 70) grade='B';
else grade='F';
5. The switch Statement
A multiway branch statement acting as a substitute for long if...else ladders. It dispatches execution based on the value of a variable.
switch(op) {
case'+': c = a + b; break;
case'-': c = a - b; break;
default: printf("Invalid");
}
18Discuss declaration, initialization and representation of array of strings. Write a program to sort an array of integers {67,98,23,54,9}.
Array of Strings
Definition: In C, an array of strings is a two-dimensional array of character types. Each string is terminated with a null character \0.