You are on page 1of 15

, , .

.
, . , , : , ,
, . , , .

, (,
, ). :
1. .
2. , .
.
3. , , .
, : name, NaMe, NAME .
4. ,
(, , ) .
, ANSI C 32 ( ++ ).
5.
.

/* */. .
++ . , //
, .
.
, .
. ,
, .

, .
- , ( ). , .
" " " ". - , , ,
- . , ,
- . ,
:
char - ;
int - ;
float - ;
double - ;
void - .
char 1 , ,
: '', ':', 'j' ( ).
int .
int . , 16-
int 2 .
-32768 32767. .
float . ,
, : -5.6, 31.28 ..
, : -1.09e+4. "" , "" - .
float 32 . 3.4-38 3.4e+38.
double .
, float (.. 64 ). double
1.7e-308 1.7e+308.
void ( ) , ,
, .
.
, . ANSI :

- unsigned;
- signed;
- short;
- long.
, :
unsigned char.
, ,
int. , :
long ;
long int ;
. . 1
(unsigned, signed, short, long) (char, int, float double),
( 16- ).
1



char
8
-128 127
unsigned char
8
0 255
signed char
8
-128 127
int
16
-32768 32767
unsigned int
16
0 65535
signed int
16
-32768 32767
short int
16
-32768 32767
unsigned short int
16
0 65535
signed short int
16
-32768 32767
long int
32
-2147483648 2147483647
unsigned long int
32
0 4294967295
signed long int
32
-2147483648 2147483647
float
32
3.4-38 3.4+38
double
64
1.7-308 1.7+308
long double
80
3.4-4932 3.4+4932

(). ,
, . :
int a, b, c;
char x, y;
. :
(, ), (, 4 ) .. (
).
:
int a = 25, h = 6;
char g = 'Q', k = 'm';
float r = 1.89;
long double n = r*123;
. , ,
. .
, .
, .
int a;

/* */

int function (int b, char c); /* (.. )*/


void main (void)
{
int d, e;
float f;
...

//
//
//

}
int function (int b, char c) /* ( -
) b c */
{
//
char g;
//
...
}
main( ), .
, , .
, . ++
. ,
, , .

:
, 123.456, 5.61-4. F ( f), 123.456F, 5.61e4f;
, 125;
, () H ( h), 275h, 344H;
, () L ( l), 361327L;
, U ( u), 62125U;
, (0), 071;
, - (0x),
0x5F;
- , , '', '2', '.' .. ,
, , , \n (
10), \0 ( 0). , .
: '\NNN', NNN - .
, : '\2', '\6' ..;
- , , : "
". , .
, '\0'. , ,
, ;
, , (:
=60+301);
long double, L ( l), : 1234567.89L.

(, ), .
- ,
, .
. ,
, . . 2 .
. ,
. .
- . ,
(: a = sin(b)+c; j++;), , ,
, , ..
. ( ), ,
{ } (" "),
. , .
=.
= ;
.
, :
x = y = z = 100;.

2

()
[]
.
->
!
~
++
-&
*
sizeof( )
*
/
%
+
<<
>>
<
<=
>
>=
==
!=
&
^
|
&&
||
?:
=
+=, - =, *=, /=, %=, <<=,
>>=, &=, |=, ^=
,



,

,


""
""
""
""
""
()

(, *= b
(.. a = a * b) ..)

. , - .
, .
1. .
2. .
3. .
(. 2): +, -, *, /, %.
. :
a = b + c; x = y - z; r = t * v; s = k / l; p = q % w;
(. . 2): &&, ||, !, >, >=, <, <= , = =, !=.
: . : -
; - . ,
, 0 1 .
, int, char, (, long
int). float, double, void ( ).
: ~, <<, >>, &, ^, |. : a=0000 1111 b=1000 1000,
~a = 1111 0000,
a << 1 = 0001 1110,
a >> 1 = 0000 0111,
a & b = 0000 1000,
a ^ b = 1000 0111,

a | b = 1000 1111.
++ --.
. ,
. (++n --n) n
, (n++ n--) - . :
a = b + c++;
a1 = b1 + ++c1;
, b = b1 = 2, c = c1 = 4. : a = 6, b = 2, c = 5, a1 = 7, b1 = 2, c1 = 5.
a = b + c++; a = b + c; c++;. a1=b1 + ++c1;
c1++;a1 = b1 + c1;.

