×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

Find output of C programs (if else statement) | set 1

Find output of c programs based on if else statement in C programming language: this section contains if else statement based programs with output and explanation.
Submitted by Manju Tomar, on July 29, 2019

Find the output of the following programs,

Program 1)

#include <stdio.h>

int main()
{
    int x = 400, y, z;
    if (x >= 500)
        y = 400;
    z = 300;
    printf("%d %d\n", y, z);

    return 0;
}

Output

32766 300

Explanation:

In the code, the condition x>=500 is false, so the variable y will not be assigned and the statement z=300 is written after the conditional statement, so it will be assigned with 300. Thus, the value of y will be a garbage value and value of z will be 300.

Program 2)

#include <stdio.h>

int main()
{
    int p = 800, q, r;

    if (p >= 700)
        q = 600;

    r = 500;

    printf("%d %d\n", q, r);

    return 0;
}

Output

600 500

Explanation:

In the code, the condition p>=700 is true, so the variable q will be assigned with the value 600 and the statement r=500 is written after the conditional statement, so it will be assigned with 500. Thus, the value of q will be a 600 value and value of r will be 500.

Program 3)

#include <stdio.h>

int main()
{
    int a = 30, b = 40;

    if (a == b);

    printf("%d %d\n", a, b);

    return 0;
}

Output

30 40

Explanation:

In the code, the condition if(a==b); is terminated with semicolon so the statement printf("%d %d\n",a,b); will not be consider as a body of the if statement. Thus, the program will print the value of a and b.

Program 4)

#include <stdio.h>

int main()
{
    int e = 4;
    float f = 4.0;
    if (e == f) {
        printf("E and F are equal\n");
    }
    else {
        printf("E and F are not equal");
    }
    return 0;
}

Output

E and F are equal

Explanation:

In the code, variable e is an integer type and variable f is a float type, while comparing them with an if statement (if(e==f)), the value of f will be truncated to an integer (due to implicit type conversion). Thus, the condition will be true and "E and F are equal" will be printed.

Program 5)

#include <stdio.h>

int main()
{
    int p = 4, q, r;

    q = p = 15;
    r = p < 15;

    printf("p = %d q = %d r = %d\n", p, q, r);

    return 0;
}

Output

p = 15 q = 15 r = 0

Explanation:

In the code, the statement q = p = 15; is assigning 15 to the variables p and q and the statement r = p<15; is assigning 0 to the variable r because p is not less than 15 (condition is false). Thus, the value of p, q and r will be 15, 15, and 0.

Program 6)

#include <stdio.h>

int main()
{
    int i = 65;

    char j = 'A';

    if (i == j) {

        printf("This place is beautiful\n");
    }

    else {
        printf("This place is not beautiful\n");
    }

    return 0;
}

Output

This place is beautiful

Explanation:

The character variable stores the ASCII code of the given character (it's also a number type of variable). Thus, the value of j will be 65 (The ASCII Code of 'A' is 65). So the condition if(i==j) will be true and "This place is beautiful" will be printed.

Program 7)

#include <stdio.h>

int main()
{
    float p = 13.25, q = 14.5;

    if (p = q) {
        printf("Hello\n");
    }

    return 0;
}

Output

Hello

Explanation:

In the statement if(p=q), p=q is not a comparison operation, we used = (assignment operator), thus the value of q will be assigned into p and the statement will be evaluated to 14.5 which is a non-zero value and conditional will be true.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.