Anda di halaman 1dari 223

2

__________________________________________________________________________________________

__________________________________________________________________________________

3
__________________________________________________________________________________________

__________________________________________________________________________________

4
__________________________________________________________________________________________

15................................................................................................

16......................................................................................................................
..................................................................................... C


1.1 19......................................................................................................
1.2 21....................................................................................................... C
1.3 30..............................................................Variables and Constants
1.4 40........................................................................................ Comments
1.5 42......................................................................................... scanf
1.6 45................................................................................................ Operators
1.7 51....................................................................................... if, else, elseif
1.8 55......................................................................................................... C
1.9 60.............................................................................
................................................................................ (2) C
2.1 72........................................................................................................ Switch
2.2 77................................................................................ Repeated loop
2.3 90................................................................................................. Arrays
2.4 103............................................................................................... Pointers
2.5 113................................................................................................ Functions
2.6 122.................................................................................. Header files
2.7 125................................................................... Files I/O
2.8 135............................................................................................ structures
2.9 145...........................................................................
......................................................................................... C
3.1 162.......................................................................................... Enumeration
167................................................................................. Command-line Arguments 3.2
3.3 )169..................................................................... Directives(Preprocessor

3.4
3.5 178............................................................................ Standard Library
220.............................................................................................
175..................................................................................

__________________________________________________________________________________

5
__________________________________________________________________________________________

15................................................................................................

16......................................................................................................................
..................................................................................... C


1.1 19................................................................................................
1.1.1 19................................................................................. texts editor
1.1.2 19............................................................................................. compiler
1.1.3 20.................................................................................................. linker
1.2 21.................................................................................................. C
1.2.1 25...........................................................................................
1.2.2 27...............................................................................................
1.2.3 28...........................................................................................................
1.3 30.....................................................Variables and Constants
1.3.1 30................................................................................ Variable Type
1.3.1.1 30......................................................................... int
1.3.1.2 31......................................................................... float
1.3.1.3 31...................................................................... double
1.3.1.4 31..................................................................... short
1.3.1.5 32...................................................................... long
1.3.1.5 32..................................................................................... char
1.3.2 32............................................................................... Variable Name
1.3.3 33............................................................................... Variable Value
1.3.4 33.........................................................................................
1.3.5 35.........................................................................
1.3.6 38...............................................................................................
1.3.7 39...........................................................................................................
1.4 40................................................................................ Comments
1.4.1 40..........................................................................................
1.4.2 40.........................................................................................
1.4.2.1 40...................................................................
1.4.2.2 40...............................................................................

__________________________________________________________________________________

6
__________________________________________________________________________________________

1.4.3
1.4.4 41......................................................................................................
1.5 42................................................................................... scanf
1.5.1 43...............................................................................................
1.5.2 44...........................................................................................................
1.6 45...................................................................... ................... Operators
1.6.1 45.............................................................. arithmetic operators
1.6.1.1 45....................................................................(++) increment
1.6.1.2 46...................................................................(--) decrement
1.6.1.3 )47..............................................................................(%
1.6.2 47................................................................ relational operators
1.6.3 48..................................................................... logical operators
1.6.4 49................................................................................................
1.6.5 50................................................................................. ..............
1.6.6 50...........................................................................................................
1.7 51................................................................................ if, else, elseif
1.7.1 51..................................................................................................... if
1.7.2 52................................................................................................. else
1.7.3 53............................................................................................ elseif
1.7.4 54...............................................................................................
1.7.5 54...........................................................................................................
1.8 55.................................................................................................... C
1.8.1 55...................................................................................... Comments
1.8.2 55............................................................................ Keywords
1.8.3 55........................................................................................ Identifiers
41.........................................................................................

56.............................................................................................. Trigraphs 1.8.3.1


1.8.4 56.......................................................................................... Constants

1.8.4.1
1.8.4.2 58........................................................................................
1.8.5 58............................................................................................... Tokens
1.8.6 58.......................................................................... String literals
1.8.7 59...............................................................................................
57.........................................................................................

__________________________________________________________________________________

7
__________________________________________________________________________________________

1.8.8
1.9 60........................................................................
1.9.1 60.................................................................................................
1.9.1.1 60.....................................................................
1.9.1.2 61..................................................................
1.9.1.3 62.........................................................
1.9.1.4 63..................................................................
1.9.2 putchar 63............................................................................... getchar
1.9.3 puts 64........................................................................................ gets
1.9.4 wprintf 65................................................................................ wscanf
1.9.5 putch getch 65............................................................... getche
1.9.6 67.................................................................................. wchar_t
1.9.7 main 67........................................................................... wmain
1.9.8 69.................................................................... ..............
1.9.9 69...............................................................................................
1.9.10 70.........................................................................................................
........................................ ........................... ............. (2) C
2.1 72................................................................................................. Switch
2.1.1 74....................................................................................... case
2.1.2 75..................................................................................... break
2.1.2 75...................................................................................... default
2.8.7 75...............................................................................................
2.8.8 76...........................................................................................................
2.2 77......................................................................... Repeated loop
2.2.1 77...................................................................................... while
2.2.2 79................................................................................ dowhile
2.2.3 81.......................................................................................... for
2.2.4 82....................................................................................... goto
2.2.5 84...............................................................................
2.2.7 87................................................................................. continue
2.2.8 88................................................................................................. ASCII
59...........................................................................................................

__________________________________________________________________________________

8
__________________________________________________________________________________________

2.2.9
2.2.10 89.........................................................................................................
2.3 90............................................................................................ Arrays
2.3.1 91....................................................................................
2.3.2 92...................................................................................
2.3.2 94..................................................................................
2.3.3 95.........................................................................
2.3.4 )( 95...............................................................................
2.3.4.1 98.............................................................................................. gets
2.3.4.2 strcpy 99..................................................................... strncpy
2.3.4.3 strcat 100..................................................................... strncat
2.3.5 101....................................................................
2.3.6 102.............................................................................................
2.3.7 102.........................................................................................................
2.4 103........................................................................................ Pointers
2.4.1 103............................................................................... Pointer Type
2.4.2 103.............................................................................. Pointer Name
2.4.3 105...................................................................................
2.4.4 107.......................................................................................
2.4.5 108.......................................................................................... reference
2.4.6 109............................................................................................. void
2.4.7 109..............................................................................................
2.4.8 110.................................................................................................
2.4.9 111.............................................................................................
2.4.10 111.......................................................................................................
2.5 113......................................................................................... Functions
2.5.1 116.............................................................................. Function Type
2.5.2 118............................................................................. Function Name
2.5.3 119................................................................. Function Parameters
2.5.4 119........................................................................................................
2.5.5 119...................................................................... #define
2.5.6 120...............................................................................
88...............................................................................................

__________________________________________________________________________________

9
__________________________________________________________________________________________

2.5.7
2.5.8 121.............................................................................................
2.5.9 121.........................................................................................................
2.6 122.................................. ......................................... Header files
2.6.1 124..........................................................................................
2.6.2 124.............................................................................
2.6.3 124.............................................................................................
2.6.4 124.........................................................................................................
2.7 125............................................................ Files I/O
2.7.1 125........................................................................................
2.7.1.1 128.......................................................................................... fopen
2.7.1.2 128.......................................................................................... fclose
2.7.1.3 128............................................................................................. exit
2.7.2 129.................................................................................
2.7.3 129........................................................................................
2.7.4 w+ a+ 130.................................................................................... r+
2.7.4.1 130............................................................................................ w+
2.7.4.2 130............................................................................................. a+
2.7.4.3 130.............................................................................................. r+
2.7.5 131...............................................................
2.7.5.1 fprintf 131..................................................................... fscanf
2.7.5.2 fgets 132........................................................................ fputs
2.7.5.3 fgetc 133........................................................................ fputc
2.7.6 134.............................................................................................
2.7.7 134.........................................................................................................
2.8 135...................................................................................... structures
2.8.1 135................................................................................. Struct Name
2.8.2 139............................................................ union
2.8.3 140...................................................................
3.8.4 142......................................................................................
2.8.5 143................................................................................... Bit-fields
2.8.6 143.............................................................................................
120............................................................ .............

__________________________________________________________________________________

10
__________________________________________________________________________________________

2.8.7
2.9 145......................................................................
2.9.1 145...................................................................... void
2.9.2 146.................................................................................... static
2.9.3 147................................................................................. typedef
2.9.4 149................................................................................................
2.9.4.1 149..............................................................................
2.9.4.2 150...........................................................................
2.9.4.3 150.................................................................................
2.9.4.4 151.................................................................................
2.9.4.5 151.................................................
2.9.4.6 152...........................................
2.9.5 wcscpy 152....................................................................... wcsncpy
2.9.6 wcscat 153........................................................................ wcsncat
2.9.7 getwchar 153.......................................................................... putwchar
2.9.8 _getws 154................................................................................ _putws
2.9.9 ) ASCII( 154....................................................................................
2.9.10 155.........................................................................
2.9.10.1 155.....................................................................................
2.9.10.2 )( 156......................................................................
2.9.10.3 156.......................................................................... extern
2.9.10.4 157............................................................................ auto
2.9.10.5 158........................................................................ register
2.9.11 158.................................................................................. sizeof
2.9.12 158................................................................................ volatile
2.9.13 159.....................................................................................
2.9.14 160................................................................................
2.9.15 160...........................................................................................
2.9.16 160.......................................................................................................
......................................................................................... C
3.1 162................................................................................... Enumeration
3.1.1 162................................................................... Enumeration Name
143.........................................................................................................

__________________________________________________________________________________

11
__________________________________________________________________________________________

3.1.2
3.1.3 166.............................................................................................
3.1.4 166.........................................................................................................
162.............................................................................................

167........................................................................ Command-line Arguments 3.2


3.2.1 167.............................................................................................

3.2.2
3.3 )169........................................................... Directives(Preprocessor
3.3.1 169......................................................................................... #include
3.3.2 169........................................................................................ #define
3.3.3 170........................................................................................... #undef
3.3.4 #else #elif #if 171............................................................ #endif
3.3.5 #ifdef 171.................................................................... #ifndef
3.3.6 172.............................................................................................. #line
3.3.7 173............................................................................................ #error
3.3.8 173........................................................................................ #pragma
3.3.9 173.................................................................... Predefined Names
3.3.10 174...........................................................................................
3.3.11 174.......................................................................................................
3.4 175.............................................................................
3.4.1 177.............................................................................................
3.4.2 177.........................................................................................................
3.5 178.................................................................... Standard Library
3.5.1 178................................................................................... assert.h
3.5.2 178................................................................................... ctype.h
3.5.2.1 178...................................................................................... isalnum
3.5.2.2 179....................................................................................... isalpha
3.5.2.3 179......................................................................................... iscntrl
3.5.2.4 180......................................................................................... isdigit
3.5.2.5 180....................................................................................... isgraph
3.5.2.6 181....................................................................................... islower
3.5.2.7 181......................................................................................... isprint
3.5.2.8 182....................................................................................... ispunct
167.........................................................................................................

__________________________________________________________________________________

12
__________________________________________________________________________________________

3.5.2.9 182....................................................................................... isspace

3.5.2.10 183..................................................................................... isupper

3.5.2.11 183...................................................................................... isxdigit


3.5.2.12 toupper 184................................................................... tolower

3.5.3
3.5.3.1 184......................................................................................... perror
3.5.4 187..................................................................................... float.h
3.5.5 187.................................................................................... limits.h
3.5.6 188................................................................................... locale.h
3.5.7 188.................................................................................... math.h
3.5.7.1 189.............................................................................................. sin
3.5.7.2 189............................................................................................. cos
3.5.7.3 189............................................................................................. tan
3.5.7.4 189............................................................................................ exp
3.5.7.5 190............................................................................................. log
3.5.7.5 190........................................................................................... pow
3.5.7.6 190............................................................................................ sqrt
3.5.7.7 190............................................................................................. ceil
3.5.7.8 191........................................................................................... floor
3.5.7.9 191........................................................................................... fabs
3.5.7.10 191........................................................................................ ldexp
3.5.7.11 192........................................................................................ fmod
3.5.8 192................................................................................. setjmp.h
3.5.9 193................................................................................... signal.h
3.5.9.1 193.......................................................................................... raise
3.5.10 193................................................................................ stdarg.h
3.5.11 194................................................................................ stddef.h
3.5.12 195.................................................................................. stdio.h
3.5.12.1 195........................................................................................ printf
3.5.12.2 195...................................................................................... sprintf
3.5.12.3 196...................................................................................... vprintf
3.5.12.4 196..................................................................................... vfprintf
184................................................................................... errno.h

__________________________________________________________________________________

13
__________________________________________________________________________________________

3.5.12.5
3.5.12.6 197........................................................................................ scanf
3.5.12.7 197....................................................................................... fscanf
3.5.12.8 198...................................................................................... sscanf
3.5.12.9 198........................................................................................ fgetc
3.5.12.10 198...................................................................................... fgets
3.5.12.11 199...................................................................................... fputc
3.5.12.12 199...................................................................................... fputs
3.5.12.13 199....................................................................................... getc
3.5.12.14 200................................................................................... getchar
3.5.12.15 200........................................................................................ gets
3.5.12.16 200....................................................................................... putc
3.5.12.17 200................................................................................... putchar
3.5.12.18 201....................................................................................... puts
3.5.12.19 201.................................................................................... ungetc
3.5.12.20 201..................................................................................... fopen
3.5.12.21 202................................................................................... freopen
3.5.12.22 202..................................................................................... fclose
3.5.12.23 202................................................................................... remove
3.5.12.24 203................................................................................... rename
3.5.12.25 203.................................................................................... tmpfile
3.5.12.26 203...................................................................................... fread
3.5.12.27 204..................................................................................... fwrite
3.5.12.28 204...................................................................................... fseek
3.5.12.29 205........................................................................................ ftell
3.5.12.30 205.................................................................................... rewind
3.5.12.31 206........................................................................................ feof
3.5.13 206.................................................................................. stdlib.h
3.5.13.1 206.......................................................................................... atof
3.5.13.2 207.......................................................................................... atoi
3.5.13.3 207.......................................................................................... atol
3.5.13.4 207......................................................................................... rand

197..................................................................................... vsprintf

__________________________________________________________________________________

14
__________________________________________________________________________________________

3.5.13.5
3.5.13.6 208........................................................................................ abort
3.5.13.7 208.......................................................................................... exit
3.5.13.8 209....................................................................................... atexit
3.5.13.9 209..................................................................................... system
3.5.13.10 209......................................................................................... abs
3.5.13.11 210........................................................................................ labs
3.5.13.12 210......................................................................................... div
3.5.13.13 210......................................................................................... ldiv
3.5.14 210................................................................................. string.h
3.5.14.1 strcpy 211................................................................. strncpy
3.5.14.2 strcat 211.................................................................. strncat
3.5.14.3 strcmp 212............................................................... strncmp
3.5.14.4 strchr 213.................................................................. strrchr
3.5.14.5 strspn 213................................................................. strcspn
3.5.14.6 214..................................................................................... strpbrk
3.5.14.7 214........................................................................................ strstr
3.5.14.8 214........................................................................................ strlen
3.5.14.9 214..................................................................................... strerror
3.5.14.10 215...................................................................................... strtok
3.5.15 215................................................................................... time.h
3.5.15.1 216......................................................................................... clock
3.5.15.2 217.......................................................................................... time
3.5.15.3 217..................................................................................... difftime
3.5.15.4 218.................................................................................... localtime
3.5.15.5 218...................................................................................... asctime
3.5.15.6 219......................................................................................... ctime
220.............................................................................................
208....................................................................................... srand

__________________________________________________________________________________

15
__________________________________________________________________________________________



... :
01/01/1989
:
) (0021364576618 ) (064576618 :
- 10 .khalil_ounis@yahoo.com :
C :
:C
C .
Switch

.
.C
www.vc4arab.com :
.

www.vc4arab.com

__________________________________________________________________________________

16
__________________________________________________________________________________________

low-level language
Machine language
) off on
(
assembly language low-level languages

ADD MOV .
BASIC COBOL C
.
C
Ken Thompson Dennis Ritchie .Bell
C low-level languages
assembly language mid-level
language .
C Unix

.
1972 C C ) B
B BCPL Martin Richards 1967
Basic Combined Programming Language
( 1969 B Bell
B C .C
__________________________________________________________________________________

17
__________________________________________________________________________________________

1978 Brian Kernighan


The C Programming Language
(Kernighan & Ritchie C) K&R C K&R C C
C

.C
1989 C ANSI C
National Standards Institute
C ISO
C .International Organization for Standardization
American

) 1988

The C Programming

Language C (.ANSI C

__________________________________________________________________________________

18
__________________________________________________________________________________________

C
1.1
1.2 C
1.3 Variables and Constants
1.4 Comments
1.5 scanf
1.6 Operators
1.7 if, else, elseif
1.8 C
1.9

__________________________________________________________________________________

19
__________________________________________________________________________________________

1.1
C texts editor
compiler linker .
:
1.1.1 :texts editor
.c
KWrite Linux Notepad .Windows
Word Windows Kword .Linux
1.1.2 :compiler

Language Assembly
..obj

Machine Language

Windows
Visual C++ C C++
MicroSoft Dev-C++ Bloodshed
.... Pelles C Quick C Turbo C
Visual C++ .
Dev-C++ :
http://www.bloodshed.net/devcpp.html

Turbo C :
http://www.pitt.edu/~stephenp/misc/downloadTC.html
__________________________________________________________________________________

20
__________________________________________________________________________________________

Pelles C :
http://www.smorgasbordet.com/pellesc/download.htm

Unix Linux Unix Linux


.
.Windows
Windows :Linux
http://www.thefreecountry.com/compilers/cpp.shtml

1.1.3 :linker
.obj .exe
.dll
:Windows

__________________________________________________________________________________

21
__________________________________________________________________________________________

1.2


C Console .c
main.c :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)"!printf("Hello, World
}6:

: :
>#include<stdio.h
)(main
{
;)"!printf("Hello, World
}


World .Console
)

#include<stdio.h> Code

Hello,

>< #include #include .


> < stdio.h

.
stdio.h Standard Input Output .h
.Header File
#include
. .

>

<

__________________________________________________________________________________

22
__________________________________________________________________________________________

)( main
.
.
{ .main
printf stdio.h
print format )(
! Hello, World
" "
.
printf
.
}

Screen

.main

{ } block :

! Hello, World :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)" printf("Hello,
6:
;)"!printf("World
}7:

.
__________________________________________________________________________________

23
__________________________________________________________________________________________

.printf
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
printf("Hello,
6:
;)"!World
}7:

printf
anti-slash printf :
\ printf("Hello,
;)"!World

.
) (stdio.h
printf
.
Hello, ! World \n
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)"!printf("Hello, \nWorld
}6:

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)" printf("Hello,
6:
;)"printf("\n
7:
;)"!printf("World
}8:

__________________________________________________________________________________

24
__________________________________________________________________________________________

\n
\n n .New line
:
>1:#include<stdio.h
2:
};)"!3:main(){printf("Hello, World


{ ) ( } ) (
:
>1:#include<stdio.h
2:
{)(3:main
4:
};)"!printf("Hello, World


. .

} {

:
{)(1:#include<stdio.h> main
2:
};)"!printf("Hello, World

#include
.
#include directive preprocessor

.
directive # .
block main:
>1:#include<stdio.h
2:

__________________________________________________________________________________

25
__________________________________________________________________________________________
)(3:main
{4:
5:
;)"printf("Hello, World!\n
6:
{
7:
;)"printf("Hello, World!\n
8:
}
9:
;)"printf("Hello, World!\n
}10:

block block
block block.
1.2.1 :

printf
printf

.
C
%d %i Decimal Integer
%f f float
.
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)printf("%d", 20
}6:

printf


:
>1:#include<stdio.h

__________________________________________________________________________________

26
__________________________________________________________________________________________
2:
)(3:main
{4:
5:
;)printf("%d", 20+5
}6:

.
%d
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)printf("%d %d", 20+5, 87
}6:


:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)printf("%d + %d = %d\n", 20, 5, 20+5
}6:

%f .%d
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)printf("%f + %f = %f\n", 1.0, 2.14, 1.0+2.14
}6:

:


.

__________________________________________________________________________________

27
__________________________________________________________________________________________

%d %f
%c c character %s s
String .
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)"printf("%c", "a
}6:

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)'printf("%c", 'a
}6:

a :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)"!printf("%s\n", "Hello, World
}6:

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)"printf("%s", "Hello, " "\n" "World
}6:


%x %X 0x 0X 0x000F %o
.
1.2.2 :
__________________________________________________________________________________

28
__________________________________________________________________________________________

-1 C main
Main .MAIN
-2 printf main :
>1:#include<stdio.h
2:
;)"3:printf("Hello, World!\n
4:
)(5:main
{6:
7:
}8:


.
-3
.
-4 )( main

.printf
-5 printf " "
.
1.2.3:
-1
-2
-3
-4
-5
-6

! Hello, World .
! Hello, World .
main
main

2 .3.5

__________________________________________________________________________________

29
__________________________________________________________________________________________

-7
-8

Hello ).(%c
).(%s%s

__________________________________________________________________________________

30
__________________________________________________________________________________________

1.3

Variables and Constants


.RAM
RAM

Access Memory

Random )(


.
C
... .
.

:

1.3.1

:Variable Type

:
1.3.1.1

:int

int Variable_Type
Integer 2 16 65536
65535 -32,768 32,767

__________________________________________________________________________________

31
__________________________________________________________________________________________

4 32
4294967296 ) (.
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int Variable_Name = 0
}6:

1.3.1.2

:float

float
1.2E-38 .3.4E+38

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;float Variable_Name = 0.0
}6:

1.3.1.3

:double

double float double 8


2.3E-308 .1.7E+308
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;double Variable_Name = 0.0
}6:

1.3.1.4

:short

short
Variable_Type 2 16 65536
65535 -32,768 32,767
.
__________________________________________________________________________________

32
__________________________________________________________________________________________

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;short Variable_Name = 0
}6:

1.3.1.5

:long

long
Variable_Type 4 32
4294967296 ) (.
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;long Variable_Name = 0
}6:

1.3.1.5

:char

char Variable_Type 1
256 0 255 -128 127
.ASCII

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;'char Variable_Name = 'A
}6:

1.3.2 :Variable Name


:
31.
.
....
__________________________________________________________________________________

33
__________________________________________________________________________________________

% # { )... _(.
.
.
1.3.3 :Variable Value
int

.float

1.3.4 :

Var 5 Var :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int Var = 5
6:
7:
;)printf("%d\n", Var
}8:

Var ) int (
5 printf Var
:
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int Var
6:
;Var = 5
7:
8:
;)printf("%d\n", Var
}9:

5 :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int Var_1 = 5
6:
;int Var_2 = Var_1

__________________________________________________________________________________

34
__________________________________________________________________________________________
;)printf("%d\n", Var_1

7:
8:
}9:

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int Num1, Num2, Num3
6:
;Num1 = 5
7:
;Num2 = 7
8:
;Num3 = Num1 + Num2
9:
10:
;)printf("%d + %d = %d\n", Num1, Num2, Num3
}11:


) (int
Num1 5 Num2 7 Num3
Num1 Num2 .
float short long

ASCII
char :

char

>1:#include<stdio.h
2:
)(3:main
{4:
5:
;'char ch = 'a
6:
7:
;)printf("%c\n", ch
}8:

ch a
a 97 ASCII :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;char ch = 97
6:
7:
;)printf("%c\n", ch

__________________________________________________________________________________

35
__________________________________________________________________________________________
}8:

char 255
.
1.3.5 :

int Num
signed Num int Num
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;signed Num = 5
6:
7:
;)printf("%d\n", Num
}8:


unsigned :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;unsigned Num = 5
6:
7:
;)printf("%u\n", Num
}8:


%u
unisgned .
: char 256
0 255 -128 127
:
__________________________________________________________________________________

36
__________________________________________________________________________________________

char 1 int 2
1 8 char 8 1 1 0
1 ) 0 (1 8 256 0 255
1 8
7 char + -
7 128.
:

char

signed char

-128 127

char

char unsigned

0 255

int

signed int

-32,768 32,767

int

unsigned int

0 65535

short

signed short

-32,768 32,767

short

unsigned short

0 65535

long

signed long

-2,147,483,648 2,147,483,647

long

unsigned long

0 4,294,967,295



5 8
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int num
6:
7:
;num = 5
8:
9:
;)printf("%d\n", num
10:
11:
;num = 8
12:
13:
;)printf("%d\n", num
}14:

__________________________________________________________________________________

37
__________________________________________________________________________________________

num 5 8 .
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;const int num
6:
7:
;num = 5
8:
9:
;)printf("%d\n", num
10:
11:
;num = 8
12:
13:
;)printf("%d\n", num
}14:

const int num


num
num

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;const int num = 5
6:
7:
;)printf("%d\n", num
}8:

const :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int const num = 5
6:
7:
;)printf("%d\n", num
}8:

__________________________________________________________________________________

38
__________________________________________________________________________________________

#define
:

:
>1:#include<stdio.h
2:
3:#define num 5
4:
)(5:main
{6:
7:
;)printf("%d\n", num
}8:

num .
preprocessor # #define preprocessors
directive directives .
1.3.6 :
-1 .
-2 .block
-3
.
-4 .
-5

const

__________________________________________________________________________________

39
__________________________________________________________________________________________

-6 .
-7 .
1.3.7:
-1

3.14 15 .
int 65535
A 65 .
A 65 .char

-5

18 89
.
preprocessor #define
5 .15

-2
-3
-4

-6

__________________________________________________________________________________

40
__________________________________________________________________________________________

1.4

comments


.
1.4.1 :

.
1.4.2 :
:
1.4.2.1 :


*/ * / :
*1:/
2:My First Program:
!3:Hello, World
4:*/
5:
6:#include<stdio.h> /* Standart Input Output Header File*/
7:
)(8:main
/*main function*/
9:{/*Start of main function*/
10:
printf("Hello, World!");/*to print Hello, World!*/
11:}/*End of main function*/

1.4.2.2 :

//:
1://My First Program:
!2://Hello, World
3:
4:#include<stdio.h> //Standart Input Output Header File
5:
)(6:main
//main function
7:{//Start of main function
8:
!printf("Hello, World!");//to print Hello, World
9:}//End of main function

C .
__________________________________________________________________________________

41
__________________________________________________________________________________________

1.4.3 :
-1
:
1:/*comment
>2:#include<stdio.h
3:
)(4:main
{5:
6:
;)"!printf("Hello, World
}7:

.
-2 :
>1://#include<stdio.h
2:
)(3:main
{4:
5:
;)"!printf("Hello, World
}6:

printf
#include stdio.h
.stdio.h
1.4.4:

__________________________________________________________________________________

42
__________________________________________________________________________________________

1.5

scanf

printf
scanf printf scanf
.
scanf .keyboard
:

*/ */
*/ *");/
*/ */
*%d\n", usr_val);/

*/*1:/
>2:#include<stdio.h
3:
)(4:main
{5:
6:
;int usr_val
7:
8:
printf("Enter a value:
9:
;)scanf("%d", &usr_val
10:
printf("Your value is:
*/
}11:

scanf
%d
printf &usr_val &
usr_val &
. usr_val
.

%d
%c scanf :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;char usr_char
6:
7:
;)" printf("Enter a character:

__________________________________________________________________________________

43
__________________________________________________________________________________________
;)scanf("%c", &usr_char
;)printf("Your character is: %c\n", usr_char

8:
9:
}10

;)"

;)"

;)val1+val2
;)val1-val2
;)val1*val2
;)val1/val2

val2,
val2,
val2,
val2,

val1,
val1,
val1,
val1,

>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int val1, val2
6:
7:
printf("1)Enter a value:
8:
;)scanf("%d", &val1
9:
10:
printf("2)Enter a value:
11:
;)scanf("%d", &val2
12:
13:
printf("%d + %d = %d\n",
14:
printf("%d - %d = %d\n",
15:
printf("%d * %d = %d\n",
16:
printf("%d / %d = %d\n",
}17



13 14 15 .16
scanf :

;)val1+val2
;)val1-val2
;)val1*val2
;)val1/val2

val2,
val2,
val2,
val2,

>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int val1, val2
6:
7:
;)" printf("Enter two value:
8:
;)scanf("%d%d", &val1, &val2
9:
10:
printf("%d + %d = %d\n", val1,
11:
printf("%d - %d = %d\n", val1,
12:
printf("%d * %d = %d\n", val1,
13:
printf("%d / %d = %d\n", val1,
}14:

scanf
.
1.5.1 :
__________________________________________________________________________________

44
__________________________________________________________________________________________

-1 scanf)(
.
-2 & .
-3
.
1.5.2:
-1


" " "
".

__________________________________________________________________________________

45
__________________________________________________________________________________________

1.6

operators

:
1.6.1 ):(arithmetic operators
) (+) (-) (/ )*(
C :) (++) (-- ).(%

:
1.6.1.1 :(++) increment


) (.
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int Inc
6:
;Inc = 0
7:
8:
;)printf("Inc = %d\n", Inc
9:
10:
;Inc++
11:
12:
;)printf("Inc = %d\n", Inc
13:
14:
;++Inc
15:
16:
;)printf("Inc = %d\n", Inc
}17:

0
.2

:
;)printf("Inc = %d\n", Inc

1:
2:

__________________________________________________________________________________

46
__________________________________________________________________________________________
;Inc = Inc+1
;)printf("Inc = %d\n", Inc
;Inc += 1
;)printf("Inc = %d\n", Inc

3:
4:
5:
6:
7:
8:
9:

Inc = Inc+1 Inc++


1 Inc = Inc+3 Inc
Inc += 1 Inc = Inc+1.
++
.
++ :

;)Inc
;)Inc++
;)Inc
;)++Inc
;)Inc

%d\n",
%d\n",
%d\n",
%d\n",
%d\n",

>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int Inc
6:
;Inc = 0
7:
8:
= printf("Inc
9:
= printf("Inc
10:
= printf("Inc
11:
= printf("Inc
12:
= printf("Inc
}13:

0 ++
.
1 .
2
.2
1.6.1.2 :(--) decrement

++ --
:
__________________________________________________________________________________

47
__________________________________________________________________________________________
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int Dec
6:
;Dec = 2
7:
8:
;)printf("Dec = %d\n", Dec
9:
10:
;Dec--
11:
12:
;)printf("Dec = %d\n", Dec
13:
14:
;--Dec
15:
16:
;)printf("Dec = %d\n", Dec
}17:

:
;)printf("Dec = %d\n", Dec
;Dec = Dec-1
;)printf("Dec = %d\n", Dec
;Dec -= 1
;)printf("Dec = %d\n", Dec

1:
2:
3:
4:
5:
6:
7:
8:
9:

1.6.1.3 ):(%


5 3 5%3 :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)printf("%d\n", 5%3
}6:

1.6.2 ):(relational operators


) (true
) (false :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)printf("%d\n", 5<3
6:
;)printf("%d\n", 5==3
7:
;)printf("%d\n", 5>3

__________________________________________________________________________________

48
__________________________________________________________________________________________
}8:

:
:0 5 .3
:0 5 .3
:1 5 .3
0 ) (false 1 ) (true
3=5 == = ==
.

3==5

C => =<
=!.
1.6.3 ):(logical operators
:
&& ""
|| ""
! ""
:

;)5<3 && 5>3


;)5==3 && 3==5
;)5>3 && 5<3
;)5<3 || 5>3
;)5==3 || 3==5
;)5>3 || 5<3
;))!(5<3
;))!(5==3
;))!(5>3

>1:#include<stdio.h
2:
)(3:main
{4:
5:
printf("%d\n",
6:
printf("%d\n",
7:
printf("%d\n",
8:
9:
printf("%d\n",
10:
printf("%d\n",
11:
printf("%d\n",
12:
13:
printf("%d\n",
14:
printf("%d\n",
15:
printf("%d\n",
}16:

:
__________________________________________________________________________________

49
__________________________________________________________________________________________

:0 .5<3
:0 .
:0 .5<3
:1 .5>3
:0 .
:1 .5>3
:1 5 .3
:1 5 .3
:0 5 .3
1.6.4 :
C << :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
printf("%d\n", 2<<8);/* = (2E8)*2 */
}6:

2 8 .2
>> :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
printf("%d\n", 256>>8);/* = printf("%d\n", (256/8)/2); */
}6:

256 8 .2
# #define:
>1:#include<stdio.h
2:
)"3:#define printer(str) printf(#str "\n
4:
5:

__________________________________________________________________________________

50
__________________________________________________________________________________________
6:main()
7:{
8:
printer(Hello);
9:}

Merge #define ##
:
1:#include<stdio.h>
2:
3:#define MergingName(str, strplus) str##strplus
4:
5:main()
6:{
7:
char MergingName(ch, 1) = 'H';
8:
char MergingName(ch, 2) = 'e';
9:
char MergingName(ch, 3) = 'l';
10:
char MergingName(ch, 4) = 'l';
11:
char MergingName(ch, 5) = 'o';
12:
13:
printf("%c%c%c%c%c\n", ch1, ch2, ch3, ch4, ch5);
14:
15:}

: 1.6.5
.
: 1.6.6
printf
1 0
&& || !

-1
-2
-3

__________________________________________________________________________________

51
__________________________________________________________________________________________

1.7

if, else, elseif

) (


.
1.7.1 :if
if :

{ }
:

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int usr_val
6:
7:
;)" printf("Enter a value:
8:
;)scanf("%d", &usr_val
9:
10:
)if(usr_val <100
11:
{
12:
;)printf("%d are lesser than 100\n", usr_val
13:
}
14:
15:
)if(usr_val >100
16:
{

__________________________________________________________________________________

52
__________________________________________________________________________________________
;)printf("%d are greater than 100\n", usr_val
}

17:
18:
}19:


100
block if
block .if
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int usr_val
6:
7:
;)" printf("Enter a value:
8:
;)scanf("%d", &usr_val
9:
10:
)if(usr_val <100
11:
;)printf("%d are lesser than 100\n", usr_val
12:
13:
)if(usr_val >100
14:
;)printf("%d are greater than 100\n", usr_val
}15:

.
1.7.2 :else
else if
:else
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int usr_val
6:
7:
;)" printf("Enter a value:
8:
;)scanf("%d", &usr_val
9:
10:
)if(usr_val<100
11:
;)printf("%d are lesser than 100\n", usr_val
12:
else
13:
;)printf("%d are greater than 100\n", usr_val
}14:

else .else
__________________________________________________________________________________

53
__________________________________________________________________________________________

if .else
:

1.7.3 :elseif
else 100
100 elseif
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int usr_val
6:
7:
;)" printf("Enter a value:
8:
;)scanf("%d", &usr_val
9:
10:
)if(usr_val<100
11:
;)printf("%d are lesser than 100\n", usr_val
12:
)else if(usr_val==100
13:
;)printf("%d are equal 100\n", usr_val
14:
else
15:
;)printf("%d are greater than 100\n", usr_val
}16:

__________________________________________________________________________________

54
__________________________________________________________________________________________

100
block 100
100
block
block .else
100

1.7.4 :
-1 else .
-2 else .if
1.7.5:
-1

-2
-3

40

40

40 40.
.else
9
) .(elseif

__________________________________________________________________________________

55
__________________________________________________________________________________________

1.8

C :
1.8.1 :Comments
C .
/ slash * /* slash
.
C
.//
1.8.2 :Keywords

int char double if else
:C
float
static
default
goto

struct
case
switch
extern

void
typedef
union
enum

return
continue
break
sizeof

volatile
for
while
do

else
register
auto
const

char
signed
unsigned
if

int
short
long
double

asm 33 .
1.8.3 :Identifiers

_ underscore
31 .
:

_ a b c d e f g h i j k l m n o p q r s t u v w x
y z

9 8 7 6 5 4 3 2 1 0

__________________________________________________________________________________

56
__________________________________________________________________________________________
A B C D E F G H I J K L M N O P Q R S T U V W X Y
Z

1.8.3.1 :Trigraphs


:
Trigraphs

=??
<??
>??
(??
)??
??/
??
!??
??-

#
{
}
[
]
\
^
|
~

:
>1:??=include<stdio.h
2:
)(3:main
<??4:
5:
;)"printf("Hello, World!\n
>??6:

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)"printf("??=\n
6:
;)"printf("??<\n
7:
;)"printf("??>\n
8:
;)"printf("??(\n
9:
;)"printf("??)\n
10:
;)"printf("??//\n
11:
;)"printf("??'\n
12:
;)"printf("??!\n
13:
;)"printf("??-\n
}14:

1.8.4 :Constants

:
__________________________________________________________________________________

57
__________________________________________________________________________________________

1.8.4.1 :

#define const
#define :
>1:#include<stdio.h
2:
"3:#define Const_Str "Hello, World!\n
4:
)(5:main
{6:
7:
;)printf(Const_Str
8:
9:
/*can't write Const_Str = "Hello, World2!\n"*/
}10:

#define .
const :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;'const char ch = 'a
6:
7:
;)printf("%c\n", ch
8:
9:
/*you can't use this:*/
10:
/*ch = 'b';*/
11:
/*printf("%c\n", ch);*/
12:
/*because it's a constant*/
}13:

const .
C :

'


Alert

\a

"

"\


Backspace

\b

?\

\\

Form feed

Newline

\f
\n

__________________________________________________________________________________

58
__________________________________________________________________________________________

\r

Carriage return

\t

\v

1.8.4.2 :


short, long, int, unsigned, signed, float, double
#define const :#define
>1:#include<stdio.h
2:
3:#define Const_Num 5
4:
)(5:main
{6:
7:
;)printf("%d\n", Const_Num
8:
9:
/*can't write Const_Num = 6;*/
}10:

#define
.
:const
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int Const_Num = 5
6:
;)printf("%d\n", Const_Num
7:
8:
/*can't write Const_Num = 6;*/
}9:

1.8.5 :Tokens
: .
1.8.6 :String literals
" "...
.
__________________________________________________________________________________

59
__________________________________________________________________________________________

1.8.7 :
.
1.8.8:

__________________________________________________________________________________

60
__________________________________________________________________________________________

1.9
printf
%d:

%d
scanf :

1.9.1 :
C
:
1.9.1.1 :


:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int num1, num2
6:
7:
;)" printf("The year now:
8:
;)scanf("%d", &num1
9:
10:
;)" printf("Your year:
11:
;)scanf("%d", &num2
12:
13:
;)printf("You have %d years!\n", num1-num2
}14:

__________________________________________________________________________________

61
__________________________________________________________________________________________


.

.
.
1.9.1.2 :


:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int num1, num2
6:
;char Char
7:
8:
;)" printf("1:(+)\n2:(-)\n3:(/)\n4:(*)\nEnter a choice:
9:
;)scanf("%c", &Char
10:
11:
;)" printf("Enter the first number:
12:
;)scanf ("%d", &num1
13:
14:
;)" printf("Enter the second number:
15:
;)scanf("%d", &num2
16:
17:
)'if(Char == '+' || Char == '1
18:
;)printf("%d + %d = %d\n", num1, num2, num1+num2
19:
20:
)'else if(Char == '-' || Char == '2
21:
;)printf("%d - %d = %d\n", num1, num2, num1-num2
22:
23:
)'else if(Char == '/' || Char == '3
24:
;)printf("%d / %d = %d\n", num1, num2, num1/num2
25:
26:
)'else if(Char == '*' || Char == '4
27:
;)printf("%d * %d = %d\n", num1, num2, num1*num2
28:
29:
else
30:
;)"printf("Error in choice!\nExiting...\n
}31:

__________________________________________________________________________________

62
__________________________________________________________________________________________


.
.

+ 1

.

Char

1.9.1.3 :

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int num, x
6:
7:
;)" printf("Enter a number:
8:
;)scanf ("%d", &num
9:
10:
)if(num<0
11:
;x = -num
12:
else
13:
;x = num
14:
15:
;)printf("|%d|=%d\n", num, x
}16:


.

.

x num
.
__________________________________________________________________________________

63
__________________________________________________________________________________________

.
1.9.1.4 :

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int num1, num2, max
6:
7:
;)" printf("Enter a number:
8:
;)scanf("%d", &num1
9:
10:
;)" printf("Enter another number:
11:
;)scanf("%d", &num2
12:
13:
)if(num1>num2
14:
;max = num1
15:
else
16:
;max = num2
17:
18:
;)printf("max = %d\n", max
}19:


.
.

max num1
max num2
.
.
1.9.2 putchar :getchar
putchar
get character .stdio.h

character

put getchar

putchar ! Hello, World:


__________________________________________________________________________________

64
__________________________________________________________________________________________
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)'putchar('H
6:
;)'putchar('e
7:
;)'putchar('l
8:
;)'putchar('l
9:
;)'putchar('o
10:
;)'putchar(',
11:
;)' '(putchar
12:
;)'putchar('w
13:
;)'putchar('o
14:
;)'putchar('r
15:
;)'putchar('l
16:
;)'putchar('d
17:
;)'putchar('\n
}18:

.printf
getchar :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;char ch
6:
7:
;)'putchar(':
8:
9:
;)(ch = getchar
10:
11:
;)putchar(ch
}12:

)( ch = getchar getchar
getchar .
1.9.3 puts :gets
puts gets stdio.h
.string

string

put

get

puts printf
printf:
-1

\n puts .

__________________________________________________________________________________

65
__________________________________________________________________________________________

-2

puts
.%d

:puts
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)"!puts("Hello, World
6:
;)"!puts("Goodbay, World
}7:

gets
.
1.9.4 wprintf :wscanf
printf scanf wide printf format
format :

wide scan

>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int num, x
6:
7:
;)" wprintf(L"Enter a number:
8:
;)wscanf(L"%d", &num
9:
10:
)if(num<0
11:
;x = -num
12:
else
13:
;x = num
14:
15:
;)wprintf(L"|%d|=%d\n", num, x
}16:

L .long
1.9.5 putch getch :getche
putche getche C linux
stdio.h Windows ) conio.h Console Input

__________________________________________________________________________________

66
__________________________________________________________________________________________

(Output getch C
Windows .
C
stdio.h .getch

ANSI

putch putchar getch getchar :


-1
-2

getchar getch .
getchar
getch .

putch :getch
/* Just in Windows and DOS OS compilers */

>1:#include<stdio.h
>2:#include<conio.h
3:
)(4:main
{5:
6:
;char ch
7:
8:
;)(ch = getch
9:
;)putch(c
10:
;)'putch('\n
}11:

