What will be the output of this code

 //1. What will be the output of this code 


#include<Stdio.h>

int main()

{

int x = 2;

if(x = 1){

printf("x is equal to 1");

    }

    

    else

    {

    printf("x is not equal to 1");

}

return 0;

}

a. error

b. x is equal to 1

c. x is not equal to 1


Ans. b. x is equal to 1, because when we write x = 1, that means we assign x =1 not asking (x==1) whether x is equal to 1 or not. so in if condition value of x becomes 1.


// 2. What will be the output of this code 


#include<Stdio.h>

int main()

{

int x = 2;

if(x = 0){

printf("x is equal to 1");

    }

    

    else

    {

    printf("x is not equal to 1");

}

return 0;

}

a. error

b. x is equal to 1

c. x is not equal to 1

Ans : x is not equal to 1 because x = 2.

Comments

Popular posts from this blog

program to (keep taking number as input from user until user enters a number which is multiple of 5.)

write a program to print the numbers from 0 to n, if n is given by user.

Write a program to print the average of 3 numbers.