C Control Statement 2

Example

#include<stdio.h>
int main()
{
    int j=1;
    while()
    {
        printf("%d\n", j++);
        if(j>5)
           break;
    }
    return 0;
}
  • There should be a semicolon in the while statement
  • The while loop should be replaced with do-while loop
  • There should be a condition in the while loop
  • No error

Explanation:

In the program, an "Expression syntax" error arises due to the absence of a conditional expression within the while loop.

For Example: while (j >5) { ... }

Therefore, to eliminate the error related to the "Expression syntax," it is essential to include a condition within the while loop.

7) If scanf statement is used for storing a value in char variable, then along with the value a carriage return (\r) also gets stored.

  • True
  • False

Explanation:

No, when the scanf function is utilized, the carriage return character signals the compiler to fetch input from the buffer once the user presses the ENTER key. Consequently, the carriage return (\r) value is not stored in memory.

8) Find out whether both the loops in a program prints the correct string length?

Example

#include<stdio.h>
main()
{
   int j;
   char s[] = "javaLogic Practice";

   for(j=0; s[j]; ++j);
      printf("%d \n", j);
   
j=0; 
   while(s[j++]);
      printf("%d ", j);
}
  • Yes, both the loops prints the correct string length
  • Only while loop prints the correct string length
  • Only for loop prints the correct string length
  • Compile error in the program

Explanation:

Example

Output: 10
	 11

When using a while loop to iterate through a string, the incorrect string length is displayed due to the variable 'i' being incremented after checking for the null terminator '\0'. This results in one more than the actual length of the string being measured in the loop.

Hence, solely the for loop accurately outputs the length of the string.

9) The break statement is used to take control out of switch and continue statement is used to take control of the beginning of the switch ?

Explanation:

No, the continue statement is exclusive to loops in C programming and cannot be used with switch statements.

10) For printing the value of a and b given below, which printf statement will you use?

Example

#include<stdio.h>
main()
float a=3.14;
double b=3.14;
  • printf("%Lf %f", a, b);
  • printf("%Lf %Lf", a, b);
  • printf("%f %Lf", a, b);
  • printf("%f %lf", a, b);

Explanation:

For displaying a double value, the format specifier %lf is employed.

When displaying float values, the format specifier %f is employed.

Hence, to display the values of variables a and b using the printf statement, the syntax would be printf("%f %lf", a, b);

Input Required

This code uses input(). Please provide values below: