You are on page 1of 103

1: C

?
C

/

035 . . . .

1-1


()


(Compilers)

(Source code)

(Object code)

(Compilers)

C
A.
B. main

C.

C


main

A.
: #include
: #include <header file>
#include <stdio.h>
#include <math.h>

M (library)

: time.h, string.h, signal.h, .
(4 Java)
.

-:
#define, #ifdef, #if, #else, #endif, #undef


stdio,
printf ( )
scanf ( )
/

math

. , ,
C, , :
( - functions)
( - variables).


.
) ) (
) )

vs.
(variable)
(RAM)
.

.. , ,

(constant)
(RAM)


.. kms_per_mile = 1.609 ( 1.609 )


, , , .

1.
2.
3.
4.
5.
6.

, underscores ( _ )

&, #, $

( )


( printf())

035 . . . .

1-14



()

char, int, float, double

: (, )
..
struct time
{
int hours; int minutes; int seconds;

};

15

(Integers)

16

(char)

-Z, a-z, 0-9, !@$%&#, \n
1 byte
()
, a, 9, , , *, \n, \,

a
b
9
*

a
b
9
*

17

ASCII

18

char,
int,
float,
double
*

1byte a..z A..Z0..9


4 bytes
-231..231
4 bytes
10-37..1038
8 bytes
10-307..10308
4 bytes

(0..232 )

*
19

+
*
/
%

num1 + num2
initial - spent
age * 6
sum / count
m%n

20

o
o char, int, oat, double
int int => int

5/2
double double=> double
5.0/2.0
int double => double
5/2.0
double int => double
5.0/2
int char
=> int

5+a

; ;
21

2
2.5
2.5
2.5
102

()

,
.

(x+(y+z)*(x-y)*3)

+- ()

,

x=++y+--z;

* / %

,

3*7*y/x

+ - ()

,

3+x-y



x=y=3
22

/

. () .

, (scanf)

, (printf)
stdio /

24

printf

printf( );
printf( , );

int age= 29;


printf(Your age is %d. Next year you will be %d\n,age,age+1);

25

printf
%d, %c, %f, %e, %s
A

%d
%f
%e
%c
%s

(int)
(double float)
( )
(char)
(string)

26


\n :
\t :
\ :
\\ : \
#include <stdio.h>
int main()
{
printf ("one");
printf ("two\n");
printf ("three\nfou\n");
printf ("five\tsix\n");
printf ("\tseven\n\n");
printf ("nine\tten\n\n\n");
system("pause");
return 0;
}
27

scanf

scanf( , );

int numbera, numberb;


scanf(%d%d,&numbera, &numberb);

28


printf ( %d %f %e %c )
%d, %f %e
23 , 23

29

scanf
: &< >
:
: &numbera
scanf

30

035 . . . .

1-31


,
(1)
(0)

32

(relational operators)

<
>
<=
>=
==
!=

0 () 1 ()
int, char, float, double

33



0 1
0 ( , false)
1 (, true)

&&
||
!



()

34

()
!
* / %
+ < <= >= >
== !=
&&
||

35

if-else

36


if(age>80)
{
print(you are old!);
}
else
{
print(you are swll young!);
}
print(Have a good day!)

if(age>80)
{
printf(you are old!);
}
printf(Have a good day!)

37

switch
switch

case
, case
A case ,
default ( )
switch ()
{
case 1: ; break;
case 2: ; break;
:
default: ;
}

38

035 . . . .

39

while

while
(1)
:

while ()
1;

while () 1;

while ()
{
11;
12;

1;
}

1 ( )

while false (0)
..

while (5>0)
{
printf( \n);
}
40

true
while

false


) Counting Loops ( )

..
int a =0;
while (a<2) { printf(Hello); a = a+1; }

B) Sentinel Loops ( )
. loop
.
..
int a =0;
while (a!=-1)
{
scanf(%d, &a); printf(Hello);
}

41

for
for
C
:
for (1; ; 2)
1;

for (1; ; 2)
{
11;
......
1;
}

1

1
2

42

true
1
2

false

do - while
do - while while

(1)
:
do
1;
while ();

do-while while
1

..
int i=0;
do
{
i++;
printf(%d\n",i);
}
while (i<10);

while

true

43

false

break
break while,
do-while for
#include <stdio.h>
main ( )
{
int i;
for (i = 1; i < 10; i++)
{
if (i == 5)
{
break;
}
printf (%d , i);
}
printf (\nExete vgei apo to loop!!!);
}

:
1234
Exete vgei apo to loop!!!

44

continue
H conwnue for, while, do-while
,
.
#include <stdio.h>
main ( )
{
int i;
for (i = 1; i < 10; i++)
{
if (i == 5)
{
continue;
}
printf (%d, i);
}
printf (\n);
}

A:
12346789

45

035 . . . .

1-46

47

Top-down




..
. -: 1) , 2)
.

C,
, .

.

48

#include <stdio.h>

void PrintMessage (void);

()

( )

main ( )
{
PrintMessage ( );

( )

}

void PrintMessage (void)



{ ( )
print (A message for you:\n\n);

print (Have a Nice Day!\n);

}
49

