### GE6151 Computer Programming THE CONDITIONAL EXPRESSION OPERATOR or TERNARY OPERATOR

THE CONDITIONAL EXPRESSION OPERATOR or TERNARY OPERATOR

This conditional expression operator takes THREE operators. The two symbols used to denote this operator are the ? and the :. The first operand is placed before the ?, the second operand between the ? and the :, and the third after the :. The general format is,

condition ? expression1 : expression2

If the result of condition is TRUE ( non-zero ), expression1 is evaluated and the result of the evaluation becomes the result of the operation. If the condition is FALSE (zero), then expression2 is evaluated and its result becomes the result of the operation. An example will help,

s = ( x < 0 ) ? -1 : x * x;

If x is less than zero then s = -1

If x is greater than zero then s = x * x

Example:

#include <stdio.h>

main()

{

int input;

printf("I will tell you if the number is positive, negative or zero!"\n");

scanf("%d", &input );

(input < 0) ? printf("negative\n") : ((input > 0) ? printf("positive\n") : printf("zero\n"));

}

BIT OPERATIONS

C has the advantage of direct bit manipulation and the operations available are,

 AND & sum = sum & 2; 4 0 OR | sum = sum | 2; 4 6 Exclusive OR ^ sum = sum ^ 2; 4 6 1's Complement ~ sum = ~sum; 4 -5 Left Shift << sum = sum << 2; 4 16
Operation Operator Comment Value of Sum before Value of sum after

Right Shift >> sum = sum >> 2; 4 0

Example:

/* Example program illustrating << and >> */

#include <stdio.h>

main()

{

int n1 = 10, n2 = 20, i = 0;

i = n2 << 4; /* n2 shifted left four times */

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

i = n1 >> 5; /* n1 shifted right five times */

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

}

Example:

/* Example program using EOR operator */

#include <stdio.h>

main()

{

int value1 = 2, value2 = 4;

value1 ^= value2; value2 ^= value1; value1 ^= value2;

printf("Value1 = %d, Value2 = %d\n", value1, value2);

}

Example:

/* Example program using AND operator */

#include <stdio.h>

main()

{

int loop;

for( loop = 'A'; loop <= 'Z'; loop++ )

printf("Loop = %c, AND 0xdf = %c\n", loop, loop & 0xdf);

}

MANAGING INPUT AND OUTPUT OPERATORS

Printf ():

printf() is actually a function (procedure) in C that is used for printing variables and text. Where

text appears in double quotes "", it is printed without modification. There are some exceptions however. This has to do with the \ and % characters. These characters are modifier's, and for the present the \ followed by the n character represents a newline character.

Example:

#include <stdio.h>

main()

{

printf("Programming in C is easy.\n");

printf("And so is Pascal.\n");

}

@ Programming in C is easy.

And so is Pascal. FORMATTERS for printf are,

Cursor Control Formatters

\n newline

\t tab

\r carriage return

\f form feed

\v vertical tab

Scanf ():

Scanf () is a function in C which allows the programmer to accept input from a keyboard.

Example:

#include <stdio.h>

main() /* program which introduces keyboard input */

{

int number;

printf("Type in a number \n");

scanf("%d", &number);

printf("The number you typed was %d\n", number);

}

FORMATTERS FOR scanf()

The following characters, after the % character, in a scanf argument, have the following effect. d read a decimal integer

o read an octal value

x read a hexadecimal value h read a short integer

l read a long integer f read a float value

e read a double value

c read a single character

s read a sequence of characters

[...] Read a character string. The characters inside the brackets

Accepting Single Characters From The Keyboard

Getchar, Putchar

getchar() gets a single character from the keyboard, and putchar() writes a single character

from the keyboard. Example:

The following program illustrates this,

#include <stdio.h>

main()

{

int i;

int ch;

for( i = 1; i<= 5; ++i )

{

ch = getchar(); putchar(ch);

}

}

The program reads five characters (one for each iteration of the for loop) from the keyboard. Note that getchar() gets a single character from the keyboard, and putchar() writes a single character (in this case, ch) to the console screen.