C Language All Important Questions
C Language All 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;
}
Unit 1,2,3,4,5 imp questions
Unit-1:
Mainframe Computers: Large systems used by big organizations for bulk data
processing like census, ERP, and financial transactions.
Embedded Computers: Built into other devices (like washing machines, microwave
ovens, and cars) to perform dedicated functions.
System Software: Includes operating systems (Windows, Linux), device drivers, and
utility programs that help run the hardware.
Middleware: Software that acts as a bridge between applications and the operating
system.
Example:
#include <stdio.h>
int main() {
int a = 10;
printf("Value: %d", a);
return 0;
}
Examples:
Assignment: =, +=, -=
Increment/Decrement: ++, --
Bitwise: &, |, ^, ~
Example:
int a;
scanf("%d", &a);
printf("Value: %d", a);
Unit-2:
Syntax:
if (condition) {
// code to be executed
}
Example:
1.2 if-else Statement: Executes one block if the condition is true, another if false.
Example:
if (num % 2 == 0) {
printf("Even number");
} else {
printf("Odd number");
}
Example:
if (num > 0) {
if (num < 100) {
printf("Number is between 1 and 99");
}
}
1.4 switch Statement: Used to select one of many blocks of code to be executed.
Syntax:
switch (expression) {
case constant1:
// code
break;
case constant2:
// code
break;
default:
// code
}
Example:
int day = 2;
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
default:
printf("Other day");
}
---
Syntax:
Example:
Syntax:
while (condition) {
// code
}
Example:
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
2.3 do-while Loop: Executes at least once, then repeats as long as condition is true.
Syntax:
do {
// code
} while (condition);
Example:
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 5);
Example:
Output: 1 2 3 4
3.2 continue Statement: Skips the current iteration and moves to the next.
Example:
Output: 1 2 4 5
Syntax:
goto label;
// code
label:
// code
Example:
int i = 1;
if (i == 1) goto jump;
printf("This will not print.");
jump:
printf("Goto used!");
Unit-3
Syntax:
datatype arrayName[size];
Example:
Accessing elements:
Key Points:
Example:
Syntax:
datatype arrayName[rows][columns];
Example:
Example:
int cube[2][2][2] = {
{{1,2},{3,4}},
{{5,6},{7,8}}
};
C provides several string handling functions through the <string.h> header file.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
strcat(str1, str2);
printf("Concatenated: %s", str1);
return 0;
}
4. Explain String Declaration? (4M)
Declaration Methods:
Character array:
Without size:
Important Points:
Unit-4
Advantages:
Code reuse
Syntax:
return_type function_name(parameters) {
// function body
}
Example:
Example:
int main() {
int result = add(5, 10);
printf("%d", result);
return 0;
}
The return statement is used to return a value from a function to the calling function.
Syntax:
return expression;
Example:
int square(int x) {
return x * x;
}
void change(int x) {
x = 20;
}
Types:
Example:
6. What is a Pointer?
datatype *pointer_name;
Example:
int a = 10;
int *p = &a;
Example:
Call:
swap(&x, &y);
Pointers and arrays are closely related. Array name is a constant pointer to the first
element.
Example:
Unit-5
Key functions:
calloc(n, size) – Allocates memory for an array of n elements and initializes to zero.
free(ptr);
Advantages:
2. What is a Structure?
Syntax:
struct structure_name {
datatype member1;
datatype member2;
...
};
Example:
struct Student {
int roll;
char name[20];
float marks;
};
Example:
struct Student {
int roll;
char name[20];
float marks;
};
Accessing:
s[0].roll = 101;
strcpy(s[0].name, "Aman");
s[0].marks = 95.5;
Used for handling records like employee list, student database, etc.
You can use pointers to structures to access or modify structure data dynamically.
Example:
struct Student {
int roll;
float marks;
};
Structure Example:
struct Data {
int i;
float f;
};
Union Example:
union Data {
int i;
float f;
};