, , , .
. , :
;
, , .
5 : , , , void. char ASCII- - 8- . int . float double .
void . - . - . - , .
, , ', , , . .
2.3
, , () , (). . , .
:
[=], [=]];
. , , .
, , . :
char ch,ch1=*;
int d=5,e,f=-1;
float fl;
double d1=0.5,d2;
. .
, . . , , .
, , , .
. , , , . ' , , . 䳿 , , .
- ' ' . ' , . ϳ ' , , .
|
|
. ' ' , .
' . .
' register:
register int c;
, , ' . , .
. , , .
, , , .
' extern.
. , , static.
:
static int c=5;
2.4
, . (, , ), ( ). .
1
- |
[] | → | arr1 [ ], 2 [j+1 ] | |||
() | → | sin (), * (b + ) | |||
! | ← | !(>b) | |||
~ | ← | ~ | |||
++ -- | ← | , 1 | ++, j - - | ||
() | ← | b = (bt) | |||
+ - | ← | =-b | |||
* | → | *b | |||
/ | → | ij | /b | ||
% | → | % b ( b - ) | |||
+ | → | + b | |||
- | → | ³ | -b | ||
<<>> | → | , | >> n, << n ( n - ) | ||
>>> | → | 0 | >>> n ( n - ) | ||
< <= > >= | → | < b, > b, >= b, <= b | |||
==!= | → | == b, !=b | |||
& | → | & b ( b - ) | |||
^ | → | , , | ^ b ( b - ) | ||
| | → | | b ( b - ) | |||
&& | → | &&b | |||
|| | → | ||b | |||
?: | → | ? | > ? 5: 6 | ||
= | ← | = b | |||
+= _= *= /= %=!= ^= <<= >>= >>>= | ← | += b ( = + b), /= b ( = / b) |
|
|
2.5
, main(). () , , #include . ' , . ҳ , :
-----------------------------------------------
#include<stdio.h>
int main(void)
-----------------------------------------------
ҳ
-----------------------------------------------
{
int q;
q=1;
printf(q=%d \n,q);
return 0;
}
------------------------------------------------
, ' . , , . , printf(). main() .
, ANSI C :
#include<stdio.h>
int main(void)
{
return 0;
}
2.6 -
- ( ). - printf() scanf().
printf()
:
printf( , );
- , , . , , :, ;
, .
- , :
;
;
, .
, :
%d ;
%f ;
%c ;
%s .
:
int i=5;
char symbol=A;
float pi=3.14159;
char *str=Hello!;
printf(%d %c %f %s,i,symbol,pi,str);
scanf()
scanf() () Enter, .
:
scanf(%_,&_>)
, ' &. , & .
:
int a;
float b;
char s1,s2[10];
printf(Enter a: );
scanf(%d,&a);
printf(Enter b: );
scanf(%f,&b);
printf(Enter symbol: );
scanf(%c,&s1);
printf(Enter string: );
scanf(%s,s2);
2.7 Linux
, , :
#include<stdio.h>
int main()
{
printf(This is my first C-program!);
return 0;
}
, , .. ' myprg.c.
, . .
, myprg, :
gcc myprg.c
, .out. , . :
|
|
a.out
':
This is my first C-program!
(.out), . , - , .out.
gcc ' , ', , . ( ' myprg.o). . , , ' . , .
, , , , . , , Enter. return getch(). , , Enter.
2.8
䳿 . .
, , . . . if switch.
if
if
if ()
1;
[ else
2]
, 1 2 - .
. . , , 1, , if else, 2, else , .
Y :
Y=a-x, x<a; Y=a, x=a; Y=a+x, x>a.
if(x<a)
Y=a-x;
else
if(x==a)
Y=a;
else
Y=a+x;
if, . . , X>0 Y Z . . ( ), . if, .
if(X>0)
{
Y=2;
Z=X-1;
}
switch
switch . , switch , if else if.
.
switch ()
{
case 1:
case 1:
...
case N:
[ default: ]
}
- . , .
|
|
.
. , . switch , , . , break ( ), switch.
, default, switch .
Y P.
switch (P)
{
case 1: Y=x+a; break;
case 2: Y=x+a+b; break;
case 3: Y=x+a+b+c; break;
case 4: Y=x+a+b+c+d; break;
default: Y=0;
}
2.9
ij , , . . , , , , . , .
while
while . :
while ()
. ( ) , . , ᒺ .
, 4 56.
int S=0,i=4;
while (i<=56)
{
S+=i;
i+=2;
}
do-while
do-while . .
Do
while ()
. do-while . . , . . .
do-while .
int S=0,i=4;
do
{
S+=i;
i+=2;
}
while (i<=56)
for
while do-while . , for. for , , , . .
for (__;
___;
___)
- .
, 2 38:
int i, S=0;
for(i=2; i<=38;i+=2)
S+=i;
break
break :
switch;
;
break . 2.8.
break if, , break 0.
for(i=0;i<x;i++)
{
if(b==i)
break;
Y=a/(b-i);
}
continue
continue . ³ , . , . while do-while , for , . , b==i, Y , .
for(i=0;i<x;i++)
{
if(b==i)
continue;
Y=a/(b-i);
}
|
|
, , .
( ) ( ).
. .
, .
:
int A[15];
:
int B[5]={8,4,-1,3,5};
. .
int C=B[2]; //C=-1;
- .
int A[15],i,n;
printf( : );
scanf(%d,&n);
printf( %d : \n);
for(i=0;i<n;i++)
scanf(%d,&A[i]);
printf( : \n);
for(i=0;i<n;i++)
printf(%d ,A[i]);
( 7 ).
:
int A[5][7];
:
5 - ;
7 - .
.
int A[15];
int,n,i,j,temp;
for(i=0;i<n;i++)
{
temp=A[i];
for(j=i+1;j<n;j++)
{
if(A[i]>A[j])
{
A[i]=A[j];
A[j]=temp;
temp=A[i];
}
}
}
.
int A[15];
int,n,i,j,temp;
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(A[j]>A[j+1])
{
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
}
2.12
, C, , :
;
;
' .
- , . - . - , , ().
:
[ ] _([ ])
{
;
;
[return];
}
. . , int. , void.
_ - , . .
_ (), - . . _ - '. , void. void, ' return ..
,
y=(2b-z)/k;
.
#include<stdio.h>
float f_y(float, float, float);
void main(void)
{
float y,b,z,k;
printf(\n Enter b: );
scanf(%f,&b);
printf(\n Enter z: );
scanf(%f,&z);
printf(\n Enter k<>0: );
scanf(%f,&k);
y=f_y(b,z,k);
printf(\n y=%f,y);
}
float f_y(float b1, float z1, float k1)
{
float a;
a=(2*b1-z1)/k1;
return a;
}
2.13
- , .
- : n .
, , , .
int factr(int n)
{
int answer;
if(n==1)
return (1);
answer=factr(n-1)*n;
return (answer);
}
. , Quick sort . , ' , . , , , .
if, . , , , . , ' .
2.14
, , : . .
. fopen():
FILE *fopen(const char *filename,const char *mode)
fopen() . , , ' . . :
const char *filename - . .
const char *mode - , .
2
rt | ³ |
wt | . , ' , |
at | ³ |
r+t | ³ |
w+t | . ' , |
a+t | ³ |
, , fopen() .
dat1.txt:
FILE *fp;
fp=fopen(dat1.txt,wt);
if(fp==NULL)
{
printf( );
exit(1);
}
exit() .
exit() , , , . , - , fopen().
exit() . s , .
, 0 - , - .
.
Fgetc()
fgetc() . . EOF.
, .
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main(void0
{
FILE *fp;
char ;
fp=fopen(dat1.txt,rt);
if(fp==NULL)
{
printf( );
exit(1);
}
while((c=fgetc(fp))!=EOF)
printf(%c,c);
fclose(fp);
getch();
}
Fgets()
:
char *fgets(char *str,int num,FILE *stream)
fgets() num-1 stream , str. , , EOF. fgets() , NULL.
.FILE *fp;
char str[256];
fp=fopen(dat1.txt,rt);
if(fp==NULL)
{
printf( );
exit(1);
}
while(fgets(str,256,fp)!=NULL)
puts(str);
fclose(fp);
Fputc()
fputc() , . . . EOF. :
FILE *fp;
char c=A;
fputc(,fp);
Fputs()
fputs() , . EOF
- . , , ' , , ', '. .
. -, ', . . -, , ' . .
:
;
.
', .
:
struct name {
type1 fieldname1;
type2 fieldname2;
typen fieldnamen;
};
:
name - ' ;
typen - - ;
fieldname - .
, - .
:
struct student {
char name[32];
char lastname[32];
int kurs;
int years;
float stypendiya;
};
, . ϳ , :
struct student first_student, second_student;
- , :
struct student {
char name[32];
char lastname[32];
int kurs;
int years;
float stypendiya;
} first_student, second_student;
:
.
->
. , :
first_student.stypendiya=500;
, 6
scanf(%f,&first student);
, :
struct student first_student{
,
,
2,
19,
500,
};
. :
struct student gr1[40];
-, ' :
gr1[i].stypendiya=500;
*(gr1+i).stypendiya=500;
(gr1+i)->stypendiya=500;
, car.dat (, car) oi8413t.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main(void)
{
struct car {
char nomer[15];
char type[15];
char mark[15];
} car_current;
char nomer[ ]=oi8413t;
char work[80];
unsigned int flag=0;
FILE *fp;
if((fp=fopen(car.dat,rt))==NULL)
{
printf((n Cannot open file);
exit(1);
}
while(fgets(work,80,fp)!=NULL)
{
sscanf(work,%s %s %s, car_current.nomer, car_current.type, car_current.mark);
if(strstr(car_current.nomer,nomer)==0)
{
continue;
}
else
{
printf(%s,work);
flag=1;
break;
}
if(flag==0)
{
printf( );
}
fclose(fp);
}
car.dat . ( work) strstr(). strstr() 0, 1.
sscanf() work , , .
1. ?
2. ? .
3. break?
4. continue?
5. return?
6. ?
7. ?
8. , .
9. .
10. . .
3.
3.1.
( ). . , . , . ֳ , .
, , .
, () , .
3.1.1
, :
;
;
;
;
/;
.
, , , -, .
, .
3.1.2
( ) , .
. , - , , , . , , ( ).
, . , ( , ), . .
, . , ( , ), , , . . , - .
. 1
3.1.3
. ϳ , , , . .
. , ( ). ( ).
, , , .
3.2
.
(). , , -: (, , ). , ( ). OS/390 IBM.
. . . , , (, ). (UNIX Windows XP).
. (
Windows 95/98/Me Microsoft,
Consumer Windows), . -.
. . , . , QNX VxWorks.
. , , , - . : , -, . ; Embedded Linux Windows CE.
1. ?
2. . ³ ?
3. ?
4
, . 䳿 .
, , 䳿 . Linux.
4.1
4.1.1 .
, , , (operating system kernel).
- . , , , , , , -. .
, . .
. , , - , .
: ( , , kernel mode) (user mode). , ( , , - ).
, 䳿 .
ϳ . , . , , (system call). , , , .
, , . , .
4.2
. .
4.2.1
, , . ( 䳿 ), ( , ).
, . ( ). ( ).
4.2.2
(, layers), . 䳺 , .
. , . Multics, 60-. .
г ; . :
, , 䳿 ;
, , 䳿 , . , ;
( ), ( , , ). , ;
, .
4.3
, , ( microkernel). ( , ). , , .
̳ . , (볺 , 볺) . ̳ , . 볺. 볺 , .
:
, ;
, ;
( , );
( , ).
. ( - 볺 , ).
, , , . , QNX , .
4.4
ﳿ ( ). ֳ ﳿ ( ) , , .
1. , .
2. ? ?
5. ϳ OC UNIX.
5.1.
, , . 䳺 , ' , , ..
䳿 . . , ' . , , . , .
GNU/UNIX GNU (GNU , GNUs Not UNIX (GNU UNIX)). , ' UNIX. GNU | UNIX, .
UNIX GNU- , UNIX. GNU , . GNU/UNIX.
GCC
, , ' , '. , UNIX-, GNU-, GCC (GNU Compiler Collection). C, C++, Java, Objective-C, Fortran Chill. .
.
gcc. c.
, , prog.c:
$gcc c prog.c
' prog.o.
'
gcc o.
$gcc prog.c o hello
prog.c:
#include<stdio.h>
main()
{
printf(Hello, World!);
}
' prog.c.
³ , gcc.
$ gcc c prog.c
' hello, o.
$ gcc prog.c o hello
:
$./hello
' Hello, world!
argc argv. - , ' .
main() - :
int main(int argc, char *argv[])
int main(int argc, char **argv)
argc , , ' .
argv - ( : argv ).
̳ , , :
char ** char *
cat
file1
file2
cat, file1, file2 , \0.
, argv[0] ( ) ' . . NULL. - . . . , . , , , .
: Hello, ' , ' :
#include<stdio.h>
int main(int argc, char *argv[])
{
if (argc!=2)
{
printf(You forgot to type your name!\n);
return 1;
}
printf(Hello, %s!, argv[1]);
return 0;
}
name, ' Sergey,
Hello, Sergey!
, ,
You forgot to type your name!
1: , :
' ;
, , .
#include<stdio.h>
int main(int argc, char *argv[])
{
int i;
printf("The program name is %s\n", argv[0]);
if(argc>1)
{
printf("The program has %d params.\n",argc-1);
for(i=1; i<argc;i++)
printf("%d param is: %s\n",i,argv[i]);
}
else
printf("The program has not parametrs!");
return 0;}
2: , ( ).
:
. fopen():
FILE *fopen(const char filename, const char mode);
fopen() .
, NULL.
:
onst char filename . ;
const char mode . :
r ;
w ;
.
#include<stdio.h>
main(int argc, char *argv[])
{
FILE *fp;
if(argc>1)
{
if((fp=fopen(argv[1],"r"))==NULL)
{
printf("Cannot opened file %s", argv[1]);
exit(1);
}
else
printf("The file %s is opened",argv[1]);
}
else
printf("Enter file name.");}
3: 1 , stdout.
:
fprintf(), printf(), .
:
int a=87;
fprintf(fp,a=%d,a);
fp - .
#include<stdio.h>
int main(int argc, char *argv[])
{
int i;
printf("The program name is %s\n", argv[0]);
if(argc>1)
{
fprintf(stdout,"The program has %d params.\n",argc-1);
for(i=1; i<