Programming in C important questions
Programming in C important questions
for(int i=1;i<=5;i++){
if(i==3) break;
printf("%d ",i);
}
Output: 1 2
for(int i=1;i<=5;i++){
if(i==3) continue;
printf("%d ",i);
}
Output: 1 2 4 5
Example:
int i=0;
while(i<3){
printf("%d ", i);
i++;
}
int i=0;
do {
printf("%d ", i);
i++;
} while(i<3);
Example:
char ch = 'a';
printf("%c", toupper(ch)); // Output: A
Syntax:
datatype array_name[size];
Example:
Accessing elements:
Memory Representation:
If marks[0] is at address 2000, marks[1] will be at 2004 (for int size 4 bytes), and so
on.
int factorial(int n) {
if(n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
Example Call:
Example:
int *ptr;
ptr = (int*) malloc(5 * sizeof(int));
if(ptr == NULL) {
printf("Memory not allocated");
} else {
// use memory
free(ptr); // release memory
}
Memory Each member has separate memory All members share same memory
Size Sum of all members Size of the largest member
Example Use Employee data Multiple data in same location
Structure Example:
struct student {
int id;
float marks;
};
Union Example:
union data {
int i;
float f;
};
10. Explain various modes of opening files in C
Mode Meaning
Example:
FILE *fp;
fp = fopen("data.txt", "r");
if(fp == NULL)
printf("File not found");
else
fclose(fp);
#include <stdio.h>
2. Global Declarations
int x = 10;
3. Main Function
int main() {
// code
return 0;
}
4. User-defined Functions
void greet() {
printf("Hello");
}
4. Operators – +, -, *, ==
6. Strings – "Hello"
1. if
2. if-else
3. else-if Ladder
switch(choice) {
case 1: printf("One"); break;
default: printf("Invalid");
}
#include <stdio.h>
int main() {
int n, rev = 0, temp, rem;
printf("Enter a number: ");
scanf("%d", &n);
temp = n;
while(n != 0) {
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
if(temp == rev)
printf("Palindrome");
else
printf("Not Palindrome");
return 0;
}
15. Write a C Program for merging two strings without pre-defined functions
#include <stdio.h>
int main() {
char str1[100], str2[100];
int i=0, j=0;
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
#include <stdio.h>
int main() {
int a[10][10], b[10][10], sum[10][10];
int i, j, n, m;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
sum[i][j] = a[i][j] + b[i][j];
printf("Sum of matrices:\n");
for(i=0;i<n;i++) {
for(j=0;j<m;j++)
printf("%d ", sum[i][j]);
printf("\n");
}
return 0;
}
Example:
int main() {
int x=10, y=20;
swap(&x, &y); // Call by reference
printf("%d %d", x, y); // Output: 20 10
}
#include <stdio.h>
struct Employee {
int id;
char name[30];
char designation[30];
char department[20];
float salary;
};
int main() {
struct Employee e;
printf("Enter Employee Details:\n");
printf("ID: "); scanf("%d", &e.id);
printf("Name: "); scanf(" %[^\n]", e.name);
printf("Designation: "); scanf(" %[^\n]", e.designation);
printf("Department: "); scanf(" %[^\n]", e.department);
printf("Salary: "); scanf("%f", &e.salary);
printf("\nEmployee Information:\n");
printf("ID: %d\nName: %s\nDesignation: %s\nDepartment: %s\nSalary: %.2f\n",
e.id, e.name, e.designation, e.department, e.salary);
return 0;
}
4. Operators – +, -, *, ==
6. Strings – "Hello"
1. if
2. if-else
3. else-if Ladder
4. switch
switch(choice) {
case 1: printf("One"); break;
default: printf("Invalid");
}
15. Write a C Program for merging two strings without pre-defined functions
#include <stdio.h>
int main() {
char str1[100], str2[100];
int i=0, j=0;
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
#include <stdio.h>
int main() {
int a[10][10], b[10][10], sum[10][10];
int i, j, n, m;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
sum[i][j] = a[i][j] + b[i][j];
printf("Sum of matrices:\n");
for(i=0;i<n;i++) {
for(j=0;j<m;j++)
printf("%d ", sum[i][j]);
printf("\n");
}
return 0;
}
Example:
int main() {
int x=10, y=20;
swap(&x, &y); // Call by reference
printf("%d %d", x, y); // Output: 20 10
}
#include <stdio.h>
struct Employee {
int id;
char name[30];
char designation[30];
char department[20];
float salary;
};
int main() {
struct Employee e;
printf("Enter Employee Details:\n");
printf("ID: "); scanf("%d", &e.id);
printf("Name: "); scanf(" %[^\n]", e.name);
printf("Designation: "); scanf(" %[^\n]", e.designation);
printf("Department: "); scanf(" %[^\n]", e.department);
printf("Salary: "); scanf("%f", &e.salary);
printf("\nEmployee Information:\n");
printf("ID: %d\nName: %s\nDesignation: %s\nDepartment: %s\nSalary: %.2f\n",
e.id, e.name, e.designation, e.department, e.salary);
return 0;
}
20. Write a C program to read characters from one file and write to another file
#include <stdio.h>
int main() {
FILE *f1, *f2;
char ch;
f1 = fopen("input.txt", "r");
f2 = fopen("output.txt", "w");
fclose(f1);
fclose(f2);
printf("File copied successfully.");
2D Array:
int arr[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Memory Representation:
int a = 10;
int *p = &a;
DMA Functions:
malloc()
calloc()
realloc()
free()
Union
Members accessed simultaneously One at a time
#define PI 3.14
#define SQUARE(x) ((x)*(x))
Q5) What is an operator and its types? Explain left and right shift.
Shift Operators:
int → integers
char → characters
float → decimal
void → no value
Example:
Example:
int fact(int n) {
if(n==0) return 1;
return n * fact(n-1);
}
Local Global
float f = (float)10;
Types:
User-defined functions
Macro Function
Storage Classes:
Example:
int isPrime(int n) {
if (n <= 1) return 0;
for(int i=2; i<n; i++)
if(n%i==0) return 0;
return 1;
}
Static Dynamic
Compile-time Runtime
Fixed size Variable size
Format specifiers:
%d → int
%f → float
%c → char
%s → string
Q20) Key features of C
Fast execution
Rich library
Structured programming
Important questions.
Syntax:
switch(expression) {
case value1:
// code;
break;
case value2:
// code;
break;
default:
// code;
}
Example:
int ch = 2;
switch(ch) {
case 1: printf("One"); break;
case 2: printf("Two"); break;
default: printf("Invalid");
}
Syntax:
Example:
Example:
int i = 5;
printf("%d", ++i); // Outputs 6 (pre-increment)
printf("%d", i++); // Outputs 6, then i becomes 7 (post-increment)
"r" – Read
"w" – Write
"a" – Append
Syntax:
Logic:
max = arr[0];
for(i = 1; i < n; i++) {
if(arr[i] > max) max = arr[i];
}
Syntax:
char str[100];
gets(str); // reads full line
scanf("%s", str); // reads till space
Syntax:
Using struct:
struct Student {
int id;
};
struct Student s1;
typedef struct {
int id;
} Student;
Student s1;
void func() {
static int x = 0;
x++;
printf("%d", x);
}
a = 0, b = 1;
for(i = 0; i < n; i++) {
c = a + b;
a = b;
b = c;
}
Recursion:
int fib(int n) {
if(n <= 1) return n;
return fib(n-1) + fib(n-2);
}
Answer:
continue: Skips the current iteration and proceeds with the next iteration of the loop.
Example:
Example:
int factorial(int n) {
if(n == 0) return 1;
else return n * factorial(n - 1);
}
Answer:
scanf(): Reads input until a whitespace character is encountered; cannot read multi-
word strings.
Example:
Answer: Pointers are variables that store the memory address of another variable.
Advantages:
Answer: Files in C are handled using the FILE pointer and functions like fopen(),
fclose(), fread(), fwrite(), fprintf(), and fscanf().
Common operations:
Answer: Storage classes in C define the scope, visibility, and lifetime of variables.
The main storage classes are:
10. What is the difference between call by value and call by reference?
Answer:
Call by value: Passes a copy of the variable's value to the function; original value
remains unchanged.
Call by reference: Passes the variable's address to the function; allows the function
to modify the original variable.
Example:
return 0;
}