A)
compiler
.
:


void

PrintMessage (void);

50

B)

,
void PrintMessage (void)
{
printf(Hello);
}
main ( )
{
PrintMessage ( );
}

Hello
51

C)


void PrintMessage (void)
{
printf (A message for you:\n\n);
printf (Have a Nice Day\n);
}
PrintMessage, o
, main ( )

52

C) :

< > < > (< >)


< >:
int, char, float, double, void,

< >:

< >:
(void)
, , .

53

1
#include <stdio.h>
void PrintMessage (int);
main ( )
{
int num;
printf ("Enter an integer: ");
scanf ("%d", &num);
PrintMessage (num);
}

:
Have a nice day
counter

void PrintMessage (int counter)


{
int i;
for (i = 0; i < counter; i++)
{
printf ("Have a nice day\n\n");
}
}
54



oat compute_area(oat a, oat b)
{
oat area;
area = a * b; area
return area; float
}
// :
oat compute_area(oat a, oat b) {
return a * b;
}
55



?
1) H
2)

3)
.
( )

56

#include <stdio.h>
float compute_area(float x, float y);

2 float compute_area(float a, float b)


{

return (a * b);

}
int main()
{
float length, width;
float area;
printf(Enter length and width: );
scanf(%f%f,&length, &width);

area = compute_area(length, width);

3 printf(The area of a rectangle with dim %f by %f m is %f sq.


}

m\n, length, width, area);


return(0);
57


#include <stdio.h>
float AverageTwo (int , int);
main ( )
{
float average;
int num1 = 5, num2 = 8;
average = AverageTwo (num1, num2);
printf (The average of %d and %d is %f\n, num1, num2, average);
}
float AverageTwo (int num1, int num2)
{
float average;
average = (num1 + num2) / 2.0;
return average;
}
58

(scope)

local ()
block {..}
block

global ()

59


#include <stdio.h>
#define d 5 //
int c=5; // (
)
int sum(int x, int y) // x,y
{
int c = 1;
// ( sum )
return x + y + c;
}
int main()
{
int a = 5, b=6; // ( main )
c=11;
printf("%d + %d = %d", a, b, sum(a,b));
}

return 0;

: 5+6=12
60

:
num 1
#include <stdio.h>
void AddOne (int);

void AddOne (int num)


{
num++;
printf (In AddOne: );
printf (num = %d\n, num);
}

main ()
{
int num = 5;
AddOne (num);
printf (In main: );
printf (num = %d\n, num);
}

To :
In AddOne: num=6
In main: num= 5

61

:
num 1
#include <stdio.h>
int AddOne (int);

int AddOne (int num)


{
num++;
printf (In AddOne: );
printf (num = %d\n, num);
return num;
}

main ()
{
int num = 5;
num = AddOne (num);
printf (In main: );
printf (num = %d\n, num);
}

To :
In AddOne: num=6
In main: num= 6
62

035 . . . .

1-63

-

(.. int, char, float,).

.

,
( ).


5
0

2 6
1 2

9
3

3
4

H
0
64

E L L O
1 2 3 4

-
,
,
..

int md[12]= {31,28,31,30,31,30,31,,,31};

md[0]
md[1]
md[2]

( )

md[11]
65

31
28
31

31


int array [5] ;

5 .



.. int array [5] = { 5, 2567, -76,500,64} ;

n 0
(n-1). H 0

array

5
0

2 6
1 2
66

9
3

3
4

n=5


<> [<>];
<> [<>]={};
, .
char a[3]; ()
char a[ ]; ()



.. char pinakas[ ]={a, b, c}; ()

67

()

(memory cells).

int a[2]={9,5};

9
int a=9,b=5;

68



.
. , ..
int a; a = 0;
0,
:
int a[10]={ };


for (i=0;i<10;i++) { a[i] =0; }

int a[10]; a[0]=0; a[1]=0;.
,
0,
69

: []
(subscript index) int
x[0] x
x[i] i- x
i- i+1


[0, -1]
int a[5]={1,2,3,4,5}; a[5]=3;
: a[5]
: a[0] a[4]

70


int x[ ]={1,2,3,5,7,11,13,17};

;

x[3] = 8258;
sum = x[0] + x[1];
x[3] += 1;
x[2] = x[0] + x[1]+1;

O :
int x[]={1,2,4,8259,7,11,13,17};
71


;

x[i] = 0;
x[i] = x[j];
x[j+k*4]= x[u] + 3;
x[x[1]]= 11;
diff = x[y]-x[foo()];
total =total + x[i++];
(x[i]== x[(int)f])

x:

5
0

2 6
1 2

9
3

3
4

int foo()
:
total=total+x[i]; i++;

72

(Linear Search)

student_id z.

.
STUDENT_NUM.

, i .

73

(Linear Search)
;

student_id

0 12345

74

1 37349
2 9995
...
54 20001




,
return

75


?

( ),

=>

, . :
(call by reference)

76




:
void FillArray ( int array[ ], int size);
( )
void FillArray ( int [ ], int);

FillArray (pin, megethos);

77


