C Programming Questions - Answers
1. Program to create a structure 'employee':
#include <stdio.h>
struct Employee {
char name[50];
int age;
float bs, da, hra, tsalary;
};
int main() {
struct Employee emp;
printf("Enter name, age, basic salary, DA, HRA: ");
scanf("%s %d %f %f %f", emp.name, &emp.age, &emp.bs, &emp.da, &emp.hra);
emp.tsalary = (1 + emp.da + emp.hra) * emp.bs;
printf("Name: %s\nAge: %d\nTotal Salary: %.2f\n", emp.name, emp.age, emp.tsalary);
return 0;
2. (a) Program using structure and separate functions:
#include <stdio.h>
struct Product {
char name[50];
float price;
int quantity;
float total;
};
void read(struct Product *p) {
printf("Enter name, price, and quantity: ");
scanf("%s %f %d", p->name, &p->price, &p->quantity);
p->total = p->price * p->quantity;
void print(struct Product p) {
printf("Name: %s\nPrice: %.2f\nQuantity: %d\nTotal: %.2f\n", p.name, p.price, p.quantity, p.total);
int main() {
struct Product prod;
read(&prod);
print(prod);
return 0;
3. Function Prototype and Parameter Types:
- Function prototype tells compiler function name, return type, and parameters.
- Used for type checking and early function calls.
Formal vs Actual Parameters:
| Formal Parameters | Actual Parameters |
|---------------------------|-----------------------------|
| In function definition | In function call |
| Act as placeholders | Real values provided |
4. Storage Classes in C:
1. auto - default for local variables.
2. static - retains value between function calls.
3. extern - used to declare global variables across files.
4. register - stores variable in CPU register for speed.
Example (static):
void counter() {
static int count = 0;
count++;
printf("%d\n", count);
5. Recursion:
Recursion is when a function calls itself to solve a problem.
Program to find factorial using recursion:
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial = %d\n", factorial(num));
return 0;
6. Pointer Variable:
- A pointer stores the address of another variable.
Initialization:
int a = 10;
int *ptr = &a;
7. Structure vs Union:
| Feature | Structure | Union |
|------------|------------------------|--------------------------|
| Memory | Separate for members | Shared for all members |
| Access | All at once | One at a time |
| Size | Sum of all members | Size of largest member |
Example (Structure):
struct Student { int id; float marks; };
Example (Union):
union Data { int i; float f; };
8. Linear Search using Function:
#include <stdio.h>
int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key)
return i;
return -1;
int main() {
int n, key;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter key to search: ");
scanf("%d", &key);
int result = linearSearch(arr, n, key);
if (result == -1)
printf("Element not found\n");
else
printf("Element found at index %d\n", result);
return 0;