You are on page 1of 26

C

( 1 - )
C
C Dennis Ritchie
Bell Labs 1972 Ken Thompson
Unix. C
Ken Thompson
BCPL
.
C
. ,
1983
Ansi C.


C
(high level languages),
Cobol Pascal (low level
languages), Assembly.
,


.
.
,
. Pascal, ,
, Basic


. Clipper Cobol
.

C, , hardware,

. ,
,

.

, C
, ,
'
. .

, C , .

.
.

, C ,
.
,
Unix C.
,
(compilers)

(interpreters)

. C

(assembly language).

, C
,

s.g

, C

.
,
.

, .

eB

oo

ks

4G

re

ek

,
.

. C

(bits) . C
,
Pascal .
,
. C () .
, , ,
. , C
.

C
C ,
. ,
C,
.
, C
,
,
.
,
C. C ,

.

C
, C
, (functions).
,
. , C ,
main().
C
main(),
.

---

(top-down
programming).

, .
,
--- (bottom-up programming).
---
,
.

Assembly C
, Assembly

Assembly
.
Assembly , ,
,
. C
Assembly.
, C

Assembly,
.


(compiler) C (source
program), . C,
(object program) (linker)

(executable file). C .c.
,
(start-up code)
(library code) .


.

, '
.

Turbo-C
Turbo-C tc Dos.
, :

File : '
,
, ,

Turbo-C, .

Edit :
.

Run : , .

Compile :
.

Project :
.

Options :
.

Debug :
.

C
/* prog01.c C */
#include <stdio.h>
main()
{
int num;

/* num */

num = 10 ; /* num */
printf(" C.\n"); /* printf() */
printf(" %d.\n", num);

}
,
:
C.
10.


/* */
(comments),
.
.
(include) stdio.h,
C.
C ,
C.
main().
main() .
{
}.
{ } begin end Pascal.
num
num (integer).
10 num.

(" "). \n
( <enter>).
num ( 10)
" ". %d
num.
}.


#include

stdio.h C
,
printf(), .
standard input/output header. C

. #include <stdio.h>
stdio.h ,
.
,
C. #
(preprocessor) C,
.
main()
C
main().
,
main() .
main() . ,
() . , ,
(),
.

,
, ,
. , /*
*/
C ,
.
, ,
{ }

,
begin end Pascal.

C.

, ..
. int - C, .


.
C.


C () : , ,
, ..
, ,
(_). , ,
.



. ,
.

C
.
;.
printf()
' .

() main() printf().

printf() " "
.


. ,
,
.
\n, ,
.
\n ,
. <enter>.

.
\t tab \b (backspace).
, , %d;

.
C .
,
#include, .
{ } ,
;.
,
.



.
. , C

.

.
.

C
/* prog02.c */
#include <stdio.h>
main()
{
int cm, metres;

metres = 2;
cm = 100 * metres;

printf(" %d %d . \n", cm, metres);

}
,
. ,
, .
, *
printf(). ,
:
200 2 .



.
/* prog03.c */
#include <stdio.h>
main()
{
printf(" .\n");
florina();
printf(" .\n");
}

florina()
{
printf(" .\n");
}
:
.

.
.
florina) , main(),
.
. florina()
,
main().

main(),
main() .



C
. C
.
{ }
,
.
,
. , ,
, , .

. C
,
, .
, .. ,
.
.
.
-.
printf()
,
. printf()
.
,


.
(debuggers)

.

- ANSI C
- (reserved words)
C
.
:
auto
continue
enum
if
short
switch
volatile

break
default
extern
int
signed
typedef
while

case
do
float
long
sizeof
union

char
double
for
register
static
unsigned

const
else
goto
return
struct
void


, ,
.


