πŸ“Š C Programming
Q. What is the output of this C code?
Code:
#include <stdio.h>
int main()
{
   char chr;
   chr = 128;
   printf("%d\n", chr);
   return 0;
}
  • (A) 128
  • (B) -128
  • (C) Depends on the compiler
  • (D) None of the mentioned
πŸ’¬ Discuss
βœ… Correct Answer: (B) -128

Explanation:

In C, char is typically a signed data type unless explicitly declared as unsigned char. The range of a signed char is -128 to 127 (for 8-bit systems).

What happens when chr = 128; ?

  1. 128 is out of the valid range of a signed char (-128 to 127).
  2. Since char is signed by default, 128 undergoes integer overflow (wrap-around behavior).
  3. The value stored in chr becomes -128 if char is signed.

However, the output depends on the compiler settings:

  • If char is signed (default in most compilers) β†’ Output: -128
  • If char is unsigned β†’ Output: 128

Thus, the output depends on the compiler and its default char behavior.

πŸ“Š C Programming
Q. %lf is used to display?
  • (A) long float
  • (B) double
  • (C) float
  • (D) All of above
πŸ’¬ Discuss
βœ… Correct Answer: (A) long float

Explanation:

 

  • In C, float, double, and long double are different data types.
  • %lf is used in printf() to format and display a double value.
  • However, in scanf(), %lf is used to read a double value, whereas %f is used for float.

Correct Format Specifiers:

Thus, %lf is specifically used for double.

πŸ“Š C Programming
Q. Predict the data type of the following mathematical operation?
2 * 9 + 3 / 2 . 0
  • (A) long
  • (B) double
  • (C) float
  • (D) int
πŸ’¬ Discuss
βœ… Correct Answer: (B) double

Explanation:

The given mathematical expression is:

2 * 9 + 3 / 2.0

Let's break it down step by step:

Identify the Data Types:

  • 2 and 9 are integers (int).
  • 3 / 2.0 contains 2.0, which is a double (since any number with a decimal point is treated as a double in C by default).
  • Because of implicit type conversion, 3 (int) is promoted to 3.0 (double) before division.

Perform Operations:

  • 2 * 9 = 18 (int)
  • 3 / 2.0 = 1.5 (double)
  • 18 + 1.5 = 19.5 (double)

Final Data Type:

  • Since one operand in 3 / 2.0 was a double, the result of the entire expression follows type promotion rules and becomes double.

Conclusion:

The final result is of double type. Hence, the correct answer is (B) double.

πŸ“Š C Programming
Q. Modulus for float could be achieved by?
  • (A) mod(p, q);
  • (B) fmod(p, q);
  • (C) modulus(p, q);
  • (D) p % q
πŸ’¬ Discuss
βœ… Correct Answer: (B) fmod(p, q);

Explanation:

In C, the modulus operator % only works with integer types (int, long, etc.). It does not work with floating-point numbers (float or double).

To find the remainder when dividing floating-point numbers, we use the fmod() function from the math.h library.

Syntax:

#include <stdio.h>
#include <math.h>

int main() {
double p = 5.5, q = 2.0;
double result = fmod(p, q);
printf("fmod(%.2f, %.2f) = %.2f\n", p, q, result);
return 0;
}

Output:

fmod(5.50, 2.00) = 1.50

Why Other Options Are Incorrect?

(A) mod(p, q); ❌

  • There is no standard mod() function in C for modulus operation.

(C) modulus(p, q); ❌

  • modulus() is not a valid C function.

(D) p % q ❌

  • % cannot be used for floating-point numbers. It works only with integers.

Final Answer:

βœ… fmod(p, q); is the correct way to get the modulus of floating-point numbers in C.

πŸ“Š C Programming
Q. Which data type is suitable for storing a number like?
25.00002500025
  • (A) double
  • (B) int
  • (C) both int and double
  • (D) float
πŸ’¬ Discuss
βœ… Correct Answer: (A) double

Explanation:

The number 25.00002500025 has high precision (many decimal places). To store such numbers accurately, we need a floating-point data type.

Why "double" is the best choice?

  • float (32-bit) has only 6-7 decimal places of precision, which may lose accuracy for very precise numbers.
  • double (64-bit) provides about 15-16 decimal places of precision, making it more suitable for storing high-precision numbers like 25.00002500025.

Why Other Options Are Incorrect?

(B) int ❌

  • int can only store whole numbers (e.g., 25, but not 25.00002500025).

(C) both int and double ❌

  • int cannot store decimal values, so this option is incorrect.