conio.h getch
getchar
\n .
getch
:
/* Just in Windows and DOS OS compilers */

>1:#include<stdio.h
>2:#include<conio.h
3:
)(4:main
{5:
6:
;char ch
7:
8:
;)(ch = getch
9:
;)putch(ch
10:
;)'putch('\n

__________________________________________________________________________________

67
__________________________________________________________________________________________
;)"printf("Press any key to exit\n
getch(); /*pause*/

11:
12:
13:
}13:


.
getche getch :
/* Just in Windows and DOS OS compilers */

>1:#include<stdio.h
>2:#include<conio.h
3:
)(4:main
{5:
6:
;char c
7:
8:
;)(c = getche
9:
;)'putch('\n
10:
;)putch(c
11:
;)'putch('\n
}12:

1.9.6 :wchar_t
char wchar_t
character type 2 :

wide-

>1:#include<stdio.h
2:
)(3:main
{4:
5:
;'wchar_t wch = 'a
6:
7:
;)printf("%c\n", wch
}8:

1.9.7 main :wmain


main
) wmain (C main
.Unicode
wmain main :
>1:#include<stdio.h
2:
)(3:wmain
{4:

__________________________________________________________________________________

68
__________________________________________________________________________________________
;)"wprintf(L"Hello, World!\n

5:
}6:

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
}6:

main
:

return

>1:#include<stdio.h
2:
)(3:main
{4:
5:
;return 0
}6:

int int
MicroSoft
:

:
>1:#include<stdio.h
2:
)(3:int main
{4:
5:
;return 0
}6:

void int void


:
>1:#include<stdio.h
2:
)(3:void main
{4:
5:
}6:

__________________________________________________________________________________

69
__________________________________________________________________________________________

1.9.8 :
printf :

%d, %i

%f, %e, %g

%u

%c

%s

][ char

%o

%x

%p

%ld

Long Decimal

%lu

int, short, long

float, double
unsigned, int, short, long

char
*char

long int

long unsigned

scanf :

%d, %i

%f, %e, %g

%u

%c

%s

][ char

%o

%x

%ld

Long Decimal

%lu

int, short, long

float, double
unsigned, int, short, long

char
*char

long int

long unsigned

1.9.9 :
.
__________________________________________________________________________________

70
__________________________________________________________________________________________

1.9.10:
-1

-2
-3
-4
-5


.
printf puts
conio conio.h


__________________________________________________________________________________

71
__________________________________________________________________________________________

)C (2
2.1 switch

repeated loops

2.2
2.3 arrays
2.4 pointers
2.5 Functions
2.6 Header files
2.7 Files I/O
2.8 structures
2.9

__________________________________________________________________________________

72
__________________________________________________________________________________________

switch

2.1

switch elseif else if


:( if, else, elseif)
:
1:#include<stdio.h>
2:
3:main()
4:{
5:
int num1, num2;
6:
char Char;
7:
8:
printf("1:(+)\n2:(-)\n3:(/)\n4:(*)\nEnter a choice: ");
9:
scanf("%c", &Char);
10:
11:
printf("Enter the first number: ");
12:
scanf ("%d", &num1);
13:
14:
printf("Enter the second number: ");
15:
scanf("%d", &num2);
16:
17:
if(Char == '+' || Char == '1')
18:
printf("%d + %d = %d\n", num1, num2, num1+num2);
19:
20:
else if(Char == '-' || Char == '2')
21:
printf("%d - %d = %d\n", num1, num2, num1-num2);
22:
23:
else if(Char == '/' || Char == '3')
24:
printf("%d / %d = %d\n", num1, num2, num1/num2);
25:
26:
else if(Char == '*' || Char == '4')
27:
printf("%d * %d = %d\n", num1, num2, num1*num2);
28:
29:
else
30:
printf("Error in choice!\nExiting...\n");
31:}

: switch
1:#include<stdio.h>
2:
3:main()
4:{
5:
int num1, num2;
6:
char Char;
7:
8:
printf("1:(+)\n2:(-)\n3:(/)\n4:(*)\nEnter a choice: ");
9:
scanf("%c", &Char);
10:
11:
printf("Enter the first number: ");
12:
scanf ("%d", &num1);
13:
14:
printf("Enter the second number: ");

__________________________________________________________________________________

73
__________________________________________________________________________________________
;)scanf("%d", &num2
)switch(Char
{
case '+':
case '1':
;)printf("%d + %d = %d\n", num1, num2, num1+num2
;break
case '-':
case '2':
;)printf("%d - %d = %d\n", num1, num2, num1-num2
;break
case '/':
case '3':
;)printf("%d / %d = %d\n", num1, num2, num1/num2
;break
case '*':
case '4':
;)printf("%d * %d = %d\n", num1, num2, num1*num2
;break
default:
;)"printf("Error in choice!\nExiting...\n
}

15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
}42:


.
switch
Char switch .
:


switch
Char Char
+ 1 Char
switch

__________________________________________________________________________________

74
__________________________________________________________________________________________

Char ...
default .

.
case '+':
case '1':
;)printf("%d + %d = %d\n", num1, num2, num1+num2
;break

switch

else, elseif

19:
20:
21:
22:

if,:

)'if(Char == '+' || Char == '1


;)printf("%d + %d = %d\n", num1, num2, num1+num2

17:
18:

:
case '+':
case '1':

19:
20:

:
'Char == '+' || Char == '1

:default
default:
;)"printf("Error in choice!\nExiting...\n

38:
39:

else :
else
;)"printf("Error in choice!\nExiting...\n

29:
30:

switch float .double


2.1.1 :case
switch

case 1

__________________________________________________________________________________

75
__________________________________________________________________________________________

switch
.
case
switch
.
2.1.2 :break
switch case
.switch
2.1.3 :default
switch
.
2.8.4 :
-1 case
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int num1, num2
6:
;int Char
7:
8:
;)" printf("1:(+)\n2:(-)\n3:(/)\n4:(*)\nEnter a choice:
9:
;)scanf("%d", &Char
10:
11:
;)" printf("Enter the first number:
12:
;)scanf ("%d", &num1
13:
14:
;)" printf("Enter the second number:
15:
;)scanf("%d", &num2
16:
17:
)switch(Char
18:
{
19:
case 1:
20:
;)printf("%d + %d = %d\n", num1, num2, num1+num2
21:
;break
22:
23:
case 2:

__________________________________________________________________________________

76
__________________________________________________________________________________________
;)printf("%d - %d = %d\n", num1, num2, num1-num2
;break
case 3:
;)printf("%d / %d = %d\n", num1, num2, num1/num2
;break
case 4:
;)printf("%d * %d = %d\n", num1, num2, num1*num2
;break
default:
;)"printf("Error in choice!\nExiting...\n
}

24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
}39:

-2 break .
2.8.5:
-1 :
Start Game .
Option
.
About Game .
.Exit Game

__________________________________________________________________________________

77
__________________________________________________________________________________________

2.2

repeated loops



:
2.2.1 :while
if
:



.
while
1 :3
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int i
6:
;i = 1
7:
8:
)while(i!=4
9:
{
10:
;)printf("\a%d ,", i
11:
;i++
12:
}
13:
14:
;)"printf(" Go!\n
}15:

__________________________________________________________________________________

78
__________________________________________________________________________________________

i
1
1 while )( i
4 block while )
i 4 i !=4 (

.
:
>1:#include<stdio.h
>2:#include<conio.h
/* Just in Windows and DOS OS compilers */
3:
)(4:main
{5:
6:
;char ch
7:
8:
;)" printf("Press any key(q=exit):
9:
;)(ch = getche
10:
11:
)'while(ch != 'q
12:
{
13:
;)" printf("\nPress any key(q=exit):
14:
;)(ch = getche
15:
}
16:
17:
;)"printf("\nExiting...\n
}18:

getche


while ch q
.q
while block
if block break
:
/* Just in Windows and DOS OS compilers */

>1:#include<stdio.h
>2:#include<conio.h
3:
)(4:main

__________________________________________________________________________________

79
__________________________________________________________________________________________
;char ch
;)" printf("Press any key(q=exit):
;)(ch = getche
)while(1
{
)'if(ch == 'q
;break
;)" printf("\nPress any key(q=exit):
;)(ch = getche
}
;)"printf("\nExiting!\n

{5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
}21:

)1 true (C while
break .switch

:
>1:#include<stdio.h
>2:#include<conio.h
/* Just in Windows and DOS OS compilers */
3:
)(4:main
{5:
6:
;char ch
7:
8:
)while(1
9:
{
10:
;)" printf("\nPress any key(q=exit):
11:
;)(ch = getche
12:
13:
)'if(ch == 'q
14:
;break
15:
}
16:
17:
;)"printf("\nExiting!\n
}18:

block while
while
.
2.2.2 :dowhile

__________________________________________________________________________________

80
__________________________________________________________________________________________

while dowhile while


while
dowhile block
.
do block
block } while :

:dowhile
:
>1:#include<stdio.h
>2:#include<conio.h
/* Just in Windows and DOS OS compilers */
3:
)(4:main
{5:
6:
;int i
7:
;i = 1
8:
9:
do
10:
{
11:
;)printf("\a%d ,", i
12:
;i++
13:
;)}while(i != 4
14:
15:
;)"printf(" Go!\n
}16:

:
/* Just in Windows and DOS OS compilers */

>1:#include<stdio.h
>2:#include<conio.h
3:
)(4:main

__________________________________________________________________________________

81
__________________________________________________________________________________________
;char ch
do
{
;)" printf("\nPress any key(q=exit):
;)(ch = getche
;)'}while(ch != 'q
;)"printf("\nExiting...\n

{5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
}15:


.
2.2.3 :for

.
for :
-1
-2
-3

.
.
) (.

:
>1:#include<stdio.h

__________________________________________________________________________________

82
__________________________________________________________________________________________
>2:#include<conio.h
/* Just in Windows and DOS OS compilers */
3:
)(4:main
{5:
6:
;int i
7:
8:
)for(i=1;i<=3;i++
9:
{
10:
;)printf("\a%d ,", i
11:
}
12:
13:
;)"printf(" Go!\n
}14:

for

i ) 3 i 3 (
.


ch ) q ch (q
:
>1:#include<stdio.h
>2:#include<conio.h
/* Just in Windows and DOS OS compilers */
3:
)(4:main
{5:
6:
;char ch
7:
8:
;)" printf("Press any key(q=exit):
9:
;)(ch = getche
10:
11:
);'for(;ch!='q
12:
{
13:
;)" printf("\nPress any key(q=exit):
14:
;)(ch = getche
15:
}
16:
17:
;)"printf("\nExiting...\n
}18:


.
2.2.4 :goto
__________________________________________________________________________________

83
__________________________________________________________________________________________

goto
.
goto
:

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;goto fin
6:
;)"printf("Begin!\n
7:
8:fin:
9:
;)"printf("Fin!\n
}10:

goto the_name_of_place
fin fin
.
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:begin:
6:
;goto fin
7:
;)"printf("Begin!\n
8:
9:fin:
10:
;)"printf("Fin!\n
11:
;goto begin
}12:

__________________________________________________________________________________

84
__________________________________________________________________________________________


:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int i
6:
;i = 0
7:
8:
;)"printf("Begin!\n
9:
10:begin:
11:
;)printf("%d ,", i
12:
;i++
13:
)if(i==10
14:
;goto fin
15:
else
16:
;goto begin
17:
18:fin:
19:
;)"printf("\nFin!\n
}20:

.goto
2.2.5 :
:
-1

-2

-3


.

.

.

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int i
6:
;i = 1
7:
8:
)while(i!=4

__________________________________________________________________________________

85
__________________________________________________________________________________________
{
;)printf("\a%d ,", i
;i++
}
;)"printf(" Go!\n

9:
10:
11:
12:
13:
14:
}15:

while :dowhile

:for

__________________________________________________________________________________

86
__________________________________________________________________________________________

goto
.

while, do...while, for :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int i
6:
7:
)for(i=1;i<=3;i++
8:
{
9:
;)printf("\a%d ,", i
10:
}
11:
12:
;)"printf(" Go!\n
}14:

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int i
6:
;i=1
7:
8:
);;(for
9:
{
10:
)if(i>3
11:
;break
12:
;)printf("\a%d ,", i
13:
;i++
14:
}
15:
16:
;)"printf(" Go!\n
}17:

__________________________________________________________________________________

87
__________________________________________________________________________________________

for while dowhile


.
2.2.7 :continue
continue
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int i
6:
;i = 1
7:
8:
)while(1
9:
{
10:
)if(i<=3
11:
{
12:
;)printf("\a%d ,", i
13:
;++i
14:
}
15:
16:
;)"printf(" Go!\n
17:
;break
18:
}
19:
}20:

1 3
1 continue
:
>1:#include<stdio.h
2:
)(3:main

__________________________________________________________________________________

88
__________________________________________________________________________________________
;int i
;i = 1
)while(1
{
)if(i<=3
{
;)printf("\a%d ,", i
;++i
;continue
}
;)"printf(" Go!\n
;break
}

{4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
}21:

dowhile .for
2.2.8 :ASCII
ASCII :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int i
6:
7:
)for(i=0;i<=255;i++
8:
;)printf("%d: %c\n", i, i
9:
}10:

0 7 8 9 13
0 \0 13
.
2.2.9 :
-1 .
-2 dowhile
.

__________________________________________________________________________________

89
__________________________________________________________________________________________

-3 for
);;(.for
-4 ) while(1 .
-5 continue .
2.2.10:
-1
-2

100 0 .
0 100 .

__________________________________________________________________________________

90
__________________________________________________________________________________________

2.3

arrays


.

20
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int arr1, arr2, arr3, arr4, arr5, ...,arr25
6:
7:
;)" printf("Arr1:
8:
;)scanf("%d", arr1
9:
10:
;)" printf("Arr2:
11:
;)scanf("%d", arr2
12:
13:
;)" printf("Arr3:
14:
;)scanf("%d", arr3
15:
16:
;)" printf("Arr4:
17:
;)scanf("%d", arr4
18:
19:
;)" printf("Arr5:
20:
;)scanf("%d", arr5
21:
22:
...
23:
24:
;)" printf("Arr25:
25:
;)scanf("%d", arr25
26:
}27:

...
200
.

:

__________________________________________________________________________________

91
__________________________________________________________________________________________

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;]int arr[24
6:
;int i
7:
8:
)for(i=0;i<25;i++
9:
{
10:
;)printf("Arr%d: ", i
11:
;)]scanf("%d", &arr[i
12:
}
13:
14:
;)"printf("***********- LIST -***********\n
15:
16:
)for(i=0;i<25;i++
17:
{
18:
;)]printf("Arr%d: %d\n", i, arr[i
19:
}
20:
}21:


.
2.3.1 :
4 ]arr[4

int

int
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;}int arr[4] = {10, 12, 13, 15
6:
7:
;)]printf("[%d][%d][%d][%d]\n", arr[0], arr[1], arr[2], arr[3
}8:

__________________________________________________________________________________

92
__________________________________________________________________________________________


0
0 0 .
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;]int arr[4
6:
;}arr[4] = {10, 12, 13, 15
7:
8:
;)]printf("[%d][%d][%d][%d]\n", arr[0], arr[1], arr[2], arr[3
}9:


:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;]int arr[4
6:
;arr[0] = 10
7:
;arr[1] = 12
8:
;arr[2] = 13
9:
;arr[3] = 15
10:
11:
;)]printf("[%d][%d][%d][%d]\n", arr[0], arr[1], arr[2], arr[3
}12:

.
2.3.2 :

.

:
>1:#include<stdio.h
2:
)(3:main
{4:

__________________________________________________________________________________

93
__________________________________________________________________________________________
;]int arr2d[2][2
;int i,j
;arr2d[0][0] = 10
;arr2d[0][1] = 20
;arr2d[1][0] = 30
;arr2d[1][1] = 40
)for(i=0;i<=1;i++
{
)for(j=0;j<=1;j++
{
;)]printf("arr2d[%d][%d] = %d\n", i, j, arr2d[i][j
}
}

5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
}22:


] [0 ].[1

2 2

.

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;}}int arr2d[2][2] = {{10, 20}, {30, 40
6:
;int i,j
7:
8:
)for(i=0;i<=1;i++
9:
{
10:
)for(j=0;j<=1;j++
11:
{
12:
;)]printf("arr2d[%d][%d] = %d\n", i, j, arr2d[i][j
13:
}
14:
}
15:
}16:

.
__________________________________________________________________________________

94
__________________________________________________________________________________________

2.3.2 :

:
:

k,

j,

>1:#include<stdio.h
2:
)(3:main
{4:
5:
;}}}int arr3d[2][2][2] = {{{10, 20},{30, 40}}, {{50, 60},{70, 80
6:
;int i, j, k
7:
8:
)for(i=0;i<=1;i++
9:
{
10:
)for(j=0;j<=1;j++
11:
{
12:
)for(k=0;k<=1;k++
13:
{
14:
]printf("arr3d[%d][%d][%d
=
%d\n",
i,
;)]arr3d[i][j][k
15:
}
16:
}
17:
}
18:
}19:

k,

j,

i,

>1:#include<stdio.h
2:
)(3:main
{4:
5:
;]int arr3d[2][2][2
6:
;int i, j, k
7:
8:
)for(i=0;i<=1;i++
9:
{
10:
)for(j=0;j<=1;j++
11:
{
12:
)for(k=0;k<=1;k++
13:
{
14:
;)printf("arr3d[%d][%d][%d] : ", i, j, k
15:
;)]scanf("%d", &arr3d[i][j][k
16:
}
17:
}
18:
}
19:
20:
)for(i=0;i<=1;i++
21:
{
22:
)for(j=0;j<=1;j++
23:
{
24:
)for(k=0;k<=1;k++
25:
{
26:
]printf("arr3d[%d][%d][%d
=
%d\n",
;)]arr3d[i][j][k

__________________________________________________________________________________

95
__________________________________________________________________________________________
}
}
}

27:
28:
29:
30:
}31:

2.3.3 :


.
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;}int arr[] = {10, 20, 30
6:
7:
;)]printf("%d\n", arr[0
8:
;)]printf("%d\n", arr[1
9:
;)]printf("%d\n", arr[2
}10:

:
;}int arr[] = {10, 20, 30

5:


:
;}}}int arr3d[][][] = {{{10, 20},{30, 40}}, {{50, 60},{70, 80


:
;}}}int arr3d[][2][2] = {{{10, 20},{30, 40}}, {{50, 60},{70, 80

2.3.4 )(:
.

__________________________________________________________________________________

96
__________________________________________________________________________________________


:
char
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;"!char text[14] = "Hello, World
6:
7:
;)printf("%s\n", text
}8:

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;]char text[14
6:
7:
;'text[0] = 'H
8:
;'text[1] = 'e
9:
;'text[2] = 'l
10:
;'text[3] = 'l
11:
;'text[4] = 'o
12:
;'text[5] = ',
13:
;' ' = ]text[6
14:
;'text[7] = 'w
15:
;'text[8] = 'o
16:
;'text[9] = 'r
17:
;'text[10] = 'l
18:
;'text[11] = 'd
19:
;'!' = ]text[12
20:
;'text[13] = '\0
21:
22:
;)printf("%s\n", text
}23:

\0

:
;"!char text[14] = "Hello, World

5:

) 13 ( 14
\0 .
__________________________________________________________________________________

97
__________________________________________________________________________________________

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;]char usr_name[255
6:
;int i
7:
;i = 0
8:
9:
;)" printf("Your name:
10:
;)scanf("%s", &usr_name
11:
12:
)'while(usr_name[i] != '\0
13:
{
14:
;++i
15:
}
16:
17:
;)printf("%s = %d characters\n", usr_name, i
}18:

:
usr_name 255



%s s string

:

__________________________________________________________________________________

98
__________________________________________________________________________________________

.
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;]char usr_name[255
6:
;int i
7:
;i = 0
8:
9:
;)" printf("Your Fullname:
10:
;)scanf("%s", &usr_name
11:
12:
)'while(usr_name[i]!='\0
13:
{
14:
;)]printf("%i: %c\n", i+1, usr_name[i
15:
;++i
16:
}
17:
18:
;)printf("%s = %d characters\n", usr_name, i
}19:

2.3.4.1 :gets

gets .


) gets(string_name :

)&string_name

scanf("%s",
>1:#include<stdio.h
2:

__________________________________________________________________________________

99
__________________________________________________________________________________________
)(3:main
{4:
5:
;]char usr_name[255
6:
7:
;)"puts("Your name:
8:
;)gets(usr_name
9:
10:
;)"puts("nice to meet you
11:
;)puts(usr_name
}12:

scanf gets gets


scanf
.
2.3.4.2 strcpy :strncpy

