0% found this document useful (0 votes)
8 views6 pages

Solved c

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)
8 views6 pages

Solved c

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 work on binary representations of data. Examples:

• AND (&): Sets bits where both operands are 1.

• OR (|): Sets bits where at least one operand is 1.

• XOR (^): Toggles bits where operands differ.

• NOT (~): Inverts bits.

• Left Shift (<<): Shifts bits to the left.

• Right Shift (>>): Shifts bits to the right.

b) Structure and Union

• Structure: Collection of variables (of different types) grouped under a single name.
Example:

Copy code

struct Point {

int x, y;

};

• Union: Similar to structure, but shares memory for all members.


Example:

Copy code

union Data {

int i;

float f;

};

c) Pre-Processor

Directives processed before compilation. Common ones:

• #include (file inclusion)

• #define (macro definition)

• #if, #else, #endif (conditional compilation)


d) Switch Case

Control statement for multi-way branching.


Example:

Copy code

switch(choice) {

case 1: printf("One"); break;

case 2: printf("Two"); break;

default: printf("Other");

e) Array of Structure

An array where each element is a structure.


Example:

Copy code

struct Point arr[10];

f) Dynamic Memory Allocation

Allocating memory at runtime using malloc, calloc, or realloc.


Example:

Copy code

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

g) Pointer Arithmetic

Operations on pointers, such as increment (++), decrement (--), or subtraction.


Example:

Copy code

int arr[] = {10, 20, 30};


int *p = arr;

p++; // Now points to arr[1].

h) Pointer to Pointer

A pointer storing the address of another pointer.


Example:

Copy code

int x = 10, *p = &x, **pp = &p;

2. Differences

a) break vs continue

• break: Exits the loop or switch entirely.

• continue: Skips the remaining code in the current iteration.

b) while vs do-while

• while: Checks the condition first.

• do-while: Executes the block at least once.

c) Recursion vs Iteration

• Recursion: Function calls itself.

• Iteration: Loops to repeat code.

d) Array vs Structure

• Array: Collection of elements of the same type.

• Structure: Collection of elements of different types.

e) Structure vs Union

• Structure: Each member has separate memory.

• Union: Members share the same memory.


f) Call by Value vs Call by Reference

• Call by Value: Copies values; original data unchanged.

• Call by Reference: Passes address; modifies original data.

g) malloc vs calloc

• malloc: Allocates uninitialized memory.

• calloc: Allocates memory and initializes it to zero.

3. Theory/Descriptive

Algorithm

Step-by-step procedure to solve a problem.


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

Advantage of 2’s Complement over 1’s Complement

• Single representation for zero.

• Simpler arithmetic operations.

Storage Classes

Define variable scope, lifetime, and linkage.


Examples: auto, static, register, extern.

Functions and Advantages

Reusable code blocks.


Example:

Copy code

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

Advantages: Code reusability, readability.

Dynamic Memory Allocation

Allocates memory at runtime, enabling efficient use of memory.


Functions: malloc, calloc, realloc, free.
4. Programs

a) Factorial (Normal/Recursion)

Copy code

int factorial(int n) {

if (n == 0) return 1;

return n * factorial(n - 1);

b) Leap Year Check

Copy code

if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) printf("Leap Year");

c) Fibonacci Series

Copy code

int fib(int n) {

if (n <= 1) return n;

return fib(n - 1) + fib(n - 2);

d) Prime Numbers in a Range

Copy code

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

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

e) Palindrome Check (String)


c

Copy code

int isPalindrome(char str[]) {

int i = 0, j = strlen(str) - 1;

while(i < j) if(str[i++] != str[j--]) return 0;

return 1;

f) GCD Using Recursion

Copy code

int gcd(int a, int b) {

return (b == 0) ? a : gcd(b, a % b);

g) File Operations

Copy code

FILE *file = fopen("test.txt", "r");

if(file) {

char line[100];

while(fgets(line, sizeof(line), file)) printf("%s", line);

fclose(file);

4o

You might also like