1. Question: What are the rules for naming variables in C?
Answer: Variable names must begin with a letter or underscore, can contain letters, digits, and
underscores, and are case-sensitive. They cannot be C keywords or contain special characters.
2. Question: Explain the difference between signed and unsigned integers in C.
Answer: Signed integers can store both negative and positive values, while unsigned integers can only
store positive values and zero, effectively doubling their maximum positive range.
3. Question: What does the `sizeof` operator do in C?
Answer: The `sizeof` operator returns the number of bytes required to store a given data type or
variable.
4. Question: How does the `printf` function format output? Give an example.
Answer: The `printf` function uses a format string with specifiers like `%d`, `%f`, etc., to display variables.
Example: `printf("Value: %d\n", x);` prints the integer value of `x`.
5. Question: What is the purpose of the `scanf` function in C?
Answer: The `scanf` function reads formatted input from standard input, matching provided format
specifiers with corresponding variable addresses.
6. Question: Write a program to demonstrate the use of arithmetic operators (+, -, *, /, %).
Answer:
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a / b);
printf("Modulus: %d\n", a % b);
return 0;
7. Question: Write the output of the below code
#include <stdio.h>
int main() {
printf("Size of char: %d bytes\n", sizeof(char));
printf("Size of int: %d bytes\n", sizeof(int));
printf("Size of float: %d bytes\n", sizeof(float));
printf("Size of double: %d bytes\n", sizeof(double));
return 0;
Answer:
Size of char: 1 bytes
Size of int: 2 bytes
Size of float: 4 bytes
Size of double: 8 bytes
8. Question: Implement a program using `scanf` to take input for an integer, a float, and a character, and
display them.
Answer:
#include <stdio.h>
int main() {
int x;
float y;
char ch;
printf("Enter an integer, a float, and a character: ");
scanf("%d %f %c", &x, &y, &ch);
printf("You entered: %d, %.2f, %c\n", x, y, ch);
return 0;
9. Question: Write a program to demonstrate the pre- and post-increment operators.
Answer:
#include <stdio.h>
int main() {
int x = 5;
printf("Initial: %d\n", x);
printf("Pre-increment: %d\n", ++x);
printf("Post-increment: %d\n", x++);
printf("Final: %d\n", x);
return 0;
```
10. Question: Demonstrate the use of bitwise AND, OR, and XOR operators in C.
Answer:
#include <stdio.h>
int main() {
int a = 5, b = 3; // 0101, 0011 in binary
printf("AND: %d\n", a & b); // 0001 -> 1
printf("OR: %d\n", a | b); // 0111 -> 7
printf("XOR: %d\n", a ^ b); // 0110 -> 6
return 0;
}
11. Question: Predict the output of the following code snippet:
#include <stdio.h>
int main() {
int a = 5, b = 10, c;
c = (a > b) ? a : b;
printf("Largest: %d\n", c);
return 0;
Answer: The output is `Largest: 10`.
12. Question: What will be printed by this code?
#include <stdio.h>
int main() {
int x = 3, y = 6, z;
z = x * ++y;
printf("%d\n", z);
return 0;
Answer: The output is `21`.
13. Question: Given this code, what is the output?
#include <stdio.h>
int main() {
int a = 4, b = 5, c = 10;
printf("%d\n", a + b * c++);
int a = 4, b = 5, c = 10;
printf("%d\n", a + b * ++c);
return 0;
}
Answer: For post increment output is ` 54`.
For pre increment output is ` 59`.
14. Question: What will this program print?
#include <stdio.h>
int main ()
int w=10,x=20,y=30,z=40;
int temp1, temp2;
temp1 = x * x /++y + z / y;
printf ("temp1= %d;\nw= %d;\nx= %d;\ny= %d;\nz= %d\n",
temp1, w,x,y,z);
y=30;
temp2 = x * x /y++ + z / y;
printf ("temp2= %d;\nw= %d;\nx= %d;\ny= %d;\nz= %d\n",
temp2, w,x,y,z);
return 0;
Answer:
temp1= 13;
w= 10;
x= 20;
y= 31;
z= 40
temp2= 14;
w= 10;
x= 20;
y= 31;
z= 40
15. Question: Evaluate the result of this program:
#include <stdio.h>
int main ()
int radius, area;
printf ("Enter radius (i.e. 10) : ");
scanf ( "%d", &radius);
area = 3.14159 * radius * radius;
printf ("\nArea = %d\n\n", area);
return 0;
Answer:
Enter radius (i.e. 10) : 1 (if the radius is put 1)
Area = 3
16. Question: Implement a program to multiply two numbers using scanf() and arithmetic operator “*”
Answer:
#include <stdio.h>
int product(int x, int y);
int main ()
int a,b,c;
/* Input the first number */
printf ("Enter a number between 1 and 100: ");
scanf ("%d", &a);
/* Input the second number */
printf ("Enter another number between 1 and 100: ");
scanf ("%d", &b);
printf ("%d times %d = %d \n", a, b, a*b);
return 0;
17. Question: Write a program that uses the conditional operator to find the maximum of two numbers.
Answer:
```c
#include <stdio.h>
int main() {
int a = 15, b = 20;
int max = (a > b) ? a : b;
printf("Maximum: %d\n", max);
return 0;
```
18. Given the following bit sizes:
short int is 2 bytes.
long long int is 8 bytes.
Calculate the ranges of signed and unsigned types for both
Answer: As 1 byte = 8 bits
Signed short int: Range = -2^(16-1) to 2^(16-1) - 1
Result: -32,768 to 32,767
Unsigned short int: Range = 0 to 2^16 - 1
Result: 0 to 65,535
Signed long long int: Range = -2^(64-1) to 2^(64-1) - 1
Result: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Unsigned long long int: Range = 0 to 2^64 - 1
Result: 0 to 18,446,744,073,709,551,615
19. Given the following variable names, identify whether each is valid or invalid according to C naming
rules. If invalid, explain why.
myVar123
_result
2value
char
user-name
Answer:
myVar123 - Valid: Contains only letters, digits, and underscores; starts with a letter.
_result - Valid: Starts with an underscore, which is allowed, but not recommended for general use.
2value - Invalid: Variable names cannot start with a digit.
char - Invalid: It is a reserved keyword in C.
user-name - Invalid: Hyphens (-) are not allowed in variable names.
20. Consider the following variable names. Identify which names adhere to good naming practices in C. If
a name does not, suggest a better alternative.
_temp
TOTAL
userInput
hello#world
calc_Avg
Answer:
_temp - Not recommended: Avoid starting with an underscore as these are often used by the operating
system. Better alternative: temp.
TOTAL - Not recommended: All-uppercase names are conventionally used for constants or macros. Better
alternative: total.
userInput - Good: Descriptive and follows camelCase naming convention.
hello#world - Invalid: Contains #, which is not allowed in variable names. Better alternative: helloWorld.
calc_Avg - Good: Descriptive and uses underscores for separation.
21. Write the output of the flowing code
#include <stdio.h>
#define PI 3.14159
const double GRAVITY = 9.8;
int main() {
double radius, area, weight, mass;
// Input for calculating area of a circle
printf("Enter the radius of the circle: ");
scanf("%lf", &radius);
// Calculate the area using the constant PI
area = PI * radius * radius;
// Input for calculating weight
printf("Enter the mass of the object (in kg): ");
scanf("%lf", &mass);
// Calculate weight using the constant GRAVITY
weight = mass * GRAVITY;
// Print results
printf("The area of the circle is: %.2lf square units\n", area);
printf("The weight of the object is: %.2lf N\n", weight);
return 0;
Expected Output:
Enter the radius of the circle: 5 (as the input is 5, for different input value will be different )
Enter the mass of the object (in kg): 10 (as the input is 10, for different input value will be different )
The area of the circle is: 78.54 square units
The weight of the object is: 98.00 N