UNIT - I
1. a) Steps to Create, Compile and Execute a C program:
1. Create: Write the C program using any text editor and save it with a .c extension. (e.g.,
program.c )
2. Compile: Use a compiler like gcc to compile the source code:
gcc program.c -o program
3. Execute: Run the executable:
./program
Example:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
b) Binary, Decimal, and Octal Number Systems:
• Binary (base 2): Uses digits 0 and 1.
• Decimal (base 10): Uses digits 0 to 9.
• Octal (base 8): Uses digits 0 to 7.
Conversion Examples:
• (540)₈ to decimal: (58^2 + 48^1 + 0*8^0) = 344
• (10110)₂ to decimal: (12^4 + 02^3 + 12^2 + 12^1 + 0*2^0) = 22
• (725)₁₀ to decimal: (710^2 + 210^1 + 5*10^0) = 725
To convert into binary: use successive division by 2 To octal: use division by 8
2. a) Type Casting in C Programming: Type casting is converting one data type to another.
Types:
• Implicit (Automatic):
int a = 5;
float b = a; // int to float
• Explicit (Manual):
1
float a = 5.5;
int b = (int)a; // float to int
b) Scope and Lifetime of a Variable:
• Scope determines where the variable can be accessed.
• Lifetime refers to how long the variable exists in memory.
Example:
#include<stdio.h>
void function() {
int a = 10; // local variable
printf("%d", a);
}
UNIT - II
3. a) Logical Operators in C:
• && (Logical AND)
• || (Logical OR)
• ! (Logical NOT)
Code snippet:
int x;
x = 9<5+3 && 6<7;
printf("%d", x);
• 9 < 8 is false (0), 6 < 7 is true (1) => 0 && 1 = 0
• Output: 0
b) Switch Statement Syntax and Importance:
switch(expression) {
case value1:
// code
break;
default:
// default code
}
• default : executes when no case matches.
• break : stops fall-through to next case.
2
4. a) Pretest vs Post-test Loops:
• Pretest: condition is tested before the loop body (e.g., while , for )
int i = 0;
while(i < 5) {
printf("%d ", i);
i++;
}
• Post-test: condition is tested after loop body (e.g., do-while )
do {
printf("%d ", i);
i++;
} while(i < 5);
b) Floyd's Triangle Program:
#include<stdio.h>
int main() {
int i, j, k = 1;
for(i = 1; i <= 5; i++) {
for(j = 1; j <= i; j++) {
printf("%d ", k++);
}
printf("\n");
}
return 0;
}
UNIT - III
5. a) Array vs Ordinary Variable:
• An array stores multiple values.
• Ordinary variable stores single value.
• Arrays are passed by reference to functions (memory address passed).
b) Reverse a String without String Library:
#include<stdio.h>
int main() {
char str[100];
int i, len = 0;
3
printf("Enter a string: ");
scanf("%s", str);
while(str[len] != '\0') len++;
for(i = len - 1; i >= 0; i--)
printf("%c", str[i]);
return 0;
}
6. a) Syntax of Structure and Union:
struct student {
int id;
char name[20];
};
union data {
int i;
float f;
};
Memory Allocation:
• Structure: total = sum of all members.
• Union: total = memory of largest member.
b) typedef in C:
typedef unsigned int ui;
int main() {
ui a = 10;
printf("%u", a);
return 0;
}