FillArray
pin

#include <stdio.h>
#define SIZE 4
//
void FillArray (int[ ], int) ;

//
void FillArray(int array[ ], int L)
{
int i;
for ( i = 0; i < L; i++)

array [i] = i;
}
}

main ( ) {

int pin [SIZE]={-1,-1,-1,-1};


//

FillArray ( pin, SIZE );

int i;

for(i=0; i<SIZE; i++)

printf(%d ,pin[i]);

A
array[0] = 0
array[1] = 1
array[2] = 2
78

array[3] = 3


#include <stdio.h>
void dummy(int k, int p[])
{
k=7;
p[5]=7;
}
main()
{
int i,A[10]={};
i=10;
A[5]=10;
dummy(i,A);
printf("%d\n",i);
printf("%d\n",A[5]);
system("pause");
}

10
7

79


C
char array[3][7] /*[][]*/
0

0
1
2

3*7 = 21



A
.. array[0][0]=, array[2][3]=
array[3][2]= [3][2]
0

0
1
2

80



-
?
,
,



(
(int, char, float)?

81

0
1
2

2.30

4.30

(.)

,

int studentGrades[100][6] =


( 100)
6
)

{99, 76, 88 , 74, 65, 53 },


{67, 71, 77 , 71, 80, 47 },

};

FOR loop
for(i=0;i<100;i++)
//
for(j=0;j<6; j++)
//
studentGrades[i][j]=0;

82

035 . . . .

1-83


String
NULL \0
Hello\0, Hi there\0, \0
( \0 )
String C,
string
) ( String)
char msg[ ]=Hello;

\0

84

string

char msg[ ]=Hello;

char msg[6];
msg[0] = 'H';
msg[1] = 'e';
msg[2] = 'l';
msg[3] = 'l';
msg[4] = 'o';
msg[5] = '\0';

char msg[ ]={H,e,l,l,o};

char msg[ ]={H,e,l,l,o,\0};

char msg[6]={H,e,l,l,o,\0};

char msg[40]=Hello;

char msg[5]={'H','e','l','l','o','\0'};

\0. String
85

char string

char c = H;
H
?

char c[ ]=H;
string H
0

\0

char c=H;
86

/
scanf (%s, name)
: &, name
printf (%s, name)

87

/ string
5 string
#include <stdio.h>
int main()
{
char name[20];
scanf("%s", name);
name[5]=;
name[6]=\0;
printf("%s", name);
return 0;
}

19

\0

19

\0

88

Strings
String
\0
Strings?
( , , )


char names[NUM_STUDENTS][NAME_LEN];
char weekdays[7][10]={Monday, Tuesday, Wednesday, Thursday,
Friday, Saturday, Sunday};
0

\0

\0
?

\0
?

\0

\0

89

string

:
strings
string

strings
string

<String.h>

90

string.h
(header file), string.h,
strings

strlen(s), string
strcpy(s1,s2), s2 s1
strcat(s1,s2), s2 s1
strcmp(s1,s2), s1 s2 s1
() s2, ,
s1 s2 ( ASCII)

91

035 . . . .

1-92





C, o
<stdio.h>

(File System)

( filesystem Windows FAT32
NTFS)
(
)

93


bytes
.. 01100011 01100001 01110010
ascii:99
ascii:97
ascii:114
c
a
r

Car
Test

Car\nTest\thelloEOF

hello
94

ascii: -1


ASCII
GIF
(Graphics Interchange Format),
folder Windows

( )
bytes (
) 4716=10001112
95



<stdio.h>

C ( )

96

(
)

FILE *, stdio.h
..
#include <stdio.h>
int main ()
{
FILE *arxeio;
// arxeio

//

}

: , , , , ...

,
97


fopen
FILE *fopen(char *filename, char *mode);
filename

mode (.. read, write, read-write)


FILE
main()
{
FILE *fp;
fp = fopen("myfile.txt", "r");
.
}

98


H mode

Mode

A. , NULL

E. ,

. ,

r+

. ,
NULL

w+

.
,

a+

. ,

99


fclose
fclose(FILE *fp);

main()
{
FILE *fp;
fp = fopen("myle.txt", "r");
fclose(fp);
.
}

100

/ /
Hello


#include <stdio.h>
int main()
Append mode
{
FILE *fp;



fp = fopen("myle.txt", "a"); //
fprint(fp, "Hello");
//
fclose(fp);


//
return 0;
}
fprint, fclose
101

/ /
To


#include <stdio.h>
main() {
FILE *fp;
// Try to open the le
if ((fp = fopen("myle.txt", a")) == NULL)
{
print("Error opening le\n");
exit(1);
}
// Write to the le Hello 5 6
fprint(fp, "Hello%d %d, 5,6);
fclose(fp);
}
102

/ + x 2 /
#include <stdio.h>
main() { FILE *fp;
int a;
if ((fp = fopen("myle.txt", r")) == NULL) {

print("Error opening le\n"); exit(1);
}
fscanf(fp, %d, &a);
printf(%d, a);

fscanf(fp, %d, &a);

printf(%d, a);



fclose(fp);

}
103

You might also like