strcpy string.h
string copy
:
>1:#include<stdio.h
>2:#include<string.h
3:
)(4:main
{5:
6:
;"!char String[] = "String
7:
;]char Empty_String[20
8:
9:
;)strcpy(Empty_String, String
10:
11:
;)printf("Empty_String = %s\n", Empty_String
}12:


.
strncpy string.h strcpy
:
>1:#include<stdio.h
>2:#include<string.h
3:
)(4:main
{5:
6:
;"!char String[] = "String
7:
;]char Empty_String[20
8:
9:
;)strncpy(Empty_String, String, 3

__________________________________________________________________________________

100
__________________________________________________________________________________________
10:
11:
12:}

printf("Empty_String = %s\n", Empty_String);

. strncpy
:
1:#include<stdio.h>
2:#include<string.h>
3:
4:main()
5:{
6:
char Empty_String[20];
7:
8:
strcpy(Empty_String, "String!");
9:
10:
printf("Empty_String = %s\n", Empty_String);
11:}

:strncat strcat 2.3.4.3

string

concatenate

string.h strcat
:

1:#include<stdio.h>
2:#include<string.h>
3:
4:main()
5:{
6:
char String[20] = "String!";
7:
8:
strcat(String, ", String2");
9:
10:
printf("String = %s\n", String);
11:}

strcat
.
: strcat strncat
1:#include<stdio.h>
2:#include<string.h>
3:
4:main()
5:{
6:
char String[20] = "String!";
7:
8:
strncat(String, ", String2", 3);
9:
10:
printf("String = %s\n", String);

__________________________________________________________________________________

101
__________________________________________________________________________________________
}11:

.
2.3.5 :

= 10

) *(arr+0

= 10

] arr[0 :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;]int arr[1
6:
;arr[0] = 10
7:
8:
;)]printf("%d\n", arr[0
}9:

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;]int arr[1
6:
;*(arr+0) = 10
7:
8:
;))printf("%d\n", *(arr+0
}9:

]arr[0

).*(arr+0

arr[0+0]); /* or printf("%d\n", arr[0]); */

arr[0-0]); /* or printf("%d\n", arr[0]); */

arr[1*1]); /* or printf("%d\n", arr[1]); */

arr[1/1]); /* or printf("%d\n", arr[1]); */

>1:#include<stdio.h
2:
)(3:main
{4:
5:
;]int arr[2
6:
7:
;arr[0+0] = 10
8:
printf("%d\n",
9:
10:
;arr[0-0] = 20
11:
printf("%d\n",
12:
13:
;arr[1*1] = 30
14:
printf("%d\n",
15:
16:
;arr[1/1] = 40
17:
printf("%d\n",
}18:

__________________________________________________________________________________

102
__________________________________________________________________________________________

2.3.6 :
-1 2 arr :
; ]int arr[2
; arr[0] = 10
; arr[8] = 20

; ]int arr[2
; arr[0] = 10
; arr[1] = 20

-2
\0 .
-3 gets .
2.3.7:
-1

-2

-3

)
puts .(gets
)
(.
! Hello, World
) 13 ( 12
.1-

__________________________________________________________________________________

103
__________________________________________________________________________________________

2.4

pointers

C .

.C
:


.
2.4.1 :Pointer Type

.signed, char,

int, float, double, long, short, unsigned,

2.4.2 :Pointer Name


:

31.
.
....
% # { )... _(.
.

__________________________________________________________________________________

104
__________________________________________________________________________________________


:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int *ptr, i
6:
;i = 10
7:
;ptr = &i
8:
9:
;)printf("*ptr = %d\n", *ptr
}10:

ptr i i
10 ptr i
&Variable_Name *ptr
ptr
.ptr
ptr i
i ptr :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int *ptr, i
6:
;i = 10
7:
;ptr = &i
8:
;*ptr = 100
9:
10:
;)printf("*ptr = %d\n", *ptr
}11:

ptr i
i .100
:
>1:#include<stdio.h
2:

__________________________________________________________________________________

105
__________________________________________________________________________________________
)(3:main
{4:
5:
;int *ptr, i
6:
;i = 10
7:
;ptr = &i
8:
9:
;)printf("i = %d\n", i
10:
;)printf("*ptr = %d\n", *ptr
11:
;)printf("&i = %p\n", &i
12:
;)printf("ptr = %p\n", ptr
}13:

i
ptr
) (
i ptr %p
pointer
.
:

2.4.3 :

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;]int arr[2
6:
;int *ptr

__________________________________________________________________________________

106
__________________________________________________________________________________________
;arr[0] = 10
;arr[1] = 20
;]ptr = &arr[0
;)]printf("%d\n", ptr[0
;)]printf("%d\n", ptr[1

7:
8:
9:
10:
11:
12:
13:
14:
}15:

2
ptr
.ptr
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;]int arr[2
6:
;int *ptr
7:
8:
;*(arr+0) = 10
9:
;*(arr+1) = 20
10:
11:
;)ptr = &*(arr+0
12:
13:
;))printf("%d\n", *(ptr+0
14:
;))printf("%d\n", *(ptr+1
}15:

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;]int arr[2
6:
;int *ptr
7:
8:
;arr[0] = 10
9:
;arr[1] = 20
10:
11:
;]ptr = &arr[0
12:
13:
;)printf("%d\n", *ptr
14:
;)printf("%d\n", *++ptr
}15:

:
>1:#include<stdio.h
2:

__________________________________________________________________________________

107
__________________________________________________________________________________________
)(3:main
{4:
5:
;]int arr[2
6:
;int *ptr
7:
8:
;arr[0] = 10
9:
;arr[1] = 20
10:
11:
;]ptr = &arr[0
12:
13:
;)printf("%d\n", arr
14:
;)printf("%d\n", ++arr
}15:

2.4.4 :
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;"!char *str = "Hello, World
6:
7:
;)printf("%s\n", str
}8:


:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;char *str
6:
7:
;)" printf("Enter a string:
8:
;)scanf("%s", str
9:
;)printf("%s\n", str
}10:


str
str str
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;char *str
6:
;char adr_str
7:

__________________________________________________________________________________

108
__________________________________________________________________________________________
;str = &adr_str
;)" printf("Enter a string:
;)scanf("%s", str
;)printf("%s\n", str

8:
9:
10:
11:
12:
13:
}14:

str &
.
& scanf :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;]char arr[255
6:
7:
;)" printf("Enter a string:
8:
;)scanf("%s", arr
9:
10:
;)printf("%s\n", arr
}11:

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;}int arr[3] = {10, 20, 30
6:
;int *ptr
7:
;int i
8:
9:
;ptr = arr
10:
11:
)for(i=0;i<=2;i++
12:
;)]printf("ptr[%d] = %d\n", i, ptr[i
}13:

= ptr

.arr

2.4.5 :reference
:
>1:#include<stdio.h
2:
{)(3:main
4:
;int ref
5:
;int *ptr
6:
;ref = 10

__________________________________________________________________________________

109
__________________________________________________________________________________________
;ptr = &ref

7:
}8:

ptr &ref .
& .reference
2.4.6 :void
void
void
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;void *p_void
6:
/* can't use void p_void; */
7:
8:
;(int)p_void = 10
9:
;)printf("(int)p_void = %d\n", p_void
10:
11:
;'(char)p_void = 'H
12:
;)printf("(char)p_void = %c\n", p_void
13:
14:
;"(char *)p_void = "Hello
15:
;)printf("(char *)p_void = %s\n", p_void
}16:

void (Type)Pointer_Name Type


.
2.4.7 :
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;]int *arr[2
6:
;int a = 10, b = 20
7:
8:
;)printf("A = %d, ", a
9:
;)printf("B = %d\n", b
10:
11:
;arr[0] = &a
12:
;*arr[0] = 5
13:

__________________________________________________________________________________

110
__________________________________________________________________________________________
;arr[1] = &b
;*arr[1] = 10
;)printf("A = %d, ", a
;)printf("B = %d\n", b

14:
15:
16:
17:
18:
}19:

.
:
>1:#include<stdio.h
2:
{)(3:main
4:
;}"char *arr[] = {"Text 1", "Text 2", "Text 3
5:
6:
;)]printf("arr[0] = %s\n", arr[0
7:
;)]printf("arr[1] = %s\n", arr[1
8:
;)]printf("arr[2] = %s\n", arr[2
}9:

2.4.8 :
.
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int p
6:
;int *pt
7:
;int **ptr
8:
9:
;p = 10
10:
;)printf("p = %d\n", p
11:
12:
;pt = &p
13:
;ptr = &pt
14:
;**ptr = 5
15:
16:
;)printf("p = %d\n", p
}17:

.

:
>1:#include<stdio.h
2:
{)(3:main
4:
;char *AdrPtr

__________________________________________________________________________________

111
__________________________________________________________________________________________
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:}

char **ptr = &AdrPtr;


ptr[0] = "Text 1";
ptr[1] = "Text 2";
ptr[2] = "Text 3";
printf("ptr[0] = %s\n", ptr[0]);
printf("ptr[1] = %s\n", ptr[1]);
printf("ptr[2] = %s\n", ptr[2]);

. int

*****ptr


: 2.4.9

: -1
1:#include<stdio.h>
2:
3:main()
4:{
5:
int *ptr;
6:
*ptr = 10;
7:
8:
printf("%d\n", *ptr);
9:}

:
1:#include<stdio.h>
2:
3:main()
4:{
5:
int *ptr;
6:
int i;
7:
*ptr = i;
8:
9:}

: -2
1:#include<stdio.h>
2:
3:main()
4:{
5:
int *ptr;
6:
int i;
7:
i = ptr;
8:
9:}

: 2.4.10
__________________________________________________________________________________

112
__________________________________________________________________________________________

-1

1:#include<stdio.h>
2:
3:main()
4:{
5:
char usr_name[255];
6:
int i;
7:
i = 0;
8:
9:
printf("Your Fullname: ");
10:
scanf("%s", &usr_name);
11:
12:
while(usr_name[i]!='\0')
13:
{
14:
printf("%i: %c\n", i+1, usr_name[i]);
15:
++i;
16:
}
17:
18:
printf("%s = %d characters\n", usr_name, i);
19:}

__________________________________________________________________________________

113
__________________________________________________________________________________________

2.5

Functions


. .
:

.
! Hello, World :
>1:#include<stdio.h
2:
)(3:void Func_HelloWorld
{4:
5:
;)"printf("Hello, World!\n
}6:
7:
)(8:main
{9:
10:
;)(Func_HelloWorld
}11:

) (prototype
:
>1:#include<stdio.h
2:
;)(3:void Func_HelloWorld
4:
)(5:main
{6:
7:
;)(Func_HelloWorld
}8:
9:

__________________________________________________________________________________

114
__________________________________________________________________________________________
)(10:void Func_HelloWorld
{11:
12:
;)"printf("Hello, World!\n
}13:

.

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)(Func_HelloWorld
}6:
7:
)(8:void Func_HelloWorld
{9:
10:
;)"printf("Hello, World!\n
}11:

prototype
Func_HelloWorld C
.
void 0 .

:
>1:#include<stdio.h
2:
;)3:void Func_Print(const char *str
4:
)(5:main
{6:
7:
;)"Func_Print("Func_Print:\n
8:
;)"Func_Print("Hello, World!\n
}9:
10:
)11:void Func_Print(const char *str
{12:
13:
;)printf("%s", str
}14:

__________________________________________________________________________________

115
__________________________________________________________________________________________


:
1:#include<stdio.h>
2:
3:void Func_Add(const int num1, const int num2);
4:void Func_Sub(const int num1, const int num2);
5:void Func_Mul(const int num1, const int num2);
6:void Func_Div(const int num1, const int num2);
7:
8:main()
9:{
10:
Func_Add(30, 10);
11:
Func_Sub(30, 10);
12:
Func_Mul(30, 10);
13:
Func_Div(30, 10);
14:}
15:
16:void Func_Add(const int num1, const int num2)
17:{
18:
printf("%d + %d = %d\n", num1, num2, num1+num2);
19:}
20:
21:void Func_Sub(const int num1, const int num2)
22:{
23:
printf("%d - %d = %d\n", num1, num2, num1-num2);
24:}
25:
26:
27:void Func_Mul(const int num1, const int num2)
28:{
29:
printf("%d * %d = %d\n", num1, num2, num1*num2);
30:}
31:
32:
33:void Func_Div(const int num1, const int num2)
34:{
35:
printf("%d / %d = %d\n", num1, num2, num1/num2);
36:}

:
1:#include<stdio.h>
2:
3:void Func_(const int num1, const char sign, const int num2);
4:
5:main()
6:{
7:
Func_(30, '+', 10);
8:
Func_(30, '-', 10);
9:
Func_(30, '*', 10);
10:
Func_(30, '/', 10);
11:}
12:
13:void Func_(const int num1, const char sign, const int num2)
14:{
15:
switch(sign)
16:
{
17:
case '+':

__________________________________________________________________________________

116
__________________________________________________________________________________________
;)%c %d = %d\n", num1, sign, num2, num1+num2

;)%c %d = %d\n", num1, sign, num2, num1-num2

;)%c %d = %d\n", num1, sign, num2, num1*num2

;)%c %d = %d\n", num1, sign, num2, num1/num2

printf("%d
;break
case '-':
printf("%d
;break
case '*':
printf("%d
;break
case '/':
printf("%d
;break

default:
;)"printf("ERROR!\n
}

18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
}33:

2.5.1 :Function Type


int
.float

return :
>1:#include<stdio.h
2:
;)3:int Int_Func(const int num
4:
)(5:main
{6:
7:
;))printf("%d\n", Int_Func(5
}8:
9:
)10:int Int_Func(const int num
{11:
12:
;return num
}13:

int num Int_Func printf



void .
C
int :
>1:#include<stdio.h
2:
;)3:Int_Func(const int num
4:

__________________________________________________________________________________

117
__________________________________________________________________________________________
5:main()
6:{
7:
printf("%d\n", Int_Func(5));
8:}
9:
10:Int_Func(const int num)
11:{
12:
return num;
13:}


:
1:#include<stdio.h>
2:
3:Int_Func(const int num);
4:
5:main()
6:{
7:
printf("%d\n", Int_Func(5));
8:}
9:
10:Int_Func(num)
11:const int num;
12:{
13:
return num;
14:}

:
1:#include<stdio.h>
2:
3:int Int_Func(const int num);
4:
5:main()
6:{
7:
printf("%d\n", Int_Func(5));
8:}
9:
10:int Int_Func(const int num)
11:{
12:
return num;
13:}

:short
1:#include<stdio.h>
2:
3:short Short_Func(const short num);
4:
5:main()
6:{
7:
printf("%d\n", Short _Func(5));
8:}
9:
10:short Short _Func(const short num)
11:{

__________________________________________________________________________________

118
__________________________________________________________________________________________
12:
13:}

return num;

char unsigned signed double float long


:
1:#include<stdio.h>
2:
3:char Char_Func(const char ch);
4:
5:main()
6:{
7:
printf("%c\n", Char_Func('C'));
8:}
9:
10:char Char_Func(const char ch)
11:{
12:
return ch;
13:}

:
1:#include<stdio.h>
2:
3:int Int_Func(const int num);
4:
6:main()
7:{
8:
int i = Int_Func(5);
9:
10:
printf("i = %d\n", i);
11:}
12:
13:int Int_Func(const int num)
14:{
15:
return num*2;
16:}

:
1:#include<stdio.h>
2:
3:char *string(char *str){
4:
return str;
5:}
6:
7:main()
8:{
9:
printf("%s\n", string("Hello, World!"));
10:}

:Function Name 2.5.2


:
__________________________________________________________________________________

119
__________________________________________________________________________________________

31.
.
....
% # { )... _(.
.
.

2.5.3 :Function Parameters


unsigned , signed,

][.short, long, int, float, double, char, char*, char


.
2.5.4:

.main
2.5.5 :#define
macros :
>1:#include<stdio.h
2:
)3:#define Add(a, b) (a+b
4:
)(5:main
{6:
7:
;int a, b
8:
;a = 50
9:
;b = 100
10:
11:
;))printf("%d + %d = %d\n", a, b, Add(a, b
}12:


.#define
:
>1:#include<stdio.h

__________________________________________________________________________________

120
__________________________________________________________________________________________
2:
)3:#define PrintF_(String) printf("%s", String
4:
)(5:main
{6:
7:
;)"PrintF_("Macro...\n
}8:

2.5.6 :
C Procedure
void
int float
.
2.5.7 :
:
>1:#include<stdio.h
2:
;))3:void CallFunc(void Func_Name(void
;)(4:void Function
5:
{)(6:main
;)CallFunc(Function
}
{))void CallFunc(void Func_Name(void
;)"printf("Call Function:\n
;)(Func_Name
}
{)(void Function
;)"printf("This is a function!\n
}


. :
>1:#include<stdio.h
2:
;))3:void CallFunc(void Func_Name(int a, int b
;)4:void Function(int a, int b
5:
{)(6:main
7:
;)CallFunc(Function
}8:
9:
{))10:void CallFunc(void Func_Name(int a, int b
11:
;)"printf("Call Function:\n
12:
;)Func_Name(10, 20

__________________________________________________________________________________

121
__________________________________________________________________________________________
}13:
14:
{)15:void Function(int a, int b
16:
;)printf("%d + %d = %d\n", a, b, a+b
}17:

2.5.8 :
-1 :
>1:#include<stdio.h
2:
;)(3:void Function
4:
)(5:main
{6:
7:
;)(Function
}8:
9:
;)(10:void Function
{11:
12:
/*Empty Funtion;*/
}13:


.
-2 .
2.5.9:
-1

-2

StrLen
.char *str
abs
.int value

const

__________________________________________________________________________________

122
__________________________________________________________________________________________

2.6

Header files

stdio.h conio.h
.

.h functions.h :
1:/*Functions Header file*/
2:
)3:int Add(int num1, int num2
{4:
5:
;return num1+num2
}6:
7:
)8:int Sub(int num1, int num2
{9:
10:
;return num1-num2
}11:
12:
)13:int Mul(int num1, int num2
{14:
15:
;return num1*num2
}16:
17:
)18:int Div(int num1, int num2
{19:
20:
;return num1/num2
}21:

functions.h
:

;))num2
;))num2
;))num2
;))num2

Add(num1,
Sub(num1,
Mul(num1,
Div(num1,

num2,
num2,
num2,
num2,

>1:#include<stdio.h
"2:#include"functions.h
3:
)(4:main
{5:
6:
;int num1 = 30, num2 = 10
7:
8:
printf("%d + %d = %d\n", num1,
9:
printf("%d - %d = %d\n", num1,
10:
printf("%d * %d = %d\n", num1,
11:
printf("%d / %d = %d\n", num1,
}12:

__________________________________________________________________________________

123
__________________________________________________________________________________________

functions.h
functions.h
main.c :

;))num2
;))num2
;))num2
;))num2

Add(num1,
Sub(num1,
Mul(num1,
Div(num1,

num2,
num2,
num2,
num2,

>1:#include<stdio.h
>2:#include<functions.h
3:
)(4:main
{5:
6:
;int num1 = 30, num2 = 10
7:
8:
printf("%d + %d = %d\n", num1,
9:
printf("%d - %d = %d\n", num1,
10:
printf("%d * %d = %d\n", num1,
11:
printf("%d / %d = %d\n", num1,
}12:

functions.h .stdio.h
.c

.c:
:
1:/*main.c*/
"2:#include"func.h
3:
{)(4:main
5:
;)(prints
}6:

:func.h
1:/*func.h*/
2:/*Function prototype*/
;)(3:void prints

:func.c
1:/*func.c*/
>2:#include<stdio.h
"3:#include"func.h
4:
5:/*Function*/
{)(6:void prints
7:
;)"printf("Hello, World!\n
}8:

__________________________________________________________________________________

124
__________________________________________________________________________________________

2.6.1 :

:
/?:&\*"<>|#%

256.
2.6.2 :

math.h
design.h
graphics.h .
2.6.3 :
-1 .
2.6.4:
-1

math.h abs
abs .

__________________________________________________________________________________

125
__________________________________________________________________________________________

2.7

Files I/O

)
(.
2.7.1 :
)(
.

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;FILE *File
6:
;]char FileName[255
7:
;]char String[255
8:
9:
;)" printf("Enter the name of file(with type)\n[MAX Character 255]:
10:
;)gets(FileName
11:
12:
;)"printf("Creating File...\n
13:
;)"File = fopen(FileName, "w
14:
;)"printf("File Created.\n
15:
16:
;)"printf("TEXT:\n
17:
;)gets(String
18:
19:
;)printf("Save Text to the file %s...\n", FileName
20:
;)fprintf(File, "%s", String
}21:

FILE
.FILE
.
.

__________________________________________________________________________________

126
__________________________________________________________________________________________


.255
gets scanf :
scanf
test test.txt scanf test
gets .
File fopen file open

w write .

.

gets

fprintf
printf printf fprintf
file print format printf
printf .
)
( .
iofile.h :
1:/*iofile.h header file
2:for files functions*/
3:#include<stdlib.h> /*for exit() fonction*/
4:
5:void CreateFile(const char *FileName, /*for the name of file*/
6:
)const char *String
/*for text of file*/
{7:
8:
;FILE *FileOut
9:
10:
)'if(*FileName == '\0
11:
{

__________________________________________________________________________________

127
__________________________________________________________________________________________
;)"printf("Error in the name of file!\n
;)exit(1
}
{)if((FileOut = fopen(FileName, "w"))==NULL
;)"printf("Can't create file!\n
;)exit(1
{}else
;)fprintf(FileOut, "%s", String
}
;)fclose(FileOut

12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
}22:

