Voici une petite série d'exercices destinée à vous rendre plus à l'aise avec les opérateurs

Qu'imprime les programmes suivants ?


#include <stdio.h>
main()
{
	int x;
	
	x = - 3 + 4 * 5 - 6; printf("%d ",x);
	x = - 3 * 4 % - 6 / 5; printf("%d ",x);
	x = ( 7 + 6 ) % 5 / 2; printf("%d ",x);
	printf("\n");
	return 0;
}

#include <stdio.h>
#define PRINTX printf("%d ",x)

main()
{
	int x=2,y,z;
	
	x *=3 + 2; PRINTX;
	x *= y = z = 4; PRINTX;
	x= y == z; PRINTX;
	x == ( y = z) ; PRINTX;
	printf("\n");
	return 0;
}

#include <stdio.h>
#define PRINT(ent) printf("%d ",ent)

main()
{
	int x,y,z;

	x=2;y=1;z=0;
	x = x && y || z; PRINT(x);
	PRINT( x || ! y && z );

	x = y = 1;
	z = x ++ - 1; PRINT(x); PRINT(z);
	z += - x ++ + ++ y; PRINT(x); PRINT(z);
	printf("\n");
	return 0;
}

#include <stdio.h>
#define PRINT(ent)   printf("%d ",ent)

main()
{
	int x,y,z;
	x=3, y=2, z=1;
	PRINT(x|y&z);
	PRINT(x|y&~z);
	PRINT(x^y&~z);
	PRINT(x&y&&z);

	x=1;y=-1;
	PRINT(!x|x);
	PRINT(~x|x);
	PRINT(x^x);
	x<<=3; PRINT(x);
	y<<=3; PRINT(y);
	printf("\n");
	return 0;
}

#include <stdio.h>
#define PRINT(ent) printf("%d ",ent)
#define DEBPRINT   printf("(")
#define FINPRINT   printf(") ")

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

	x += y +=z;
	PRINT( x < y ? y : x);

	DEBPRINT;
	PRINT( x < y ? x ++ : y ++);
	PRINT(y); PRINT(z);
	FINPRINT;

	DEBPRINT;
	PRINT(z += x < y ? x ++ : y ++);
	PRINT(y); PRINT(z);
	FINPRINT;

	x=3; y=z=4;
	PRINT( (z >= y >= x) ? 1 : 0);
	PRINT( z >= y && y >= x);
	printf("\n");
	return 0;
}

#include <stdio.h>
#define PRINTXYZ printf("(%d,%d,%d) ",x,y,z)

main()
{
	int x,y,z;

	x = y = z =1 ;
	++x || ++y && ++z; PRINTXYZ;
	x = y = z = 1;
	++x && ++y || ++z; PRINTXYZ;
	x = y =z =1;
	++x && ++y && ++z;  PRINTXYZ;
	x = y = z = -1;
	++x && ++y || ++z; PRINTXYZ;
	x = y = z = -1;
	++x || ++y && ++z; PRINTXYZ;
	x = y = z = -1;
	++x && ++y && ++z; PRINTXYZ;
	printf("\n");
	return 0;
}





Retour aux exercices


Retout au cours