|
The
conditional operator (? :) is a ternary operator (it takes three
operands). The conditional operator works as follows
-
The
first operand is implicitly converted to bool. It is evaluated and
all side effects are completed before continuing.
-
If
the first operand evaluates to true (1), the second operand is
evaluated.
-
If
the first operand evaluates to false (0), the third operand is
evaluated.
The
result of the conditional operator is the result of whichever operand is
evaluated — the second or the third. Only one of the last two operands is
evaluated in a conditional expression.
|
#include<stdio.h>
void main()
{
int a,b;
printf("Enter value of a);
scanf("%d",&a);
printf("Enter value of b);
scanf("%d",&b);
(a>b)?printf("%d is
big",a):printf("%d is big",b);
getch();
}
|
|
|
Where
type refers to an existing data type may belong to any class of type ,
including the user defined ones. Remember that the new type is 'new' only
in name , but not the data type. typedef can not create a new type. Some
example of type definition are
typedef int unit;
typedef float marks;
|
Here , unit symbolizes int and mark symbolizes folat. They can be later
used to declare variables as follows.
units batch1, batch2;
marks name1[50], name2[50];
|
Bacth1
and batch2 are declare as int variable and name1[50] and name2[50] are declared
as 50 element of floating point array variables. The main advantage of
typedef is that we can create meaningful data type name for increasing the
readability of the program
Another
user-defined data type is enumerated data type provided by ANSI standard.
It is defined as follows.
enum identifier { value1,value2,....valuen };
|
The identifier is user -defined enumerated data type which can be used to declare
variable that can have one of the value enclosed with in the braces (
known as enumeration constant). After this we can declare variables
to be of this new type as below.
enum identifier v1,v2,...vn.
|
The enumerated variable v1,v2,..vn can only have one of the following
types are valid;
An example
|
enum day { Monday, Tuesday,...,Sunday};
enum day week_st, Week_end;
Weel_st= Monday;
week_end= Friday;
if(week_st==Tuesday)
week_end=Saturday;
|
The compiler automatically assign integer digit beginning with 0 to all
the enumeration constants that is , the enumeration constant value1 is
assign 0, value 1 , and so on. However the automatic assignment can be
overridden by assigning values explicitly to the enumeration constant For
example
enum day { Monday=1, Tuesday,...Sunday };
|
Here, the constant Monday is assigned the value of 1. The remaining
constants are assigned values that increase successively by 1.
The
definition and deceleration of enumerated variable can be combined in one statement
enum day { Monday,....Sunday} Week_st, Week_end;
|
|