UIU: Learn C by Examples : Expressions


Program 1


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int max (int a, int b, int c) 
{
    return a > b ?  a > c ? a : c : b > c ? b : c;
}

int main ()
{
    printf ("%d", max (5, 2, 3));
    
    printf ("%d", max (2, 5, 3));
    
    printf ("%d", max (3, 2, 5));
    
	return 0;
}

// @END_OF_SOURCE_CODE

Program 2


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int a = -3, b = 0, c;

    c = ++a && ++b;

    printf ("%d\n", c);

	return 0;
}

// @END_OF_SOURCE_CODE

Program 3


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    printf ("%d\n", 10 & 2);

    printf ("%d\n", 10 | 2);

    printf ("%d\n", 5 & 2);

    printf ("%d\n", 5 | 2);

    printf ("%d\n", !2);

    printf ("%d\n", !-2);

    printf ("%d\n", !0);

	return 0;
}

// @END_OF_SOURCE_CODE

Program 4


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    printf ("%d\n", 8 << 1);

    printf ("%d\n", 8 >> 1);

    printf ("%d\n", 12 << 1);

    printf ("%d\n", 12 >> 1);

	return 0;
}

// @END_OF_SOURCE_CODE

Program 5


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int a = 4, b = -1, c = 0;
    int p, q, r, s;

    p = a || b || c;

    q = a && b && c;

    r = a || b && c;

    s = a && b || c;

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


	return 0;
}

// @END_OF_SOURCE_CODE

Program 6


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int x = 4, y, z;

    y = --x;
    z = x--;

    printf ("%d %d %d\n", x, y, z);

	return 0;
}

// @END_OF_SOURCE_CODE

Program 7


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int x = 55;

    printf ("%d %d %d", x > 55, x = 60, x <= 55);

	return 0;
}

// @END_OF_SOURCE_CODE

Acknowledgment: http://indiabix.com/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s