1
Preparations
Data types
Constants and Variables
Type Conversions
Operators,and Expressions
2
Preparations
convert Binary OctalHexadecimal into Decimal
100123452 592121202121211 1 1 0 1 1
100128 94868381136
10012316 7 9 7 81610162161516121AF
< >
Chapter 3 Data types,Operators,and Expressions
3
0
59
592
292
142
72
32
12
0
(59)10=(111011)2
1
1
0
1
1
1
1 1 1 0 1 1
159
1598
198
28
0
(159)10=(237)8
2 3 7
7
3
2
459
45916
2816
116
0
(459)10=(1CB)16
1 C B
11
12
1
Chapter 3 Data types,Operators,and Expressions
convert Binary OctalHexadecimal into Decimal
4
Binary and Octal
Binary into Octal
Octal into Binary 3
(1101001)2=(001,101,001)2=(151)8
(246)8=(010,100,110)2=(10100110)2
000 ~ 0
001 ~ 1
010 ~ 2
011 ~ 3
100 ~ 4
101 ~ 5
110 ~ 6
111 ~ 7
< >
Chapter 3 Data types,Operators,and Expressions
5
Binary and Hexadecimal
Binary into Hexadecimal
Hexadecimal into Binary 4
(11010101111101)2=(0011,0101,0111,1101)2=(357D)16
(4B9E)16=(0100,1011,1001,1110)2=(100101110011110)2
0000 ~ 0
0001 ~ 1
0010 ~ 2
0011 ~ 3
0100 ~ 4
0101 ~ 5
0110 ~ 6
0111 ~ 7
1000 ~ 8
1001 ~ 9
1010 ~ A
1011 ~ B
1100 ~ C
1101 ~ D
1110 ~ E
1111 ~ F
< >
Chapter 3 Data types,Operators,and Expressions
6
Byte and Bit
Memory Byte
ByteAddress
Byte8Bit
Bit01
01234567
0
1
2
3
4
5
6
7
8
9
10 ……..
.
< >
7
6
4
3
2
5
1
Chapter 3 Data types,Operators,and Expressions
7
– ——
–
– 1
– 11
–
<
+7 00000111 00000111 00000111
-7 10000111 11111000 11111001
Chapter 3 Data types,Operators,and Expressions
8
3.1 Data types
< >
1,
2,
3,
Chapter 3 Data types,Operators,and Expressions
Constructed
types
pointers
void
Character types char
enum
integer
floating single precision floating point float
double precision floating point double
short int
long int
int
arrays
structures
unions
Arithmetic
types
9
Basic types
< >
Type Keywords Available range of valuesBits
integer
(signed)int 16 -32768~32767
(signed)short 16 -32768~32767
(signed)long 32 -2147483648~2147483647
16unsigned int 0~65535
32 0~4294967295unsigned long
unsigned short 16 0~65535
float 32 1e-37~1e38
double 64 1e-307~1e308
char 8
:,:
long double 80 1e-4931~1e4932
character
Chapter 3 Data types,Operators,and Expressions
10
3.2 Constants and Variables
– Identifiers ()
An identifier is a sequence of letters,digits and
underscore _ ;
The first character must be a letter or an underscore _;
Upper and lower case letters are different ;
Can not use keywords as variable names ;
It‘s wise to choose variable names that are related to the
purpose of the variable ;
Notice,such as l and I,o and 0……..sum Sum M.D.John day Date 3days
student_name #33 lotus_1_2_3
char a>b _above $123
< >
M.D.J 3da
#33
$123a>b
Chapter 3 Data types,Operators,and Expressions
11
– Use upper case letters
– It is macro definition,not statements.
#define PRICE 30
Constants
A #define line defines a symbolic
name or symbolic constant to be a particular string of characters,
#define identifier replacement text (constant)
>
>
>
>
< >
(ch2_1.c)
#define PRICE 30
main()
{
int num,total;
num=10;
total=num*PRICE;
printf("total=%d",total);
}
total=300
Chapter 3 Data types,Operators,and Expressions
12
Integer constants
……
……
……
<
……
0123 = ( )10
0x123 = ( )10
0Xff = ( )10
83
291
255
0123 = ( )10
0x123 = ( )10
0Xff = ( )10
Chapter 3 Data types,Operators,and Expressions
12 12L30000 int65536 long int
13
Floating-point constants
……
<
Chapter 3 Data types,Operators,and Expressions
14
Character constants
\101‘ -----------?A‘?\012‘ -----------‘\n‘
\376‘ -----------‘?‘?\x61‘ -----------‘a‘
\60‘ -----------‘0‘?\483‘ ----------(?)
:
A‘-------‘\101‘-------‘\x41‘--------65
<
‘—— ‘——
‘——? ‘——
a’ ‘A’ ‘?’ ‘\n’ ‘\101’
(ch2_001.c,ch2_004.c)
main()
{
printf("\101 \x42 C\n");
printf("I say:\"How are you?\"\n");
printf("\\C Program\\\n");
printf("Turbo \'C\'");
}
()
A B C
Isay:”How are you?”
\C Program\
Turbo ‘C’
main()
{ printf(―Y\b=\n‖);
}
=
The complete set of escape sequences is:
vertical tab\v
hexadecimal number\xhhhorizontal tab\t
octal number\dddcarriage return\r
double quote\''new line \n
single quote\'formfeed\f
question mark\?backspace\b
backslash\\alert (bell) character \a
Chapter 3 Data types,Operators,and Expressions
15
<
String constants
―‖
‘
h e l l o \0‖
a a \0a‘ ―a‖
\0
,char ch;
ch=―A‖;
,char ch;
ch=?A‘;
Chapter 3 Data types,Operators,and Expressions
16
…...
address
int a=1,b=-3,c;
a
b
c
2 bytes
2 bytes
2 bytes
address
address
…...
1
-3
random
number
– Variables
Name and Value
Declarations
data type var1 [ var2… var n] ;
< >
A variable may be initialized in its declaration;
:
int a,b,c;
float data;
Determine allocated bytes
and the range of the value
valid identifiers
:
int a=2,b,c=4;
float data=3.67;
char ch=?A‘;
int x=1,y=1,z=1;
int x=y=z=1;
All variables must be declared before use.
1
int student;
stadent=19; //Undefined symbol ‘statent’ in function main
2
float a,b,c;
c=a%b; // illegal use of floating point in function main
The declarations are generally at the beginning of the function.
main()
{ int a,b=2;
float data;
a=1;
data=(a+b)*1.2;
printf(―data=%f\n‖,data);
}
main()
{ int a,b=2;
a=1;
float data;
data=(a+b)*1.2;
printf(―data=%f\n‖,data);
}
3
a
name
value
unit
Chapter 3 Data types,Operators,and Expressions
17
Integer variables
Floating variables
~
Character variables float a;
a=111111.111; /* a=111111.1*/
double b;
b=111111.111; /* b=111111.111*/
‘
‘
‘? ‘< > There is no string variables
Chapter 3 Data types,Operators,and Expressions
18
/*ch2_003.c*/
#define PRICE 12.5
main()
{ int num=3;
float total;
char ch1,ch2=?D‘;
total=num*PRICE;
ch1=ch2-?A‘+?a‘;
printf(―total=%f,ch1=%c\n‖,total,ch1);
}
macro definition
declarations of variables
output
total=37.500000,ch1=d
Chapter 3 Data types,Operators,and Expressions
19
3.3 Type conversions
< >
Chapter 3 Data types,Operators,and Expressions
20
double float
long
unsigned
int char,short
:
Must convert
Convert when operands
are diffrent
char ch;
int i;
float f;
double d;
ch/i + f*d - (f+i)
int
int
double
double
double
double
double
double
int
int
double
double
double
double
double
double
10+?a‘ +i*f - d/l
int i;
float f;
double d;
long l;
< >
Chapter 3 Data types,Operators,and Expressions
21
main()
{ float x;
int i;
x=3.6;
i=(int)x;
printf(―x=%f,i=%d‖,x,i);
}
x=3.600000,i=3
Precision lost
Higher types convert into lower types may…
< >
Chapter 3 Data types,Operators,and Expressions
22
3.4 Operators,and Expressions
< >
Chapter 3 Data types,Operators,and Expressions
23< >
Chapter 3 Data types,Operators,and Expressions
24
Arithmetic Operators and Expressions
Arithmetic Operators
5/2 =
-5/2 =
5%2 =
-5%2 =
1%10 =
5%1 =
5.5%2
< >
5/2 2
-5/2 = -2
1
- -1
1
0
(?)
Chapter 3 Data types,Operators,and Expressions
25
Increment and decrement operators
― ‖
― ‖
< >
Chapter 3 Data types,Operators,and Expressions
26
Chapter 3 Data types,Operators,and Expressions
27
’
,”
Chapter 3 Data types,Operators,and Expressions
28
Assignment Operators and Expressions
Simple assignment Operators
Compound assignment Operators
a+=3 a=a+3
x*=y+8 x=x*(y+8)
x%=3 x=x%3
< >
Chapter 3 Data types,Operators,and Expressions
29
14
Left operands must be variables,can not be constants and
expressions
Assignment expressions can be nestedfloat f;
int i;
i=10;
f=i;
f=10.0
int i;
i=2.56; //i=2;
< >
Chapter 3 Data types,Operators,and Expressions
30
Comma Operators and Expressions
,,……
15
The value of the result are the value of the
a=3*5,a*4
a=3*5,a*4,a+5
x=(a=3,6*3)
x=a=3,6*a
a=1;b=2;c=3;
printf(―%d,%d,%d‖,a,b,c);
printf(―%d,%d,%d‖,(a,b,c),b,c);
< >
//a=15,60
//a=15,20
//18x=18
//,18,x=3
//1,2,3
//3,2,3
Chapter 3 Data types,Operators,and Expressions
:
/*ch2_6.c*/
#include <stdio.h>
main()
{ int x,y=7;
float z=4;
x=(y=y+6,y/z);
printf("x=%d\n",x);
}
x=3
31
Relational Operators and Expressions
<
<=
>
>=
==
!=
Precedence,6
Precedence,7
The results of relational expressions are represented
by 1 (true) or 0 (false)
int a=3,b=2,c=1,d,f;
a>b
(a>b)==c
b+c<a
d=a>b
f=a>b>c< >
//1
//1
//0
//d=1
//f=0
Chapter 3 Data types,Operators,and Expressions
32
Notice:
< >
a=0; b=0.5; x=0.3;
a<=x<=b 0
5>2>7>8C
0
int i=1,j=7,a;
a=i+(j%4!=0);
a= 2
a‘>0
A‘>100
1
0
Chapter 3 Data types,Operators,and Expressions
33
Notice:
=‖ ==‖
int a=0,b=1;
if(a=b)
printf(―a equal to b‖);
else
printf(―a not equal to b‖);
1.0/3.0*3.0==1.0
fabs(1.0/3.0*3.0-1.0)<1e-6
0
Chapter 3 Data types,Operators,and Expressions
34
a b !a !b a&&b a||b
T
F
T
F
FF
T
T
Logical Operators and Expressions
< >
T
F
F
F
F
F
T
T
F
F
T
T
T
F
T
T
Chapter 3 Data types,Operators,and Expressions
35
! (2)
&& (11)
|| (12)
a<=x && x<=b
a>b&&x>y
a==b||x==y
!a||a>b
!,right-to-left
&&,left-to-right
||,left-to-right
< >
// (a<=x) && (x<=b)
//(a>b)&&(x>y)
//(a==b)||(x==y)
//(!a)||(a>b)
Chapter 3 Data types,Operators,and Expressions
a=4;b=5;
!a
a&&b
a||b
!a||b
4&&0||2
5>3&&2||8<4-!0
c‘&&?d‘
//1
//0
//1
//1
//1
//1
//(5>3)&&2||(8<(4-(!0))) 1
36
! (2)
&& (11)
|| (12)
!,right-to-left
&&,left-to-right
||,left-to-right
< >
a&&b&&c //ab
ab c
a||b||c //ab
ab c
a=1;b=2;c=3;d=4;m=1;n=1;
(m=a>b)&&(n=c>d) //m=0,n=1
Chapter 3 Data types,Operators,and Expressions
37
if (a>b)
printf(“%d”,a);
else
printf(“%d”,b);
printf(“%d”,a>b?a:b);
a+|b|
printf(“a+|b|=%d\n”,b>0?a+b:a-b);
expr1
expr2 expr3
0 =0
(a==b)Y?:?N?
(x%2==1)?1:0
(x>=0)?x:-x
(c>=?a? && c<=?z?)?c-?a?+?A?:c
Conditional operators can be nested;
x>0?1:(x<0?-1:0)
Precedence,13
Associativity,right-to-left
a>b?a:c>d?c:d? a>b?a:(c>d?c:d)
expr1expr2expr3 may be different types,the value
of the expression takes the higher type.
< >
xa?:?b? //x=0,b?; x?0,a?
x>y?1:1.5 //x>y,1.0; x<y,1.5
Chapter 3 Data types,Operators,and Expressions
Conditional Operators and Expressions
expr1? expr2,expr3
Preparations
Data types
Constants and Variables
Type Conversions
Operators,and Expressions
2
Preparations
convert Binary OctalHexadecimal into Decimal
100123452 592121202121211 1 1 0 1 1
100128 94868381136
10012316 7 9 7 81610162161516121AF
< >
Chapter 3 Data types,Operators,and Expressions
3
0
59
592
292
142
72
32
12
0
(59)10=(111011)2
1
1
0
1
1
1
1 1 1 0 1 1
159
1598
198
28
0
(159)10=(237)8
2 3 7
7
3
2
459
45916
2816
116
0
(459)10=(1CB)16
1 C B
11
12
1
Chapter 3 Data types,Operators,and Expressions
convert Binary OctalHexadecimal into Decimal
4
Binary and Octal
Binary into Octal
Octal into Binary 3
(1101001)2=(001,101,001)2=(151)8
(246)8=(010,100,110)2=(10100110)2
000 ~ 0
001 ~ 1
010 ~ 2
011 ~ 3
100 ~ 4
101 ~ 5
110 ~ 6
111 ~ 7
< >
Chapter 3 Data types,Operators,and Expressions
5
Binary and Hexadecimal
Binary into Hexadecimal
Hexadecimal into Binary 4
(11010101111101)2=(0011,0101,0111,1101)2=(357D)16
(4B9E)16=(0100,1011,1001,1110)2=(100101110011110)2
0000 ~ 0
0001 ~ 1
0010 ~ 2
0011 ~ 3
0100 ~ 4
0101 ~ 5
0110 ~ 6
0111 ~ 7
1000 ~ 8
1001 ~ 9
1010 ~ A
1011 ~ B
1100 ~ C
1101 ~ D
1110 ~ E
1111 ~ F
< >
Chapter 3 Data types,Operators,and Expressions
6
Byte and Bit
Memory Byte
ByteAddress
Byte8Bit
Bit01
01234567
0
1
2
3
4
5
6
7
8
9
10 ……..
.
< >
7
6
4
3
2
5
1
Chapter 3 Data types,Operators,and Expressions
7
– ——
–
– 1
– 11
–
<
+7 00000111 00000111 00000111
-7 10000111 11111000 11111001
Chapter 3 Data types,Operators,and Expressions
8
3.1 Data types
< >
1,
2,
3,
Chapter 3 Data types,Operators,and Expressions
Constructed
types
pointers
void
Character types char
enum
integer
floating single precision floating point float
double precision floating point double
short int
long int
int
arrays
structures
unions
Arithmetic
types
9
Basic types
< >
Type Keywords Available range of valuesBits
integer
(signed)int 16 -32768~32767
(signed)short 16 -32768~32767
(signed)long 32 -2147483648~2147483647
16unsigned int 0~65535
32 0~4294967295unsigned long
unsigned short 16 0~65535
float 32 1e-37~1e38
double 64 1e-307~1e308
char 8
:,:
long double 80 1e-4931~1e4932
character
Chapter 3 Data types,Operators,and Expressions
10
3.2 Constants and Variables
– Identifiers ()
An identifier is a sequence of letters,digits and
underscore _ ;
The first character must be a letter or an underscore _;
Upper and lower case letters are different ;
Can not use keywords as variable names ;
It‘s wise to choose variable names that are related to the
purpose of the variable ;
Notice,such as l and I,o and 0……..sum Sum M.D.John day Date 3days
student_name #33 lotus_1_2_3
char a>b _above $123
< >
M.D.J 3da
#33
$123a>b
Chapter 3 Data types,Operators,and Expressions
11
– Use upper case letters
– It is macro definition,not statements.
#define PRICE 30
Constants
A #define line defines a symbolic
name or symbolic constant to be a particular string of characters,
#define identifier replacement text (constant)
>
>
>
>
< >
(ch2_1.c)
#define PRICE 30
main()
{
int num,total;
num=10;
total=num*PRICE;
printf("total=%d",total);
}
total=300
Chapter 3 Data types,Operators,and Expressions
12
Integer constants
……
……
……
<
……
0123 = ( )10
0x123 = ( )10
0Xff = ( )10
83
291
255
0123 = ( )10
0x123 = ( )10
0Xff = ( )10
Chapter 3 Data types,Operators,and Expressions
12 12L30000 int65536 long int
13
Floating-point constants
……
<
Chapter 3 Data types,Operators,and Expressions
14
Character constants
\101‘ -----------?A‘?\012‘ -----------‘\n‘
\376‘ -----------‘?‘?\x61‘ -----------‘a‘
\60‘ -----------‘0‘?\483‘ ----------(?)
:
A‘-------‘\101‘-------‘\x41‘--------65
<
‘—— ‘——
‘——? ‘——
a’ ‘A’ ‘?’ ‘\n’ ‘\101’
(ch2_001.c,ch2_004.c)
main()
{
printf("\101 \x42 C\n");
printf("I say:\"How are you?\"\n");
printf("\\C Program\\\n");
printf("Turbo \'C\'");
}
()
A B C
Isay:”How are you?”
\C Program\
Turbo ‘C’
main()
{ printf(―Y\b=\n‖);
}
=
The complete set of escape sequences is:
vertical tab\v
hexadecimal number\xhhhorizontal tab\t
octal number\dddcarriage return\r
double quote\''new line \n
single quote\'formfeed\f
question mark\?backspace\b
backslash\\alert (bell) character \a
Chapter 3 Data types,Operators,and Expressions
15
<
String constants
―‖
‘
h e l l o \0‖
a a \0a‘ ―a‖
\0
,char ch;
ch=―A‖;
,char ch;
ch=?A‘;
Chapter 3 Data types,Operators,and Expressions
16
…...
address
int a=1,b=-3,c;
a
b
c
2 bytes
2 bytes
2 bytes
address
address
…...
1
-3
random
number
– Variables
Name and Value
Declarations
data type var1 [ var2… var n] ;
< >
A variable may be initialized in its declaration;
:
int a,b,c;
float data;
Determine allocated bytes
and the range of the value
valid identifiers
:
int a=2,b,c=4;
float data=3.67;
char ch=?A‘;
int x=1,y=1,z=1;
int x=y=z=1;
All variables must be declared before use.
1
int student;
stadent=19; //Undefined symbol ‘statent’ in function main
2
float a,b,c;
c=a%b; // illegal use of floating point in function main
The declarations are generally at the beginning of the function.
main()
{ int a,b=2;
float data;
a=1;
data=(a+b)*1.2;
printf(―data=%f\n‖,data);
}
main()
{ int a,b=2;
a=1;
float data;
data=(a+b)*1.2;
printf(―data=%f\n‖,data);
}
3
a
name
value
unit
Chapter 3 Data types,Operators,and Expressions
17
Integer variables
Floating variables
~
Character variables float a;
a=111111.111; /* a=111111.1*/
double b;
b=111111.111; /* b=111111.111*/
‘
‘
‘? ‘< > There is no string variables
Chapter 3 Data types,Operators,and Expressions
18
/*ch2_003.c*/
#define PRICE 12.5
main()
{ int num=3;
float total;
char ch1,ch2=?D‘;
total=num*PRICE;
ch1=ch2-?A‘+?a‘;
printf(―total=%f,ch1=%c\n‖,total,ch1);
}
macro definition
declarations of variables
output
total=37.500000,ch1=d
Chapter 3 Data types,Operators,and Expressions
19
3.3 Type conversions
< >
Chapter 3 Data types,Operators,and Expressions
20
double float
long
unsigned
int char,short
:
Must convert
Convert when operands
are diffrent
char ch;
int i;
float f;
double d;
ch/i + f*d - (f+i)
int
int
double
double
double
double
double
double
int
int
double
double
double
double
double
double
10+?a‘ +i*f - d/l
int i;
float f;
double d;
long l;
< >
Chapter 3 Data types,Operators,and Expressions
21
main()
{ float x;
int i;
x=3.6;
i=(int)x;
printf(―x=%f,i=%d‖,x,i);
}
x=3.600000,i=3
Precision lost
Higher types convert into lower types may…
< >
Chapter 3 Data types,Operators,and Expressions
22
3.4 Operators,and Expressions
< >
Chapter 3 Data types,Operators,and Expressions
23< >
Chapter 3 Data types,Operators,and Expressions
24
Arithmetic Operators and Expressions
Arithmetic Operators
5/2 =
-5/2 =
5%2 =
-5%2 =
1%10 =
5%1 =
5.5%2
< >
5/2 2
-5/2 = -2
1
- -1
1
0
(?)
Chapter 3 Data types,Operators,and Expressions
25
Increment and decrement operators
― ‖
― ‖
< >
Chapter 3 Data types,Operators,and Expressions
26
Chapter 3 Data types,Operators,and Expressions
27
’
,”
Chapter 3 Data types,Operators,and Expressions
28
Assignment Operators and Expressions
Simple assignment Operators
Compound assignment Operators
a+=3 a=a+3
x*=y+8 x=x*(y+8)
x%=3 x=x%3
< >
Chapter 3 Data types,Operators,and Expressions
29
14
Left operands must be variables,can not be constants and
expressions
Assignment expressions can be nestedfloat f;
int i;
i=10;
f=i;
f=10.0
int i;
i=2.56; //i=2;
< >
Chapter 3 Data types,Operators,and Expressions
30
Comma Operators and Expressions
,,……
15
The value of the result are the value of the
a=3*5,a*4
a=3*5,a*4,a+5
x=(a=3,6*3)
x=a=3,6*a
a=1;b=2;c=3;
printf(―%d,%d,%d‖,a,b,c);
printf(―%d,%d,%d‖,(a,b,c),b,c);
< >
//a=15,60
//a=15,20
//18x=18
//,18,x=3
//1,2,3
//3,2,3
Chapter 3 Data types,Operators,and Expressions
:
/*ch2_6.c*/
#include <stdio.h>
main()
{ int x,y=7;
float z=4;
x=(y=y+6,y/z);
printf("x=%d\n",x);
}
x=3
31
Relational Operators and Expressions
<
<=
>
>=
==
!=
Precedence,6
Precedence,7
The results of relational expressions are represented
by 1 (true) or 0 (false)
int a=3,b=2,c=1,d,f;
a>b
(a>b)==c
b+c<a
d=a>b
f=a>b>c< >
//1
//1
//0
//d=1
//f=0
Chapter 3 Data types,Operators,and Expressions
32
Notice:
< >
a=0; b=0.5; x=0.3;
a<=x<=b 0
5>2>7>8C
0
int i=1,j=7,a;
a=i+(j%4!=0);
a= 2
a‘>0
A‘>100
1
0
Chapter 3 Data types,Operators,and Expressions
33
Notice:
=‖ ==‖
int a=0,b=1;
if(a=b)
printf(―a equal to b‖);
else
printf(―a not equal to b‖);
1.0/3.0*3.0==1.0
fabs(1.0/3.0*3.0-1.0)<1e-6
0
Chapter 3 Data types,Operators,and Expressions
34
a b !a !b a&&b a||b
T
F
T
F
FF
T
T
Logical Operators and Expressions
< >
T
F
F
F
F
F
T
T
F
F
T
T
T
F
T
T
Chapter 3 Data types,Operators,and Expressions
35
! (2)
&& (11)
|| (12)
a<=x && x<=b
a>b&&x>y
a==b||x==y
!a||a>b
!,right-to-left
&&,left-to-right
||,left-to-right
< >
// (a<=x) && (x<=b)
//(a>b)&&(x>y)
//(a==b)||(x==y)
//(!a)||(a>b)
Chapter 3 Data types,Operators,and Expressions
a=4;b=5;
!a
a&&b
a||b
!a||b
4&&0||2
5>3&&2||8<4-!0
c‘&&?d‘
//1
//0
//1
//1
//1
//1
//(5>3)&&2||(8<(4-(!0))) 1
36
! (2)
&& (11)
|| (12)
!,right-to-left
&&,left-to-right
||,left-to-right
< >
a&&b&&c //ab
ab c
a||b||c //ab
ab c
a=1;b=2;c=3;d=4;m=1;n=1;
(m=a>b)&&(n=c>d) //m=0,n=1
Chapter 3 Data types,Operators,and Expressions
37
if (a>b)
printf(“%d”,a);
else
printf(“%d”,b);
printf(“%d”,a>b?a:b);
a+|b|
printf(“a+|b|=%d\n”,b>0?a+b:a-b);
expr1
expr2 expr3
0 =0
(a==b)Y?:?N?
(x%2==1)?1:0
(x>=0)?x:-x
(c>=?a? && c<=?z?)?c-?a?+?A?:c
Conditional operators can be nested;
x>0?1:(x<0?-1:0)
Precedence,13
Associativity,right-to-left
a>b?a:c>d?c:d? a>b?a:(c>d?c:d)
expr1expr2expr3 may be different types,the value
of the expression takes the higher type.
< >
xa?:?b? //x=0,b?; x?0,a?
x>y?1:1.5 //x>y,1.0; x<y,1.5
Chapter 3 Data types,Operators,and Expressions
Conditional Operators and Expressions
expr1? expr2,expr3