0% found this document useful (0 votes)
15 views11 pages

Alishan Paper

The document provides an overview of C programming concepts, including basic data types, control statements, recursion, pointers, algorithms, operators, and file handling. It includes code examples for various functions such as computing power using recursion, accessing array elements with pointers, reversing strings, and implementing bubble sort. Additionally, it discusses dynamic memory allocation, structures, unions, and storage classes in C.
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)
15 views11 pages

Alishan Paper

The document provides an overview of C programming concepts, including basic data types, control statements, recursion, pointers, algorithms, operators, and file handling. It includes code examples for various functions such as computing power using recursion, accessing array elements with pointers, reversing strings, and implementing bubble sort. Additionally, it discusses dynamic memory allocation, structures, unions, and storage classes in C.
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/ 11

✅ SECTION A (4 × 5 = 20 marks)

Q1. Describe the C programming basic data types in detail.

C has the following basic data types:

Data Type Size Description

int 4 Integer values


bytes

char 1 byte Characters

float 4 Single precision decimal


bytes

double 8 Double precision decimal


bytes

Signed and Unsigned Variants:

●​ signed int: Allows negative and positive (e.g. -2,147,483,648 to 2,147,483,647)​

●​ unsigned int: Only positive (e.g. 0 to 4,294,967,295)​

Q2. Explain the use of break and continue statements in loops with
examples.

●​ break: Terminates the loop or switch.​

●​ continue: Skips the rest of the loop body and proceeds to the next iteration.​

Example:
c
CopyEdit
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
printf("%d ", i); // Skips printing 3
}

Q3. Define recursion. Write a program to compute a^b using recursion.

Recursion is when a function calls itself.

Code:
c
CopyEdit
#include <stdio.h>
int power(int a, int b) {
if (b == 0) return 1;
return a * power(a, b - 1);
}
int main() {
int a = 2, b = 3;
printf("Result = %d", power(a, b));
return 0;
}

Q4. Explain pointer and write a program to access array elements using a
pointer.

●​ A pointer is a variable that stores address of another variable.​

Code:
c
CopyEdit
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4};
int *ptr = arr;

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


printf("%d ", *(ptr + i));
}
return 0;
}

✅ SECTION B (Any 4 × 10 = 40 marks)


Q1. a) Define Algorithm + 10 Characteristics (6 Marks)

An algorithm is a finite sequence of steps to solve a problem.

Characteristics:

1.​ Clear input/output​

2.​ Finiteness​

3.​ Effectiveness​

4.​ Correctness​

5.​ Simplicity​

6.​ Language-independent​

7.​ Flexibility​

8.​ Generality​

9.​ Efficient (Time/Space)​

10.​Feasible​

Q1. b) Draw flowchart for Fibonacci (4 Marks)

✍️ Draw this yourself in exam using below logic:]


[

Start → Input N → a = 0, b = 1 → Loop till N → Print a → c = a + b → a = b, b = c → End


Q2. What is an operator? Describe types in C with examples.

Operators are symbols to perform operations.

Types:

1.​ Arithmetic: +, -, *, /, %​

2.​ Relational: <, >, ==, !=​

3.​ Logical: &&, ||, !​

4.​ Bitwise: &, |, ^​

5.​ Assignment: =, +=, -=​

6.​ Unary: ++, --​

7.​ Ternary: condition ? x : y;​

8.​ Special: sizeof, &, *​

Q3. C Program to reverse string and check PALINDROME


c
CopyEdit
#include <stdio.h>
#include <string.h>

int main() {
char str[100], rev[100];
int i, len;

strcpy(str, "madam");
len = strlen(str);

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


rev[i] = str[len - i - 1];
rev[i] = '\0';
if (strcmp(str, rev) == 0)
printf("Palindrome");
else
printf("Not Palindrome");

return 0;
}

Q4. Swap numbers using Call by Value and Call by Reference

Call by Value:
c
CopyEdit
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
}

Call by Reference:
c
CopyEdit
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

Q5. a) Bubble Sort 6th iteration – Array state

Array:​
74, 32, 56, 48, 89, 24, 66, 5, 9, 91, 18, 28

You sort step-by-step and show array after 6th pass.​


(Manually dry run bubble sort loop 6 times.)
Q5. b) Code for Bubble Sort
c
CopyEdit
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1]) {
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}

Q6. Write notes on any 2

🅐 Pointer in C Programming

A pointer is a variable that stores the memory address of another variable.

Syntax:
c
CopyEdit
int a = 10;
int *ptr = &a;

Here:

●​ a is a normal integer variable.​

●​ &a gives the address of a.​

●​ *ptr is a pointer to int which stores the address of a.​

Example:
c
CopyEdit
#include <stdio.h>
int main() {
int a = 5;
int *ptr = &a;
printf("Value of a = %d\n", *ptr); // Dereferencing
printf("Address of a = %p\n", ptr);
return 0;
}

🟢 Use: Efficient memory management, dynamic memory, arrays, and function arguments.

🅑 Dynamic Memory Allocation

In C, you can allocate memory at runtime using functions like:

●​ malloc() – allocates memory​

●​ calloc() – allocates and initializes memory​

●​ realloc() – resizes memory block​

●​ free() – deallocates memory​

Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int*)malloc(5 * sizeof(int)); // allocate space for 5
ints

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


ptr[i] = i + 1;

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


printf("%d ", ptr[i]);

free(ptr); // deallocate memory


return 0;
}

🟢 Use: When size of data is unknown at compile time.

🅒 Structure and Union

Both are used to group different data types.

Structure:

Allocates separate memory to each member.

c
CopyEdit
struct Student {
int id;
char name[20];
float marks;
};

Union:

All members share the same memory. Useful when only one member is used at a time.

c
CopyEdit
union Data {
int i;
float f;
};

🟢 Structure vs Union:
Feature Structure Union

Memory Separate Shared

Use All members One member at a


Case needed time
🅓 Storage Classes in C

They define scope, lifetime, and visibility of variables.

Storage Keyword Meaning / Scope


Class

Automatic auto Default for local variables

External extern Global, visible across files

Static static Retains value between


calls

Register registe Stored in CPU register


r

Example of static:
void fun() {
static int x = 0;
x++;
printf("%d ", x);
}

Each time you call fun(), value of x will increase because of static.

🅔 File in C Programming

In C, files are used to store and retrieve data permanently (unlike variables, which are
temporary in memory).

✅ Why use files?


●​ To store data permanently.​

●​ To read/write large amounts of data.​

●​ Useful for report generation, logs, etc.​


📂 File Operations in C
Operation Function

Open a file fopen()

Close a file fclose()

Read a character fgetc()

Write a character fputc()

Read a string fgets()

Write a string fputs()

Read/Write formatted fscanf(),


fprintf()

📌 File Opening Modes in fopen()


Mode Meaning

"r" Read (file must exist)

"w" Write (creates new or overwrites)

"a" Append

"r+" Read + Write

"w+" Write + Read (clears file)

"a+" Read + Append

🧪 Example: Write and Read a File


c
CopyEdit
#include <stdio.h>

int main() {
FILE *fptr;
// Writing to file
fptr = fopen("data.txt", "w");
if (fptr == NULL) {
printf("File can't be opened\n");
return 1;
}
fprintf(fptr, "Hello File Handling in C");
fclose(fptr);

// Reading from file


char ch;
fptr = fopen("data.txt", "r");
while ((ch = fgetc(fptr)) != EOF)
putchar(ch);
fclose(fptr);

return 0;
}

You might also like