:
stdlib.h
)( exit .
library

standard

CreateFile
.


.
FileOut NULL NULL:
#define NULL 0

NULL
><
.
exit 1
true 0 false
.

__________________________________________________________________________________

128
__________________________________________________________________________________________

fclose
.
close

file

iofile.h :main.c
>1:#include<stdio.h
"2:#include"iofile.h
3:
)(4:main
{5:
6:
;]char FileName[255
7:
;]char String[255
8:
9:
;)"printf("Enter the name of file(with type)\n
10:
;)" printf("[MAX Character 255]:
11:
;)gets(FileName
12:
13:
;)"printf("TEXT:\n
14:
;)gets(String
15:
16:
;)"printf("Creating File and save text...\n
17:
;)CreateFile(FileName, String
}18:

2.7.1.1 :fopen

stdio.h
) (
:

2.7.1.2 :fclose

stdio.h
* FILE
.
2.7.1.3 :exit

__________________________________________________________________________________

129
__________________________________________________________________________________________

stdlib.h 1 0
1 0 .
2.7.2 :

.
w fopen a :
{)if((FileOut = fopen(FileName, "w"))==NULL

16:

:
{)if((FileOut = fopen(FileName, "a"))==NULL

16:

a .appending
2.7.3 :
.
iofile.h :
)1:void DisplayFile(const char *FileName
{2:
3:
;FILE *FileIn
4:
;]char String[1024
5:
6:
)'if(*FileName == '\0
7:
{
8:
;)"printf("Error in name of file!\n
9:
;)exit(1
10:
}
11:
12:
{)if((FileIn = fopen(FileName, "r"))==NULL
13:
;)"printf("Can't Open file!\n
14:
;)exit(1
15:
{}else
16:
;)fgets(String, 1024, FileIn
17:
;)printf("%s", String
18:
}
19:
;)fclose(FileIn
}20:

.
__________________________________________________________________________________

130
__________________________________________________________________________________________

.
.
fopen r w r .read
fgets stdio.h
file get string

*FileIn

.FILE

fgets ) (
.
)( main

:main.c

;)"printf("/////////Reading\\\\\\\\\n
;)DisplayFile(FileName

1:
2:

.
2.7.4 w+

a+

:r+

) w( ) a ( ) r (
:
2.7.4.1 :w+

2.7.4.2 :a+

.
2.7.4.3 :r+

.
__________________________________________________________________________________

131
__________________________________________________________________________________________

t r s b d
.
2.7.5 :

stdio.h :
2.7.5.1 fprintf :fscanf

fprintf fscanf scanf


file
:
file scan

>1:#include<stdio.h
2:
)(3:main
{4:
5:
;FILE *FileOut
6:
;]char Str[100
7:
;int Var
8:
9:
;)"FileOut = fopen("fscanf.txt", "w+
10:
11:
;)fprintf(FileOut, "%s %d", "Hello", 255
12:
13:
;)fseek(FileOut, 0, SEEK_SET
14:
15:
;)fscanf(FileOut, "%s", Str
16:
;)fscanf(FileOut, "%d", &Var
17:
18:
;)printf("%s\n", Str
19:
;)printf("%d\n", Var
20:
;)fclose(FileOut
}21:

FILE .FileOut
100 .
.
.
Hello .255
__________________________________________________________________________________

132
__________________________________________________________________________________________

fseek stdio.h
file seek
FILE 0
:
SEEK_SET -1 .
SEEK_CUR -2 .
SEEK_END -3 .
SEEK_SET 0
.
Str
.Hello
255 .Var
.
2.7.5.2 fgets :fputs

fgets fputs puts


file put string puts
:fputs
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;FILE *FileOut
6:
7:
;)"FileOut = fopen("fputs.txt", "w
8:
9:
;)fputs("Hello, World!\n", FileOut
10:
;)fclose(FileOut
}11:

__________________________________________________________________________________

133
__________________________________________________________________________________________

fputs
. FileOut
:fputc fgetc 2.7.5.3

fputc fgetc
:
1:#include<stdio.h>
2:
3:main()
4:{
5:
FILE *FileOut;
6:
FILE *FileIn;
7:
char ch;
8:
9:
FileOut = fopen("fputc.txt", "w");
10:
FileIn = fopen("fputc.txt", "r");
11:
12:
fputc('A', FileOut);
13:
fclose(FileOut);
14:
15:
ch = fgetc(FileIn);
16:
fclose(FileIn);
17:
18:
printf("%c\n", ch);
19:}

. fclose
:
1:#include<stdio.h>
2:
3:main()
4:{
5:
FILE *FileInOut;
6:
char ch;
7:
8:
FileInOut = fopen("fputc.txt", "w");
9:
10:
fputc('A', FileInOut);
11:
fclose(FileInOut);
12:
13:
FileInOut = fopen("fputc.txt", "r");
14:
15:
ch = fgetc(FileInOut);
16:
fclose(FileInOut);
17:
18:
printf("%c\n", ch);
19:}

__________________________________________________________________________________

134
__________________________________________________________________________________________


.C
2.7.6 :
.
2.7.7:
-1


results.dat.

__________________________________________________________________________________

135
__________________________________________________________________________________________

2.8

structures

)(

C
.
struct
)( :

2.8.1 :Struct Name


:

31.
.
....
% # { )... _(.
.
.

:
>1:#include<stdio.h
2:
3:struct _2D

__________________________________________________________________________________

136
__________________________________________________________________________________________
{4:
5:
;int x
6:
;int y
;}7:
8:
)(9:main
{10:
11:
;struct _2D Struct_2D
12:
;int x, y
13:
14:
;)" printf("Enter X:
15:
;)scanf("%d", &Struct_2D.x
16:
17:
;)" printf("Enter Y:
18:
;)scanf("%d", &Struct_2D.y
19:
20:
)for(x=0;x<=Struct_2D.x;x++
21:
{
22:
;)printf("%c", 219
23:
)for(y=0;y<Struct_2D.y;y++
24:
;)printf("%c", 219
25:
;)"printf("\n
26:
}
27:
}28:

.
._2D
x y ._2D
struct _2D
Struct_2D _2D Struct_2D
struct _2D _2D
Struct_2D .
.
:
;struct _2D Struct_2D_1, Struct_2D_2

11:

Struct_2D_1 .Struct_2D_2

__________________________________________________________________________________

137
__________________________________________________________________________________________


:
1:struct _2D
{2:
3:
;int x
4:
;int y
;5:}Struct_2D

:
1:struct _2D
{2:
3:
;int x
4:
;int y
;5:}Struct_2D_1, Struct2D_2

:
>1:#include<stdio.h
2:
3:struct _2D
{4:
5:
;int x
6:
;int y
;7:}Struct_2D
8:
)(9:main
{10:
11:
;int x, y
12:
13:
;)" printf("Enter the position X:
14:
;)scanf("%d", &Struct_2D.x
15:
16:
;)" printf("Enter the position Y:
17:
;)scanf("%d", &Struct_2D.y
18:
19:
)for(x=0;x<=Struct_2D.x;x++
20:
{
21:
;)printf("%c", 219
22:
)for(y=0;y<Struct_2D.y;y++
23:
;)printf("%c", 219
24:
;)"printf("\n
25:
}
26:
}27:


:
>1:#include<stdio.h
2:
3:struct _Value
{4:

__________________________________________________________________________________

138
__________________________________________________________________________________________
5:
int x;
6:
float y;
7:
char *Str;
8:}Value;
9:
10:main()
11:{
12:
Value.x = 10;
13:
Value.y = 10.00;
14:
Value.Str = "Hello, World";
15:
16:
printf("%d\n", Value.x);
17:
printf("%f\n", Value.y);
18:
printf("%s\n", Value.Str);
20:}

:
1:#include<stdio.h>
2:
3:struct _Value
4:{
5:
int x;
6:
float y;
7:
char *Str;
8:}Value = {10, 10.00, "Hello, World"};
9:
10:main()
11:{
12:
printf("%d\n", Value.x);
13:
printf("%f\n", Value.y);
14:
printf("%s\n", Value.Str);
15:}

:
1:struct _Value
2:{
3:
int x = 10;
4:
float y = 10.00;
5:
char *Str = "Hello World";
6:};

:_ Value
1:#include<stdio.h>
2:
3:int x = 10;
4:float y = 10.00;
5:char *Str = "Hello
6:
7:main()
8:{
9:
printf("%d\n",
10:
printf("%f\n",
11:
printf("%s\n",
12:}

World";

x);
y);
Str);

__________________________________________________________________________________

139
__________________________________________________________________________________________

2.8.2 :union
union
struct union
union struct :
>1:#include<stdio.h
2:
3:union _Union
{4:
5:
;int x
6:
;int y
;7:}Union
8:
)(9:main
{10:
11:
;Union.x = 10
12:
;Union.y = 15
13:
14:
;)printf("Union.x = %d\n", Union.x
15:
;)printf("Union.z = %d\n", Union.y
}16:

15 10 15 15
:
>1:#include<stdio.h
2:
3:union _Union
{4:
5:
;int x
6:
;int y
;7:}Union
8:
)(9:main
{10:
11:
;Union.y = 15
12:
;Union.x = 10
13:
14:
;)printf("Union.x = %d\n", Union.x
15:
;)printf("Union.z = %d\n", Union.y
}16:

struct
union
:
>1:#include<stdio.h
2:
3:union _Union
{4:

__________________________________________________________________________________

140
__________________________________________________________________________________________
5:
int x1, x2;
6:
int y1, y2;
7:}Union;
8:
9:main()
10:{
11:
Union.x1 = 10;
12:
13:
printf("Value of Union.x1 =
14:
printf("Value of Union.x2 =
15:
printf("Value of Union.y1 =
16:
printf("Value of Union.y2 =
17:
18:
printf("Address of Union.x1
19:
printf("Address of Union.x2
20:
printf("Address of Union.y1
21:
printf("Address of Union.y2
22:}

%d\n",
%d\n",
%d\n",
%d\n",
=
=
=
=

Union.x1);
Union.x2);
Union.y1);
Union.y2);

%p\n",
%p\n",
%p\n",
%p\n",

&Union.x1);
&Union.x2);
&Union.y1);
&Union.y2);

union
:
1:#include<stdio.h>
2:
3:union _Union
4:{
5:
int x;
6:
float y;
7:}Union;
8:
9:main()
10:{
11:
Union.x = 10;
12:
Union.y = 15.00;
13:
14: printf("Union.x = %d\n", Union.x);
15:
printf("Union.z = %f\n", Union.y);
16:}

x y
.
: 2.8.3

:
1:#include<stdio.h>
2:
3:struct _Arr
4:{
5:
int x;
6:}Arr[2];
7:

__________________________________________________________________________________

141
__________________________________________________________________________________________
8:main()
9:{
10:
Arr[0].x = 10;
11:
Arr[1].x = 20;
12:
13:
printf("Arr[0].x = %d\n", Arr[0].x);
14:
printf("Arr[1].x = %d\n", Arr[1].x);
15:}

:
1:struct _Arr
2:{
3:
int x;
4:}Arr[2] = {10, 20};

:
1:
2:

Arr[0].x = 10;
Arr[1].x = 20;


:
1:#include<stdio.h>
2:
3:struct _Arr
4:{
5:
int x;
6:
float y;
7:
char *Str;
8:}Arr[2] = {
9:
{10, 10.00, "Str1"},
10:
{20, 20.00, "Str2"}
11:};
12:
13:main()
14:{
15:
/*Arr[0] :*/
16:
printf("Arr[0].x = %d\n", Arr[0].x);
17:
printf("Arr[0].y = %f\n", Arr[0].y);
18:
printf("Arr[0].Str = %s\n", Arr[0].Str);
19:
20:
/*Arr[1] :*/
21:
printf("Arr[1].x = %d\n", Arr[1].x);
22:
printf("Arr[1].y = %f\n", Arr[1].y);
23:
printf("Arr[1].Str = %s\n", Arr[1].Str);
24:}

:
1:#include<stdio.h>
2:
3:struct _ptr
4:{

__________________________________________________________________________________

142
__________________________________________________________________________________________
5:
;int x
;6:}Addr_ptr, *ptr
7:
)(8:main
{9:
10:
;ptr = &Addr_ptr
11:
;ptr->x = 10
12:
13:
;)printf("ptr->x = %d\n", ptr->x
14:
15:
/*Or*/
16:
;(*ptr).x = 20
17:
;)printf("(*ptr).x = %d\n", (*ptr).x
}18:


.
ptr .Adrr_ptr
x 10 > -
.
2.8.4 :


x y:
>1:#include<stdio.h
2:
3:struct _line
{4:
5:
struct _point
6:
{
7:
;int x
8:
;int y
9:
;}point_1, point_2
;10:}line
11:
)(12:main
{13:
14:
/*point 1:*/
15:
;line.point_1.x = 10
16:
;line.point_1.y = 10
17:
/*point 2:*/
18:
;line.point_2.x = 100
19:
;line.point_2.y = 10
}20:

__________________________________________________________________________________

143
__________________________________________________________________________________________

_point _line _point


x .y
2.8.5 :Bit-fields
:
>1:#include<stdio.h
2:
{3:struct _Bit_Fields
4:
;unsigned _8Bit: 8
/*1 Byte*/
5:
;unsigned _16Bit: 16
/*2 Byte*/
;6:}Bit_Fields
7:
{)][8:main(int argc, char *argv
9:
;Bit_Fields._8Bit = 255
10:
;Bit_Fields._16Bit = 65535
11:
12:
;)printf("_8Bit = %d\n", Bit_Fields._8Bit
13:
;)printf("_16Bit = %d\n", Bit_Fields._16Bit
}14:

8 _8Bit 1 255
16 _16Bit 2 .65535
2.8.6 :
-1 :
>1:#include<stdio.h
2:
3:struct Struct
{4:
5:
;int x, y
;}6:
7:
)(8:main
{9:
10:
;Struct.x = 10
11:
;Struct.y = 20
}12:

-2 .
2.8.7:

__________________________________________________________________________________

144
__________________________________________________________________________________________

-1

time hh, mm, ss int


time
.HH :MM :SS

__________________________________________________________________________________

145
__________________________________________________________________________________________

2.9
scanf &
:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int i
6:
7:
;)scanf("%d", &i
}8:

i scanf
.
2.9.1 :void
void:
>1:#include<stdio.h
2:
;)3:Function(void
4:
)(5:main
{6:
7:
}8:
9:
)10:Function(void
{11:
12:
}13:

:
)1:main(void
{2:
3:
}4:

void

void :
>1:#include<stdio.h

__________________________________________________________________________________

146
__________________________________________________________________________________________
2:
;)(3:Function
4:
)(5:main
{6:
7:
}8:
9:
)(10:Function
{11:
12:
}13:

:
)(1:main
{2:
3:
}4:

.
2.9.2 :static
static :
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;int x
6:
7:
)for(x=0; x<10; x++
8:
{
9:
;static int Static = 0
10:
;Static++
11:
12:
;)printf("%d\n", Static
13:
}
}14:

static Static 0
Static
.
static Static 1
.
static :
__________________________________________________________________________________

147
__________________________________________________________________________________________
1:#include<stdio.h>
2:
3:int Test_static(int a);
4:
5:main()
6:{
7:
int i;
8:
9:
for(i=0;i<=10;i++)
10:
printf("%d * %d = %d\n", i, i, Test_static(i));
11:}
12:
13:int Test_static(int a)
14:{
15:
static int c = 0;
16:
int a_c = a*c;
17:
c++;
18:
19:
return a_c;
20:}

.0 static
0 static
.
:typedef 2.9.3
typedef
:
1:#include<stdio.h>
2:
3:typedef int Decimal;
4:
5:main()
6:{
7:
/* or typedef int Decimal; here*/
8:
Decimal Var_1, Var_2;
9:
Decimal Var_3;
10:
11:
Var_1 = 10;
12:
Var_2 = 20;
13:
Var_3 = Var_1 + Var_2;
14:
15:
printf("%d\n", Var_3);
16:}

.Decimal

typedef int

__________________________________________________________________________________

148
__________________________________________________________________________________________

Decimal Var_1 Var_2 Var_3


.
int Decimal
.
:
>1:#include<stdio.h
2:
3:struct _2D
{4:
5:
;int x, y
;}6:
7:
)(8:main
{9:
10:
;struct _2D A
11:
;struct _2D B
12:
13:
;A.x = 10
14:
;B.y = 20
15:
16:
;)printf("%d\n", A.x
17:
;)printf("%d\n", B.y
}18:

:
>1:#include<stdio.h
2:
3:typedef struct _2D
{4:
5:
;int x, y
;6:}Struct1, Struct2
7:
)(8:main
{9:
10:
;Struct1 S1_1, S1_2
11:
;Struct2 S2_1, S2_2
12:
13:
;S1_1.x = 10, S1_2.x = 20
14:
;S2_1.y = 30, S2_2.y = 40
}15:

: typdef
:
>1:#include<stdio.h
2:
)(3:main

__________________________________________________________________________________

149
__________________________________________________________________________________________
4:{
5:
6:
7:
8:}

typedef float PI;


PI = 10.00;
printf("%f\n", PI);

: 2.9.4
C
:
: 2.9.4.1

:
:str.h
1:/* string.h */
2:
3:/*strcopy(pointers)*/
4:
5:void strcopy(char *From, char *To){
6:
while((*To++ = *From++)!='\0')
7:
;
8:}
9:
10:/*strcopy(arrays);*/
11:/*void strcopy(char From[], char To[]){
12:
int i;
13:
i = 0;
14:
15:
while((To[i] = From[i])!='\0')
16:
++i;
17:}*/

:
1:/*main.c*/
2:
3:#include<stdio.h>
4:#include"str.h"
5:
6:main()
7:{
8:
char *From = "STRING";
9:
char Empty[6];
10:
11:
strcopy(From, Empty);
12:
13:
printf(Empty);
14:}

__________________________________________________________________________________

150
__________________________________________________________________________________________

From Empty
Empty .
2.9.4.2 :


:
>1:#include<stdio.h
2:
)3:void Change(int *a, int *b
{4:
5:
;int c
6:
;c = *a
7:
;*a = *b
8:
;*b = c
}9:
10:
)(11:main
{12:
13:
;int a, b
14:
;a = 5
15:
;b = 10
16:
17:
;)printf("a = %d, b = %d\n", a, b
18:
19:
;)Change(&a, &b
20:
21:
;)printf("a = %d, b = %d\n", a, b
}22:

*b

*a,

2.9.4.3 :

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;const int Const = 10
6:
;int *ptr = &Const
7:
8:
;)printf("Const = %d\n", Const
9:
;*ptr = 5
10:
;)printf("Const = %d\n", Const
11:
}12:

__________________________________________________________________________________

151
__________________________________________________________________________________________

.
: 2.9.4.4


:
1:#include<stdio.h>
2:#include<string.h>
3:
4:void Reverse_Str(char *);
5:
6:main()
7:{
8:
char *str = "Hello, World!";
9:
10:
printf("%s\n", str);
11:
Reverse_Str(str);
12:
printf("%s\n", str);
13:}
14:
15:void Reverse_Str(char *String){
16:
int i = strlen(String)-1, j = 0;
17:
char ch;
18:
19:
while(j<i){
20:
ch = String[j];
21:
String[j] = String[i];
22:
String[i] = ch;
23:
j++, i--;
24:
}
25:}

: 2.9.4.5

:
1:#include<stdio.h>
2:
3:void ConvertToBinary(int);
4:
5:main()
6:{
7:
int Decimal;
8:
9:
printf("Decimal Number: ");
10:
scanf("%d", &Decimal);
11:
printf("%d in Binary = ", Decimal);
12:
ConvertToBinary(Decimal);
13:}
14:
15:void ConvertToBinary(int num){
16:
int i = 0, Binary[32];
17:
18:
while(num>0){
19:
if((num%2)==0){
20:
Binary[i] = 0;

__________________________________________________________________________________

152
__________________________________________________________________________________________
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:}

num /= 2, ++i;
}
else
{
Binary[i] = 1;
num /= 2, ++i;
}
}
--i;
while(i>=0)
{
printf("%d", Binary[i]), --i;
}
printf("\n");

: 2.9.4.6


:
1:#include<stdio.h>
2:
3:void To_Capital_letter(char ch[]);
4:
5:main()
6:{
7:
char *ch = "hello";
8:
9:
printf("Small Letters: %s\n", ch);
10:
To_Capital_letter(ch);
11:
printf("Capital Letters: %s\n", ch);
12:}
13:
14:void To_Capital_letter(char ch[])
15:{
16:
int i=0;
17:
18:
while(ch[i]!='\0'){
19:
if(ch[i]>=97 && ch[i]<=122)
20:
ch[i] = ch[i]-32;
21:
++i;
22:
}
23:}

.
:wcsncpy wcscpy 2.9.5
wide

string.h strcpy wcscpy


: strcpy character string copy

__________________________________________________________________________________

153
__________________________________________________________________________________________
1:#include<stdio.h>
2:#include<string.h>
3:
4:main()
5:{
6:
wchar_t *wStr = L"Hello";
7:
wchar_t Empty[20];
8:
9:
wcscpy(Empty, wStr);
10:
11:
wprintf(L"%s\n", Empty);
12:}

