สอน chap2 ที่ปริ๊นมา
TRANSCRIPT
-
8/10/2019 Chap2
1/34
240-120 Introduction to Computer Programming
--- 2 ---
1
2 Introduction to C Language Programming
1.
1.
1.1 1.2
1.3
1.4
1.5 1.6
1.7 1.8
1.9
1.10
2.
2.1
2.2 ASCII
2.3
2.4 2.5
3.
3.1
3.2 3.3
3.4 3.5
3.6 -3.7
-
8/10/2019 Chap2
2/34
240-120 Introduction to Computer Programming
--- 2 ---
2
1. (Introduction to basic C language)
240-120 Introduction to ComputerProgramming
1.1
(Function)(Procedure) 1 main
main
main ( )
{ printf ( My house) ;
printf (is very beautiful);
triangle( );
box( );
prinf(finish) ;
}
triangle( ) triangle{ /* draw triangle */
}
box ( ) box{ /* draw box */
}
1.1
printf ..
printf..
triangle
box
printf.
triangle
box
-
8/10/2019 Chap2
3/34
240-120 Introduction to Computer Programming
--- 2 ---
3
main main
1.2 2
main 2
1.2.1 (Heading)
( )
int main(char) main char int
1.2.2 (Compound Statements) 3
(Variable Declaration)
(Statement) ;
{ }
-
8/10/2019 Chap2
4/34
240-120 Introduction to Computer Programming
--- 2 ---
4
(Heading){ (Variable Declarations)
(Statements)
}
1.3
1. (Source file) .c .cpp (Editor)
( )
2. (C Compiler) (Object file) .obj ( )
3. (Linker)
(C Library) .exe ( )
1.2
1.
Source file
C Compiler
ComputerObject fileComputer Execute file
Linker Library
-
8/10/2019 Chap2
5/34
240-120 Introduction to Computer Programming
--- 2 ---
5
#i ncl ude
voi d mai n( voi d){
pr i nt f ( Hel l o Wor l d) ;}
1.c2.
1.obj3.
stdio.h ( #include ) 1.exe** printf printf stdio.h
stdio.h printf
1.4 3
1.4.1
.h1.4.2 1.4.3
2.2
#i ncl ude
char a;
i nt mai n( voi d){
a = 23;
pr i nt f ( Hel l o Wor l d) ;r et ur n a;
}
1.3
-
8/10/2019 Chap2
6/34
240-120 Introduction to Computer Programming
--- 2 ---
6
Hello World
- #include stdio.h - main main
- char a a - a = 23 a 23
- printf(Hello World) printf
** (C Statement) ;
1.5 1. printf , scanf , for2. ; printf(Hello) ;3. (Free Format)
printf(Hello); printf(Goodbye); a = 95;
1 1
1.6 (Comment)
/* */
/* ..*/
/*
. .
..*/
-
8/10/2019 Chap2
7/34
240-120 Introduction to Computer Programming
--- 2 ---
7
1.7 2
1.7.1 (Keywords) auto default float register struct volatile break
do far return switch while case double
goto short typedef char else if signed
union const enum int sizeof unsigned continue
extern long static void
1.7.2 (User Defines words)
test Test
_
_
** Borland C 32
1.8 (Variable)
char, int, long, float, double, unsigned int, unsigned long int,
2 1. Global Variable
0 ()
-
8/10/2019 Chap2
8/34
240-120 Introduction to Computer Programming
--- 2 ---
8
2. Local Variable
, , ,.....;
1. char 1
1 A , b , 1 , ?2. integer
-32768 32767 2 5 -10 25343. long 2
( long int )4. float a.b x 10e
4 3.4E-38 3.4E+38 6 10.625 -6.67
5.
double
float
2 8 1.7E-308 1.7E+3086. unsigned
() unsigned int
Char 8 ASCII character (-128 127)Unsigned char 8 0-255
Int 16 -32768 32767long int 32 -2147483648 2147483649Float 32 3.4E-38 3.4E+38 6 Double 64 1.7E-308 1.7E+308 12 Unsigned int 16 0 65535Unsigned long int 32 0 4294967296
1.1
-
8/10/2019 Chap2
9/34
240-120 Introduction to Computer Programming
--- 2 ---
9
11. #i ncl ude2. #def i ne PI 3. 141593. i nt ar ea; / * gl obal var i abl e */
4. mai n( ) 5. { f l oat r adi us; / * l ocal var i abl e * / 6. f l oat pr ocess( ) / * f uncti on decl ar at i on */ 7. pr i nt f ( Radi us = ?) ; scanf ( %f , r adi us) ; 8. pr ocess( ) ; 9. pr i nt f ( Ar ea = %f , ar ea) ; 10. pr i nt f ( %f , r adi us) ;
}
11. f l oat pr ocess( ) 12. { f l oat r adi us; / * l ocal var i abl e */ 13 pr i nt f ( Radi us=?) ; scanf ( %f , r adi us) ; 14 ar ea=PI *r adi us*r adi us; 15 pr i nt f ( Ar ea = %f , ar ea) ; 16 pr i nt f ( %f , r adi us) ;
}
2 i nt a ;
a -35768 32767
3 i nt num1=8; num1 8
4 f l oat money, pr i ce ; money price 6
5 char ch= A ch 1 A
6 unsi gned l ong i nt t est ;
-
8/10/2019 Chap2
10/34
240-120 Introduction to Computer Programming
--- 2 ---
10
test
char a,b,c,d; /* a,b,c,d character */unsigned e; /* e unsigned int */
char key = A; /* key character A*/
char name = SAM /* name character SAM */
1.9 (Preprocessor)
#
#include #define #if #program
#endif #error #ifndef #undef
#elif #else #ifdef
2
1.9.1. #include
#include #include filename
#include dos.h
#include sample.h sample.h
* Option directory
* (Header Files) .h
1 stdio.h printf
-
8/10/2019 Chap2
11/34
240-120 Introduction to Computer Programming
--- 2 ---
11
main() process clrscr() conio.h include conio.h
1.9.2 #define
#define ()
#define TEN 10 TEN 10#define PI 3.141592654 PI 3.141592654
constant 3
1 2 3 ( ASCII)
1
255 decimal int 0xFF hexadecimal int 0377 octal int 255L 255l long int 255U 255u unsigned int 0xFFUL unsigned long
int
15.75E2 floating point -1.23 floating point .123 floating point 123F floating point
a character
string ,
1.2
-
8/10/2019 Chap2
12/34
240-120 Introduction to Computer Programming
--- 2 ---
12
1.10 (Statement and Expression)
, (Statement)
; {} (Statement)
(Operands) (Operators) 3 * 2 - 1 + 7 a * 5
7 + 5 a + b
( a * 3 ) + 5 2.0 * sin(2.12)
-
8/10/2019 Chap2
13/34
240-120 Introduction to Computer Programming
--- 2 ---
13
2. (Input and Output)
2.1
2.2 (ASCII) American Standard Code for
Information Interchange 128 128
Extended ASCII code - .
(Characters) Character
A A
4 4
b b
\0 0
\x7 7
(String)
(Double
qoute) Hello World! Hello World!
-
8/10/2019 Chap2
14/34
240-120 Introduction to Computer Programming
--- 2 ---
14
2.3
printf
printf ( control , argument list ) ;
2 control
control 2
printf ( sum of x = );
sum of x =
(Format Code)
%
%d int %ld long int long%u unsigned int unsigned%c char %s string %o int (octal) %x int (hexa) %f float %e float, double %lf double double
-
8/10/2019 Chap2
15/34
240-120 Introduction to Computer Programming
--- 2 ---
15
2.1 printf argument list
argument list
1 ( , )* argument list Control Control Argument list
** (Format Code)
Escape Sequence \ (Back-Slash) 1
Escape Sequence
\a 0x07 \b 0x08 cursor \f 0x0c \n 0x0a \r 0x0d cursor \t 0x09 tab \v 0x0b tab
\\ 0x5c \\ 0x2c
\ 0x22 \? 0x3f ?
2.2 Escape Sequence
-
8/10/2019 Chap2
16/34
240-120 Introduction to Computer Programming
--- 2 ---
16
1 printf
#i ncl udemai n( ){ i nt sum=5; f l oat e=11. 55;
pr i nt f ( Thi s i s t he t est Pr ogr am. ) ;pr i nt f ( The r esul t i s %d \ n, sum) ;pr i nt f ( The r esul t i s %f %e , e, e) ;
}
Thi s i s t he t est Pr ogram. The r esul t i s 5The r esul t i s 11. 550000 1. 155000e+01
2 printf
#i ncl udemai n( ){ char a= A ;
char name=Ar ee;i nt x, y;
pr i nt f ( What i s the f i r st char acter \ n);pr i nt f ( The f i r st char acter i s %c \ n\ n, a) ;x=10; y=15;pr i nt f ( x+y= %d \ n, x+y) ;
pr i nt f ( %d 5 = %d \ n, y, y- 5) ;x=65;pr i nt f ( x i s %c, x) ;
pr i nt f ( Thi s progr am r un by %s, name) ;}
What i s t he f i r st char act erThe f i r st char act er i s A
x+y=2515- 5=10x i s 65 Thi s pr ogr am r un by Ar ee
(field) + -
-
8/10/2019 Chap2
17/34
240-120 Introduction to Computer Programming
--- 2 ---
17
3 printf
#i ncl ude voi d mai n( ){
pr i nt f ( | 1234567890| \ n) ;pr i nt f ( | %10s| \ n, pr i ce) ;
} price 10
% 3
| 1234567890|| pr i ce|
price 10 - printf() 3
pr i nt f ( | %- 10s| \ n, pr i ce) ;
| 1234567890|| pr i ce |
float a.b a b 6 printf() 4
pr i nt f ( | %10. 3f | \ n, 1999. 95) ;
6
-
8/10/2019 Chap2
18/34
240-120 Introduction to Computer Programming
--- 2 ---
18
| 1234567890|| _ _1999. 950|
1999.95 10 3
int string
7 printf() 4
pr i nt f ( | %. 10s| \ n, Thi s program i s r unni ng) ;
pr i nt f ( | %. 10d| \ n, 20) ;
7
| 1234567890|| Thi s pr ogr || 0000000020|
2.4
scanf
scanf ( control , argument list );
control control
printf** control scanf
-
8/10/2019 Chap2
19/34
240-120 Introduction to Computer Programming
--- 2 ---
19
argument list argument list
( , )
scanf & (string) ** scanf argument list 1 scanf
char a; int b;
char name[22]; /* name 22 21 \0 22 */
scanf(%c %d,&a,&b); /* scan char a scan int b */
scanf(%s,name); /* scan string name */
2 scanf integer x1
30 letter
#i ncl udemai n( ){ i nt x1;
char l et t er [ 30] ;scanf ( %d, &x1) ;scanf ( %s, l et t er ) ;
}
2.5 2.5.1
getchar ( )
getchar ( ) 1 enter
-
8/10/2019 Chap2
20/34
240-120 Introduction to Computer Programming
--- 2 ---
20
getchar ( ) ;
#i ncl udemai n ( ){ char ch;
ch=getchar ( ) ;}
1 ch enter
2.5.2 getch ( )getch ( ) 1
enter
getch ( ) ;
#i ncl ude mai n ( ){ char x;
x = get ch( ) ;}
-
8/10/2019 Chap2
21/34
240-120 Introduction to Computer Programming
--- 2 ---
21
1 x enter
2.5.3 gets ( )gets ( ) ()
( gets = get string)
gets ( n ) ;
n \0 enter
#i ncl ude mai n( ){ char name[10] ;
gets( name) ;}
name 10 name 9 name 10 () \0
2.5.4 putchar( )
putchar( ) 1
putchar ( ) ;
-
8/10/2019 Chap2
22/34
240-120 Introduction to Computer Programming
--- 2 ---
22
#i ncl ude
mai n ( ){ char x;
x=get ch ( ) ;pr i nt f ( Her e i s t he out put \ n) ;put char ( x ) ;
}
A
Here i s t he out putA
2.5.5 puts ( )puts ( )
puts ( n ) ;
n
#i ncl ude mai n ( ){ char name [ 10] ;
get s ( name) ;put s ( name) ;
}
name
-
8/10/2019 Chap2
23/34
-
8/10/2019 Chap2
24/34
240-120 Introduction to Computer Programming
--- 2 ---
24
C (Precedence)3.2
( ) - * -
* / + - * -
3.2 Precedence
a + b* c C % * / + - a+b+c a b c
%
/
a / b
a b integer integera b float float
39 / 5 739. / 5 7.8
-
8/10/2019 Chap2
25/34
240-120 Introduction to Computer Programming
--- 2 ---
25
integer floating operand
1/3( 0) 1.0/3.0 0.333 1.0/3.0 1.0/3 1/3.0 0 (Increment & Decrement)
- ++ - --
2 - (prefix) ++n- (postfix) n++
1 n ++n n++ ()++n n++
n 5
x = n++; x 5x = ++n; x 6
(i+j)++
-
8/10/2019 Chap2
26/34
240-120 Introduction to Computer Programming
--- 2 ---
26
3.2 (Relational, Equality, and Logical Operators)
Operator < (Relational Operator) >
=
== (Equality Operator) !=
! (Logical Operator) &&
||
3.3 3.2.1 (Relational Operator)
2 int 1 0 (0 )
4
< () > () = ( )
ab /* a=10 b=8 1 () */-1 >= (2.2*X+3.3)
-
8/10/2019 Chap2
27/34
-
8/10/2019 Chap2
28/34
240-120 Introduction to Computer Programming
--- 2 ---
28
**
1 !a /* a */!(x+7.7)
!(a
-
8/10/2019 Chap2
29/34
240-120 Introduction to Computer Programming
--- 2 ---
29
&& ()
2 3.4
P Q P && Q
0 0 0
0 1 0
1 0 0
1 1 1
3.4 && P Q
|| ()
2 3.5
P Q P || Q
0 0 0
0 1 1
1 0 1
1 1 1
3.5 || P Q
! ()
3.6
P !P
0 1
1 0
-
8/10/2019 Chap2
30/34
240-120 Introduction to Computer Programming
--- 2 ---
30
3.6 ! P
3.3
(Compound Operator)
+ 10
+= -= /= %=
= |= ^=
i = i + 1; i += 1;
i = I a; i - = a;i = 1 * (a + 1); i = *=a+1;i = i / (a-b); i /= a-b;i = i %101; i %= 101;i = i >= i;i = i & 01; i |= 0xf;i = i & 01; i | = 0xf;i = i ^ (07 | 0xb); i ^ = 07 | 0xb;
3.4. (Sizeof Operator)
Variable op= expression;
Variable = variable op expression;
Sizeof sizeof ()
-
8/10/2019 Chap2
31/34
240-120 Introduction to Computer Programming
--- 2 ---
31
i nt ABC;
pr i nt f ( %d, si zeof ( ABC) ) ;
2
ABC integer 2 integer
3.5. (Conditional Operator)
? (Condition Expressions) 1 ? 2 : 3
1 0 () 2 1 0 3
c = ((a+5)? (a+1) : 0);
a 1 a+5 c 2 a+1 a 5 a+5 c 0
#i ncl udevoi d mai n( voi d)
{ i nt a, b;a = 15;b = ( ( a+5) ? ( a+1) : 0) ;pr i nt ( b= %d\ n, b) ;a=0;
b=( ( a+5) ?( a+1) : 0) ;pr i nt f ( b= %d\ n, b) ;a=5;b=( ( a+5) ?( a+1) : 0) ;pr i nt f ( b= %d\ n, b) ;
}
b = 16
-
8/10/2019 Chap2
32/34
240-120 Introduction to Computer Programming
--- 2 ---
32
b = 1b = 0
3.6 -
- 3.7
(), [], ->, |, ~, ++, --, +(), -(), *, &(type), sizeof *, /, % +, - ()
= ==, != & ^ | && || ? : =, +=, -=, /=, %=, &=, ^=, |=, = ,
3.7
-
8/10/2019 Chap2
33/34
240-120 Introduction to Computer Programming
--- 2 ---
33
3.7 (Casting) int int
(int)(a*5.2) output int
(float)(b+5) output float
()
-
8/10/2019 Chap2
34/34
240-120 Introduction to Computer Programming34
C
a.ba
ab
+ d.
ab
ba
+
b.2
c
ba+ e.
2
+
dc
ba
c.
c
ba
cb
a
+
f.abc
cba
acbcab
++
++
a, b, c, d integer 4 float