1.2
– C is terse,it has fewer keywords
– C is structured and modular.
– C is the basis for C++ and Java
– C is portable.Code written on one machine can
be easily moved to another.
< >
>
>
>>
The C Programming Language Chapter 1 An Overview of C
32 keywords ( )
auto break case char const
continue default do double else
enum extern float for goto
if int long register return
short signed sizeof static struct
switch typedef unsigned union void
volatile while
<
The C Programming Language Chapter 1 An Overview of C
<
The C Programming Language Chapter 1 An Overview of C
1.3
/* example1.1 The first C Program*/
#include <stdio.h>
main()
{
printf(“Hello,World!”);
}
>
notation
Include information
about standard library
define a function named main
statement
output
Hello,World!
The C Programming Language Chapter 1 An Overview of C
/* example1.1 calculate the sum of a and b*/
#include <stdio.h>
/* This is the main program */
main()
{ int a,b,sum;
a=10;
b=24;
sum=add(a,b);
printf(”sum=%d\n",sum);
}
/* This function calculates the sum of x and y */
int add(int x,int y)
{ int z;
z=x+y;
return(z);
}
sum=34
>
The C Programming Language Chapter 1 An Overview of C
–
< >
main( )
{ ……………….
………………
…………..
…………..
………
……….
……………
…………….
……………….
………………..
}
main( )
{
int i,j,sum;
sum=0;
for(i=1; i<10;i++)
{
for(j=1;j<10;j++)
{
sum+=i*j ;
}
}
printf(“%d\n”,sum);
}
:
TAB
{}
The C Programming Language Chapter 1 An Overview of C
–
–
– main()
– mainmain
– C
– ;
– /* */,
–
< >
/*This is the main /* of example1.1*/ */
The C Programming Language Chapter 1 An Overview of C
1.4 C
C
< >
f i l e,c
f i l e,obj
file.exe
,
*.c
*.obj
,
*.exe
.c,o b j,e x e
The C Programming Language Chapter 1 An Overview of C
=+
…
…
1+2+3+…+100=
11+234…,1005050
2100+1+99+2+98+…+ 49+51+50=100+49*100+50=5050
step1
step2
step3
step4
step12?x 3? y
step2x+y 2+3
step35? z
step4 z
#include <stdio.h>
void main( )
{ int x,y,z;
x=2; y=3;
z=x+y;
printf(“z=%d\n”,z);
printf(“%d+%d=%d\n”,x,y,z);
}
:
z=5
2+3=5
1.
2.
3.
4.
5.
1,
,
,
2.,
,
3,
A
B
P
A B
A
P
A
P
4,N-S
A
B
A B
P
A
P
P
A
While Until
10
#include <stdio.h>
void main( )
{ int x,max,i ;
scanf(“%d”,&max);
i=1;
do
{ scanf(“%d”,&x);
if (x>max) max=x;
i=i+1;
}
while ( i<9) ;
printf(“max=%d”,max) ;
}
5
5
t 1
i 2
i <= 5,:
t = t * i
i = i + 1
()
t
BEGIN()
1 => t
2 => i
while i <= 5
{ t * i => t
i + 1 => i
}
print t
END()
1
2
3
4
P
p1 p2 p3
p12p11 p32p31 p33
Preparations
Data types
Constants and Variables
Type Conversions
Operators,and Expressions
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
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
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
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
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
– ——
–
– 1
– 11
–
<
+7 00000111 00000111 00000111
-7 10000111 11111000 11111001
Chapter 3 Data types,Operators,and Expressions
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
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
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……..s m Sum M.D.John day Date 3days
student_name #33 lotus_1_2_3
char a>b _above $123
< >
M.D.John 3days
#33
char $123a>b
Chapter 3 Data types,Operators,and Expressions
– 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
Integer constants
……
……
……
<
0123 = ( )10
0x123 = ( )10
0Xff = ( )10
83
291
255
( 10
10
ff ( )10
Chapter 3 Data types,Operators,and Expressions
Floating-point constants
……
<
Chapter 3 Data types,Operators,and Expressions
Character constants
\101‘ -----------?A‘?\012‘ -----------‘\n‘
\376‘ -----------‘?‘?\x61‘ -----------‘a‘
\60‘ -----------‘0‘?\483‘ ----------(?)
:
A‘-------‘\101‘-------‘\x41‘--------65
<
‘—— ‘——
‘——? ‘——
a’ ‘A’ ‘?’ ‘\n’
(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
<
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
…...
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
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
/*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
3.3 Type conversions
< >
Chapter 3 Data types,Operators,and Expressions
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
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
3.4 Operators,and Expressions
< >
Chapter 3 Data types,Operators,and Expressions
< >
Chapter 3 Data types,Operators,and Expressions
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
Increment and decrement operators
― ‖
― ‖
< >
Chapter 3 Data types,Operators,and Expressions
Chapter 3 Data types,Operators,and Expressions
’
,”
Chapter 3 Data types,Operators,and Expressions
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
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
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
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
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
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
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
! (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
! (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
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