C.
/* prog04.c */
#include <stdio.h>
main()
{
float draxmes, euro;
char beep;

/* 2 */
/* */

beep = \007

/* */

printf(" ;\n");
printf(" \n");
printf(" .\n");
scanf("%f", &euro); /* */
draxmes = 340.75 * euro;
*/

/* 340.75

printf("%c %.2f%c. \n", beep, draxmes, beep);


}
, :
;
.
100.00
34075.00


' ,
(float) (char)
.

%f %c printf() . .2
,
.

scanf(). %f
&euro scanf()
euro. &
.
printf() scanf()
, scanf()
printf()

.
beep .


. C
- :
int, long, short, unsigned, char, float double.
int long, short unsigned
. char
float, double long double
.
- ,
.
,
.
,
,
. , 7 0.71, 0.7 101 = 7.

'

, .


int , .
,
. (bytes)

-32768 +32767. - int
.
, ,
, .
, ' .

scanf().

.
C ,
(=)
, :
int a=10;
int b, cs=20;
C .
0 () 0x
0X . , 16
020
0x10 0X10. '
.
%d
, int,
int ,
int.
:
/* prog05.c printf() */
#include <stdio.h>
main()
{
int ten = 10;
printf("%d %d %d\n", ten, 2, ten-2);
}
:
10 2 8
, ,
%o %x.
:
/* prog06.c 100 */
#include <stdio.h>
main()

{
int x = 100;
printf(" = %d; = %o; = %x\n", x, x, x);
}
:
= 100; = 144; = 64
0 0x .


C -
: unsigned, long short. short int short
, ' , int
. long int
long
' , int .
unsigned 0 65535
-32768 +32767.
,
unsigned ,
' . long
,
int. , , long
,
. short
.
long,
l L
.
unsigned %u,
long %ld l o
x. , %h short.

, char
, .

,
. ASCII
65 . char
:
char name, city;
C char
-128 +127 C char
0 255.
char :
char grade = 65;

/* ASCII */

char grade = 'A';

/* */

' ' C ,
" " ,
.


ASCII ,
(backspace), enter (beep). C
.
ASCII , :
char beep = 7;
ASCII,
ASCII
\ , :
beep = '\007';
,
0. ANSI C
, \ x
X 1 3 . , Control-P
'\x10' '\X010'.

,
(escape sequence) :
\a

- beep (ANSI C)

\b

( )

\f

\n

(enter)

\r

\t

(tab)

\v

(ANSI C)

\\

(\)

\'

(')

\"

(") (ANSI C)


:
next = '\n';
next
. printf() %c
.
,
char, .
%c printf()
. :
/* prog07.c - */
#include <stdio.h>
main()
{
char ch;
printf(" : \n");
scanf("%c", &ch);

printf(" %c %d. \n", ch, ch);


}
:
:
C
C 67.
printf() ch ,
( %c)
( %d). printf()

.

float double

C float
real Pascal. ,
, .
double ( ) long double.
:
float a=10.25e-3;
double b;
long double c;
f F ANSI C
float, .. 2.3f 9.11E9F, l L
long double, .. 54.31l 4.32e4L.
printf() %f
float double
%e . long
double %Lf %Le.

sizeof

C , sizeof,
bytes. :
/* prog08.c */
#include <stdio.h>
main()
{
printf(" int %d bytes. \n", sizeof(int));
printf(" char %d bytes. \n", sizeof(char));
printf(" long %d bytes. \n", sizeof(long));
printf(" double %d bytes. \n", sizeof(double));
}
:
int 2 bytes.
char 1 bytes.
long 4 bytes.
double 8 bytes.


.
/* prog09.c */
#include <stdio.h>
main()
{
float weight;
int size, letters;
char name[40];

printf(" ! ; \n");
scanf("%s", name);
printf("%s, ; \n", name);
scanf("%f", &weight);
size = sizeof name;
letters = strlen(name);
printf(" , %s, %2.2f . \n", name,
volume);
printf(", %d , \n", letters);
printf(" %d bytes .\n", size);
}
, :
! ;

, ;
100
, , 100,00 .
, 6 ,
40 bytes .
:
1.
.
2. %s
. name,
weight, &
scanf().
4. C strlen()
.
, , ;

,
: " 1993".

,
. C
char
. C
\0
.
0,
ASCII 0.

.
:
char name[40];
' name 40
.


:
/* prog10.c */
#include <stdio.h>
#define PRAISE " !"
main()
{
char name[50];

printf(" ;\n");
scanf("%s", name);
printf(" , %s. %s\n", name, PRAISE);
}

:
;

, . !
scanf() .
. ,
scanf() %s
. C ,
gets(), ,
.
"x" 'x' 'x'
(char), "x" ,
char. , "x"
, 'x' . srtlen()
, sizeof

.

C

. C
.
, :
float taxrate;
taxrate = 0.2;
taxrate
. C
,
, :
#define TAXRATE 0.2
, 0.2
TAXRATE. , ,
.
#define, (;),
C C


.
#define.
/* prog11.c = 3.14159 */
#include <stdio.h>
#define PI 3.14159
main()
{
float area, circum, radius;

printf(" : \n");
scanf("%f", &radius);
area = PI * radius * radius;
circum = 2.0 * PI * radius;
printf(" :\n");
printf(" = %1.2f, = %1.2f\n", circum, area);
}
:
:
6.0
:
= 37.70, = 113.10
#define
.
. :
#define BEEP '\007'
#define TEE 'T'
#define ESC '\033'

#define OOPS " "


#define TOES = 20, TOES
= 20 20.

#define
#define
, .. const.h. ,
#include "const.h" ,
const.h
#define .
"const.h"
,
<const.h> .
#define
, C Pascal, :
#define program main()
#define begin {
#define end }
#define times *
, ,
, , . C,
Pascal.



, ,
:
/* prog12.c */
#include <stdio.h>
main()
{

printf(" ");
printf(" .\n");
printf(" \
.\n");
printf(" "
" .\n");
}
:
.
.
.

You might also like