?:.
y = x ? a: b;
y = a, x (.. ), y = b, ().
y = (a>b) ? a: b;
( b), y = max(a, b).
, = + 5; : a += 5;.
+ (. . 2).

.
("") . :
() ,
- ). :
int a = 30000;
float b;
........
b = (float) a * 12;.
a float. , ,
a*12>32767 .
.

- , , (,
). ,
.
& ,
= &;
. & ;
&(+7) &28 .
*
,
z = *y;
z , .
y = &x; //
z = *;
z = x.
*:
int *, *b, *;
char *d;.
char *d , , d, char.

. - , * ,
, . , :
* = 7;
*x *=5;
(*z)++;
7 , ,
z. ,
. , , , *z = 5, (*z)++
, *z = 6, *z++ z ( ++ z, *z
).
. - ,
y++ ; .
.

, .
: for, while do - while. , :
for (_1; _2; _3) _.
, , . 1,
2, 3 , .
. _1 ,
_3 , _2 , ,
. :
for (i = 1; i < 10; i++)
{
...
}
for , . ,
for(;;) {...}
- .
while :
while () _.
() () . ,
. , while .
do-while :
do {_} while ();.
while do - while , do - while
. , .
, .
,
for, while do - while.
break continue. break
, continue .

: if - else, switch goto.
:
if (_)
_1;
else
_2;.
, _1, - _2.
, . if
else . if - else if else

. if, .
else if. :
if ( a > b)
{
c = a + b;
d++;
}
else
{
c = a - b;
d--;
}
, if ( a > b && x >= y).
switch .
:
switch ()
{
case _1:
_1;
break;
case _2:
_2;
break;
........
........
default:
_default;
}
( )
( ). .
( ).
default , . default
. default , ,
.

break, switch.
switch.

:
goto ;
- , . goto ,
, .

.

.
. , . ,
. .
, :
. , :
int a[100];
char b[20];
float d[50];

100 : [0], [1], ..., [99] ( ).


b char, - float.
, . ,
char [10][20]; . .
, .. ,
. , :
[5][9].
:
int a[2][3];,
: a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2].
- , (
[0][0]). , a = 1000. [0][1] 1002 ( int
2 ), [0][2] - 1004 ..
. ,
, , .

