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

C answers

Uploaded by

promita0769
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)
9 views

C answers

Uploaded by

promita0769
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/ 6

1.

Short Notes

a) Bitwise Operator

Operators that perform bit-level operations on data. Examples include AND (&), OR (|), XOR (^), NOT
(~), Left Shift (<<), and Right Shift (>>). They are useful for low-level programming, masking bits, and
optimizing performance.

b) Structure and Union

• Structure: A user-defined data type in C that groups variables of different types. Memory is
allocated for all members.

• Union: A user-defined data type where all members share the same memory location, so
only one member holds a value at a time.

c) Pre-Processor

A tool that processes code before compilation. Common preprocessor directives include #define,
#include, #ifdef, and #pragma.

d) Switch Case

A control statement used for decision-making, where a variable’s value is matched against multiple
case labels. If no match is found, the default block is executed.

e) Array of Structure

An array where each element is a structure. Useful for managing collections of records, e.g., student
data.

f) Dynamic Memory Allocation

Allocating memory during runtime using functions like malloc, calloc, realloc, and free. Enables
efficient memory management.

g) Pointer Arithmetic

Manipulating memory addresses using arithmetic operations like addition, subtraction, increment
(++), and decrement (--). Example: accessing array elements via pointers.

h) Pointer to Pointer

A pointer that stores the address of another pointer. Used for dynamic allocation of multi-
dimensional arrays and passing functions by reference.

2. Differences

a) break vs continue

• break: Exits the loop or switch statement immediately.

• continue: Skips the current iteration and continues with the next iteration.

b) while vs do-while
• while: Condition is checked before executing the loop body.

• do-while: Loop body is executed at least once before the condition is checked.

c) Recursion vs Iteration

• Recursion: A function calls itself until a base condition is met.

• Iteration: Repeating a block of code using loops.

d) Array vs Structure

• Array: Collection of elements of the same type.

• Structure: Collection of variables of different types.

e) Structure vs Union

• Structure: Each member has a separate memory.

• Union: All members share the same memory location.

f) Call by Value vs Call by Reference

• Call by Value: Function receives a copy of the variable. Changes do not affect the original.

• Call by Reference: Function receives the address of the variable. Changes affect the original.

g) malloc vs calloc

• malloc: Allocates memory without initialization.

• calloc: Allocates and initializes memory to zero.

3. Theory / Descriptive

Algorithm and Characteristics

An algorithm is a step-by-step procedure to solve a problem.


Characteristics: Finiteness, Input, Output, Definiteness, Effectiveness.

Flowchart Example

Diagrammatic representation of an algorithm using symbols like ovals (start/end), rectangles


(process), and diamonds (decision).

Advantages of 2’s Complement over 1’s Complement

• Unique representation of zero.

• Simplified arithmetic operations (no end-around carry).

Storage Classes

Specify the lifetime, scope, and visibility of variables. Types: auto, static, extern, register.
Example:

void example() {
static int count = 0; // retains value across calls

Functions

Reusable blocks of code.


Example:

int add(int a, int b) { return a + b; }

Advantages: Modularity, code reusability, debugging ease.

Dynamic Memory Allocation

Allocating memory during runtime using functions like malloc, calloc, realloc. Example:

int *ptr = (int *)malloc(5 * sizeof(int));

Definitions

• Array: Collection of elements of the same type in contiguous memory.

• Function: Reusable block of code with a specific task.

• Recursion: A function that calls itself.

• Structure: Grouping of variables of different types.

• Union: Like a structure but with shared memory.

• Pointer: Stores the address of a variable.

4. Programs

Factorial (Normal and Recursive)

// Iterative

int factorial(int n) {

int fact = 1;

for (int i = 1; i <= n; i++) fact *= i;

return fact;

// Recursive

int factorialRec(int n) {

if (n <= 1) return 1;

return n * factorialRec(n - 1);


}

Leap Year Check

bool isLeapYear(int year) {

return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

Fibonacci Series

Copy code

void fibonacci(int n) {

int a = 0, b = 1, next;

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

printf("%d ", a);

next = a + b;

a = b;

b = next;

Prime Numbers in a Range

bool isPrime(int n) {

if (n <= 1) return false;

for (int i = 2; i * i <= n; i++)

if (n % i == 0) return false;

return true;

void printPrimes(int start, int end) {

for (int i = start; i <= end; i++)

if (isPrime(i)) printf("%d ", i);

Palindrome Check

bool isPalindrome(char str[]) {


int l = 0, h = strlen(str) - 1;

while (l < h) {

if (str[l] != str[h]) return false;

l++, h--;

return true;

GCD Using Recursion

Copy code

int gcd(int a, int b) {

if (b == 0) return a;

return gcd(b, a % b);

Reverse String Without strrev()

void reverseString(char str[]) {

int l = 0, h = strlen(str) - 1;

while (l < h) {

char temp = str[l];

str[l++] = str[h];

str[h--] = temp;

Compare Strings Without strcmp()

bool compareStrings(char str1[], char str2[]) {

while (*str1 && *str2) {

if (*str1 != *str2) return false;

str1++, str2++;

return *str1 == *str2;


}

File Reading

void readFile(const char *filename) {

FILE *file = fopen(filename, "r");

char line[256];

if (file == NULL) {

printf("Error opening file!");

return;

while (fgets(line, sizeof(line), file))

printf("%s", line);

fclose(file);

You might also like