Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

1. What is output of below program?

int main()
{
const int a = 10;
printf("%d",++a);
return 0;
}

(A) 11
(B) 10
(C) Compilation Error
(D) 0

Ans: C

Hint:
We can't modify the value of const variables. Result is compilation error.

2. How many loops are there in C?

(A) 2
(B) 3
(C) 4
(D) 1

Ans: B

Hint: C Language has only three loops - for, while, do while

3. What should be the output:


int main()
{
int a = 10.5;
printf("%d",a);
return 0;
}

(A) 10.5
(B) 10
(C) 0
(D) Compilation Error

Ans: B

Hint: compiler will convert float values to integer value after seeing %d so instead of printing 10.5 it will print it integer
value i.e 10

4. What should be the output:


int main()
{
int a = 10/3;
printf("%d",a);

1
return 0;
}

(A) 3.33
(B) 3.0
(C) 3
(D) 0

Ans: C

Hint: integer division ( int/ int ) is int only hence instead of printing 3.33 it will print 3 only. Float part is discarded in
such cases.

5. What should be the output:


int main()
{
int x = 10;
{
int x = 0;
printf("%d",x);
}
return 0;
}

(A) 10
(B) Compilation Error
(C) 0
(D) Undefined

Ans: C

Hint: It will print nearest values that is x =0

6. How many main() function we can have in our project?

(A) 1
(B) 2
(C) No Limit
(D) Depends on Compiler

Ans: A

Hint: We can't have more than one main() function in a project.

while loop in C

A while loop in C programming repeatedly executes a target statement as long as a given


condition is true.

Syntax
The syntax of a while loop in C programming language is −

2
while(condition) {
statement(s);
}
Here, statement(s) may be a single statement or a block of statements. The condition may be
any expression, and true is any nonzero value. The loop iterates while the condition is true.
When the condition becomes false, the program control passes to the line immediately following
the loop.

Flow Diagram

Here, the key point to note is that a while loop might not execute at all. When the condition is
tested and the result is false, the loop body will be skipped and the first statement after the while
loop will be executed.

Example

#include <stdio.h>

int main () {

/* local variable definition */


int a = 10;

/* while loop execution */


while( a < 20 ) {
printf("value of a: %d\n", a);
a++;
}

3
return 0;
}

When the above code is compiled and executed, it produces the following result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
do...while loop in C
Unlike for and while loops, which test the loop condition at the top of the loop,
the do...while loop in C programming checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at least
one time.

Syntax
The syntax of a do...while loop in C programming language is −
do {
statement(s);
} while( condition );
Notice that the conditional expression appears at the end of the loop, so the statement(s) in the
loop executes once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop
executes again. This process repeats until the given condition becomes false.

Flow Diagram

4
Example

#include <stdio.h>

int main () {

/* local variable definition */


int a = 10;

/* do loop execution */
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );

return 0;
}

When the above code is compiled and executed, it produces the following result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
for loop in C
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times.

5
Syntax
The syntax of a for loop in C programming language is −

for ( initialization; condition; increment ) {

statement(s);
}
Here is the flow of control in a 'for' loop −
 The init step is executed first, and only once. This step allows you to declare and initialize
any loop control variables. You are not required to put a statement here, as long as a
semicolon appears.
 Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false,
the body of the loop does not execute and the flow of control jumps to the next statement
just after the 'for' loop.
 After the body of the 'for' loop executes, the flow of control jumps back up to
the increment statement. This statement allows you to update any loop control variables.
This statement can be left blank, as long as a semicolon appears after the condition.
 The condition is now evaluated again. If it is true, the loop executes and the process
repeats itself (body of loop, then increment step, and then again condition). After the
condition becomes false, the 'for' loop terminates.

Flow Diagram

6
Example

#include <stdio.h>

int main () {

int a;

/* for loop execution */


for( a = 10; a < 20; a = a + 1 ){
printf("value of a: %d\n", a);
}

return 0;
}

When the above code is compiled and executed, it produces the following result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
7
value of a: 17
value of a: 18
value of a: 19

C - if statement
The syntax of the  if  statement in C programming is:

if (test expression)
{
// statements to be executed if the test expression is true
}

How if statement works?

The  if  statement evaluates the test expression inside the parenthesis  () .


 If the test expression is evaluated to true, statements inside the body of  if  are executed.
 If the test expression is evaluated to false, statements inside the body of  if  are not
executed.

Example

#include <stdio.h>

int main () {

/* local variable definition */


int a = 10;

/* check the boolean condition using if statement */

if( a < 20 ) {
/* if condition is true then print the following */
printf("a is less than 20\n" );
}

printf("value of a is : %d\n", a);

8
return 0;
}

When the above code is compiled and executed, it produces the following result −
a is less than 20;
value of a is : 10

if...else Statement
The  if  statement may have an optional  else  block. The syntax of the  if..else  statement is:

if (test expression) {
// statements to be executed if the test expression is true
}
else {
// statements to be executed if the test expression is false
}

How if...else statement works?

If the test expression is evaluated to true,

 statements inside the body of  if  are executed.


 statements inside the body of  else  are skipped from execution.
If the test expression is evaluated to false,

 statements inside the body of  else  are executed


 statements inside the body of  if  are skipped from execution.

9
Example : if...else statement

// Check whether an integer is odd or even

#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);

// True if the remainder is 0


if (number%2 == 0) {
printf("%d is an even integer.",number);
}
else {
printf("%d is an odd integer.",number);
}

return 0;
}

Output

Enter an integer: 7
7 is an odd integer.

When the user enters 7, the test expression  number%2==0  is evaluated to false. Hence, the
statement inside the body of  else  is executed.
10
11

You might also like