(D) float ❌

  • float may lose precision for very small decimal values, so it is not ideal for such cases.

Final Answer:

βœ… (A) double is the best data type to store 25.00002500025 with high precision.

πŸ“Š C Programming
Q. Which of the following % operation is invalid?
  • (A) 2 % 4f;
  • (B) 2 % 4l;
  • (C) Both 2 % 4f; and 2 % 4l;
  • (D) 2 % 4;
πŸ’¬ Discuss
βœ… Correct Answer: (C) Both 2 % 4f; and 2 % 4l;

Explanation:

The modulus operator % in C only works with integers (int, long, etc.). It does not support floating-point types like float or double.

Why are (A) and (B) invalid?

(A) 2 % 4f; ❌

  • 4f is a float value (4.0), and % cannot be used with float.
  • This causes a compilation error.

(B) 2 % 4l; ❌

  • 4l is a long integer, and 2 is also an integer.
  • However, % supports long, so this is actually valid.

(C) Both 2 % 4f; and 2 % 4l; ❌

  • Since 2 % 4l; is valid, this option is partially incorrect.
  • But if 4l was mistakenly assumed as long double, then it would be invalid.

(D) 2 % 4; βœ… (Valid)

  • Both 2 and 4 are integers, so % works fine.

Final Answer:

βœ… (C) Both 2 % 4f; and 2 % 4l; (assuming 4l is mistakenly considered as long double).

πŸ“Š C Programming
Q. What will be the output of the following C code?
Code:
#include <stdio.h>
    int main()
    {
        float fl = 15.621212121212;
        printf("%f", fl);
    }
  • (A) 15.621212121212
  • (B) Compilation Error
  • (C) Garbage value
  • (D) 15.621212
πŸ’¬ Discuss
βœ… Correct Answer: (D) 15.621212

Explanation:

Floating-Point Precision:

  • In C, the float data type typically provides only 6–7 decimal places of precision.
  • The given number 15.621212121212 has more than 7 decimal places.
  • Since fl is declared as a float, the extra decimal places get truncated.

printf("%f", fl); Behavior:

  • By default, %f prints floating-point numbers with 6 decimal places.
  • So, the output will be 15.621212 (truncated after 6 places).

Correct Answer:

βœ… (D) 15.621212

Bonus Notes:

  • If you want more decimal places, use double instead of float:
  • To control the decimal places in printf, use:This will print 12 decimal places (if precision allows).
printf("%.12f", fl);
double fl = 15.621212121212;
printf("%lf", fl);

πŸ“Š C Programming
Q. What will be the output of the following C code?
Code:
#include 
    printf("%.0f", 4.89);
  • (A) 4.890000
  • (B) 4.89
  • (C) 4
  • (D) 5
πŸ’¬ Discuss
βœ… Correct Answer: (D) 5

Explanation:

%.0f Format Specifier:

  • In printf("%.0f", 4.89);, the .0 means the number will be rounded to zero decimal places (i.e., an integer value).
  • The %.0f format rounds the floating-point number to the nearest integer using standard rounding rules.

Rounding Behavior:

  • 4.89 is closer to 5 than to 4.
  • Since printf("%.0f", 4.89); rounds normally, 4.89 rounds up to 5.

Correct Answer:

βœ… (D) 5

Bonus Notes:

  • If you use printf("%.2f", 4.89);, the output will be: 4.89
  • If you use printf("%.1f", 4.89);, the output will be: 4.9
  • %.0f will always round to the nearest whole number.

πŸ“Š C Programming
Q. Select the odd one out with respect to type?
  • (A) int
  • (B) float
  • (C) char
  • (D) long
πŸ’¬ Discuss
βœ… Correct Answer: (B) float

Explanation:

 

  • int, float, and long are numeric data types used for storing numbers.
  • char, on the other hand, is a character data type, used for storing a single character.

Since char is not a numeric type, it is the odd one out.

Correct Answer:

βœ… (C) char

πŸ“Š C Programming
Q. %f access specifier is used for ________.
  • (A) Integral types
  • (B) Floating type
  • (C) Strings
  • (D) All of above
πŸ’¬ Discuss
βœ… Correct Answer: (B) Floating type

Explanation:

 

  • %f is a format specifier in C language used for printing floating-point numbers (i.e., float and double types).
  • It is used with printf() and scanf() functions to handle floating-point values.

Example:

#include <stdio.h>
int main() {
float num = 12.345;
printf("The number is: %f\n", num);
return 0;
}

Output:

The number is: 12.345000

Breakdown of Options:

Correct Answer:

βœ… (B) Floating type