Professional Documents
Culture Documents
2016
vii
C C + + . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
. . . . . . . . . . . vii
ix
1 C , 1
1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 . . . . . . . . . . . . . . . . . . . . . 1
1.3 printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4.1 sizeof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5.1 (increment)(decrement) . . . . . . . . . 5
1.6 scanf . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.7.1 #define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2 13
2.1 () (<,>, <=, >=, ==, !=) . . . . . . . . . . . 13
2.2 'if' 'if ... else' . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.3 switch ... case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.4.1 for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.4.2 break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.4.3 while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.4.4 continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.5 () enum . . . . . . . . . . . . . . . . . . . . . . . 28
3 , , , 31
3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
i
3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.2.1 NULL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.4 . . . . . . . . . . . . . . . . . . . . . . . . 40
3.5 . . . . . . . . . . . . . . . . 42
3.6 extern "C" . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.7 C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.8 C . . . . . . . . . . . . . . . . . . . . . . . 47
4 51
4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.1.1 . . . . . . . . . . . . . . . . 51
4.1.2 C99 . . . . . . . . . . . . . . . . . . . . . . . 52
4.1.3 . . . . . . . . . . . . . . . . . . . . . . . . 52
4.1.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4.1.5 : . . . . . . . . . . . . . . . . . . . . . . . . 56
4.1.6 : . . . . . . . . . . . . . . . . 57
4.1.7 : . . . . . . . . . . . . . . 57
4.1.8 : . . . . . . . . . . . . . . . . . . . . . . . 58
4.1.9 : . . . . . . . . . . . . . . . . . . . 59
4.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
4.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
4.3.1 : . . . . . . . . . . . . . 63
4.3.2 : . . . . . . . . . . . . . . . . . . . . . . . . . 65
4.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
4.4.1 . . . . . . . . . . . . . . 66
4.4.2 : . . . . . . . . . . . . . 67
4.4.3 2D . . . . . . . . . . . . . . . . . . . . . . . 68
5 71
5.1 . . . . . . . . . . . . . . . . . . . . 71
5.2 . . . . . . . . . . . . . . . . . . . . . . . 72
5.3 . static . . . . . . . . . . 73
5.4 volatile . . . . . . . . . . . . . . . . . . . . . . . . . . 76
5.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
5.6 void . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
5.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
5.7.1 malloc() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
5.7.2 free() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
5.7.3 realloc() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
5.7.4 calloc() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
6 : struct union 83
6.1 struct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
6.2 . . . . . . . . . . . . . . . . . . . . . . 86
6.3 : . . . . . . . . . . . . . . . . . . . . 86
6.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
6.5 . . . . . . . . . . . . . . . . . . . . . . . 89
6.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
6.7 typedef struct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
6.8 union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
7 97
7.1 AND, OR, XOR NOT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
7.2 . . . . . . . . . . . . . . . . . . 98
7.3 . . . . . . . . . . . . . . . . . . . . . . . . 99
7.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
7.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
8 103
8.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
8.1.1 / . . . . . . . . . . . . . . . . . . . . . 104
8.1.2 getc() putc() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
8.1.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
8.1.4 fscanf() fprintf() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
115
C
, -
.
, -
. .
C++
, C .
-
.
, C . ,
.
.
, -
C ,
1 .
, -
.
, .
, .
, 2 .
2016.
1
, -
http://lshoshia.science.tsu.ge. ,
shoshia at hotmail dot com
2
,
.
C C++
Unix ,
.
. -
. -
. C.
C++ 80- AT&T . . -
, C++ C ,
. C++ , -
.cpp, .C .cc . , C++ -
C++ .
C++ C , C++ C -
. C C++- .
-. - C C++ . C -
, gcc C++ -
.
C++
C++ , -
, C C++ -
, . C
, . gtk , Gnome
, Gimp ,
: gsl , gts -
.
C FORTRAN ,
FORTRAN . C++
, -
, , -
. C++ C -
.
C++ C + . -
C
SmallTalk Simula . , , Pascal,
vii
C, . -
. -
, .
, .
. . -
, .
.
, -
-
. , C++
, -
.
viii
1.1 printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 . sizeof . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 printf scanf. . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 scanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.6 float double . . . . . . . . . . . . . . . . . . 7
2.1 printf . . . . . . . . . . . . . . . . . . . . 17
2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.3 switch case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.5 n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.1 . . . . . . . . . . . . . . . . . . 33
3.2 . . . . . . . . . . . . . . . . . . . . 35
3.3 . . . . . . . . . . . . . . . . . . . . . 37
3.4 const . . . . . . . . . . . . . . . . . . . . . . . 38
3.5 . . . . . . . . . . . . . . . . . . . . . . . . . 40
3.6 myheader.h . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.7 myheader.c . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.8 main.cpp . . . . . . . . . . . . . . . 45
5.1 testglobal.cpp . . . . . . . . . . . . . . 73
5.2 util.h . . . . . . . . . . . . . . . . . . . 74
5.3 util.h . c c++ 74
5.4 util.c . . . . . . . . . . . . . . . . . . . 74
5.5 void void . . . . . . . . . . . . . . . . . . . . . . 78
8.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
8.2 / . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
ix
1
C ,
1.1
C A,B . . . Z
0,1,2. . . 9
+,-,=. . . <
;,. . . `;
C : , , , .
C . . Object object .
,
. :
int x=4;
int x2=4;
int 2x=4;
: ,, .
, () -
. , -
. , .
N , {2N /2, 2N /2 1} .. (unsigned) -
{0, 2N 1}
1.2
, 1 . :
bool a;
int i;
unsigned int j; ( ) 2 .
float x;
double t;
char character;
,
1
, ,
. " static . -
.. "
.71.
2
1
1. C ,
.
, -
, .
, -
.
, -
.
-
. :
bool a=true;
int i=0;
float x=0.0;
double t=0.0;
char character='\0'; . .. .
1.3 printf
1.1 printf
\t .
\a .
\\ Backslash.
\" Double quote.
\' (single quote) .
\? .
\b (backspace) 1 .
\f
\n
\r
\v .
\xdd s .
printf() -
. :
printf("Testing...\n..1\n...2\n....3\n");
2
1.4. printf
1.4 printf
:
6 value1 = 50; //
7 value2 = 25;
8 sum = value1 + value2 ;//
9 //
10 // output
11 printf ("%d+%d= %d\n", value1 , value2 , sum );
12
13 return 0;
14 }
%d. . -
, % printf
. printf :
) ,
) %, , ,
%. %- "-
, ..
) ( ) -
, ,
%.
) " " (
)
3
1. C ,
"%" , "d" , .. . -
. d int .
. %d
%i printf , scanf
:
) %d .
) %i , -
"0x" "0".
1.2 . sizeof
4
1.5. C
32
33 return 0;
34 }
[1-16] .14- ..
. , .
. [17-30] -
. sizeof .
1
long , long long , short , unsigned , signed .
1.5 C
: +,-,*,/ .. %. ,
"c=a%b;" c a/b. -
, .
. . 2 (6/4=1). -
:
/, . -
()-, ()
. 1+3+4-3-7+4 ((((1+3)+4)-3)-7)+4 +
- .
1.5.1 (increment)(decrement)
(++) (- - ) -
. int a=3;int q=4; ++a;--q; a ,b
. -
. /
,
/ .
:
int a, b, c =0;
a=++c;
b=c++;
printf( %d, %d, %d,a,b,++c);
1
,
2
.
5
1. C ,
1.6 scanf
1.3 printf scanf.
"%d",
, , float
"%f".
-
. -
.
1.4 scanf
1.5
6
1.6. scanf
%g %e double . float
double . 1
7
1. C ,
30 printf ("dx=%.3g\n", dx );
31 printf ("dx=%.6g\n", dx );
32 printf ("dx= %.12g \n", dx);
33 printf ("dx= %.13g \n", dx);
34 printf ("\n-------------------------\n");
35
1.1: printf
printf puts
. printf -
8
1.6. scanf
puts .
printf("Hello.\n");
puts("Hello.");
1. , :
C .
main , .
'', '' .
';'.
2. ?
3. , 123 23 .
4.
1
"\0"-
9
1. C ,
5. ?
6. , -
(. 1.1)
"%g" float double
. "%e" "%f" , .
1.7
#include <stdio.h> -
stdio.h -
( <>). #include "filename.h". ,
filename.h, -
( , ) -
. #include "../filename.h" #include
"dirname/filename.h". ,
, 'dirname' -
.
1.7.1 #define
#define ,
#include . #define .
#define R 100
#define PI 3.14159
( )
, . , # -
.
PI PI
. #define ..
, ,
.. :
10
1.7.
shemoitanet radiusi: 2
12.5663706144 ,
%.2f
, %.5f . -
. "3.119455" .
11
2
, -
/ . -
, .
, ; " Shift -
....." .. ,
, . if,if
else, switch.
, .. .
1 if('') '1';
'' '1'; -
.
1 if('')
2 {
3 '1';
4 '2';
5 'N';
6 }
13
2.
1 if('')
2 {
3 '1';
4 '2';
5 ....
6 'N';
7 }
8 else
9 {
10 '11 ';
11 '12 ';
12 ....
13 '1N';
14 }
'' :
{
'1';
'2';
....
'N';
}
''
{
'11';
'12';
....
'1N';
}
. :
14
2.2. 'if' 'if ... else'
if ... else :
if ... else
if( )
{
if( 1 )
{
_1;
}
else
{
_2;
}
}
else
{
_3;
}
15
2.
9 if( a > c)
10 {
11 printf ("a is greatest ");
12 }
13 else
14 {
15 printf ("c is greatest ");
16 }
17 }
18 else
19 {
20 if( b> c)
21 {
22 printf ("b is greatest ");
23 }
24 else
25 {
26 printf ("c is greatest ");
27 }
28 }
29 return 0;
30 }
if .. else if :
if(piroba 1)
{
statement-block1;
}
else if(piroba 2)
{
statement-block2;
}
else if(piroba 3 )
{
statement-block3;
}
else
default-statement;
'piroba n' .
'else' 'default-statement'. if
:
16
2.2. 'if' 'if ... else'
3 {
4 int a;
5 printf (" shemoitane ricxvi ");
6 scanf ("%d",&a);
7 if( a%5 ==0 && a%8 ==0)
8 {
9 printf (" iyofa 5 ze da 8-ze");
10 }
11 else if( a%8 ==0 )
12 {
13 printf (" iyofa 8-ze");
14 }
15 else if(a%5 ==0)
16 {
17 printf (" iyofa 5-ze");
18 }
19 else
20 {
21 printf ("ar iyofa arc 5-ze arc 8-ze");
22 }
23 return 0;
24 }
2.1 printf
17 if ( num1 == num2 ) {
18 printf ( "%d is equal to %d\n", num1, num2 );
19 } // end if
20
21 if ( num1 != num2 ) {
17
2.
1 switch (n) {
2 case constant1 :
3 brdazneba romelic sruldeba roca n= constant1 ;
4 break ;
5 case constant2 :
6 brdazneba romelic sruldeba roca n= constant2 ;
7 break ;
8 .
9 .
10 .
11 default :
12 brdazneba romelic sruldeba roca n ar akmayofilebs
13 arc ert zemot agceril pirobas ;
14 }
n . n 'constant'
( ). break,
18
2.3. switch ... case
, switch . case -
break case : n=constant.
case break.
: :
2.2
1 # include <stdio.h >
2 int main(void) {
3 char o;
4 float num1,num2 ;
5 printf (" Select an operator either + or - or * or / \n");
6 scanf ("%c",&o);
7 printf (" Enter two operands : ");
8 scanf ("%f%f",&num1, &num2 );
9 switch (o) {
10 case '+':
11 printf ("%.1f + %.1f = %.1f" ,num1, num2, num1+num2 );
12 break ;
13 case '-':
14 printf ("%.1f - %.1f = %.1f" ,num1, num2, num1 -num2 );
15 break ;
19
2.
16 case '*':
17 printf ("%.1f * %.1f = %.1f" ,num1, num2, num1*num2 );
18 break ;
19 case '/':
20 printf ("%.1f / %.1f = %.1f" ,num1, num2, num1/num2 );
21 break ;
22 default :
23 /* If operator is other than +, -, * or /, */
24 /* error message is shown */
25 printf (" Error ! operator is not correct ");
26 break ;
27 }
28 return 0;
29 }
switch .
1 // case_ test.cpp : Defines the entry point for the console application.
2 //
3 // switch case test programm case_ test.c
4
20
2.3. switch ... case
35 }
36 return 0;
37 }
:
?
:
1. .
2. .
3. (
273 )
4. .
:
2.4
2.4 11
:
, -
21
2.
.
:
, . -
. , , .
:
shemotanilia X cami.
X cami = A dge, B saati, C suti, D cami
2.4
, -
. . -
, ( : ,
, , , ...)
. .
2.4.1 for
for
for(; ;)
{
;
}
.
' ;' ''
.
.
n
2.5 n
1 # include <stdio.h >
2 int main(void ){
3 int n, count, sum =0;
4 printf (" shemoitanet n mnishvneloba. \n");
5 scanf ("%d",&n);
6 for( count =1; count <=n;++ count ) // for cikli ckdeba rocaf count >n
7 {
8 sum += count ; /* es igivea rac sum=sum+ count */
9 }
10 printf (" %d ricxvis jami=%d" ,n,sum );
11 return 0;
12 }
2.6
22
2.4.
:
N
, (19) (19):
printf, for '\t', '\n'
:
, :
2.7
1 # include <stdio.h >
2 int main ()
3 {
4 int n,i;
5 printf (" shemoitanet mteli dadebiti ricxvi : ");
6 scanf ("%d",&n);
7 printf ("%d -s gamyofebia : ", n);
8 for(i=1;i<=n;++i)
9 {
10 if(n%i ==0)
11 printf ("%d ",i);
12 }
13 return 0;
14 }
:
, :
2.8
1 # include <stdio.h >
2 int main ()
3 {
4 int num1, num2, min,i ;
23
2.
if ..
2.9
1 # include <stdio.h >
2 int main ()
3 { int i,j;
4 for(i=1;i <5;i++)
5 { printf ("\n");
6 for(j=i;j >0;j--)
7 {
8 printf (" %d",j);
9 }
10 }
11 return 0;
12 }
for , -
, . ,
break.
2.4.2 break
break switch . -
if, switch . break ,
(if, switch, for, while)
.
24
2.4.
6 int i,n;
7 printf (" Maximum no. of inputs \n");
8 scanf ("%d",&n);
9 for(i=1;i <=n;++i){
10 printf (" Enter n%d: ",i);
11 scanf ("%f",&num );
12 if(num <0.0)
13 break ; // for loop breaks if num <0 .0
14 sum=sum+num;
15 }
16 average =sum /(i -1);
17 printf (" Average =%.2f" ,average );
18 return 0;
19 }
i=1.
:
i = 1
2.4.3 while
while
while( )
{
;
}
25
2.
26
2.4.
while do..while, :
do
{
;
}while( )
.
2.4.4 continue
continue. break
continue .
27
2.
2.5 () enum
:
. , myColor,
: red, green, blue -
( enumerated type) .
enum.
:
enum MainColors {red, green, blue};
MainColors
red, green blue.
enum MainColors .
MainColors . -
. :
enum Month{Jan,Feb,Mar,Apr,May,Jun...};
enum WeekDay {Sun,Mon,Wed,Thr...};1
enum Direction {Left,Right,Up,Down};
?
(Jan,Feb,Mar...) ,
0, 1, 2 ..
day 1;
, :
enum Direction {Left,Right,Up=11,Down};
:Left=0,Right=1,Up=11,Down=12.
28
2.5. () enum
29
2.
30
3
, , ,
3.1
:
10 scanf ("%lf",&x);
11 printf ("x axali mnishvneloba =%lf\n",x );
12 printf ("x misamarti =%d atobitshi \n",&x );
13 printf ("x misamarti =%x teqvsmetobitshi \n",&x );
14
15 return 0;
16
17 }
,
x . -
, ( )
( ,
.. )
.
(-
.
.. () ( -
) ).
31
3. , , ,
3.2
, . -
, ,
. , -
(
) ,
- . :
(px) , px . &(px)
, px . -
1 .
-
.
3.2.1 NULL
. NULL . -
NULL 0. , -
, . ..
:
1 int main(void)
2 {
3 int *p = NULL;
1
void ,
. .
32
3.2.
4 if (p == NULL) {
5 printf ("p is uninitialized !\n");
6 } else {
7 printf ("p points to %d\n", *p);
8 }
9 return 0;
10 }
, -
NULL.
,
:
3.1
33
3. , , ,
33 if(px!= NULL)
34 printf ("px araa NULL e.i. utitebs \n" );
35 printf (" mnishvneloba romelzec utitebs px=%lf\n",(*px ));
36 printf (" misam. romelzec utitebs px=%x \n",&(* px) );
37 return 0;
38 }
3.3
: main, scanf, printf. -
. .. (main) C -
. .
C .
"-
". "" ,
. -
.
:2.4 .21 -
.
:
.
(
void , int ) (
) . , -
.. main . ,
main 1 :
main main
int solve_sqeq(double _a, double _b, double _c)2 .
, main -
( , ),
.
34
3.3.
. -
, : ,
?
, ?
,
, . -
1, 2, 0.
. :
3.2
35
3. , , ,
10 }
11 else if(d==0)
12 {
13 printf ("D=0 aqvs 1 amonaxseni \n");
14 _x1=-_b /(2*_ a);
15 _x2=_x1;
16 printf ("x1=x2=%g\n",_x1 );
17 return 1;
18 }
19 printf ("aris 2 fesvi :\n");
20 _x1=( -_b-sqrt(d ))/(2*_ a);
21 _x2=( -_b+sqrt(d ))/(2*_ a);
22 printf ("x1=%g :\n x2=%g\n",_x1,_x2);
23 return 2;
24 }
25 int main(int argc, char* argv [])
26 {
27 double a=1;
28 double b=2;
29 double c=4;
30 double x1 =0;
31 double x2 =0;
32 int result = -1;
33 puts(" shemoitanet kv. gant. koeficientebi : a, b, c");
34 scanf (" %lf%lf%lf ",&a,&b,&c);
35 result = solve _sqeq( a,b,c, x1,x2 );
36 return 0;
37 }
, .
.
, -
. ,
. x1,x2
,
, x1,x2 .
.
3.3 . 37 ,
. a,b,c -
.
36
3.3.
3.3
. -
.
. ,
, ()
, ,
37
3. , , ,
(, ).
,
const.
const -
( . const
' '(read only) ).
:
3.4 const
38
3.3.
) x1,x2.
) x1,x2 .
) result .
) a,b,c .
33 return 2;
34 }
35 int main(int argc, char* argv [])
36 {
37 double a=1;
38 double b=2;
39 double c=4;
39
3. , , ,
40 double x1 =0;
41 double x2 =0;
42 int result = -1;
43 puts(" shemoitanet kv. gant. koeficientebi : a, b, c");
44 scanf (" %lf%lf%lf ",&a,&b,&c);
45 result = solve _sqeq( a,b,c, x1,x2 );
46 switch ( result )
47 {
48 case 0:
49 printf ("D <0 ar aqvs amoxsna \n");
50 break ;
51 case 1:
52 printf ("D<EPS=%g aqvs 1 fesvi \n",EPS );
53 printf ("x1=%g :\n x2=%g\n", x1, x2);
54 break ;
55 case 2:
56 printf ("D>EPS=%g aqvs 2 fesvi \n",EPS );
57 printf ("x1=%g :\n x2=%g\n", x1, x2);
58 break ;
59 default :
60 break ;
61 }
62
63 return 0;
64 }
:
1. 2.2 .19 . pow -
.
2. :
3.
, ( . -
) -
.. , .
EPS .
( ) -
DEBUG . switch
....
3.4
.
, , -
, .. main , -
.
40
3.4.
3.5
C -
. 3.4 ,
C++ . (-
3.2), ( 3.4) -
41
3. , , ,
( 3.5) C++-, C-
. C- .
( ), -
(
). C++-
. 3.1
(. 33) C++-. C 9, 25, 32
.
3.5
(. main )
, . C/C++ -
( ) .
".c, .cpp, .cc, .cxx" , ".h,
.hpp, .hh" 1 . "stdio.h"
"math.h" . . -
(.c, .cpp) . ,
.h , -
" " 2 . ".h" , ,
.
3.5 -
3
myheader.c, myheader.h :
3.6 myheader.h
1 // file myheader.h
2 # ifndef MYHEADER ___H
3 # define MYHEADER ___H
4 # include <math.h >
5 # include <stdio.h >
6 # include <stdlib.h >
7 # ifdef __ cplusplus
8 extern "C" {
9 # endif
10 int solve _sqeq( const double * _a, const double * _b,
11 const double * _c, double * _x1, double * _x2 );
12 # ifdef __ cplusplus
13 }
14 # endif
15 # endif
16 };
1
".h" "header" ()
2
3
: http://lshoshia.science.tsu.ge.
42
3.5.
) '.c' C
) .. C / C++
. ,
C- ( '.c' ( -
) '.lib' '.a' 1 ) ( -
, , ) C++ .
s '.h' . 7-12
C ( ) -
C++ . extern , (
, , ) ,
("linking") . -
.
, ""
("unresolved external symbol")2 . 2
3 , myheader.c
myheader.obj ( myheader.o - -
)
. -
.
.
#include "myheader.h" . -
"" . , MYHEADER_H
. -3 MYHEADER_H ( )
myheader.obj . .
#include "myheader.h", ""
myheader.obj , -2 , MYHEADER_H -
( 1) . 2-3 -
, "" myheader.obj
1
Windows '.lib' Linux Unix
'.a'
2
extern "C" .
43
3. , , ,
. -
(error: "Multiple defined symbols"). 12-
13 #endif -2 #ifndef ( If not defined
), -7 .
myheader.c
3.7 myheader.c
1 // file myheader.c
2 # include <math.h >
3 # include <stdio.h >
4 # define EPS 0 .0000001
5 # include " myheader.h "
6 int solve _sqeq( const double * _a, const double * _b,
7 const double * _c, double * _x1, double * _x2)
8 {
9 const double & bb =_c;// es mushaobs C++ magram ara C-shi
10 //C-shi ar gvaqvs reference -- misamarTis agebis operacia.
11 // misamarti shenaxulia pointershi -- mimtitebelshi
12 double d=(*_b )*(*_ b) -4*(*_a )*(*_ c);
13 if(d <0){
14 # ifdef _ DEBUG
15 printf ("D <0 ar aqvs amoxsna \n");
16 # endif
17 return 0;
18 }
19 else if(d<EPS)
20 {
21 (*_ x1 )= -(*_b )/(2*(*_ a));
22 _x2 =_x1;
23 # ifdef _ DEBUG
24 printf ("D<EPS=%g aqvs 1 fesvi \n",EPS );
25 printf ("x1=x2=%g\n",*_x1);
26 # endif
27 return 1;
28 }
29 *_x1 =( -(*_b)-sqrt(d ))/(2*(*_ a));
30 *_x2 =( -(*_b)+ sqrt(d ))/(2*(*_ a));
31 # ifdef _ DEBUG
32 printf ("D>EPS=%g aqvs 2 fesvi \n",EPS );
33 printf ("x1=%g :\n x2=%g\n",*_ x1, *_x2);
34 # endif
35 return 2;
36 };
. -
44
3.5.
#include myheader.h.
.
main
3.8 main.cpp .
1 // file main.cpp
2 # include " myheader.h "
3 # define EPS 0 .0000001
4 int main(int argc, char* argv [])
5 {
6 double a=1;
7 double b=2;
8 double c=4;
9 double x1 =0;
10 double x2 =0;
11 int result = -1;
12 puts(" shemoitanet kv. gant. koeficientebi : a, b, c");
13 scanf (" %lf%lf%lf ",&a,&b,&c);
14 result =solve _sqeq (&a,&b,&c,&x1,&x2);
15 switch ( result )
16 {
17 case 0:
18 printf ("D <0 ar aqvs amoxsna \n");
19 break ;
20 case 1:
21 printf ("D<EPS=%g aqvs 1 fesvi \n",EPS );
22 printf ("x1=%g :\n x2=%g\n", x1, x2);
23 break ;
24 case 2:
25 printf ("D>EPS=%g aqvs 2 fesvi \n",EPS );
26 printf ("x1=%g :\n x2=%g\n", x1, x2);
27 break ;
28 default :
29 break ;
30 }
31 return 0;
32 }
, -
( myheader.h ) ( -
myheader.c ). #include myheader.h.
#include -
, myheader.h .
.
45
3. , , ,
1 extern "C" {
2 int f1(int );
3 int f2(int );
4 int f3(int );
5 };
, f1, f2, f3 .
extern "C" C++ C++ -
C-.
1 extern "C" {
2 void p(int ){
3 /* not mangled */
4 }
5 };
: http://en.wikipedia.org/wiki/Name mangling
3.7 C
C . .cpp
.c , -
C . GNU gcc C
gcc , C++ g++.
C myheader.h -
#ifdef __cplusplus extern "C" .
46
3.8. C
: ) C
) C C++
.
C .h
#endif -
( error: unxepected end of file).
#endif , , Just hit Enter.
3.8 C
, C++ C . ,
/ C++
C C++ ( -
C++ 2003 -
http://stlport.sourceforge.net/,
C 2013 C C89. Microsoft VS 2013
C99 .) GNU (
AVRStudio Amtel ) -
C89 C99 ) .
C "K&R C"( C), -
C 1972-1989 . C
-
/.
1983 (ANSI) C -
. 1989 C ANSI X3.159-1989
"Programming Language C". C89 ANSI-C.
1990 ANSI-C C
(ISO 9899:1990 C90) C90
C89 . 1990-1999 C C90.
1999 C (ISO 9899:1999 C99). 1999-
2011 C C99.
( VS2013 )
2011 ( , -
)ISO 9899:2011.
C11. http://en.wikipedia.org/wiki/C11 (C standard revision)
, ( -
) C
C89 "K&R C" .
, C99 Microsoft VS
C++ . , Microsoft VS 2013 -
.
, C (C99 ), -
GNU gcc Microsoft VS C ,
47
3. , , ,
, C++ .
C -
C++ .
C89 C99
// C99 .
C89 31 . C99 63.
C99 :inline, restrict,_Bool, _Imaginery, _Complex
C89 main
. C99 main int 0 -
.
C99 C89:
long long int unsigned long long int const . C99 -
const float .
. -
, .
: (-
). ';'.
. '{'
'}'. C89 -
'{' . C99 ( C++)
. :
1 {
2 int x=1;
3 printf (" Hello \n");
4 }
C++ C (C89,C90) -
C89 :
1 {
2 printf (" Hello \n");
3 int x=1;
4 }
1 int n=10;
2 for(int i=0;i<n;i++)
3 {
4 .....
5 }
48
3.8. C
1 int n=10;
2 int i;
3 for( i=0;i<n;i++)
4 {
5 .....
6 }
C89 .
, int. C99( C++) -
.
C99( C++) , -
. C89 .
C89 , int .
K&R C C89 :
K&R C enum , unsigned int,
long long,const,void .. C89- -
.
K&R C , -
. C89:
K&R:
C :
K&R C -
.
, .
49
3. , , ,
: if, else, switch, case, break, default, for, do, while, continue, goto (11).
: return, void (2)
: struct, enum, union (3)
: auto, register, extern, const, volatile, static (6).
ISO C99 5 , 37:
Bool, Complex, Imaginary, inline, restrict (5).
ISO C11 7 , of 44:
Alignas, Alignof, Atomic, Generic, Noreturn, Static assert, Thread local (7).
: C- C89 .
C89 . C++
C++. C++ ( C++
), C99. GNU gcc -
C99 C89 C++-
1 . Visual Studio 2013 C99 C++11 2 . . 3
C99 . C99
. http://en.wikipedia.org/wiki/C99
1
https://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/C-Extensions.html
2
http://www.drdobbs.com/cpp/interview-with-herb-sutter/231900562
3
http://blogs.msdn.com/b/vcblog/archive/2013/07/19/c99- library-support-in-visual-studio-2013.aspx
50
4
4.1
C- . :
type name[dim];
C- ( ).
.. int
i adr_i, i+1 -
adr_i2=adr_i+sizeof(int). int age[5];,
:
51
4.
:
1. C -
( 'Watch' )
8 return 0;
9 }
-
. -
0.
4.1.2 C99
2010 ( C
C++), 2013 C99
GNU gcc . C99 -
, , . :
int a[6] = { [4] = 29, [2] = 15 }; int a[6] = { 0, 0, 15, 0, 29, 0 };(
) :
int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };( -
) . =
. :
struct point { int x, y; };
struct point p = { .y = yvalue, .x = xvalue };( 2010
C C++. / 2013)
struct point p = { xvalue, yvalue };
-
, :
struct point ptarray[10] = { [2].y = yv2, [2].x = xv2, [0].x = xv0 };
: , 2, y
yv2, xxv2, x xv0.
2013.
4.1.3
. :
: scanf("%d",&age[2]);
i+1 ( -
52
4.1.
( ) age[0] ): scanf("%d",&age[i]);
:printf("%d",age[0]);
i+1 :printf("%d",age[i]);
:
-
char line[100];
1 main( ) {
2 int n, c;
3 char line [100];
4 n = 0;
5 while ( (c= getchar ( )) != '\n' ) {
6 if( n < 100 )
7 line[n] = c;
8 n++;
9 }
10 printf (" length = %d\n", n);
11 }
4.1.4
C , ,
.
53
4.
1 # define SIZE 3
2
3 void main ()
4 {
5 float x[SIZE ];
6 float *p;
7 int i;
8 /* initialize the array x */
9 /* use a "cast" to force i */
10 /* into the equivalent float */
11 for (i = 0; i < SIZE; i++)
12 x[i] = 0.5*( float )i;
13 /* print x */
14 for (i = 0; i < SIZE; i++)
15 printf (" %d %f \n", i, x[i]);
16 /* make fp point to array x */
17 fp = x;
18 /* print via pointer arithmetic */
19 /* members of x are adjacent to */
20 /* each other in memory */
21 /* *( fp+i) refers to content of */
22 /* memory location (fp+i) or x[i] */
23 for (i = 0; i < SIZE; i++)
24 printf (" %d %f \n", i, *(fp+i));
25 }
x[i] x i- , fp = x x ,
*(fp+i) , i -
, fp, x[i].
, -
. 10 arr[10]. -
arr[0] arr[9] .
arr[10], arr[13] .. .
, .
, unsigned int
54
4.1.
7 int i;
8 x[n /2]=10 .0;
9 x[2]=4 .0;
10 x[7]=3 .5;
11 x[8]=x[2]+x[7];
12 for(i=0;i<SIZE;i++)
13 {
14 printf ("x[%d ]= %lf\n",i,x[i]);
15 }
16 return 0;
17 }
55
4.
.
4.1.5 :
, -
. : .. -
10 . .
.
1. 20.
2. , 10 11 .
3. -3 -4 110 .
4. 6-12 20 .
5. -8 . [1-10]
(-11 )
(-9 )
6. [13-16] , -
. .
:
, 10
, , .. 1,2,3. . . , -
1,2,3. . . .. 0 .
56
4.1.
4.1.6 :
'*' .
14-20 . -
. '*'
.
:
4.1.7 :
-
. . , .
6 . 6,000,000 . -
.
57
4.
4.1.8 :
:Fi = Fi1 + Fi2 . -
0 1.
58
4.1.
:
. . .
:
? ?
4.1.9 :
-
(, ,)
21 }
22 while ( numberToConvert != 0 );
23 // display the results in reverse order
24 printf (" Converted number = ");
25 for (-- index ; index >= 0; --index ) {
26 nextDigit = convertedNumber [ index ];
27 printf ("%c", baseDigits [ nextDigit ]);
28 }
29 printf ("\n");
30 return 0;
31 }
const, , .
( const
, . -
.
, -
)
59
4.
:
.
: convertedNumber ?
64 convertedNumber[64], long int? 25- ?
25- --index? ? nextDigit
? ? , -
> 16- .
4.2
printf . -
. C++
std::string. C- . ( string) -
. , , C
string.
char string1[] = "first"; , +
.. '0'.
char string1[] = { 'f', 'i', 'r', 's', 't', '\0'};
, char string2[] = { 'f', 'i', 'r', 's', 't'}; string1.
C string string1 string2. '0' ,
C- , -
. C
C- -
string.h. scanf -
: char string2[ 20 ];
scanf( "%19s", string2 );
scanf . %19. , 19 -
. . ,
, -
. 20 ,
19 , -20 .. '\0' . -
'&'
, C- . scanf
string2- 19 , ,
. scanf . -
. string
: printf( "%s\n", string2 ); -
/string
. 19 . -20 string1
( )" %c ".
: '\0' . -
, 16-17 , -
60
4.2.
.
.
15 // output strings
16 printf ( " string1 is: %s\ nstring2 is: %s\n"
17 " string1 with spaces between characters is :\n",
18 string1, string2 );
19 // output characters until null character is reached
20 for ( i = 0; i < SIZE && string1 [ i ] != '\0 '; ++i ) {
21 printf ( "%c ", string1 [ i ] );
22 } // end for
23 puts( "" );
24 } // end main
,
( ).
61
4.
18 i=0;
19 while ( (text _1[i] = message [i]) != '\0 ' )
20 i++;
21 printf ("Text _1: %s\n", text _1);
22
4.3
-
( ) . :
, .
:
62
4.3.
, .
. .
(
),
4.3.1 :
-
.
.
6. , -
,
float average(float a[], int N); 6 N .
, -
. , -
,
. , -
.
63
4.
9 {
10 int a[ SIZE ] = { 0, 1, 2, 3, 4 }; // initialize array a
11 size_t i; // counter
12 puts( " Effects of passing entire array by reference :\n\nThe "
13 " values of the original array are:" );
14 // output original array
15 for ( i = 0; i < SIZE; ++i ) {
16 printf ( "%3d", a[ i ] );
17 } // end for
18 puts( "" );
19 // pass array a to modifyArray by reference
20 modifyArray ( a, SIZE );
21
32
33 // output value of a[ 3 ]
34 printf ( "The value of a[ 3 ] is %d\n", a[ 3 ] );
35 } // end main
36 // in function modifyArray, "b" points to the original array "a"
37 // in memory
38 void modifyArray ( int b[], size_t size )
39 {
40 size_t j; // counter
41
64
4.3.
. C ,
. const
, ,
.
:
,
. -
, .
4.3.2 :
.. Buble Sort .
1 // Fig. 6.15: fig06 _15 .c
2 // Sorting an array 's values into ascending order.
3 # include <stdio.h >
4 # define SIZE 10
5 // function main begins program execution
6 int main( void )
7 {
8 // initialize a
9 int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
10 int pass; // passes counter
11 size_t i; // comparisons counter
12 int hold; // temporary location used to swap array elements
13 puts( "Data items in original order " );
14 // output original array
15 for ( i = 0; i < SIZE; ++i ) {
16 printf ( "%4d", a[ i ] );
17 } // end for
18
19 // bubble sort
20 // loop to control number of passes
21 for ( pass = 1; pass < SIZE; ++ pass ) {
22
65
4.
:
1. ,
.
2. , .
3. , .
4. :
10 , , , -
:
, -
.
4.4
C -
. : float a[2][6]; a .
:
. 1 . 2 . 3 . 4 . 5 . 6
. 1 a[0][0] a[0][1] a[0][2] a[0][3] a[0][4] a[0][5]
. 2 a[1][0] a[1][1] a[1][2] a[1][3] a[1][4] a[1][5]
, , -
. -
. ,
4.4.1
C :
int c[2][3]={{1,3,0}, {-1,5,9}}; (2,3) -
(3 ) -.
int c[][3]={{1,3,0}, {-1,5,9}}; -
( , ).
int c[2][3]={1,3,0,-1,5,9}; -
.
int myPoints[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9} };
int myPoints[9][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9}};
:
66
4.4.
: -
, .
. arr[i][j][k][m] i*j*k*m.
4.4.2 :
-
,
.
67
4.
4.4.3 2D
,
68
4.4.
9 }
10
11 int main ()
12 {
13 int arr [][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
14 print (arr, 3);
15 return 0;
16 }
( -
) .
:
1) C99
C99 , C -
. C99 -
.
C99 -
-. 2) .
-
.
.. / . . printf -
, arr int :
69
4.
10 int main ()
11 {
12 int arr [][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
13 int m = 3, n = 3;
14 print (( int *) arr, m, n);
15 return 0;
16 }
3)
.
, C ,
, .
1 -
. 2) 3) C99
2D malloc() .
70
5
5.1
int a=3; , int -
a , 3. /
, .
auto,
. ?
?
, .
, {...} . -
. -
. :
, -
. a b -
, a b .
, -
. ,
71
5.
( ) . -
. . -9
c=7; .
, a b main()
, ,
, ,
.
5.2
main() ? -
a b main() ,
{...} ( ) .
:
9 int main ()
10 {
11
'18'. a b .
. -
(static storage
duration)
(automatic storage duration). -
.
72
5.3. . static
5.3 . static
static ,
. -
.
, -
() . -
( linking) -
, . -
.
.
-
, . , -
( ) ()
extern. static
. :
5.1 testglobal.cpp .
1 int d=5;
2 static int e=10;
3 #include <stdio.h >
4 # include " util.h "
5 extern int b;
6 int main ()
7 {
8 b=8;
9 a=7;
10 // printf ("\ na*b=%d * %d =%d" ,a,b,a *b);
11 {
12 int a=5;
13 int b=6;
14 int c=8;
15 printf ("\ nqveblokshi a*b=%d * %d =%d" ,a,b,a *b);
16 printf ("\ nqveblokshi c=%d",c);
17 }
18 // printf ("\ nisev gare bloki b=%d",b );
19 puts("\n");
20 printf ("\n%d\n" ,mult _and_add (3,5 ));
21 puts("\n");
22 printf ("\n%d\n" ,mult _and_add (3,5 ));
23 return 0;
24 }
util.h util.c .
73
5.
5.2 util.h .
1 # ifndef TEST_UTIL_H
2 # define TEST_UTIL_H
3 static int a=3;
4 int mult_and_add(int _x, int _y);
5 # endif
, C++ ( C++) -
:
1 # ifndef TEST_UTIL_H
2 # define TEST_UTIL_H
3 static int a=3;
4 # ifdef __ cplusplus
5 extern "C" {
6 # endif
7
10 # ifdef __ cplusplus
11 }
12 # endif
13 # endif
5.4 util.c .
1 int b=4;
2 # include " util.h "
3 extern int d;
4 int mult_and_add(int _x, int _y)
5 {
6 static int lb =0;
7 ++lb;
8 d=8;
9 b=14;
10 a+=1;
11 return _x*_y+a+lb;
12 }
. testglobal.c
int d=5; static int e=10; , d
74
5.3. . static
( ), e -
, . -5 extern int b;
, int b -
, . extern ,
. -
.
main() b (-8 ).
e . -9 -
a . ? a ,
-12 , : ) 11-17
, -
. ) -9 a
. ,
. , a -
.
extern int b; ?
util.h. int mult_and_add(int _x, int _y);
a static . -
? util.c. -9 , a
. a -
, .
, -
static . static -
,
, util.h
.
util.c int b=4; , -
extern int b; .
extern int d;
util.c .
-6 -
static int lb=0; ,
,
, ,
.
.
,
,
( ).
, , .
75
5.
(. -
). , -
, (
). ( -
),
.. , -
.
( ,
), (
..
)
. -
(-
extern ,
)
, , -
,
-
static
extern ()
.
extern , static
. -
.
register. -
'register int c;'. register ,
. -
. -
register .
5.4 volatile
volatile , -
/. -
, 1
(const). -
,
.
76
5.5.
5.5
. :
1 int main(void)
2 {
3 int a = 5;
4 int b = 10;
5 float f;
6 f = a / b; /* calculate 5 divided by 10 */
7 printf ("%.2f\n", f);
8 return 0;
9 }
0; , int
. .
, ?
, , float double .
-6 :
f =(float) a / b;
5.6 void
( -
)
void. void void , -
, void . void ,
, void , -
. , void
. , / -
.. .
void ? , void , -
, (type casting).
/ . void -
? (linked list). ,
. ,
. .
. ,
,
. , ,
.
void ( 5.5 .78). -2 void
10 . index -
77
5.
: ,
.
13 int main(void)
14 {
15 char *s = "some data!"; /* s points to a constant */
16 int a = 10;
17 int *b;
18 char *s2; /* when we call get_ pointer (), we 'll store */
19 int *b2;
20 b = &a; /* b is a pointer to a */
21 /* now let 's store them both, even though they 're */
22 append _ pointer (s);
23 append _ pointer (b);
24 /* they 're stored ! let 's get them back! */
25 s2 =( char *) get_ pointer (0); /* this was at index 0 */
26 b2 =( int *) get_ pointer (1); /* this was at index 1 */
27 printf ("\n b2 arsebuli nishvneloba *b2=%d",*b2 );
28 printf ("\n s2 masivia s2 []= %s\n",s2 );
29 return 0;
30 }
-6 index++ ,
index index 25- -2
void 10 .
index : ,
. -
6 index++ , index
index 25- 26-
( ), .
78
5.7.
5.7
(-
) -
. ,
( )
(, , -
.).
C : malloc()
. free() malloc() -
. realloc() calloc() malloc()
.
5.7.1 malloc()
malloc() . -
NULL. void* -
.
malloc() ,
, (. 12 int ), -
sizeof() ,
. :
1 int *p;
2 p = malloc ( sizeof (int) * 12); // gamoyavi mexsiereba 12 int_tvis
? -
NULL-
1 int *p;
2 p = malloc ( sizeof ( float ) * 6400);
3 if (p == NULL) {
4 printf ("ver moxerxda mexsierebis allokacia !\n");
5 exit (1);
6 }
1 int *p;
2
: C . -
?
79
5.
malloc()
. free()
5.7.2 free()
, malloc()
calloc()
1 int *p;
2 p = malloc ( sizeof (int) * 37); // 37 ints!
3 free(p); // on second thought, never mind!
malloc() free() -
( int 37 )
-
(. , ) .
5.7.3 realloc()
, malloc() calloc() -
.
100 , 200 . -
, realloc()
, -
.
:
80
5.7.
5, 23 -
.
. 5 .
, malloc() realloc()
( ) ( ,
). .
realloc() .
() . -
, .
5 -
. 20 .
5.7.4 calloc()
calloc() realloc() :
1.
2.
p = malloc(10 * sizeof(int));
p = calloc(10, sizeof(int));
, .
81
6
: struct union
C : struct -
union
6.1 struct
C .
, C
. (
void , -
). ,
. ,
? 100 1000? - -
. ,
6 , 6 . 6
, 6 .., .
C struct .
.
1 struct Employee
2 {
3 char m_name [50];
4 int m_ birthDateDay ;
5 int m_ birthDateMonth ;
6 int m_ birthDateYear ;
7 int m_ Salary ;
8 };
Employee(), char 50
name int ( , ,
)
struct , , '{' '}'
Employee .
. -
. const
. .
.
.
83
6. : struct union
5 struct SEmployee
6 {
7 char m_Name [50];
8 int m_ BirthDateDay ;
9 int m_ BirthDateMonth ;
10 int m_ BirthDateYear ;
11 int m_ Salary ;
12 };
13
14
15 int main ()
16 {
17 struct SEmployee X;
18
X , Employee -
. X : m_Name, m_BirthDateYear, m_BirthDateMonth,
m_BirthDateDay, m_Salary.
.
-
. , .
: X.m_Salary ,
X Employee m_Salary, -
, ,
..
:
84
6.1. struct
12 void main ()
13 {
14 struct Employee X, Y, Z;
15 strcpy (X.m_ Name, " Ahmed Said");
16 X.m_ BirthDateDay = 22;
17 X.m_ BirthDateMonth = 12;
18 X.m_ BirthDateYear = 1980;
19 X.m_ Salary = 50;
20 printf ("X contains (%s, %d/%d/%d, %d )\r\n", X.m_ Name,
21 X.m_ BirthDateDay, X.m_ BirthDateMonth,
22 X.m_ BirthDateYear, X.m_ Salary );
23 strcpy (Y.m_ Name, X.m_Name );
24 Y.m_ BirthDateDay = X.m_ BirthDateDay ;
25 Y.m_ BirthDateMonth = X.m_ BirthDateMonth ;
26 Y.m_ BirthDateYear = X.m_ BirthDateYear ;
27 Y.m_ Salary = X.m_ Salary ;
28 printf ("Y contains (%s, %d/%d/%d, %d )\r\n", Y.m_ Name,
29 Y.m_ BirthDateDay, Y.m_ BirthDateMonth,
30 Y.m_ BirthDateYear, Y.m_ Salary );
31
32 Z = X;
33
16-19 .
20- X . 24-27 Y- X -
85
6. : struct union
: Y X
. 28- Y .
X . 32-
Z X '='
. , -
. . 34- Z .
struct Employee .
6.2
.
1 void main ()
2 {
3 struct Employee X = {" Ahmed Said", 22, 12, 1980, 500};
4 printf ("X contains (%s, %d/%d/%d, %d )\r\n", X.m_Name,
5 X.m_ BirthDateDay, X.m_ BirthDateMonth,
6 X.m_ BirthDateYear, X.m_ Salary );
7 }
6.3 :
struct Employee 100 .
.
:
5 struct Employee
6 {
7 char m_Name [50];
8 int m_ BirthDateDay ;
9 int m_ BirthDateMonth ;
10 int m_ BirthDateYear ;
11 int m_ Salary ;
12 };
13 int main ()
14 {
15 struct Employee employees [100] , tempEmployee ;
16 int count = 0, i, j;
17 char firstName [50], secondName [50];
18
19 do
20 {
86
6.3. :
45 for(i=0;i<count -1;i++)
46 {
47 for(j=i+1;j<count ;j++)
48 {
49 if(
50 employees [i].m_ BirthDateYear > employees [j].m_ BirthDateYear ||
51 ( employees [i].m_ BirthDateYear == employees [j].m_ BirthDateYear &&
52 employees [i].m_ BirthDateMonth > employees [j].m_ BirthDateMonth )||
53 ( employees [i].m_ BirthDateYear == employees [j].m_ BirthDateYear &&
54 employees [i].m_ BirthDateMonth == employees [j].m_ BirthDateMonth
55 && employees [i].m_ BirthDateDay > employees [j].m_ BirthDateDay ))
56 {
57 tempEmployee = employees [i];
58 employees [i] = employees [j];
59 employees [j] = tempEmployee ;
60 }
61 }
62 }
63
87
6. : struct union
employees 100 . -
.
, i -
. 57-59-
. -
'=' (j i
).
strcat
strcat
6.4
,
.
, . , -
TDate .
-
, , ,
.. :
1 struct TDate
2 { int m_Day;
3 int m_Month ;
4 int m_Year;
5 };
struct Employee :
1 struct Employee
2 { char m_Name [50];
3 struct TDate m_ BirthDate ;
4 struct TDate m_ GraduationDate ;
5 int m_ Salary ;
6 };
88
6.5.
X.m_GraduationDate.m_Year , X
m_GraduationDate m_Year-
:
6.5
, : -
, , ,
.
5 struct TDate
6 {
7 int m_Day;
8 int m_ Month ;
9 int m_Year;
10 };
11
12 struct Employee
13 {
14 char m_Name [50];
15 struct TDate m_ GraduationDate ;
16 struct TDate m_ BirthDate ;
17 int m_ Salary ;
18 };
19 struct Date ReadDate (char dateName [])
20 {
21 struct TDate date;
22 printf (" Enter %s (day/ month /year) example (23/3/1970): ",
23 dateName );
24 scanf ("%d/%d/%d", & date.m _Day, & date.m _ Month,
25 & date.m _Year );
89
6. : struct union
26 return date;
27 }
28 struct Employee ReadEmploee ()
29 {
30 struct Employee employee ;
31 char firstName [50], secondName [50];
32
50 return employee ;
51 }
52 void PrintEmploee ( struct Employee emploee )
53 {
54 printf ("%s\t%d/%d/%d\t%d/%d/%d\t%d\r\n",
55 emploee.m _ Name,
56 emploee.m _ BirthDate.m _ Year,
57 emploee.m _ BirthDate.m _ Month,
58 emploee.m _ BirthDate.m _Day,
59 emploee.m _ GraduationDate.m _ Year,
60 emploee.m _ GraduationDate.m _ Month,
61 emploee.m _ GraduationDate.m _Day,
62 emploee.m _ Salary );
63 }
64 void main ()
65 {
66 struct Employee X = ReadEmploee ();
67 PrintEmploee (X);
68 }
90
6.5.
14 return -1;
15 }
16
17 void main ()
18 {
19 struct Date X = ReadDate ("X Date");
20 struct Date Y = ReadDate ("Y Date");
21 switch ( CompareDates (X, Y))
22 {
23 case -1: printf ("X date < Y date\r\n"); break ;
24 case 0: printf ("X date = Y date\r\n"); break ;
25 case 1: printf ("X date > Y date\r\n"); break ;
26 }
27 }
:
.
:
enum GENDER .
, -
.
:
. -
, ,-, . , -
.
91
6. : struct union
6.6
, -
. ,
.
. -
, -
:
14 void main ()
15 {
16 struct Employee X = {" Ahmed Said", {22, 12, 1990} ,
17 {2, 7, 1970} , 5000};
18 PrintEmploee (&X);
19 }
. -
. -
.
.
, -
'->'
, -
PrintEmploee(X)
50 + 3 4 + 3 4 + 4 = 78 .
(PrintEmploee(&X)) ,
, , 4
.
92
6.7. typedef struct
1 void main ()
2 {
3 t_ Employee X = {" Ahmed Said", {22, 12, 1990} ,
4 {2, 7, 1970} , 5000};
5 PrintEmploee (&X);
6 }
typedef
1 typedef struct {
2 int a;
3 float b;
4 } someData ;
someData X;
6.8 union
uninon -
. . , char
double . .
sizeof(char)+sizeof(double).
, -
sizeof(double) .
.
6.1:
,
,
93
6. : struct union
. -
. -
.
( ) .
UNUMValue sizeof(double),
( ) -
-.
:
16 switch (type)
17 {
18 case INT: result.u _ intValue = value1.u _ intValue +
19 value1.u _ intValue ; break ;
20
27 return result ;
28 }
29 void main ()
30 {
31 union UNUMValue V1, V2, R;
32
94
6.8. union
:
. , -
double , int float
.
:
-
. , ,
(enum) ( , ,
) -
.
:
-
. ,
, . -
.
95
7
z&=x|y;
z=z&(x|y);
, : &, |, ~, -
: (AND) &&, (OR)||, (NOT) !.
.
7.1:
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
8 (1 ) :
97
7.
z=x&y:
0011 0111
0001 1001 &
0001 0001 /* result is 17 (dec) = 0x11 (hex) */
& (AND) z x y
.
.
z=x|y z 63:
0011 0111
0001 1001 |
0011 1111 /* result is 63 (dec) = 0x3f (hex) */
| (OR) z x y
.
.
. (XOR) z=x^y z 46:
0011 0111
0001 1001 ^
0001 1110 /* result is 46 (dec) = 0x2e (hex) */
^ (XOR) z x y
.
( ).
~( ) 1 0- 0- 1-
.
. z=~x:
0011 0111 ~
1100 1000 /* result is 200 (dec) = 0xc8 (hex) */
7.2
<< >>
. x<<n x
n .
.
1 char x = 0x19; /* Equals 0001 1001 binary (25 decimal ). */
2 char y = x << 2; /* Equals 0110 0100 binary (100 decimal ). */
-
.
. x
x>>n -
. x
. -
.. ,
98
7.3.
. ,
. :
45, -19. , -
, -
.
, 2n , -
2n .
, -
. , :
x * 2 x << 1
x / 16 x >> 4
x % 8 x & 7
,
. -
2n /
.
7.3
( ) -
, ~ , -
(precendence) ( )
!.
<, >, &, | ^ . &
^, |.
&& ||.
, , -
, .
7.4
:
1. ..
(.. (flags) -
0 1, .
, , ..)
2. .. (hardware) (-
, ),
( ) .
99
7.
.
( -
/) ( ).
masking( ??)
- ( - bit-mask) . (mask) -
, -
1 ( )
1 enum {
2 FIRST = 0x01, /* 0001 binary */
3 SECND = 0x02, /* 0010 binary */
4 THIRD = 0x04, /* 0100 binary */
5 FORTH = 0x08, /* 1000 binary */
6 ALL = 0x0f /* 1111 binary */
7 };
. -
. ALL, ( mask)
, .
-
.
1 enum {
2 FIRST = 1 << 0,
3 SECND = 1 << 1,
4 THIRD = 1 << 2,
5 FORTH = 1 << 3,
6 ALL = ~(~0 << 4)
7 };
:
. ~0, -
. , ,
:
1111 1111 /* ~0 */
1111 0000 /* ~0 << 4 4 */
0000 1111 /* ~(~0 << 4) */
, 4 .
( masks) /-
/ , ..
:
1 unsigned flags = 0;
2 flags |= SECND | THIRD | FORTH ; /*(set) 2,3,4 (1110) .*/
1
enum struct union-
100
7.5.
1. | ( , mask) -
~ (
), , , -
.
2. , -
.
3. |= &= -
. flags
( ).
4. , ^= -
( ). -
.
5.
-
. ()
& .
7.5
C .. ,
.. . -
-
.
:
1 struct {
2 unsigned first : 1;
3 unsigned secnd : 1;
4 unsigned third : 1;
5 unsigned forth : 1;
6 } flags ;
flags, -
.
. , . ,
/ :
101
7.
.
C -
,
. ,
( Implementation dependent)
. ,
.
16 , 32. -
( ).
.. ( ).
,
.
, .
,
. 1
1
http://www.snippets.org
102
8
ssd , . -
.
. -
. C .
. C.
C- -
8.1
-
.
, .
, -
.
,
/ .
8.1:
.
.
. "\n" C-. -
103
8.
, -
.
. . OSX
"\r"(Enter ), MS-DOS "\r\n" "Ctrl-
Z", , "Ctrl-Z" -
, 256 .
MS-DOS ,
.
, , -
.
.
, , C -
: .
. -
. -
C . C ,
"\r" "\n" "\n" "\r" . MS-
DOS C "\r \n" "\n" -
"\n" "\r \n".
-
. MS-DOS "-
" "\r" "\n"
. , , -
, -
/ .
, C ,
C .
C : -
(stdin) (stdout) -
(stderror). -
.
. .
getchar() scanf(). -
putchar(),puts() printf().
8.1.1 /
:
104
8.1.
28 return 0;
29 }
stdio.h . -8
. -
( , ,
, // ..)
-9 . -
argc .
, , argv -
( ).
.
. . -
. -
.
-
.
argc=2 2 (
, ). -10
.
105
8.
8.2:
106
8.1.
"r"
"w" . .
"a" .
"r+" .
"w+" . .
. .
"a+" ( ),
,
.
"ab+", "a+b", ,
"wb+","w+b", "ab+" .
"a+b","rb","wb", "ab"
8.1: .
. fopen ( -
).
fclose() . fclose() 0.
8.1.2 getc() putc()
getchar() putchar() , -
. putc()
putc(ch,pFile), c char pFile (
stdout, stderr);
8.1.3
-
.
8.1
1 int ch;
2 FILE * pFile ;
3 pFile = fopen ("test.txt", "r");
4 while (ch!= EOF)
5 {
6 ch=getc( pFile );
7 putchar (ch );
8 }
107
8.
1 int ch;
2 FILE * pFile ;
3 ch=getc(pFIle );
4 pFile = fopen ("test.txt", "r");
5 while (ch != EOF)
6 {
7 ch=getc(pFile );
8 putchar (ch);
9 }
1 int ch;
2 FILE * pFile ;
3 ch=getc(pFIle );
4 pFile = fopen ("test.txt", "r");
5 while ((ch=getc( pFile )!= EOF)
6 {
7 putchar (ch);
8 }
7 int main(void)
8 {
9 FILE *fp;
10 char words [MAX ];
11
108
8.1.
31 return 0;
32 }
-5 . -
. -
/, .
, 401 (
'\0' words).
-14 printf puts . fprintf -
.
-14 (
). .
-20 . fscanf
. scanf . 40
.. . ,
# , -
. , .
, . ; .. .ini , // C/C++ %
-20 .
24- rewind("") -
.
25-26 : -
28-29 : .
1 , -
. :
109
8.
8.2 /
110
8.1.
46 {
47 printf ("Can 't open %s\n", filename1 );
48 exit( EXIT_FAILURE );
49 }
50
111
8.
:
53- : -" ( -
-" " -
). .
. fscanf -
. read
2 . 54- . 113- .
. . -
. /
. ,
112
8.1.
. .
, , . -
. , , ,
.. (
. ),
, . -
rewind
.
fscanf .
:
1. .
2. .
3. .
,
, -.
. .. .
. -
char (,
). / ( ) -
.
113
case, 1820, 26, 27, 29, 40, 45, 91, 94 if, 1318, 21, 2325, 29, 32, 33, 3539,
char, 1, 2, 4, 6, 7, 1921, 3133, 3539, 41, 44, 53, 56, 65, 67, 79, 81, 87,
41, 45, 5153, 55, 56, 5862, 78, 91, 101, 102, 105, 108112
8386, 8890, 93, 9799, 105, 108, int, 14, 6, 7, 911, 1417, 1926, 28, 31
110, 113 42, 4446, 48, 49, 5181, 8386,
const, 38, 39, 41, 42, 44, 49, 59, 65, 68, 88, 89, 91, 9395, 97, 105, 107,
continue, 27
L
long, 4, 49, 59, 60, 97, 105
D
default, 1820, 27, 29, 40, 45 M
do, 59, 86 main, 24, 6, 7, 911, 1417, 1926, 28, 31
double, 1, 2, 4, 6, 7, 11, 21, 3142, 44, 45, 41, 45, 5259, 6163, 65, 6773,
49, 55, 67, 77, 9395, 110112 75, 77, 78, 80, 8486, 9094, 105,
108, 110
E
malloc, 70, 7981
else, 1417, 33, 3639, 41, 44, 56
enum, 28, 29, 49, 91, 94, 95, 100, 102 N
EOF, 26, 105, 107, 108 NULL, 3234, 58, 79, 105, 108
extern, 42, 43, 46, 7376
P
F printf, 24, 611, 1425, 2729, 3141,
FILE, 105, 107, 108, 110 44, 45, 48, 5373, 7779, 8487,
float, 1, 2, 4, 6, 7, 1921, 24, 54, 63, 66, 8992, 94, 95, 105, 108112
67, 77, 79, 9395 putc, 105, 107
fopen, 105108, 110, 111 putchar, 106108
115
R T
realloc, 7981 true, 2
register, 76 typedef, 93
return, 3, 5, 711, 1417, 1925, 29, 31
41, 44, 45, 49, 52, 53, 55, 56, 58, U
59, 62, 63, 6774, 77, 78, 80, 84, union, 83, 94, 100
8891, 94, 105, 106, 109, 112 unsigned, 1, 4, 25, 26, 49, 58, 97, 100102,
S 105
116