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

Write a program to check if a number is prime or not.

Write a program to print the grades to a student.

write a program (keep taking numbers as input from user until user enters an Odd number)