: string.h strncpy wcsncpy


1:#include<stdio.h>
2:#include<string.h>
3:
4:main()
5:{
6:
wchar_t *wStr = L"Hello";
7:
wchar_t Empty[20];
8:
9:
wcsncpy(Empty, wStr, 4);
10:
11:
wprintf(L"%s\n", Empty);
12:}

:wcsncat wcscat 2.9.6


: strncat strcat wcsncat wcscat
1:#include<stdio.h>
2:#include<string.h>
3:
4:main()
5:{
6:
wchar_t *wStr = L"Hello";
7:
wchar_t *wStr2 = L", World!";
8:
9:
wcscat(wStr, wStr2);
10:
11:
wprintf(L"%s\n", wStr);
12:
13:
/*
14:
wcsncat(wStr, wStr2, 4);
15:
16:
wprintf(L"%s\n", wStr);
17:
*/
18:}

:putwchar getwchar 2.9.7

__________________________________________________________________________________

154
__________________________________________________________________________________________

getwchar getchar stdio.h


wide character putwchar putchar put wide character
:

get

>1:#include<stdio.h
2:
)(3:main
{4:
5:
;wchar_t wch
6:
7:
;)(wch = getwchar
8:
;)putwchar(wch
}9:

2.9.8 _getws :_putws


_getws gets _putws puts
stdio.h ws wide string:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;]wchar_t wch[255
6:
7:
;)_getws(wch
8:
;)_putws(wch
}9:

2.9.9 ) ASCII(:
ASCII American Standard Code for Information Interchange
:

__________________________________________________________________________________

155
__________________________________________________________________________________________

:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;)printf("The number %d is the character %c in ASCII code.\n", 210, 210
}6:

210 ASCII
)(.
2.9.10 :

.
2.9.10.1 :

:
>1:#include<stdio.h
2:
)(3:void Func
{4:
5:
;int a
/*Local Variable*/
}6:
7:
)(8:main
{9:
10:
;int b
/*Local Variable*/

__________________________________________________________________________________

156
__________________________________________________________________________________________
}11:

a Func b
a Func b Func
main .
2.9.10.2 )(:


:
>1:#include<stdio.h
2:
;3:int ab
/*External Variable*/
4:
)(5:void Func
{6:
7:
;ab = 10
/*Use The External Variable ab*/
}8:
9:
)(10:main
{11:
12:
;)(Func
13:
printf("%d\n", ab); /*print The External Variable ab*/
}14:

ab ab
Func Func ab .
0.
2.9.10.3 :extern

extern
:
>1:#include<stdio.h
2:
;3:int ab
4:
)(5:void Func
{6:
7:
;int ab
8:
;ab = 10
9:
;)printf("%d\n", ab
}10:
11:
)(12:main

__________________________________________________________________________________

157
__________________________________________________________________________________________
;ab = 5
;)(Func
;)printf("%d\n", ab

{13:
14:
15:
16:
}17:

ab Func ab
extern :
>1:#include<stdio.h
2:
;3:int ab
4:
)(5:void Func
{6:
7:
;extern int ab
8:
;ab = 10
9:
;)printf("%d\n", ab
}10:
11:
)(12:main
{13:
14:
;ab = 5
15:
;)(Func
16:
;)printf("%d\n", ab
}17:

ab .ab
2.9.10.4 :auto

auto automatic
static:
>1:#include<stdio.h
2:
)(3:void Func
{4:
5:
;static int Static = 0
6:
;auto int Auto = 0
7:
8:
;)printf("Static = %d\n", Static++
9:
printf("Auto
;)= %d\n", Auto++
}10:
11:
)(12:main
{13:
14:
;int i = 0
15:
16:
{)while(i<=3
17:
;)(Func
18:
;i++
19:
}
}20:

__________________________________________________________________________________

158
__________________________________________________________________________________________

Auto .auto
2.9.10.5 :register


register
(Central Processing Unit) CPU:
>1:#include<stdio.h
2:
)(3:main
{4:
5:
;register Reg_Var = 4
6:
7:
;)printf("Reg_Var = %d\n", Reg_Var
}8:

.
2.9.11 :sizeof
sizeof
bytes :
>1:#include<stdio.h
2:
3:
)(4:main
{5:
6:
;]int SizeOfArray[800
7:
8:
printf("short
;))= %d Byte(s)\n", sizeof(short
9:
printf("int
;))= %d Byte(s)\n", sizeof(int
10:
printf("unsigned
;))= %d Byte(s)\n", sizeof(unsigned
11:
printf("signed
;))= %d Byte(s)\n", sizeof(signed
12:
printf("long
;))= %d Byte(s)\n", sizeof(long
13:
printf("float
;))= %d Byte(s)\n", sizeof(float
14:
printf("double
;))= %d Byte(s)\n", sizeof(double
15:
;))printf("SizeOfArray = %d Byte(s)\n", sizeof(SizeOfArray
}16:

.
2.9.12 :volatile
volatile const
:
__________________________________________________________________________________

159
__________________________________________________________________________________________

>1:#include<stdio.h
2:
{)][3:main(int argc, char *argv
4:
;volatile int Vol_Var = 10
5:
6:
;)printf("Vol_Var = %d\n", Vol_Var
}7:

2.9.13 :

:
>1:#include <stdio.h
2:
)3:void Func(int num
{4:
5:
;)printf("%d\n", num
6:
;)Func(num+1
}7:
8:
)(9:main
{10:
11:
;)Func(5
}12:


65535
int num :
>1:#include <stdio.h
2:
)3:void Func(int num
{4:
5:
)if(num <= 10
6:
{
7:
;)printf("%d\n", num
8:
;)Func(num+1
9:
}
}10:
11:
)(12:main
{13:
14:
;)Func(5
}15:

5 .10
2.9.14 :
__________________________________________________________________________________

160
__________________________________________________________________________________________

printf

:
>1:#include<stdio.h
2:
{)][3:main(int argc, char *argv
4:
;"!char str[] = "Hello, World
5:
;float flt = 3.1415F
6:
int dec
;= 1000
7:
8:
/*String*/
9:
;)printf("%s\n", str
10:
;)printf("%5s\n", str
11:
;)printf("%-5s\n", str
12:
;)printf("%5.5s\n", str
13:
;)printf("%-5.5s\n", str
14:
15:
;)"printf("\n
16:
17:
/*Floating*/
18:
;)printf("%10.0f\n", flt
19:
;)printf("%.1f\n", flt
20:
;)printf("%.2f\n", flt
21:
;)printf("%.3f\n", flt
22:
;)printf("%.4f\n", flt
}23:

.
2.9.15 :
.
2.9.16:
-1

__________________________________________________________________________________

161
__________________________________________________________________________________________

C -
Enumeration 3.1

Command-line Arguments 3.2


Directives(Preprocessor) 3.3

3.4
Standard Library 3.5

__________________________________________________________________________________

162
__________________________________________________________________________________________

3.1

Enumeration

) (Enumeration int
enum enumeration
.
:

3.1.1 :Enumeration Name


:
31.

.
....
% # { )... _(.
.
.

3.1.2 :

.
.
__________________________________________________________________________________

163
__________________________________________________________________________________________

: enum
1:#include<stdio.h>
2:
3:enum _COLOR
4:{
5:
RED = 0,
6:
BLUE,
7:
GREEN
8:};
9:
10:main()
11:{
12:
enum _COLOR Color;
13:
14:
/*Color = RED;*/
15:
Color = BLUE;
16:
/*Color = GREEN;*/
17:
18:
if(Color == RED){
19:
printf("Color = %d\n", Color);
20:
}else if(Color == BLUE){
21:
printf("Color = %d\n", Color);
22:
}else if(Color == GREEN){
23:
printf("Color = %d\n", Color);
24:
}else
25:
printf("Error!\n");
26:}

:
1:#include<stdio.h>
2:
3:enum _COLOR
4:{
5:
RED = 0,
6:
BLUE,
7:
GREEN
8:}Color;
9:
10:main()
11:{
12:
/*Color = RED;*/
13:
Color = BLUE;
14:
/*Color = GREEN;*/
15:
16:
if(Color == RED){
17:
printf("Color = %d\n", Color);
18:
}else if(Color == BLUE){
19:
printf("Color = %d\n", Color);
20:
}else if(Color == GREEN){
21:
printf("Color = %d\n", Color);
22:
}else
23:
printf("Error!\n");
24:}

:_ COLOR
__________________________________________________________________________________

164
__________________________________________________________________________________________
;enum _COLOR Color = BLUE

:
enum _COLOR
{
RED = 0,
BLUE,
GREEN
;}Color = BLUE

.
enum
enum.
enum
.int
RED 0 BLUE 1 GREEN .2
RED 0 0 .
BLUE 0 0
RED 0 BLUE 0 GREEN
.1
:
>1:#include<stdio.h
2:
3:enum _COLOR
{4:
5:
RED = 0,
6:
BLUE,
7:
GREEN
;}8:
9:
)(10:main
{11:
12:
enum _COLOR cRed = RED,
13:
cBlue = BLUE,
14:
;cGreen = GREEN
}15:

__________________________________________________________________________________

165
__________________________________________________________________________________________

:
1:#include<stdio.h>
2:
3:enum _COLOR
4:{
5:
RED = 0,
6:
BLUE,
7:
GREEN
8:}cRed = RED, cBlue = BLUE, cGreen = GREEN;
9:
10:main()
11:{
12:
13:}

:
1:#include<stdio.h>
2:
3:enum _NUMBER
4:{
5:
Num = 10,
6:
Num2 = 20
7:};
8:
9:main()
10:{
11:
enum _NUMBER Number = Num;
12:
int i = 0;
13:
14:
printf("i
= %d\n", i);
15:
i = Number;
16:
17:
printf("Number = %d\n", Number);
18:
printf("i
= %d\n", i);
19:}

:
1:#include<stdio.h>
2:
3:enum _COLOR
4:{
5:
BLACK,
6:
WHITE,
7:
RED,
8:
GREEN,
9:
BLUE
10:};
11:
12:main()
13:{
14:
enum _COLOR Color;
15:
16:
printf("colors:\n");
17:
printf("0)BLACK\n1)WHITE\n2)RED\n3)GREEN\n4)BLUE\n");
18:
printf("choose a color:");
19:
scanf("%d", &Color);

__________________________________________________________________________________

166
__________________________________________________________________________________________

;)"are Black\n
;)"are White\n
;)"are Red\n
;)"are Green\n
;)"are Blue\n

{)if(Color == BLACK
printf("Your choice
{)}else if(Color == WHITE
printf("Your choice
{)}else if(Color == RED
printf("Your choice
{)}else if(Color == GREEN
printf("Your choice
{)}else if(Color == BLUE
printf("Your choice
}

20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
}32:

3.1.3 :
-1 :
>1:#include<stdio.h
2:
3:enum _NUMBER
{4:
5:
Zero,
6:
One,
;}7:
8:
)(9:main
{10:
11:
;enum _NUMBER = ZERO
}12:

-2 .
3.1.4:
-1

_POWER Off On
_POWER Power
scanf 0 Off
(On) 1 Power .0

__________________________________________________________________________________

167
__________________________________________________________________________________________

Command-line Arguments 3.2

main
) main(void
Command-line Arguments
.
int argc
main argc
argument count :
>1:#include<stdio.h
2:
{)3:main(int argc
4:
;)printf("argc = %d\n", argc
}5:

argc
argv argument vector argc
argv :argv
>1:#include<stdio.h
2:
{)][3:main(int argc, char *argv
4:
;)]printf("argv[argc] = %s\n", argv[argc-1
}5:

argc-1
argc ] argv[1
argc 1 ].argv[0
3.2.1 :
.
3.2.2:
__________________________________________________________________________________

168
__________________________________________________________________________________________

__________________________________________________________________________________

169
__________________________________________________________________________________________

3.3

)Directives(Preprocessor


# .
3.3.1 :#include


. :
include
:
>1:#include<FileName.h

.FileName

:
"1:#include"FileName.h

><
) (include " " .
#include .
3.3.2 :#define
#define
Macros #define :
#define Constant_Name Constant_Value

__________________________________________________________________________________

170
__________________________________________________________________________________________

Constant_Name Constant_Value .
#define
:
""Hello
''H
3.14
10

String
Character
fNumber
iNumber

1:#define
2:#define
3:#define
4:#define

Macros :
1:#define Macro_Add(i, j) i+j

) (
i j :
>1:#include<stdio.h
2:
3:#define Macro_Add(i, j) i+j
4:
{)][5:main(int argc, char *argv
6:
;int l
7:
8:
;)l = Macro_Add(10, 20
9:
10:
;)printf("%d\n", l
11:
12:
/*or*/
13:
14:
;))printf("%d\n", Macro_Add(10, 20
}15:

3.3.3 :#undef
#undef #define

#define:
>1:#include<stdio.h
2:
3:#define Constant
5
4:#define Macro_Add(i, j) i+j
5:
6:#undef Constant
7:#undef Macro_Add
8:
{)][9:main(int argc, char *argv
10:
printf("%d\n", Constant); /*Error C2065: 'Constant':
11:
undeclared identifier*/
12:

__________________________________________________________________________________

171
__________________________________________________________________________________________
/*Errors*/

;)Macro_Add(5, 10

13:
}14:

Constant 5
Macro_Add
Constant Macro_Add .
3.3.4 #else #elif #if :#endif

else #if if #elif else if

if

#else .else

#endif

>1:#include<stdio.h
2:
3:#define dNum
20
4:
5:#if dNum == 10
6:
"#define Result "dNum = 10
7:#elif dNum == 20
8:
"#define Result "dNum = 20
9:#else
10:
"?? = #define Result "dNum
11:#endif
12:
{)][13:main(int argc, char *argv
14:
;)printf("%s\n", Result
}15:

3.3.5 #ifdef :#ifndef


#ifdef :
>1:#include<stdio.h
2:
3:#define dNum
4:
5:#ifdef dNum
6:
"#define Result "dNum Defined
7:#else
8:
"#define Result "dNum Undefined
9:#endif
10:
{)][11:main(int argc, char *argv
12:
;)printf("%s\n", Result
}13:

Defined

dNum dNum

__________________________________________________________________________________

172
__________________________________________________________________________________________
1:#include<stdio.h>
2:
3:#define dNum
4:
5:#if defined dNum
6:
#define Result "dNum Defined"
7:#else
8:
#define Result "dNum Undefined"
9:#endif
10:
11:main(int argc, char *argv[]){
12:
printf("%s\n", Result);
13:}

: #ifdef

#ifndef

1:#include<stdio.h>
2:
3:#define dNum
4:
5:#ifndef dNum
6:
#define Result "dNum Undefined"
7:#else
8:
#define Result "dNum Defined"
9:#endif
10:
11:main(int argc, char *argv[]){
12:
printf("%s\n", Result);
13:}

:
1:#include<stdio.h>
2:
3:#define dNum
4:
5:#if !defined dNum
6:
#define Result "dNum Undefined"
7:#else
8:
#define Result "dNum Defined"
9:#endif
10:
11:main(int argc, char *argv[]){
12:
printf("%s\n", Result);
13:}

:#line 3.3.6
#line
:
1:#line LineNumber

__________________________________________________________________________________

173
__________________________________________________________________________________________


LineNumber :
"1:#line LineNumber "FileName

" ".
) (.
3.3.7 :#error

:
1:#ifndef dNum
!2:#error dNum are not declared
3:#endif
4:
{)][5:main(int argc, char *argv
6:
}7:

#error dNum
.
3.3.8 :#pragma
#pragma
.
3.3.9 :Predefined Names
Macros Underscore
:

____LINE

____FILE

____TIME

__________________________________________________________________________________

174
__________________________________________________________________________________________
____DATE

____STDC

1 .C

:
>1:#include<stdio.h
2:
{)][3:main(int argc, char *argv
4:
;)__printf("Line: %d\n", __LINE
5:
;)__printf("File: %s\n", __FILE
6:
;)__printf("Date Of Compilation: %s\n", __DATE
7:
;)__printf("Time Of Compilation: %s\n", __TIME
8:
;)__printf("ANSI C(0=false, 1=true): %d\n", __STDC
}9:

3.3.10 :
.
3.3.11:
.

__________________________________________________________________________________

175
__________________________________________________________________________________________

3.4

:
>1:#include<stdio.h
2:
;)3:int Add(int a, int b
4:
{)][5:main(int argc, char *argv
6:
;int a = 10, b = 20, c = 30, d
7:
8:
;)d = Add(a, b, c
9:
10:
;)printf("%d\n", d
}11:
12:
{)13:int Add(int a, int b
14:
;return a+b
}15:

Add
.
printf scanf
printf :
>1:#include<stdio.h
>2:#include<stdarg.h
3:
;)4:void NewPrintf(char *, ...
5:
{)][6:main(int argc, char *argv
7:
;)NewPrintf("%d\a, %d\a, %d\a\n", 1, 2, 3
8:
;)"NewPrintf("Hello!\n
}9:
10:
{)11:void NewPrintf(char *format, ...
12:
;va_list Argument
13:
;char *str
14:
;int Decimal
15:
16:
;)va_start(Argument, format
17:
18:
{)for(str=format;*str;str++
19:
{)'if(*str != '%
20:
;)putchar(*str
21:
;continue
22:
}
23:
24:
{)switch(*++str
25:
case 'd':
26:
case 'i':

__________________________________________________________________________________

176
__________________________________________________________________________________________
case 'D':
case 'I':
;)Decimal = va_arg(Argument, int
;)printf("%d", Decimal
;break
default:
;)putchar(*str
;break
}
}
;)va_end(Argument

stdarg.h
.

argument

27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
}40:

standard

NewPrintf
... .
va_list variable list
stdarg.h .NewPrintf
va_start
va_list NewPrintf
.format
va_arg

argument

variable

va_list
Decimal .
:
>1:#include<stdio.h
>2:#include<stdarg.h
3:
;)4:int Add(int NumOfArg, ...
5:
{)][6:main(int argc, char *argv
7:
;int a = 10, b = 20, c = 30, d
8:
9:
;)d = Add(3, a, b, c
10:

__________________________________________________________________________________

177
__________________________________________________________________________________________
11:
printf("%d\n", d);
12:}
13:
14:int Add(int NumOfArg, ...){
15:
va_list Arg;
16:
int sum = 0, i;
17:
18:
va_start(Arg, NumOfArg);
19:
20:
for(i=0;i<NumOfArg;i++)
21:
sum += va_arg(Arg, int);
22:
23:
return sum;
24:}

: 3.4.1
.
: 3.4.2
.

__________________________________________________________________________________

178
__________________________________________________________________________________________

3.5

Standard Library

C
.
3.5.1 :assert.h
assert
0
macro

assert assert :
>1:#include<stdio.h
>2:#include<assert.h
3:
{)][4:main(int argc, char *argv
5:
assert(10<20); /*true*/
6:
assert(20<10); /*false*/
}7:

assert.h assert
.
assert
.
assert assert
20<10 main.c .6
3.5.2 :ctype.h
ctype.h
type .

character

3.5.2.1 :isalnum
__________________________________________________________________________________

179
__________________________________________________________________________________________

is alphanumeric
0:
>1:#include<stdio.h
>2:#include<ctype.h
3:
{)][4:main(int argc, char *argv
5:
;int c = 1, c2 = 65
6:
7:
;)printf("%d = %c\n", isalnum(c), c
8:
;)printf("%d = %c\n", isalnum(c2), c2
}9:

0 1 ASCII .
1 65 A ASCII
.
3.5.2.2 :isalpha

is alphabetic
1 0:
>1:#include<stdio.h
>2:#include<ctype.h
3:
{)][4:main(int argc, char *argv
5:
;int c = 49, c2 = 65
6:
7:
;)printf("%d = %c\n", isalpha(c), c
8:
;)printf("%d = %c\n", isalpha(c2), c2
}9:

0 49 1 ASCII
.
1 65 A ASCII A
.
3.5.2.3 :iscntrl

is control
0 31 128 255
__________________________________________________________________________________

180
__________________________________________________________________________________________

32 127
:
>1:#include<stdio.h
>2:#include<ctype.h
3:
{)][4:main(int argc, char *argv
5:
;int c = 65, c2 = 0
6:
7:
;)printf("%d = %c\n", iscntrl(c), c
8:
;)printf("%d = %c\n", iscntrl(c2), c2
}9:

0 65 A ASCII
.
0 .ASCII
3.5.2.4 :isdigit


48 57 48 57
ASCII 0 :
>1:#include<stdio.h
>2:#include<ctype.h
3:
{)][4:main(int argc, char *argv
5:
;int c = 65, c2 = 48
6:
7:
;)printf("%d = %c\n", isdigit(c), c
8:
;)printf("%d = %c\n", isdigit(c2), c2
}9:

0 65 A ASCII
.

48 ASCII .0
3.5.2.5 :isgraph


0 :
__________________________________________________________________________________

181
__________________________________________________________________________________________
>1:#include<stdio.h
>2:#include<ctype.h
3:
{)][4:main(int argc, char *argv
5:
;int c = 0, c2 = 48
6:
7:
;)printf("%d = %c\n", isgraph(c), c
8:
;)printf("%d = %c\n", isgraph(c2), c2
}9:

0 0 ASCII .
48 ASCII 0
.
3.5.2.6 :islower


0:
>1:#include<stdio.h
>2:#include<ctype.h
3:
{)][4:main(int argc, char *argv
5:
;int c = 65, c2 = 97
6:
7:
;)printf("%d = %c\n", islower(c), c
8:
;)printf("%d = %c\n", islower(c2), c2
}9:

0 65 A ASCII A
.
97 ASCII a
.
3.5.2.7 :isprint

32 126 ASCII
) (
0 :
>1:#include<stdio.h
>2:#include<ctype.h
3:

__________________________________________________________________________________

182
__________________________________________________________________________________________
{)][char *argv
;c2 = 65
;)= %c\n", isprint(c), c
;)= %c\n", isprint(c2), c2

