C Exam
C Exam
// Input/Output Statements
printf("Enter a number: ");
scanf("%d", &num);
// Processing
printf("You entered: %d", num);
---
3. Data Types in C
---
4. Operators and Expressions
C supports various operators:
1. Arithmetic Operators: + - * / %
2. Relational Operators: > < >= <= == !=
3. Logical Operators: && || !
4. Assignment Operators: = += -= *= /=
5. Increment/Decrement: ++ --
Example:
int a = 5, b = 10;
int sum = a + b; // Expression: sum = 15
---
5. Assignment Statement
Used to store values in variables:
int x = 10;
float y = 3.14;
char ch = 'A';
---
6. Conditional Statements
Used to make decisions:
(i) If-Else Statement
if (x > 0) {
printf("Positive number");
} else {
printf("Negative number");
}
(ii) Switch Statement (Multi-Branching)
int day = 3;
switch (day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
case 3: printf("Wednesday"); break;
default: printf("Invalid day");
}
---
7. Looping Statements
Loops are used for repeated execution.
(i) For Loop
for(int i = 1; i <= 5; i++) {
printf("%d ", i);
}
(ii) While Loop
int i = 1;
while(i <= 5) {
printf("%d ", i);
i++;
}
(iii) Do-While Loop
int i = 1;
do {
printf("%d ", i);
i++;
} while(i <= 5);
---
8. Nested Looping Statements
Loops inside loops:
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 3; j++) {
printf("* ");
}
printf("\n");
}
Output:
***
***
***
---
9. Break and Continue Statements
Example of break
for(int i = 1; i <= 5; i++) {
if(i == 3) break;
printf("%d ", i);
}
Output: 1 2
Example of continue
for(int i = 1; i <= 5; i++) {
if(i == 3) continue;
printf("%d ", i);
}
Output: 1 2 4 5
---
10. Unconditional Branching (goto Statement)
Used to jump to a specific part of the program.
#include <stdio.h>
int main() {
int num = 5;
goto skip; // Jump to label
printf("This will not be printed!");
skip:
printf("Jumped to skip label");
return 0;
}
Output: Jumped to skip label
---
Summary
Character Set: Letters, digits, special symbols
Structure of C Program: Header, main function, input/output
Data Types: int, float, double, char
Operators & Expressions: Arithmetic, logical, assignment
Conditional Statements: If-else, switch
Loops: For, while, do-while, nested loops
Break vs Continue: Exit loop vs skip iteration
Goto Statement: Unconditional jump
This concise explanation covers all the key concepts for a 10-mark answer. Let me know if you
need more details!
Module III (Functions) [10 Marks]
1. Functions in C
A function is a block of code that performs a specific task. It helps in code reusability,
modularity, and easier debugging.
Defining and Accessing Functions
A function in C is defined as:
return_type function_name(parameters) {
// Function body
return value; // If applicable
}
To call (access) a function:
function_name(arguments);
Example: Function to Add Two Numbers
#include <stdio.h>
// Function definition
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 3); // Function call
printf("Sum: %d", result);
return 0;
}
Output: Sum: 8
---
2. Passing Arguments to Functions
There are two ways to pass arguments:
(i) Call by Value
A copy of the variable is passed to the function.
void modify(int x) {
x = x + 10;
}
int main() {
int num = 5;
modify(num);
printf("%d", num); // Output: 5 (original value unchanged)
return 0;
}
(ii) Call by Reference
The actual variable is passed using pointers.
void modify(int *x) {
*x = *x + 10;
}
int main() {
int num = 5;
modify(&num);
printf("%d", num); // Output: 15 (value changed)
return 0;
}
---
3. Function Prototypes
A function prototype declares a function before its definition. It helps the compiler check
function calls for correctness.
int add(int, int); // Function prototype
int main() {
int sum = add(4, 6);
printf("%d", sum);
return 0;
}
int add(int a, int b) { // Function definition
return a + b;
}
---
4. Categories of Functions
Functions in C are categorized based on arguments and return type:
---
5. Nesting of Functions
A function can call another function inside it.
#include <stdio.h>
void greet() {
printf("Hello! ");
}
void welcome() {
greet(); // Calling another function
printf("Welcome to C programming.");
}
int main() {
welcome();
return 0;
}
Output: Hello! Welcome to C programming.
---
6. Recursion
A function that calls itself is called a recursive function.
Example: Factorial using Recursion
#include <stdio.h>
int factorial(int n) {
if(n == 0)
return 1;
return n * factorial(n - 1); // Recursive call
}
int main() {
printf("Factorial of 5: %d", factorial(5));
return 0;
}
Output: Factorial of 5: 120
---
7. Use of Library Functions
C has built-in library functions in header files like <stdio.h>, <math.h>, <string.h>, etc.
Example:
#include <math.h>
printf("Square root of 16: %.2f", sqrt(16));
Output: Square root of 16: 4.00
---
8. Scope, Visibility, and Lifetime of Variables
Variables in C have different scopes and lifetimes:
(i) Local Variables
Declared inside a function/block.
Accessible only inside that function.
void func() {
int x = 10; // Local variable
printf("%d", x);
}
(ii) Global Variables
Declared outside functions.
Accessible throughout the program.
int x = 10; // Global variable
void func() {
printf("%d", x);
}
(iii) Static Variables
Retains value between function calls.
void count() {
static int num = 0;
num++;
printf("%d ", num);
}
int main() {
count();
count();
count();
return 0;
}
Output: 1 2 3
(iv) Automatic Variables (auto)
Default for local variables.
void func() {
auto int x = 5; // Local variable
}
(v) Register Variables (register)
Stored in CPU registers for fast access.
register int count = 10;
---
Summary
Functions: Reusable blocks of code.
Function Calls: function_name(arguments);
Argument Passing: Call by value & reference.
Function Categories: With/without arguments & return value.
Nesting of Functions: One function calling another.
Recursion: A function calling itself.
Library Functions: Predefined functions (printf(), sqrt(), etc.).
Variable Scope & Lifetime: Local, global, static, auto, register.
---
Module IV (Arrays & Structures) [10 Marks]
This module covers arrays, strings, structures, and unions in C programming.
---
1. Arrays in C
An array is a collection of elements of the same data type stored in contiguous memory
locations.
(i) Declaration and Initialization of One-Dimensional Array
int arr[5] = {10, 20, 30, 40, 50}; // Initialization
Accessing elements:
printf("%d", arr[2]); // Output: 30
(ii) Two-Dimensional Array
A 2D array is used to store data in a matrix format.
int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
Accessing elements:
printf("%d", matrix[1][2]); // Output: 6
(iii) Multi-Dimensional Array
An array with more than two dimensions:
int arr[2][2][2] = { {{1,2}, {3,4}}, {{5,6}, {7,8}} };
Accessing elements:
printf("%d", arr[1][0][1]); // Output: 6
(iv) Dynamic Arrays
Dynamic memory allocation allows creating arrays at runtime using malloc() and calloc().
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
arr = (int*) malloc(5 * sizeof(int)); // Allocating memory for 5 integers
if (arr == NULL) {
printf("Memory allocation failed");
return 1;
}
for (int i = 0; i < 5; i++) {
arr[i] = i + 1;
}
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
free(arr); // Free memory
return 0;
}
Output: 1 2 3 4 5
---
2. Character Arrays & Strings
A string is an array of characters ending with \0 (null character).
(i) Declaring and Initializing Strings
char str1[] = "Hello"; // String initialization
char str2[10] = {'H', 'e', 'l', 'l', 'o', '\0'};
(ii) String Input & Output
char name[20];
scanf("%s", name); // Input (stops at space)
printf("%s", name); // Output
(iii) String Library Functions (string.h)
#include <string.h>
char str1[] = "Hello";
char str2[] = "World";
strcat(str1, str2); // Concatenation
strlen(str1); // Length
strcpy(str1, str2); // Copy
strcmp(str1, str2); // Compare
---
3. Structure in C
A structure groups different types of data under one name.
(i) Defining & Initializing a Structure
struct Student {
int roll;
char name[50];
float marks;
};
struct Student s1 = {101, "John", 85.5}; // Initialization
(ii) Accessing Structure Members
printf("%d %s %.2f", s1.roll, s1.name, s1.marks);
---
4. Operations on Individual Members
Each structure member can be accessed and modified separately.
s1.marks = 90.5; // Modify structure member
---
5. Arrays of Structures
An array of structures stores multiple records.
struct Student students[2] = {
{101, "Alice", 88.5},
{102, "Bob", 76.0}
};
printf("%s", students[0].name); // Output: Alice
---
6. Arrays within Structures
A structure can contain an array as a member.
struct Student {
char name[50];
int marks[3];
};
struct Student s1 = {"John", {85, 90, 80}};
printf("%d", s1.marks[1]); // Output: 90
---
7. Structure and Functions
(i) Passing Structure to a Function
void display(struct Student s) {
printf("%s %d", s.name, s.roll);
}
int main() {
struct Student s1 = {101, "Alice", 90};
display(s1);
return 0;
}
---
8. Union in C
A union is similar to a structure but shares memory among members.
(i) Defining & Using a Union
union Data {
int i;
float f;
char str[20];
};
union Data data;
data.i = 10;
printf("%d", data.i);
Difference Between Structure & Union
---
Summary
Arrays: 1D, 2D, Multi-dimensional, Dynamic allocation.
Strings: Character arrays, string.h functions.
Structures: Used for grouping different data types.
Operations on Structures: Accessing members, modifying values.
Arrays in Structures: Storing multiple records.
Structures & Functions: Passing structures as arguments.
Unions: Memory-efficient structures where members share memory.
This concise explanation covers all key points for a 10-mark answer in C programming. Let me
know if you need further clarification!
Module V (Pointers & File Handling) [10 Marks]
module covers pointers and file handling in C programming.
1. Pointers in C
A pointer is a variable that stores the address of another variable.
(i) Declaration & Initialization of Pointer Variables
int a = 10;
int *ptr = &a; // Pointer storing address of a
(ii) Accessing Pointer Variables
& → Address-of operator (gets memory address)
* → Dereference operator (access value at address)
printf("%p", &a); // Prints memory address
printf("%p", ptr); // Prints address stored in ptr
printf("%d", *ptr); // Dereferences pointer (Output: 10)
---
2. Passing Pointers to Functions
Pointers allow functions to modify actual variables.
(i) Call by Value (No Modification)
void modify(int x) {
x = x + 10;
}
int main() {
int num = 5;
modify(num);
printf("%d", num); // Output: 5 (Unchanged)
return 0;
}
(ii) Call by Reference (Modification)
void modify(int *x) {
*x = *x + 10;
}
int main() {
int num = 5;
modify(&num);
printf("%d", num); // Output: 15 (Value changed)
return 0;
}
---
3. Pointer Operations
(i) Pointer Arithmetic
Example:
int arr[] = {10, 20, 30};
int *ptr = arr;
printf("%d", *(ptr + 1)); // Output: 20
---
4. Pointers and Arrays
A pointer can be used to access an array.
int arr[] = {10, 20, 30};
int *ptr = arr;
for (int i = 0; i < 3; i++) {
printf("%d ", *(ptr + i));
}
Output: 10 20 30
---
5. Array of Pointers
An array storing multiple pointers.
int a = 10, b = 20;
int *arr[2] = {&a, &b};
printf("%d", *arr[1]); // Output: 20
---
6. Pointer to Function
A function pointer stores the address of a function.
#include <stdio.h>
void greet() {
printf("Hello, World!");
}
int main() {
void (*func_ptr)(); // Function pointer
func_ptr = greet; // Assign function address
func_ptr(); // Call function
return 0;
}
Output: Hello, World!
---
7. File Handling in C
Files store data permanently.
(i) Opening a File
FILE *file = fopen("data.txt", "w");
---
(ii) Closing a File
fclose(file);
---
(iii) Creating & Writing to a File
FILE *file = fopen("data.txt", "w");
fprintf(file, "Hello, File!");
fclose(file);
---
(iv) Reading from a File
char data[50];
FILE *file = fopen("data.txt", "r");
fgets(data, 50, file);
printf("%s", data);
fclose(file);
---
(v) Processing Unformatted Data
Reading/writing binary data:
struct Student {
int roll;
float marks;
};
FILE *file = fopen("data.bin", "wb");
struct Student s = {101, 85.5};
fwrite(&s, sizeof(s), 1, file);
fclose(file);
---
Summary
Pointers: Store memory addresses.
Pointer Operations: Arithmetic (ptr+1), dereferencing (*ptr).
Pointer and Arrays: Pointer used to traverse arrays.
Function Pointers: Store function addresses.
File Handling: Open, close, read, write.
Binary Files: Store structured data.