int a[5];
[0], a[1], a[2], a[3], a[4]. *
int *;
= &a[0]; [0].
, y+1 ,
( int - 2 , long - 4 , (double - 8 ..).
, = &a[0]; :
= . [1] *(+1). , - a,
: a[i] *(+i) - .
. -
=; y++; - . - , a = y; a++;
, .
. :
_[...] ... [...] = { };
:
int a[5] = {0, 1, 2, 3, 4};
char ch[3] = {'d', 'e', '9'};
int b[2][3] = {1, 2, 3, 4, 5, 6};
: b[0][0] = 1; b[0][1] = 2; b[0][2] = 3; b[1][0] = 4; b[1][1] = 5; b[1][2] = 6.
, , , : char *m[5];.
m[5] - , char.

,
. , - .
char [10]; 10 .
, char.
, , ,
, , , .
, , .. , .
.
:
char array[7] = "";
char s[ ] = {'', '', '', '', '', '', '\0'};.
.
- . char *b;
b, .
b .
b ="IBM PC";, :
- - "IBM PC", ('\0');
- ( 'I') b.

, ..
.
,
string.h. strcpy( ), strcat( ), strlen( ) strcmp( ).
string1 string2 - , strcpy( ) :
strcpy(string1, string2);
string2 string1. string1
, string2. ,
.
strcat( ) :
strcat(string1, string2);
string2 string1 , string1,
string2 . , , .
strlen( ) , . a - ,
:
a = strlen(string);
strcmp( ) 0, .

- (, , ,
..). ,
.
struct,
, :
struct { _1 _1;
_n _n;
.........
};
. ,
,
sruct date {
char day;
char month;
int year;
};.
, , ,
:
struct date {
char day;
char month;
int year;
} a, b, c;,
. ;
. , :
struct date day3;
day3 date.
, :
struct man {
char name[20], fam[20];
struct date bd;
int age;
};
data : day, month, year, int. man
name, fam, bd voz. - name[20] fam[20] - 20
. bd ( ) data. age
int. , , :
struct man Students[100];.

Students, 100 man.


, ,
, :
man4[i].age = 19;
man4[j].bd.day = 22;
man4[j].bd.year = 1988;
& . , day:
struct date {int d, m, ;} day;
day - date, : d, m, .
struct date *db;
db date.

db = &day;
db.
d, m, :
(*db).d; (*db).m; (*db).y;
, db - , *db - . ,
, , . ->.
:
db -> d; db -> m; db -> ;
typedef
, (
). typedef. :
typedef ;
"" - "" - .
:
typedef int INTEGER;
:
INTEGER , b;.
, int a,b;. , INTEGER
int.

. -
, . signed int unsigned int 1 16
. . , :
struct prim {
int a:2;
unsigned b:3;
int c:5;
int d:1;
unsigned d:5; } i, j;
( ). : unsigned d:6,
, 0 - 5 .

. , &.

.
enum. :

10

enum seasons (spring, summer, autumn, winter);.


seasons. , seasons, .
spring , ,
(summer=1, autum=2 winter = 3). :
enum seasons , b, ;.
(, b, c) : spring, summer, autumn winter.
:
enum seasons (spring, summer, autumn, winter) a, b, ;
(, , , ,
, ). :
enum days (man=5, tues=8, wed=10, thur, fri, sat, sun} my_week;
mon=5, tues=8,wed=10, thur=11, fri=12, sat=13, sun=14.

().
. .
, .
. (.. )
, (.. )
(.. main( )), . ,
, .
.
:
_( __1, __2, ...);
, . , ,
int.
, :
(int, float, ...);,
, :
(int , float b, ...);.
.
. :
_( __1, __2,...)
{

}
return,
:
return ;
, .
:
int f(int a, int b)
{
if (a > b) return a;
return b;
}

11

:
c = f(15, 5);
c = f(d, g);
.

f(d, g);.
return ;
.
( return ).
, ..
, . ,
. , , . :
void swap(int *a, int *b)
{
int *tmp = *a;
*a = *b;
*b = *tmp;
}
swap(&b, &c) ( b ) ,
b .
, ,
. .
:
1. (: int m[100];).
2. (: int m[];).
3. (: int *m;). .
.
.
, , , ,
.

: (), (),
.
() .
. ,
, .
,
extern. :
extern int a; /* a; */
.
( auto).
,
auto.
static.
() (). , ,
. ,
. , .
, ,
. , , .
, : extern, auto, static, register.
:
__ _;
, .
, , ,
- .

12

. :
, ..
. ,
, .
. . :
int (*f)( );
, f - , . ,
int *f( ); , f - , . ,
. : *f -
; f - . ( )
.

.
. *.h
( ).
:
#include <___h>,
:
#include <p18f4520.h>

- , .
: ,
, . ,
#. ( ).
.

#define
.
(), "" - , "" , , .
. :
#define MAX 25
#define BEGIN {
MAX 25.
BEGIN.
#define :
#define (, ..., ) ,
,
. , :
#define MAX(val1, val2) f(val1, val2)
MAX(a, b); , f(a, b);.
, ,
\.
, ##, :
#define PR(x, ) x##y
PR(, 3) 3. , ,
#define z(a, b, c, d) a(b##c##d)
z(sin, x, +, y) sin(x+y).
#include :
#include " "

13


#include < >.
.
.
, . , ,
, , #include <...>. #include
.
.
. #if, #else, #elif, #endif, #ifdef, #ifndef.
#if :
#if _ _
#endif
. ,
, , .
#else else , :
#if _
__1
#else
__2
#endif
, __1, __2.
#elif "else if". :
#if _
_
#elif __1
__1
#elif __n
__n
#endif
: if...else if...else if...

#ifdef
, , ..
#define.

#ifndef
, .
, , #else (#elif )
#endif. , #else #endif,
, #else ( #else , #endif). #if #ifndef
"" .

#undef
, , .. .
:
#ifdef WRITE
#undef WRITE
#endif
WRITE (.. #define WRITE ), ,
WRITE , .. .
#error :
#error __.

14

,
. .
.

15