4:main(int argc,
5:
int c = 1,
6:
7:
printf("%d
8:
printf("%d
}9:

0 1 ASCII
.
65 A ASCII
.
3.5.2.8 :ispunct
is punctuation
0
:
>1:#include<stdio.h
>2:#include<ctype.h
3:
{)][4:main(int argc, char *argv
5:
;int c = 44, c2 = 65
6:
7:
;)printf("%d = %c\n", ispunct(c), c
8:
;)printf("%d = %c\n", ispunct(c2), c2
}9:

44 ASCII
.
65 A ASCII
.
3.5.2.9 :isspace


9 13
0 :
>1:#include<stdio.h

__________________________________________________________________________________

183
__________________________________________________________________________________________
>2:#include<ctype.h
3:
{)][4:main(int argc, char *argv
5:
;int c = 9, c2 = 97
6:
7:
;)printf("%d = %c\n", isspace(c), c
8:
;)printf("%d = %c\n", isspace(c2), c2
}9:

9 ASCII
.
0 97 ASCII a
.
3.5.2.10 :isupper


0 :
>1:#include<stdio.h
>2:#include<ctype.h
3:
{)][4:main(int argc, char *argv
5:
;int c = 65, c2 = 97
6:
7:
;)printf("%d = %c\n", isupper(c), c
8:
;)printf("%d = %c\n", isupper(c2), c2
}9:

65 ASCII A
.
0 97 ASCII a
.
3.5.2.11 :isxdigit

is hexadecimal digital
A-F 9 0
0 :
>1:#include<stdio.h
>2:#include<ctype.h

__________________________________________________________________________________

184
__________________________________________________________________________________________
3:
{)][4:main(int argc, char *argv
5:
;int c = 70, c2 = 71
6:
7:
;)printf("%d = %c\n", isxdigit(c), c
8:
;)printf("%d = %c\n", isxdigit(c2), c2
}9:

70 F ASCII
F .15
71 G ASCII
G .
3.5.2.12 toupper :tolower

toupper tolower
toupper:
>1:#include<stdio.h
>2:#include<ctype.h
3:
{)][4:main(int argc, char *argv
5:
;'int c = 'a', c2 = 'B
6:
7:
;))printf("%c = %c\n", c, toupper(c
8:
;))printf("%c = %c\n", c2, tolower(c2
}9:

a
toupper B
tolower .
3.5.3 :errno.h
errno.h
. .perror
3.5.3.1 :perror

perror print error .

__________________________________________________________________________________

185
__________________________________________________________________________________________


:
>1:#include<stdio.h
>2:#include<stdlib.h
>3:#include<errno.h
4:
{)][5:main(int argc, char *argv
6:
;FILE *File
7:
;]char FileName[255
8:
9:
;)"perror("situation
10:
11:
;)" printf("Enter The File Name:
12:
;)gets(FileName
13:
14:
;)"File = fopen(FileName, "r
15:
16:
{)if(!File
17:
;)"perror("situation
18:
;)printf("errno = %d\n", errno
19:
;)exit(1
20:
{}else
21:
;)"perror("situation
22:
;)"printf("File Founded!\n
23:
;)fclose(File
24:
}
}25:

perror situation
.No error
perror
errno
printf .2
errno :

EINVAL
ERANGE
EBADF
EDOM
EACCES
EMFILE
EEXIST
E2BIG
EXDEV

Invalid argument
Result too large
Bad file descriptor
Domain error
Permission denied
Too many open files
File exists
Arg list too long
Improper link

__________________________________________________________________________________

186
__________________________________________________________________________________________

No child processes
Resource temporarily unavailable
Exec format error
Not enough space
No such file or directory
No space left on device

ECHILD
EAGAIN
ENOEXEC
ENOMEM
ENOENT
ENOSPC

:
1:#include<stdio.h>
2:#include<errno.h>
3:
4:main(int argc, char *argv[]){
5:
int i;
6:
7:
for(i=0;i<=42;i++){
8:
errno = i;
9:
printf("%d:", i);
10:
perror("");
11:
}
12:}

: errno.h
1:#define EPERM
2:#define ENOENT
3:#define ESRCH
4:#define EINTR
5:#define EIO
6:#define ENXIO
7:#define E2BIG
8:#define ENOEXEC
9:#define EBADF
10:#define ECHILD
11:#define EAGAIN
12:#define ENOMEM
13:#define EACCES
14:#define EFAULT
15:#define EBUSY
16:#define EEXIST
17:#define EXDEV
18:#define ENODEV
19:#define ENOTDIR
20:#define EISDIR
21:#define EINVAL
22:#define ENFILE
23:#define EMFILE
24:#define ENOTTY
25:#define EFBIG
26:#define ENOSPC
27:#define ESPIPE
28:#define EROFS
29:#define EMLINK
30:#define EPIPE
31:#define EDOM
32:#define ERANGE
33:#define EDEADLK
34:#define ENAMETOOLONG

1
2
3
4
5
6
7
8
9
10
11
12
13
14
16
17
18
19
20
21
22
23
24
25
27
28
29
30
31
32
33
34
36
38

__________________________________________________________________________________

187
__________________________________________________________________________________________
35:#define
36:#define
37:#define
38:#define

ENOLCK
ENOSYS
ENOTEMPTY
EILSEQ

39
40
41
42

:float.h 3.5.4


2
1
E
1 +37
128
1E+37
1024
6
15
1E-5
1E-9
24
53
1E-37
-125
1E-37
-1021

FLT_RADIX
FLT_ROUNDS
FLT_MAX
FLT_MAX_EXP
DBL_MAX
DBL_MAX_EXP
FLT_DIG
DBL_DIG
FLT_EPSILON
DBL_EPSILON
FLT_MANT_DIG
DBL_MANT_DIG
FLT_MIN
FLT_MIN_EXP
DBL_MIN
DBL_MIN_EXP

float.h
.float.h
:limits.h 3.5.5
:


8
-128
-32767
-2147483647
-32768
-128
2147483647
127
32767
255

CHAR_BIT
CHAR_MIN
INT_MIN
LONG_MIN
SHRT_MIN
SCHAR_MIN
LONG_MAX
SCHAR_MAX
SHRT_MAX
UCHAR_MAX

__________________________________________________________________________________

188
__________________________________________________________________________________________
UINT_MAX
ULONG_MAX
USHRT_MAX
INT_MAX
CHAR_MAX

65535
4294967295
65535
32767
127

.limits.h
3.5.6 :locale.h

)( :
0
1
2
3
4
5
LC_ALL
LC_TIME

LC_ALL
LC_COLLATE
LC_CTYPE
LC_MONETARY
LC_NUMERIC
LC_TIME
LC_MIN
LC_MAX

1:#define
2:#define
3:#define
4:#define
5:#define
6:#define
7:#define
8:#define


.
locale.h .
3.5.7 :math.h
.
:
1:#define M_E
2.71828182845904523536
2:#define M_LOG2E
1.44269504088896340736
3:#define M_LOG10E
0.434294481903251827651
4:#define M_LN2
0.693147180559945309417
5:#define M_LN10
2.30258509299404568402
6:#define M_PI
3.14159265358979323846
7:#define M_PI_2
1.57079632679489661923
8:#define M_PI_4
0.785398163397448309616
9:#define M_1_PI
0.318309886183790671538
10:#define M_2_PI
0.636619772367581343076
11:#define M_2_SQRTPI 1.12837916709551257390
12:#define M_SQRT2
1.41421356237309504880
13:#define M_SQRT1_2 0.707106781186547524401

.math.h
__________________________________________________________________________________

189
__________________________________________________________________________________________

:sin 3.5.7.1

sine double sin


:
1:#include<stdio.h>
2:#include<math.h>
3:
4:main(int argc, char *argv[]){
5:
double x = 3.14/2;
6:
7:
printf("sine x = %f\n", sin(x));
8:}

:cos 3.5.7.2

double cosine cos


:
1:#include<stdio.h>
2:#include<math.h>
3:
4:main(int argc, char *argv[]){
5:
double x = 3.14/2;
6:
7:
printf("cosine x = %f\n", cos(x));
8:}

:tan 3.5.7.3

double tangent tan


:
1:#include<stdio.h>
2:#include<math.h>
3:
4:main(int argc, char *argv[]){
5:
double x = 3.14/4;
6:
7:
printf("tangent x = %f\n", tan(x));
8:}

:exp 3.5.7.4

double exponential exp


: e
1:#include<stdio.h>
2:#include<math.h>

__________________________________________________________________________________

190
__________________________________________________________________________________________
3:
4:main(int argc, char *argv[]){
5:
double x = 3.0;
6:
7:
printf("exponential x = %f\n", exp(x));
8:}

:log 3.5.7.5

double logarithm log


:
1:#include<stdio.h>
2:#include<math.h>
3:
4:main(int argc, char *argv[]){
5:
double x = 100.0;
6:
7:
printf("logarithm x = %f\n", log(x));
8:}

:pow 3.5.7.5

double power pow

: double
1:#include<stdio.h>
2:#include<math.h>
3:
4:main(int argc, char *argv[]){
5:
double x=5.0, y=3.0;
6:
7:
printf("%f\n", pow(x, y));
8:}

:sqrt 3.5.7.6

double square sqrt


:
1:#include<stdio.h>
2:#include<math.h>
3:
4:main(int argc, char *argv[]){
5:
double x=9.0;
6:
7:
printf("%f\n", sqrt(x));
8:}

:ceil 3.5.7.7
__________________________________________________________________________________

191
__________________________________________________________________________________________

ceil double
3.1 4 3.0
3.0:
>1:#include<stdio.h
>2:#include<math.h
3:
{)][4:main(int argc, char *argv
5:
;double x=9.5
6:
7:
;))printf("%f\n", ceil(x
}8:

3.5.7.8 :floor

floor ceil 3.1 3.0:


>1:#include<stdio.h
>2:#include<math.h
3:
{)][4:main(int argc, char *argv
5:
;double x=10.5
6:
7:
;))printf("%f\n", floor(x
}8:

3.5.7.9 :fabs

fabs
:

absolute

float
>1:#include<stdio.h
>2:#include<math.h
3:
{)][4:main(int argc, char *argv
5:
;double x=-10.5, y=10.5
6:
7:
;))printf("%f\n", fabs(x
8:
;))printf("%f\n", fabs(y
}9:

3.5.7.10 :ldexp

double int
2 :
>1:#include<stdio.h
>2:#include<math.h
3:
{)][4:main(int argc, char *argv
5:
;double x=2.0, y=4.0

__________________________________________________________________________________

192
__________________________________________________________________________________________
printf("%f\n", ldexp(x, y)); /*y = 2*2*2*2, x = 2; x*y = 32.000000*/

6:
7:
}8:

3.5.7.11 :fmod

double
:
>1:#include<stdio.h
>2:#include<math.h
3:
{)][4:main(int argc, char *argv
5:
;double x=2.0, y=4.0
6:
7:
;))printf("%f\n", fmod(x, y
}8:

3.5.8 :setjmp.h
setjmp longjmp setjmp
jmp_buf longjmp
jmp_buf .int setjmp
longjmp goto
) (Global:
>1:#include<stdio.h
>2:#include<setjmp.h
>3:#include<stdlib.h
4:
;)5:void Return(void
;6:jmp_buf jmp
7:
{)][8:main(int argc, char *argv
9:
;int value
10:
11:
;)value = setjmp(jmp
12:
{
13:
;)"printf("Block 1:\n
14:
{)if(value==1
15:
;)printf("longjmp = %d\n", value
16:
;)exit(1
17:
}
18:
}
19:
20:
{
21:
;)"printf("Block 2:\n
22:
;)"printf("Call Return...\n
23:
;)(Return
24:
}
}25:
26:
{)27:void Return(void

__________________________________________________________________________________

193
__________________________________________________________________________________________
;)longjmp(jmp, 1

28:
}29:

3.5.9 :signal.h
signal raise .raise
3.5.9.1 :raise

int

:

SIGABRT

22

SIGFPE

SIGILL

SIGINT

SIGSEGV

11

SIGTERM

15

abort

3.5.10 :stdarg.h
standard argument .
stdarg.h va_list
initialize va_start
va_list
. va_arg
val_list
va_list
va_end:
>1:#include<stdio.h
>2:#include<stdarg.h
3:
4:void Arg_List(int list, ...); /*Function prototype*/

__________________________________________________________________________________

194
__________________________________________________________________________________________
5:
6:main(int argc, char *argv[]){
7:
Arg_List(5, 10, 20, 30, 40, 50);
8:}
9:
10:void Arg_List(int list, ...){
11:
va_list pList;
12:
int iArg=0, i;
13:
14:
printf("There are %d argument(s)\n", list);
15:
16:
va_start(pList, list); /*initialize the pList pointer*/
17:
18:
for(i=01;i<=list;i++){
19:
iArg = va_arg(pList, int);
20:
printf("argument %d = %d\n", i, iArg);
21:
}
22:
23:
va_end(pList);
24:
25:}

:stddef.h 3.5.11
NULL Standard Definition
:
1:#define NULL

: size_t
1:typedef unsigned size_t;

:
1:#include<stdio.h>
2:#include<stddef.h>
3:
4:main(int argc, char *argv[]){
5:
size_t uInt = 65535;
6:
7:
printf("%u\n", uInt);
8:}

: ptrdiff_t
1:typedef long

ptrdiff_t;

:
1:typedef int

ptrdiff_t;

__________________________________________________________________________________

195
__________________________________________________________________________________________

:
>1:#include<stdio.h
>2:#include<stddef.h
3:
{)][4:main(int argc, char *argv
5:
;ptrdiff_t Int = 65535
6:
;ptrdiff_t Long = 2147483647
7:
8:
;)printf("Int = %d\n", Int
9:
;)printf("Long = %d\n", Long
}10:

3.5.12 :stdio.h
Standard Input Output

.
.
3.5.12.1 :printf


:
>1:#include<stdio.h
2:
{)][3:main(int argc, char *argv
4:
;int Number = 100
5:
;'char Char = 'A
6:
;"char String[] = "Hello, World!\n
7:
8:
;)printf("%d\n", Number
9:
;)printf("%c\n", Char
10:
;)printf(String
}11:

3.5.12.2 :sprintf


:
>1:#include<stdio.h
2:
{)][3:main(int argc, char *argv
4:
;]char Buffer[255
5:
6:
sprintf(Buffer, "Number = %d\nCharacter = %c\nString = %s\n",

__________________________________________________________________________________

196
__________________________________________________________________________________________
7:
8:
9:
10:}

100, 'A', "Hello, World!");


printf(Buffer);

:vprintf 3.5.12.3

: va_list printf
1:#include<stdio.h>
2:#include<stdarg.h>
3:
4:void ArgLst(char *format, ...);
5:
6:main(int argc, char *argv[]){
7:
int Number = 100;
8:
char Char = 'A';
9:
char String[] = "Hello, World!";
10:
11:
ArgLst("Number = %d\nChar = %c\nStrin = %s\n",\
12:
Number, Char, String);
13:}
14:
15:void ArgLst(char *format, ...){
16:
va_list Lst;
17:
18:
va_start(Lst, format);
19:
vprintf(format, Lst);
20:
va_end(Lst);
21:}

:vfprintf 3.5.12.4

: vprintf
1:#include<stdio.h>
2:#include<stdarg.h>
3:
4:void ArgLst(char *format, ...);
5:
6:main(int argc, char *argv[]){
7:
int Number = 100;
8:
char Char = 'A';
9:
char String[] = "Hello, World!";
10:
11:
ArgLst("Number = %d\nChar = %c\nStrin = %s\n",\
12:
Number, Char, String);
13:}
14:
15:void ArgLst(char *format, ...){
16:
va_list Lst;
17:
FILE *File;
18:
19:
File = fopen("Info.txt", "w");
20:
21:
va_start(Lst, format);
22:
vfprintf(File, format, Lst);
23:
va_end(Lst);
24:
fclose(File);

__________________________________________________________________________________

197
__________________________________________________________________________________________
25:}

:vsprintf 3.5.12.5

: vprintf sprintf
1:#include<stdio.h>
2:#include<stdarg.h>
3:
4:void ArgLst(char *format, ...);
5:
6:main(int argc, char *argv[]){
7:
int Number = 100;
8:
char Char = 'A';
9:
char String[] = "Hello, World!";
10:
char buffer[255];
11:
12:
ArgLst(buffer, "Number = %d\nChar = %c\nStrin = %s\n",\
13:
Number, Char, String);
14:
15:
printf(buffer);
16:}
17:
18:void ArgLst(char *buffer, char *format, ...){
19:
va_list Lst;
20:
21:
va_start(Lst, format);
22:
vsprintf(buffer, format, Lst);
23:
va_end(Lst);
24:}

:scanf 3.5.12.6

:
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
char string[255];
5:
6:
printf("Your name, Please: ");
7:
scanf("%s", string);
8:
printf("Nice to meet you %s!\n", string);
9:}

:fscanf 3.5.12.7

:
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
FILE *File;
5:
char buffer[255];
6:
7:
File = fopen("Info.txt", "r");
8:

__________________________________________________________________________________

198
__________________________________________________________________________________________
9:
10:
11:
12:}

fscanf(File, "%s", buffer);


printf("%s\n", buffer);

:sscanf 3.5.12.8

:
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
char buffer[] = "Hello, World! 15";
5:
char string1[255], string2[255];
6:
int number;
7:
8:
sscanf(buffer, "%s%s%d", string1, string2, &number);
9:
10:
11:
printf("String = %s %s\n", string1, string2);
12:
printf("Number = %d\n", number);
13:}

:fgetc 3.5.12.9


:
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
FILE *File;
5:
char ch;
6:
7:
File = fopen("Info.txt", "r");
8:
9:
while(feof(File)==0){
10:
ch = fgetc(File);
11:
printf("%c", ch);
12:
}
13:}

:fgets 3.5.12.10

: fgetc
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
FILE *File;
5:
char buffer[255];
6:
7:
File = fopen("Info.txt", "r");
8:
9:
while(feof(File)==0){
10:
fgets(buffer, 255, File);

__________________________________________________________________________________

199
__________________________________________________________________________________________
11:
12:
13:
14:
15:}

printf(buffer);
}
fclose(File);

:fputc 3.5.12.11

:
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
FILE *File;
5:
6:
File = fopen("Info.txt", "w");
7:
8:
fputc('A', File);
9:
fclose(File);
10:}

:fputs 3.5.12.12

:
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
FILE *File;
5:
6:
File = fopen("Info.txt", "w");
7:
8:
fputs("Hello, World!", File);
9:
fclose(File);
10:}

:getc 3.5.12.13

: fgetc
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
FILE *File;
5:
char ch;
6:
7:
File = fopen("Info.txt", "r");
8:
9:
while(feof(File)==0){
10:
ch = getc(File);
11:
printf("%c", ch);
12:
}
13:
14:
fclose(File);
15:}

__________________________________________________________________________________

200
__________________________________________________________________________________________

:getchar 3.5.12.14

:
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
char ch;
5:
6:
printf("Enter a character: ");
7:
ch = getchar();
8:
printf("Your character is: %c\n", ch);
9:}

:gets 3.5.12.15

:
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
char str[255];
5:
6:
printf("Enter a string(Max character 255): ");
7:
gets(str);
8:
printf("Your string are: %s\n", str);
9:}

:putc 3.5.12.16

: fputc
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
FILE *File;
5:
6:
File = fopen("Info.txt", "w");
7:
8:
putc('A', File);
9:
fclose(File);
10:}

:putchar 3.5.12.17

:
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
char ch = 'A';
5:
6:
printf("ch = ");
7:
putchar(ch);
8:
putchar('\n');

__________________________________________________________________________________

201
__________________________________________________________________________________________
9:}

:puts 3.5.12.18


:
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
char str1[] = "Line 1: Hello, World!";
5:
char str2[] = "Line 2: Hello, World!";
6:
7:
puts(str1);
8:
puts(str2);
9:}

:ungetc 3.5.12.19

:
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
FILE *File;
5:
6:
File = fopen("Info.txt", "w");
7:
8:
ungetc('A', File);
9:
fclose(File);
10:}

:fopen 3.5.12.20



:
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
FILE *File;
5:
6:
File = fopen("FileName.Ext", "r");
7:
8:
if(File==NULL)
9:
printf("File does not exist!\n");
10:
else
11:
printf("File exist now!\n");
12:
13:
File = fopen("FileName.Ext", "w");
14:
if(File!=NULL)
15:
printf("File Created!\n");

__________________________________________________________________________________

