C Programming Exam
C Programming Exam
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:
./program
Example:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Conversion Examples:
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
Example:
#include<stdio.h>
void function() {
int a = 10; // local variable
printf("%d", a);
}
UNIT - II
3. a) Logical Operators in C:
Code snippet:
int x;
x = 9<5+3 && 6<7;
printf("%d", x);
switch(expression) {
case value1:
// code
break;
default:
// default code
}
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++;
}
do {
printf("%d ", i);
i++;
} while(i < 5);
#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
#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;
}
struct student {
int id;
char name[20];
};
union data {
int i;
float f;
};
Memory Allocation:
b) typedef in C: