Professional Documents
Culture Documents
สอน Chap2 ที่ปริ๊นมา
สอน Chap2 ที่ปริ๊นมา
--- 2 ---
main
main ( )
{ printf ( My house) ;
printf ..
printf (is very beautiful);
printf..
triangle( );
triangle
box( );
box
prinf(finish) ;
printf.
}
triangle( )
triangle
{ /* draw triangle */
triangle
}
box ( )
box
{
/* draw box */
box
}
1.1
--- 2 ---
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)
;
{ }
--- 2 ---
(Heading)
{ (Variable Declarations)
(Statements)
}
1.3
1. (Source file)
.c .cpp (Editor)
(
)
2. (C Compiler)
(Object file) .obj (
)
3. (Linker)
(C Library)
.exe (
)
C Compiler
Source file
Computer
Object file
Linker
Library
Computer
Execute file
1.2
1.
--- 2 ---
#include <stdio.h>
void main(void)
{
printf(Hello World);
}
1.c
2.
1.obj
3.
stdio.h ( #include ) 1.exe
** printf
printf stdio.h
stdio.h
printf
1.4
3
1.4.1
.h
1.4.2
1.4.3
2.2
#include <stdio.h>
char
int
{
a;
main(void)
a = 23;
printf(Hello World);
return a;
1.3
--- 2 ---
Hello World
1.7
2
1.7.1 (Keywords)
auto
default float
register struct
volatile
do
far
return
switch
while
case
goto
short
typedef char
else
if
union
const
enum
int
sizeof
unsigned
extern
long
static
void
break
double
signed
continue
0 ( )
--- 2 ---
2. Local Variable
, , ,.....;
1. char 1
1 A , b , 1 , ?
2. integer
-32768 32767 2 5 -10 2534
3. 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+308
6. unsigned
( )
unsigned int
Char
8
ASCII character (-128 127)
Unsigned char
8
0-255
Int
16
-32768 32767
long int
32
-2147483648 2147483649
Float
32
3.4E-38 3.4E+38 6
Double
64
1.7E-308 1.7E+308 12
Unsigned int
16
0 65535
Unsigned long int
32
0 4294967296
1.1
--- 2 ---
1
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
#include<stdio.h>
#define PI 3.14159
int area;
/* global variable */
main( )
{ float radius;
/* local variable */
float process()
/* function declaration */
printf (Radius = ?);
scanf (%f,radius);
process( );
printf(Area = %f,area);
printf(%f,radius);
}
11.
12.
13
14
15
16
float process( )
{ float radius;
/* local variable */
printf(Radius=?);
scanf (%f,radius);
area=PI*radius*radius;
printf(Area = %f,area);
printf(%f,radius);
}
2
int a ;
a
-35768 32767
3
int num1=8;
num1 8
4
float money,price ;
money price
6
5
char ch=A
ch 1 A
6
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 <filename> #include filename
#include <dos.h>
dos.h
#include sample.h sample.h
* <>
Option directory
*
(Header Files) .h
1 stdio.h printf
--- 2 ---
11
#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
character
a
string
,
1.2
--- 2 ---
12
;
{}
(Statement)
(Operands) (Operators) 3 * 2 - 1 + 7 a * 5
7 + 5
a +b
(a * 3) + 5
2.0 * sin(2.12)
--- 2 ---
13
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!
--- 2 ---
14
2.3
printf
list ) ;
2
control
control 2
printf ( sum of x = );
sum of x =
(Format Code)
%
%d
%ld
%u
%c
%s
%o
%x
%f
%e
%lf
int
long int
unsigned int
char
string
int (octal)
int (hexa)
float
float, double
double
long
unsigned
double
--- 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
\b
\f
\n
\r
\t
\v
\\
\
\
\?
0x07
0x08
0x0c
0x0a
0x0d
0x09
0x0b
0x5c
0x2c
0x22
0x3f
cursor
cursor
tab
tab
\
?
--- 2 ---
16
1 printf
#include<stdio.h>
main( )
{
int sum=5;
float e=11.55;
printf(This is the test Program. );
printf(The result is %d \n,sum);
printf(The result is %f %e ,e,e);
}
2 printf
#include<stdio.h>
main()
{ char a=A ;
char name=Aree;
int x,y;
printf(What is the first character \n);
printf(The first character is %c \n\n,a);
x=10; y=15;
printf(x+y= %d \n,x+y);
printf( %d 5 = %d \n,y,y-5);
x=65;
printf(x is %c,x);
printf(This program run by %s,name);
}
(field) + -
--- 2 ---
17
3 printf
#include <stdio.h>
void main()
{
printf(|1234567890|\n);
printf(|%10s|\n,price);
}
price 10
%
3
|1234567890|
|
price|
price 10
-
printf() 3
printf(|%-10s|\n,price);
|1234567890|
|price |
float a.b a
b 6 printf()
4
printf(|%10.3f|\n,1999.95);
6
--- 2 ---
18
|1234567890|
|_ _1999.950|
1999.95 10 3
int string
7 printf()
4
printf(|%.10s|\n,This program is running);
printf(|%.10d|\n,20);
7
|1234567890|
|This progr|
|0000000020|
2.4
scanf
control
control
printf
** control scanf
--- 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
#include<stdio.h>
main( )
{
int x1;
char letter[30];
scanf(%d,&x1);
scanf(%s,letter);
}
2.5
2.5.1 getchar ( )
getchar ( ) 1 enter
--- 2 ---
20
getchar ( ) ;
#include<stdio.h>
main ( )
{
char ch;
ch=getchar ( );
}
1 ch
enter
2.5.2 getch ( )
getch ( ) 1
enter
getch ( ) ;
#include <stdio.h>
main ( )
{
char x;
x = getch( ) ;
}
--- 2 ---
21
1 x
enter
2.5.3 gets ( )
gets ( ) ( )
( gets = get string)
gets ( n ) ;
n \0
enter
#include <stdio.h>
main( )
{
char name[10];
gets(name);
}
name 10
name 9 name 10 ( ) \0
2.5.4 putchar( )
putchar( ) 1
putchar ( ) ;
--- 2 ---
22
#include<stdio.h>
main ( )
{
char x;
x=getch ( ) ;
printf (Here is the output \n) ;
putchar ( x );
}
2.5.5 puts ( )
puts ( )
puts ( n ) ;
n
#include <stdio.h>
main ( )
{
char name [10];
gets (name) ;
puts (name);
}
name
--- 2 ---
23
3. (Operators)
(Operators)
3
(type) (size of)
3.1 (Arithmetic Operators)
+
*
/
%
-++
(Modulo)
1
1
A+B
A-B
A*B
A/B
5%3=1 2 2
A -- A=A-1
A++ A=A+1
3.1
(Operation)
--- 2 ---
24
C
(Precedence) 3.2
()
*/
+-
* -
* -
3.2 Precedence
a + b* c C % * / + a+b+c a b
c
%
/
a/b
a b integer integer
a b float float
39 / 5 7
39. / 5 7.8
--- 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 = ++n;
x 5
x 6
(i+j)++
--- 2 ---
26
(Relational Operator)
(Equality Operator)
(Logical Operator)
<
>
<=
>=
==
!=
!
&&
||
3.3
3.2.1 (Relational Operator)
2
int
1
0 (0
)
4
< ( ) > ( ) <= ( ) >= ( )
a<3 /* a 5 0 () */
a>b /* a=10 b=8 1 () */
-1 >= (2.2*X+3.3)
--- 2 ---
27
/*
b c a */
a =< b
a< = b
/* = < */
/* < = */
&& ()
|| ( )
--- 2 ---
28
**
1
!a
/* a */
!(x+7.7)
!(a<b || c<d) /* a b ||
c d
|| */
a!
/* ! */
a! = b /* != */
&& ||
2
a&&b
/* a b a b
*/
a || b
/* a b a b
*/
!(a<b) && C /* a b
&& C */
3 && (-2 * a + 7)
/* && 3 */
a &&
/* */
a| |b
/* || */
a&b
/* && */
&b
/* b */
--- 2 ---
Q
0
1
0
1
P && Q
0
0
0
1
3.4 && P Q
|| ( )
2 3.5
P
0
0
1
1
Q
0
1
0
1
P || Q
0
1
1
1
3.5 || P Q
! ()
3.6
P
0
1
!P
1
0
--- 2 ---
29
30
10
+= -=
/=
%=
<<= >>= |=
^=
Variable op= expression;
i = i + 1;
i = I a;
i = 1 * (a + 1);
i = i / (a-b);
i = i %101;
i = i << 1;
i = i >> i;
i = i & 01;
i = i & 01;
i = i ^ (07 | 0xb);
i += 1;
i - = a;
i = *=a+1;
i /= a-b;
i %= 101;
i <<=1;
i >>= i;
i |= 0xf;
i | = 0xf;
i ^ = 07 | 0xb;
31
int ABC;
printf(%d,sizeof(ABC));
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
#include<stdio.h>
void main(void)
{
int a,b;
a = 15;
b = ((a+5)? (a+1) : 0);
print(b= %d\n,b);
a=0;
b=((a+5)?(a+1) : 0);
printf(b= %d\n,b);
a=5;
b=((a+5)?(a+1) : 0);
printf(b= %d\n,b);
}
b = 16
--- 2 ---
32
b = 1
b = 0
3.6 -
- 3.7
3.7
--- 2 ---
33
3.7 (Casting)
int int
( )
(int)(a*5.2)
(float)(b+5)
output int
output float
--- 2 ---
34
C
a.
ab
a+b
b.
b
a+ 2
c
c.
a
+c
b
b
a
c
d.
a+b
ab
e.
a +b
cd
f.
ab + bc + ac
a+b+c
abc
a, b, c, d integer 4
float
--- 2 ---