202
__________________________________________________________________________________________
16:
17:
18:
19:
20:
21:
22:
23:}

if(File==NULL)
printf("File does not exist!\n");
else
printf("File exist now!\n");
fclose(File);

stdout stdin

:freopen 3.5.12.21

FILE fopen
: stderr

1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
FILE *File;
5:
6:
File = freopen("Info.txt", "w", stderr);
7:
8:
if(!File)
9:
fprintf(stdout, "Error!\n");
10:
else
11:
fprintf(File, "String!");
12:
13:
fclose(File);
14:}

:fclose 3.5.12.22

_fcloseall

1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
FILE *File1, *File2, *File3;
5:
6:
File1 = fopen("Info1.txt", "w");
7:
File2 = fopen("Info2.txt", "w");
8:
File3 = fopen("Info3.txt", "w");
9:
10:
fclose(File1);
11:
12:
printf("%d file(s) closed by _fcloseall()\n",\
13:
_fcloseall());
14:}

:remove 3.5.12.23

:
1:#include<stdio.h>
2:

__________________________________________________________________________________

203
__________________________________________________________________________________________
3:main(int argc, char *argv[]){
4:
FILE *File;
5:
6:
/*Create temporary.tmp*/
7:
File = fopen("temporary.tmp", "w");
8:
fclose(File);
9:
10:
/*remove temporary.tmp*/
11:
remove("temporary.tmp");
12:}

:rename 3.5.12.24

:
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
printf("Rename Info.txt To Data.dat...\n");
5:
rename("Info.txt", "Data.dat");
6:
printf("Operation Terminate...\n");
7:}

temporary

:tmpfile 3.5.12.25

file

1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
FILE *File;
5:
6:
printf("Create a Temporary File...\n");
7:
File = tmpfile();
8:
printf("Temporary File Created...\n");
9:
10:
/*At exit*/
11:
printf("Temporary File deleted...\n");
12:}

:fread 3.5.12.26




: FILE
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
FILE *File;

__________________________________________________________________________________

204
__________________________________________________________________________________________
5:
6:
7:
8:
9:
10:
11:}

char buffer[255];
File = fopen("Info.txt", "r");
fread(buffer, sizeof(char), 255, File);
printf("%s\n", buffer);

:fwrite 3.5.12.27

fread
: fread
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
FILE *File;
5:
char buffer[] = "String!";
6:
7:
File = fopen("Info.txt", "w");
8:
9:
fwrite(buffer, sizeof(char), 7, File);
10:
fclose(File);
11:}

:fseek 3.5.12.28


long FILE
5
int
: SEEK_END SEEK_SET SEEK_CUR
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
FILE *File;
5:
char ch_set, ch_cur, ch_end;
6:
7:
File = fopen("Info.txt", "r");
8:
9:
/*Beginning of file*/
10:
fseek(File, 0L, SEEK_SET);
11:
12:
printf("SEEK_SET Begin:\n");
13:
while(feof(File)==0){
14:
ch_set = fgetc(File);
15:
printf("%c", ch_set);
16:
}
17:
printf("\nSEEK_SET End.\n");
18:
19:
/*Current position of file pointer*/

__________________________________________________________________________________

205
__________________________________________________________________________________________
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:}

fseek(File, 0L, SEEK_CUR);


printf("SEEK_CUR Begin:\n");
while(feof(File)==0){
ch_cur = fgetc(File);
printf("%c", ch_cur);
}
printf("\nSEEK_CUR End.\n");
/*End of file*/
fseek(File, 0L, SEEK_END);
printf("SEEK_END Begin:\n");
while(feof(File)==0){
ch_end = fgetc(File);
printf("%c", ch_end);
}
printf("\nSEEK_END End.\n");

:ftell 3.5.12.29

:
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
FILE *File;
5:
int Position;
6:
char buff[3];
7:
8:
File = fopen("Info.txt", "r");
9:
10:
/* Move the pointer of file 3 bytes
11:
by reading character*/
12:
fread(buff, sizeof(char), 3, File);
13:
14:
Position = ftell(File);
15:
printf("Position after read 3 characters is %d\n",\
16:
Position);
17:}

: fseek(File,

:rewind 3.5.12.30

0L, SEEK_SET)

1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
FILE *File;
5:
char ch;
6:
7:
File = fopen("Info.txt", "r");
8:
9:
fseek(File, 0L, SEEK_END);
10:
printf("fseek(File, 0L, SEEK_END):\n");
11:
while(feof(File)==0){
12:
ch = fgetc(File);
13:
printf("%c", ch);

__________________________________________________________________________________

206
__________________________________________________________________________________________
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:}

}
printf("\n----------------\n");
rewind(File);
printf("rewind(File):\n");
while(feof(File)==0){
ch = fgetc(File);
printf("%c", ch);
}
printf("\n");

:feof 3.5.12.31

0
:
1:#include<stdio.h>
2:
3:main(int argc, char *argv[]){
4:
FILE *File;
5:
6:
File = fopen("Info.txt", "r");
7:
8:
fseek(File, 0L, SEEK_END);
9:
if(feof(File)==0)
10:
printf("Position of pointer is in the end of file\n");
11:
else
12:
printf("Position of pointer is not in the end of file\n");
13:}

:stdlib.h 3.5.13
Standard Library stdlib
. )(
:atof 3.5.13.1

double
:
1:#include<stdio.h>
2:#include<stdlib.h>
3:
4:main(int argc, char *argv[]){
5:
char str[3] = "123";
6:
double num;
7:
8:
num = atof(str);
9:

__________________________________________________________________________________

207
__________________________________________________________________________________________
10:
11:}

printf("%f\n", num);

:atoi 3.5.13.2

atof
: int
1:#include<stdio.h>
2:#include<stdlib.h>
3:
4:main(int argc, char *argv[]){
5:
char str[3] = "123";
6:
int num;
7:
8:
num = atoi(str);
9:
10:
printf("%d\n", num);
11:}

:atol 3.5.13.3

atoi
: long
1:#include<stdio.h>
2:#include<stdlib.h>
3:
4:main(int argc, char *argv[]){
5:
char str[3] = "123";
6:
long num;
7:
8:
num = atoi(str);
9:
10:
printf("%ld\n", num);
11:}

:rand 3.5.13.4

:
1:#include<stdio.h>
2:#include<stdlib.h>
3:
4:main(int argc, char *argv[]){
5:
int i, j=0;
6:
for(i=0;i<=10;i++){
7:
j=rand()%100;
8:
printf("j = %d\n", j);
9:
}
10:}

__________________________________________________________________________________

208
__________________________________________________________________________________________

100
100 .
3.5.13.5 :srand

rand
time:
>1:#include<stdio.h
>2:#include<stdlib.h
>3:#include<time.h
4:
{)][5:main(int argc, char *argv
6:
;int i
7:
8:
;))srand(time(NULL
9:
10:
{)for(i=0;i<=10;i++
11:
;)printf("%d\n", rand()%100
12:
}
}13:

3.5.13.6 :abort


:
>1:#include<stdio.h
>2:#include<stdlib.h
3:
{)][4:main(int argc, char *argv
5:
;FILE *File
6:
7:
{)))"if(!(File=fopen("FileName.txt", "r
8:
;)"printf("Can't open file\n
9:
;)(abort
10:
}
11:
12:
;)fclose(File
}13:

3.5.13.7 :exit

1 EXIT_FAILURE
1 EXIT_SUCCESS 0 :
>1:#include<stdio.h
>2:#include<stdlib.h
3:

__________________________________________________________________________________

209
__________________________________________________________________________________________
4:main(int argc, char *argv[]){
5:
FILE *File;
6:
7:
if(!(File=fopen("FileName.txt", "r"))){
8:
printf("Can't open file\n");
9:
exit(EXIT_FAILURE);
10:
}else
11:
exit(EXIT_SUCCESS);
12:
13:
fclose(File);
14:}

:atexit 3.5.13.8

:
1:#include<stdio.h>
2:#include<stdlib.h>
3:
4:void AtExit(){
5:
printf("Program - end\n");
6:}
7:
8:main(int argc, char *argv[]){
9:
atexit(AtExit);
10:
11:
printf("Program - start\n");
12:}

:system 3.5.13.9

:
1:#include<stdio.h>
2:#include<stdlib.h>
3:
4:main(int argc, char *argv[]){
5:
6:
printf("Message!\n");
7:
8:
/*In DOS OS*/
9:
system("cls");
10:
11:
/*In Unix/Linux OS*/
12:
system("clear");
13:}

:abs 3.5.13.10

: int
1:#include<stdio.h>
2:#include<stdlib.h>
3:
4:main(int argc, char *argv[]){
5:
int i=-10, j=20;
6:

__________________________________________________________________________________

210
__________________________________________________________________________________________
7:
8:
9:}

printf("absolute value if 'i' is %d\n", abs(i));


printf("absolute value if 'j' is %d\n", abs(j));

:labs 3.5.13.11

: long abs
1:#include<stdio.h>
2:#include<stdlib.h>
3:
4:main(int argc, char *argv[]){
5:
long i=-10, j=20;
6:
7:
printf("absolute value if 'i' is %d\n", labs(i));
8:
printf("absolute value if 'j' is %d\n", labs(j));
9:}

:div 3.5.13.12

: int
1:#include<stdio.h>
2:#include<stdlib.h>
3:
4:main(int argc, char *argv[]){
5:
int a=12, b=6;
6:
7:
printf("%d/%d = %d\n", a, b, div(a, b));
8:}

:ldiv 3.5.13.13

: long
1:#include<stdio.h>
2:#include<stdlib.h>
3:
4:main(int argc, char *argv[]){
5:
long a=12, b=6;
6:
7:
printf("%d/%d = %d\n", a, b, ldiv(a, b));
8:}

:string.h 3.5.14
str string.h
.memory mem string
.str
__________________________________________________________________________________

211
__________________________________________________________________________________________

3.5.14.1 strcpy :strncpy

strcpy
.
strncpy
strcpy :
>1:#include<stdio.h
>2:#include<string.h
3:
{)][4:main(int argc, char *argv
5:
;"char str[] = "Hello
6:
;]char empty[5
7:
;]char empty2[5
8:
9:
;)strcpy(empty, str
10:
;)strncpy(empty2, str, 3
11:
12:
;)printf("empty = %s\n", empty
13:
;)printf("empty2 = %s\n", empty2
}14:

empty :
>1:#include<stdio.h
>2:#include<string.h
3:
{)][4:main(int argc, char *argv
5:
;"char str[] = "Hello
6:
;"char empty[5] = "empty
7:
8:
;)strcpy(empty, str
9:
10:
;)printf("empty = %s\n", empty
}11:

3.5.14.2 strcat :strncat

strcat strncat
:strcat
>1:#include<stdio.h
>2:#include<string.h
3:
{)][4:main(int argc, char *argv
5:
;"char str[] = "Hello
6:
7:
;)printf("str = %s\n", str
8:
;)"strcat(str, ", World
9:
;)printf("str = %s\n", str
}10:

__________________________________________________________________________________

212
__________________________________________________________________________________________

:strncat
1:#include<stdio.h>
2:#include<string.h>
3:
4:main(int argc, char *argv[]){
5:
char str[] = "Hello";
6:
7:
printf("str = %s\n", str);
8:
strncat(str, ", World", 3);
9:
printf("str = %s\n", str);
10:}

:strncmp strcmp 3.5.14.3

strcmp
0 0
strncmp
:strcmp
1:#include<stdio.h>
2:#include<string.h>
3:
4:main(int argc, char *argv[]){
5:
char str1[] = "Hello";
6:
char str2[] = "Hello2";
7:
int cmp = strcmp(str1, str2);
8:
9:
if(cmp>0)
10:
printf("str1 > str2\n");
11:
else
12:
printf("str1 < str2\n");
13:}

:strncmp
1:#include<stdio.h>
2:#include<string.h>
3:
4:main(int argc, char *argv[]){
5:
char str1[] = "Hello";
6:
char str2[] = "Hello2";
7:
int cmp = strncmp(str1, str2, 3);
8:
9:
if(cmp>0)
10:
printf("str1 > str2\n");
11:
else
12:
printf("str1 < str2\n");
13:}

:strrchr strchr 3.5.14.4


__________________________________________________________________________________

213
__________________________________________________________________________________________

strchr
strchr strrchr
:
1:#include<stdio.h>
2:#include<string.h>
3:
4:main(int argc, char *argv[]){
5:
char str[] = "Hello", *strdest;
6:
char ch = 'l';
7:
int result;
8:
9:
printf("%s\n", str);
10:
printf("12345\n");
11:
12:
strdest = strchr(str, ch);
13:
result = (int)(strdest-str+1);
14:
printf("First '%c' in position %d\n", ch, result);
15:
16:
strdest = strrchr(str, ch);
17:
result = (int)(strdest-str+1);
18:
printf("Last '%c' in position %d\n", ch, result);
19:}

:strcspn strspn 3.5.14.5

strspn
strcspn 0
:
1:#include<stdio.h>
2:#include<string.h>
3:
4:main(int argc, char *argv[]){
5:
char str1[] = "HHHeeelllooo";
6:
char str2[] = "He";
7:
char str3[] = "lllooo";
8:
int result;
9:
10:
result = strspn(str1, str2);
11:
printf("There are %d character(s) of '%s' in string '%s'\n",\
12:
result, str2, str1);
13:
14:
result = strcspn(str1, str3);
15:
printf("First '%s' in string '%s' is start at character %d\n",\
16:
str3, str1, result);
17:}

.
:strpbrk 3.5.14.6
__________________________________________________________________________________

214
__________________________________________________________________________________________

strpbrk
:
1:#include<stdio.h>
2:#include<string.h>
3:
4:main(int argc, char *argv[]){
5:
char str[] = "Hello";
6:
char *result = strpbrk(str, "l");
7:
8:
printf("%s\n", result);
9:}

:strstr 3.5.14.7

strchr strstr
:
1:#include<stdio.h>
2:#include<string.h>
3:
4:main(int argc, char *argv[]){
5:
char str1[] = "Hello, World!", str2[] = "World";
6:
char *strdest = strstr(str1, str2);
7:
int result;
8:
9:
result = (int)(strdest-str1+1);
10:
printf("The word '%s' is at position %d in string '%s'\n",\
11:
str2, result, str1);
12:
13:}

:strlen 3.5.14.8

:
1:#include<stdio.h>
2:#include<string.h>
3:
4:main(int argc, char *argv[]){
5:
char str[] = "Hello";
6:
int result = strlen(str);
7:
8:
printf("'%s' = %d character(s)\n", str, result);
9:}

:strerror 3.5.14.9

:
1:#include<stdio.h>
2:#include<string.h>
3:#include<stdlib.h>
4:

__________________________________________________________________________________

215
__________________________________________________________________________________________
5:main(int argc, char *argv[]){
6:
char str[10];
7:
8:
printf("Enter a string (Max 10 characters): ");
9:
10:
if((strlen(gets(str)))>10){
11:
printf("%s\n", strerror(12));
12:
exit(1);
13:
}else
14:
printf("'%s' = %d character(s)\n",\
15:
str, strlen(str));
16:
17:}

.
:strtok 3.5.14.10

: strtok
1:#include<stdio.h>
2:#include<string.h>
3:
4:main(int argc, char *argv[]){
5:
char *string,
6:
str[] = "(Hello, World)",
7:
tok[] = " (),I";
8:
int i;
9:
10:
string = strtok(str, tok);
11:
12:
for(i=0;i<2;i++){
13:
printf("%s\n", string);
14:
string = strtok(NULL, tok);
15:
}
16:}

:time.h 3.5.15
time.h
: tm
1:struct
2:
int
3:
int
4:
int
5:
int
6:
int
7:
int
8:
int
9:
int
10:
int
11:};

tm
{
tm_sec;
tm_min;
tm_hour;
tm_mday;
tm_mon;
tm_year;
tm_wday;
tm_yday;
tm_isdst;

: time_t clock_t
__________________________________________________________________________________

216
__________________________________________________________________________________________

;1:typedef long time_t


;2:typedef long clock_t

.time.h
3.5.15.1 :clock


CLOCKS_PER_SEC CLK_PER_SEC :
>1:#include<stdio.h
>2:#include<time.h
3:
{)][4:main(int argc, char *argv
5:
{);;(for
6:
;)printf("%d\n", clock()/CLOCKS_PER_SEC
7:
)if((clock()/CLOCKS_PER_SEC)==5
8:
;break
9:
}
}10:

60
60 24
.
:
>1:#include<stdio.h
>2:#include<time.h
3:
;)4:void delay(int second
5:
{)][6:main(int argc, char *argv
7:
;int waitsec
8:
9:
;)" printf("wait(in seconds):
10:
;)scanf("%d", &waitsec
11:
;)delay(waitsec
12:
;)"printf("Time terminated...\n
}13:
14:
{)15:void delay(int second
16:
;int sec
17:
18:
{);;(for
19:
;sec = clock()/CLOCKS_PER_SEC
20:
)if(sec==second
21:
;break
22:
}
}23:

__________________________________________________________________________________

217
__________________________________________________________________________________________

:time 3.5.15.2

: 1970 1 00:00
1:#include<stdio.h>
2:#include<time.h>
3:
4:main(int argc, char *argv[]){
5:
time_t Seconds, Minutes, Hours, Days;
6:
7:
Seconds = time(NULL);
8:
Minutes = Seconds/60;
9:
Hours
= Minutes/60;
10:
Days
= Hours/24;
11:
12:
printf("%ld\tseconds since 01/01/1970.\n", Seconds);
13:
printf("%ld\tminutes since 01/01/1970.\n", Minutes);
14:
printf("%ld\t\thours since 01/01/1970.\n", Hours);
15:
printf("%ld\t\tdays since 01/01/1970.\n", Days);
16:}

NULL Seconds time


:
1:#include<stdio.h>
2:#include<time.h>
3:
4:main(int argc, char *argv[]){
5:
time_t Seconds, Minutes, Hours, Days;
6:
7:
time(&Seconds);
8:
Minutes = Seconds/60;
9:
Hours
= Minutes/60;
10:
Days
= Hours/24;
11:
12:
printf("%ld\tseconds since 01/01/1970.\n", Seconds);
13:
printf("%ld\tminutes since 01/01/1970.\n", Minutes);
14:
printf("%ld\t\thours since 01/01/1970.\n", Hours);
15:
printf("%ld\t\tdays since 01/01/1970.\n", Days);
16:}

:difftime 3.5.15.3

time_t

:
1:#include<stdio.h>
2:#include<time.h>
3:
4:main(int argc, char *argv[]){
5:
time_t Start, End;
6:
int i;

__________________________________________________________________________________

218
__________________________________________________________________________________________
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:}

Start = time(NULL);
for(i=0;i<=40000;i++){
printf("%d\n", i);
}
End = time(NULL);
printf("Loop taken %.0f seconds to terminate...\n",\
difftime(End, Start));

:localtime 3.5.15.4

: tm 1900
1:#include<stdio.h>
2:#include<time.h>
3:
4:main(int argc, char *argv[]){
5:
time_t Seconds;
6:
int Year, Month, Day, Hour, Minute, Second;
7:
struct tm* Time;
8:
9:
time(&Seconds);
10:
11:
Time = localtime(&Seconds);
12:
Year = Time->tm_year+1900, Month= Time->tm_mon+1,
13:
Day = Time->tm_mday;
14:
15:
Hour = Time->tm_hour, Minute = Time->tm_min,
16:
Second = Time->tm_sec;
17:
18:
printf("Date: %.4d/%.2d/%.2d\n",Year, Month, Day);
19:
printf("Time: %.2d:%.2d:%.2d\n", Hour, Minute, Second);
20:}

:asctime 3.5.15.5

DDD MMM D

: HH :MM :SS YYYY


1:#include<stdio.h>
2:#include<time.h>
3:
4:main(int argc, char *argv[]){
5:
time_t Seconds;
6:
struct tm* Time;
7:
8:
time(&Seconds);
9:
10:
Time = localtime(&Seconds);
11:
12:
printf("Date/Time: %s", asctime(Time));
13:}

:ctime 3.5.15.6
__________________________________________________________________________________

219
__________________________________________________________________________________________

DDD MMM D

time()
: HH :MM :SS YYYY

1:#include<stdio.h>
2:#include<time.h>
3:
4:main(int argc, char *argv[]){
5:
time_t Seconds;
6:
7:
time(&Seconds);
8:
9:
printf("Date/Time: %s", ctime(&Seconds));
10:}

__________________________________________________________________________________

220
__________________________________________________________________________________________


) (
) .(...Delphi Java

... :
C++ ) Assembly( :
C++

.
.C++
.C++
.
.
C ) (C++, C#
)
(... .www.vc4arab.com :
...
.www.arabteam2000.com :

... .www.cb4a.com :
__________________________________________________________________________________

221
__________________________________________________________________________________________


BASIC .www.2jm2.com :

Delphi .www.orwah.net :

.www.gamesdevteam.com :
Linux Arabian :
/http://www.arabian.arabicos.com

mysmartbb :
/http://www.mysmartbb.com


__________________________________________________________________________________

222
__________________________________________________________________________________________

__________________________________________________________________________________

223
__________________________________________________________________________________________

__________________________________________________________________________________

Anda mungkin juga menyukai