baekseok university

12
#include < #include <stdio.h stdio.h> void void main(void) main(void) { int int i, j; , j; int int x; x; printf printf(" ("뇊ꯍꌱ 뇊ꯍꌱ 넺ꇚ뼍겑꿙 넺ꇚ뼍겑꿙 : "); : "); scanf scanf("%d", &x); ("%d", &x); while(x != 1) { while(x != 1) { printf printf("%d ("%d ", x); ", x); School of Information and Com if(x % 2 == 0) if(x % 2 == 0) x /= 2; x /= 2; else else x = x*3 + 1; x = x*3 + 1; } printf printf("1 ("1\n"); n"); } BaekSeok University mmunication

Upload: others

Post on 27-Dec-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BaekSeok University

#include <#include <stdio.hstdio.h>>

voidvoid main(void)main(void){{

intint ii, j;, j;intint x;x;

printfprintf("("���������������� : ");: ");

scanfscanf("%d", &x);("%d", &x);

while(x != 1) {while(x != 1) {printfprintf("%d("%d ", x);", x);

Sch

oo

l of In

form

atio

n a

nd

Co

mm

un

icatio

n

printfprintf("%d("%d ", x);", x);if(x % 2 == 0)if(x % 2 == 0)

x /= 2;x /= 2;elseelse

x = x*3 + 1;x = x*3 + 1;}}printfprintf("1("1\\n");n");

}}

BaekSeok University

Sch

oo

l of In

form

atio

n a

nd

Co

mm

un

icatio

n

Page 2: BaekSeok University

C��������

��� ���

auto extern sizeofbreak float staticcase for structchar goto switchconst if typedefcontinue int uniondefault long unsigneddo register voiddouble return volatileelse short whileenum signed

Note��������� ��� ����� ��������� ���� �� ����� ! "#(unsigned) char 8-bit 0 .. 255

signed char 8-bit -128 .. 127

(signed) short (int) 16-bit -32768 .. 32767

unsigned short (int) 16-bit 0 .. 65535

•$%&'()(case sensitive)•&*+,-

;�./

, 012345+&*� ;�()�36�� ��.0&*�3412789�:;�� ��.

•����-

main()��29<=/�0�.

•{ }-����+0����+>?��.

•@�ABCD-��E � 0�. (printf, scanf, getchar, putchar, gets, puts, etc…)•.F��EGH�I

header file�

include�3� 0�.

•�����+ �-JKLMN

,ON, _E � �3PQ����, R'� �+S&'�� ����.

•��T���UV @�A:B�WX�. @�A:B���YZ2� void

E� 0�.

��(Visual

C++��)

���(��)

��

unsigned short (int) 16-bit 0 .. 65535

(signed) int 32-bit -2147483648 .. 2147483647

unsigned (int) 32-bit 0 .. 4294967295

(signed) long (int) 32-bit -2147483648 .. 2147483647

unsigned long (int) 32-bit 0 .. 4294967295

float 32-bit 3.4E-38 .. 3.4E+38

double 64-bit 1.7E-308 .. 1.7E+308

long double 64-bit 1.7E-308 .. 1.7E+308

C����C+[\

(]^)-

/*� /�]9 */

�,_�. �`-3412789[\�a���.

/* 3b2[\�>�.3412789[\� a� ��. */

C++����C++

+[\(]^)

-/* … */

�cP� 01[\//� ��

.

// 3b2[\� >�.// �deA01�[\��.

��(constant):���������� ��������� ���. const������ ����� �, !"#�$%��&'.

����&'f���-gh.�i9 �:_�. j, ‘a’, ‘B’, ‘\n’k l�m�. C29&'f��Q�<nop0�.

����qrs/(\ or \)

� /��IGt&'�uWPv�&'E �:;w� 0�.

Page 3: BaekSeok University

����

�����

�����

xy z {| !}\a 0x07 BEL Bell

\b 0x08 BS Backspace

\f 0x0C FF Formfeed

\n 0x0A LF Newline(linefeed)

\r 0x0D CR Carriage return

\t 0x09 HT Tab(horizontal)

\v 0x0B VT Tab(vertical)

\\ 0x5C \ Backslash

\’ 0x27 ‘ Single quote

\” 0x22 “ Double quote

\? 0x3F ? Question mark

\0ooo Any 3~� 8��\xHH Any ��~ 16��\XHH Any ��~ 16��

����&'�f���gh.�i9 �:_�. [+

: &'�+,�./�b?]NULL(0)

&'W�W��.

“�`-&'���

.”

���enum

����I,�9WQ]X��+@�AE��(

��)��

, ������W���. Q��op�tQ����W���.��

��

��

Q��op�tQ����W���.

enum weekdays {SUN=0, MON, TUE, WED, THU, FRI, SAT};enum weekdays today=SUN; /*

��� ��2���f� SUN$B */

� �: 345+��0@�A:B+��E��type array_name[��� ��];

int array_one[7]; /* 75+Q�EWX���� */

����int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30 , 31, 30, 31}; /*

Q]X�b�^Q */double average[10] = {0, }; /*

U  ���0��b¡ */

int score[] = {7, 5, 9, 8, 4, 8, 8}; /* ��5�'� PQ */

���float m[50][20]; /* ¢bW 50*20£ 2¤¥���� */

��:����¦e29@�A§*, ¨�/©§��0ª� .

type variable_name[=«¬­];

int int_variable; /* Q� ����

*/double floating_variable; /* ®� ���� */

int max, min, sum=0; /* ����

,�b��¯°0YZ�b�^Q */

Page 4: BaekSeok University

��

!"�

#$%

��� �type *pointer_name;

Note±£A�� �b²2 ¨�/�b¡�3³0��int *p = &a;orint *p;p = &a; 3b9±£A´� ��µE W¶·�� ¸´ ¹ º»¼I �½µ��+��ºW���Null(�) ���¾±£A��=¿Tl�� ����.

int *pn = 0; /* ¾±£A�� */

int *pn = NULL; /* �Tl���]���. */

���� �����struct tag { ÀÁ Â� };

struct Dot {int x, y;}; //��

struct Dot p = {1, 1}; //�b�Q+ Note'b'Ã+(ÄÅEÆÇ�� ����� >'b'Ã+(Äű£A�WÈ� ���

�����: �( �) ����*+ �,-����*+�((ÉÊËÌÍ_

ËÎ_ÏÐÑÒ) ÉÊÓ_ÏÐÑÔÕ;

��.�� = (int)®����; ®����=(double)Q����

;

� ��� : @�A�(:B

)��Ö34@�AW× �t.Ø��YZ �Ù�W�0@�A�-'�Ú� @�A.ØÛ?W�Ü� �0�.

&'%

()*�

struct Dot p = {1, 1}; //�b�Q+

���!"#$%struct Dot *ptr = p; //

(ÄÅp2 $0±£A ptr

��p.x = 3; //

ÆÇx+ÝÞßàá

ptr->x = 4; // ÆÇ

x+âÞßàá

ptr->x�

(*ptr).xTl�.

&'� �union tag { ÀÁ Â� };

&'�!"#$%union Spot {int x, y;} p;p.x = 4;Display(p.x); //

Úã0� ä : ÆÇ

x��./

Display(p.y); // eÚã0åä

p.y = 7;Display(p.y); //

Úã0� ä : ÆÇ

y��./

()*+ �struct tag { æçèé êëìí};

(ÄÅTH Å��î��(ÄÅ+ÆÇT�a¶H Å�tï/ð2ÆÇñ+052���§*���

Page 5: BaekSeok University

�+,

-�

struct port {Led_0 : 1;Other_leds : 7;} PortA;

()*+#$%PortaA.led_0 = 1;PortA.other_leds = 0;

�ò�� óôõö÷�øùúûü�ü� �I �ýP�UV þ� � ��,-��.�typedef <

Â�ÿÔÕ> ���_

ÔÕÐ�;

typedef unsigned long int Distance;Distance i;

/� �type ��Ð�(��É���ç);

int add(int a, b);

µúú��+��E���I����V5+Q���E5�������/�.�type ��Ð�(��É���ç) {��� ;

�ò�� �������ùú �ô�� ��ö�+)�� ��ú��öµ��ù�E���3 �������ùú�ô�� ��ö�:B�� �� ��0��VQ�E ������0��

printf-�

��� ;

}

int add(int a, int b) {return a + b;

}

0'1 : printf(��PQ&'�,

5��¶á�); ��PQ� ¦ $�¡½2CD������ !" #$%&'%c

>�&'%d 10

X�Q�%o 8

X�Q�(0�()2�h)

%x 16XQ�

(0xW()2 �h)

%u 10X�*+Q�

%ld long int%f

>�Q+�e�%�ð�(®�)

%lf ,�Q+�e�%�ð�(®�)%e P��%s

&'�%g ®����P��ñ���./%p

±£A%i 10

XQ�int�»� ¹ µúú-.º/�+�- 0W���

printf(“ABC”); ABCprintf(“%d\n”,5); 5printf(“%c %c %c”,’A’,’B’,’C’); A B Cprintf(“From sea ”);printf(“to shining “);printf (“C”); From sea to shining C

printf(“From sea \n”); From seaprintf(“to shining \n“); to shiningprintf (“C”); C

leg1=200.3; leg2=357.4;printf(“It was %fmiles”,leg1+leg2);

It was 557.700012 milesnum1=10; num2=33;

printf(“%d\t%d\n”,num1,num2); 10 33

big=11e+23;printf(“%e \n”,big); 1.100000e+24printf(“%c \n”,’?’); ?printf(“%d \n”,’?’); 63printf(“\007 That was a beep\n”);

Page 6: BaekSeok University

23��4 567895:123# 456|7 56| 893:-;<=;N + - * / %->?(@A)

=;N = += -= *= /= %= &= ^= |= <<= >>=-BC=;N & | ^-DE=;N && || !-FG/HI=;N * & (JKLFG)-MN(BO)

=;N == != < <= > >=-PGQRS=;N . -> (PGQRSFG)-TU=;N ,-GV=;N ?:

-@WXN=;N []-YZ[\=;N ()-sizeof

=;N sizeof-]^E_=;N # ## (]^E_FG)

./�

./�

#include <stdio.h>main() {

float x=333.123456;double y=333.1234567890123456;printf ("%f\n",x);printf ("%.1f\n",x);printf ("%20.3f\n",x);printf ("%-20.3f\n",x);printf ("%020.3f\n",x);printf ("%f\n",y);printf ("%.9f\n",y);printf ("%.20f\n",y);printf ("%20.4e\n",y);

}

333.123444333.1

333.123333.1230000000000000333.123333.123457333.123456789333.12345678901232304270

3.331e+02

15 2 () [] . -> `a->bca14 1 ! ~ ++ -- + - * & (type) sizeof bca->`a13 2 * / % `a->bca12 2 + - `a->bca11 2 << >> `a->bca10 2 < <= > >= `a->bca9 2 == != `a->bca8 2 & `a->bca7 2 ^ `a->bca6 2 | `a->bca5 2 && `a->bca4 2 || `a->bca3 2 ?: `a->bca2 2 = *= /= %= += -= &= ^= |= <<= >>= `a->bca1 2 , `a->bca

3;23�56| 56 123#+ de 12

- fe 12

* ge 13

/ hie 13

% hjklm(n�o, p�qrstu�vw) 13+(xy) z�{|(}lm~q ~��vw) 14-(xy)

w�{|}lm~� {|� �� 14

++ 1��(n�o) 14

-- 1��(n�o) 14

./�0123

/4./�

Page 7: BaekSeok University

()23�=;N =; ����& �� AND,

V ��UV 1�½

1, ��P�½0 9

| �� OR,V ��UV 0

�½0, ��P�½ 1 9

^ �XOR, V ��W9� �î½1, l½ 0 10

~>���'�9� ��E¨²/�, 0->1, 1->0

�10

<< �Ü/��, � ��E�Ü� �� 10

>> �ÖÜ/��, � ��E�ÖÜ� �� 10���Í

1 : %0001 0010���Í2 : %0101 0110

------------------------------------------------��Í& : &0001 0010��Í| : &0101 0110��Í^ : %0100 0100

���Í: %0101 0110

------------------------------------------------��Í~ : %1010 1001��Í<< : %1010 1100��Í>> : %0010 1011

<=((>)23�56| 56 123#== �� 9

!= ��� 9

> �� 10

< �� 10

>= ��h��. 10

<= ��h�� 10

Note��-��/��'��Z+ �� ���3� �kEöõ�ù ��  µ��ù� ¨Ù0��56./�

()./�

?@23�=;N =; ����&& ¡¶¢(logical AND) 5|| ¡¶�(logical OR) 4

! ¡¶eQ(NOT) 14

��Í^ : %0100 0100

��Í>> : %0010 1011

Note�Ü/��-££/�wW*�Ü ���ǶI �ÖÜ2¤� BD����� ü����ÖÜ/��-¥¥/29�W*�ÖÜ ��EǶI �Ü2�W�� ���¦��'W������ùú�½ü��W�I ����ùú�YZ�e§��W�W���¡¶��'� true��

falseE¨�0�. true

�0��©���

, false�

0��

. ¡¶��'��f1 ��

0� ¨Ù0�.¿

:

if (SW1 || SW2) LED1 = 1; else LED2 = 1;��SW1

��SW2

Wtrue(0

��ª½)�½�

(SW1 || SW2)�

1(true)�½

LED1�

ON��

.V��SW1k SW2

WUV0�½

else&*�®«�t LED2

WON��

.

�A23� ? :

expr1 ? expr2 : expr3;

(i % 2) ? printf(“odd\n”) : printf(“even\n”);��iW¬�

(�ýPW 1: true)

�½oddCD��P�½

(�ýPW0: false) even

CD.

78./�

$9./�

Page 8: BaekSeok University

:;(�+)

./�

<�=>./�

B5CD23�­1 = ­

2;

int a;

a = 5;

�ò�� ��µE���I��� ºE�®0��counter = counter + 1;�`-�=k l�.

counter += 1;

sizeof23�ÞV>���'sizeof

�¦��'W¯U¶29¤P��Hâ�°�� >?��k��¨Ù0�.

E9CD23�­1 op=

­2;3b9 op� ����'

(binary operator) +, -, *, /, &, |, ^, <<, >>�±� ��

.,��®��'��=kl-²k��.­1 =

­1 op

­2;

items = items * 3;�`-�=kl�.

items *= 3;

./�

?@./�

sizeof(char) /* 1 ¨Ù */sizeof(int) /* 4 ¨Ù */sizeof(double) /* 8 ¨Ù */

C29�³´��'E� �3345+ ����0�. ������ �-��+��op0�.�

1, �

2, …, �

n;����Ü29�ÖÜ�µW��, ¶· �+�k :B�²Å�+�kW��.

int i, j, array[5];i = j = 0;array[j+2, i+1] = 1;&-$¸Ú��=k l�¹º0�.

-»�¼�½t��&-�

-�¾&

-¨,&(¿�:loops)-¹b&

-,�&(¼À

)

�ò�� �� �T ÁE���I º5+°%EWX��µõõµÂE��0��´PÃò��µõõµÂÄþÅ ¹ þ»k l��ÆÇ�½³´��'+� ÁÈÉ �Èþ� �Èþ+ ����

Page 9: BaekSeok University

A B

1C�if

switch

if�if (�

) &

1; [else&

2;]

if (movex == 1) x = x + 20; else y = y – 10;

F-G-HI�JÊÐË_­ÌÍ

:

;&*2»�¼�½�� �Í� 2WPW��.

1. ÎÄÏ goto&29ÐÚPE./��@� 0�.¿

:

loop : Display(message);goto loop;

2. Switch &29ÐÚPE(¹��@� 0�.

�YZ2� caseT

default¼�»�¼�� W���

.case

f��: &

;default :

&;

switch (input) {case 1 : LED1 = 1;case 2 : LED2 = 1;case 3 : LED3 = 1;default : LED7= 1;

�&*-Î0Ѩ,�3���´�µÂ��E§C0��Ò'à0¦ -�Ó�ö�Ô&�ÕI�¼�Note:ù��ù ����Ò�Ö�¾�¯°�w� �� ���-Ö×/

switch�switch (

�) {

casef��

1 : &

1;

.

.

DE�while,

do~

while

for

default : LED7= 1;}�ò���� �2 ÉE ØÙ Ò0���k �� þÉW���.

casef��

n : &

n;[default :

&;]

} �ò��BD�2g¼9 øÚ�WÛÈ`��� �µ�ù2]®��ÄÏ���w�úù µ��öW ®«���while�while (

�) &

;

int s, i;s = i = 0;while (i < 6) {

s = s + 2;i = i + 1;

}

do while�do &

while (�

);

int s, i;s = i = 0;do {

s = s + 2;i = i + 1;

} while (i < 7);

���s2 2

E7ÙÒ0�.

�k s�

14W��

.

for �for ([«¬­]; [ÜÝ­]; [ÉÞ­]);

for ( s=0, i=0; i<5; i++) {s += 2;

}

�ò���� �2 ÉEºÙÒ0���k �� þü����

Page 10: BaekSeok University

continue�¿� ¦29 continueE ��½�ýPe¹� ®«�P�I¿�+Se¹� ��0�.

FGDE�

H��break

continue

break �¿� ¦29 break&-'bWÝÞ%ß�t�� switch, for, while, do

¼À�àá �â�.

int i=0, s=1; // ��

iT

sE���I�b��PQ0�.

while (1) { // Î0¿�if (i == 4) break;s = s * 2;i++;

}

KE�(while, do~while, for)LM�NKE�O�/C�H�.

for ( x=2; x<5; x++) {int y=1;printf(“%5c%d

>\n”, ‘ ‘, x);

while(y<10) {printf(“%2d * %2d = %2d\n”, x, y, x*y);y++;

}printf(“\n”);

} �ò���� �2 ÉE¢0�-ãäA �W.W�½ åõùµæ&�®«��/��k �� þØ�����ò��((>�CD0��ç+U �2 $]9Â� þ29 0èP�0��

¿� ¦29 continueE ��½�ýPe¹� ®«�P�I¿�+Se¹� ��0�.

int i=0, s=1; // ��

iT

sE���I�b��PQ0�.

while (1) { // Î0¿�s = s * 2;i++;if (i != 4) continue;break;

}

continue

goto,

return

goto�goto »�¼_

�)';

loop : Display(message);goto loop;

return �Øé��E,¦I§C0ê�ë�â�. ¨Ù�-Ö×��.

return [�

];

…c = add(4, 5);…int add(int a, int b) {

return a+ b;}

�ò�29�Î0¿�E¨,�½9Display ��E§C�3®«0�.

�ò�� �� �2 ÉE¢0�-ãäA �W .W�ª½�ô�ö���ù&�®«��/��k �� þØ����E9�(GP: Blocks),�&��¼À- { }

�ì£íÜ+&î�ï0�.

Page 11: BaekSeok University

IJ8�

K<�

QR@�ð�  ðúù ��ùð� úù  ðù�� ð� �úù  ðù��ùð�����úù ðù�ú� ð���ù ðùõõôõð��úù STU �#define �ñ�_

­ÌÍ<òó�>

#define ERR_MSG “Out of range!”…main() {

…if (error) Show(ERR_MSG);…

}

ôõ�4� Úööû÷øùE&'� úû�ö ô  õµ��ùüý�$Å�I øÔôÓ��� úû�ö ô  õµ��ùüýECD0��VWX�YZ[STU#define �ñ�_

­ÌÍ(<��É���ç>) òó�

.�\]^LSTU#undef �ñ�_

­ÌÍ#undef

�)29Q]X¢�EG���q��0�. �deA� �¢��Q+�P��`�â[0�.

#define MAX(A, B) ((A) > (B)) ? (A) : (B)…x = MAX(a + b, c + d);²<¶b��=kl��Ù�3ôõ�����0�.

x = ((a + b) > (c + d)) ? (a + b) : (c + d);

Note#ifdef

�#ifundef

�P/'Ec½¢�WQ+�t��PEþ>�� ��.

V_�/#include <header_

�>

#include “header_�

`Uab#include “C:\my_files\test.h”

Page 12: BaekSeok University

assert.hctype.herrno.hfloat.hlimits.hlocale.hmath.hsetjmp.hsignal.hstdarg.hstddef.hstdio.hstdlib.hstring.htime.h

<assert.h>Functions: assert();

<ctype.h>Functions: isalnum();isalpha();iscntrl();isdigit();isgraph();islower();isprint();ispunct();isspace();isupper();isxdigit();tolower();toupper();

<error.h>Macros: EDOMERANGE

<float.h>Macros: FLT_ROUNDSFLT_RADIXFLT_MANT_DIG

<limits.h>Macros: CHAR_BITSCHAR_MINSCHAR_MAXUCHAR_MAXCHAR_MINCHAR_MAXMB_LEN_MAXSHRT_MINSHRT_MAXUSHRT_MAXINT_MININT_MAXUINT_MAXLONG_MINLONG_MAXULONG_MAX

<locale.h>Macros: NULLLC_ALLLC_COLLATELC_CTYPELC_MONETARYLC_NUMERICLC_TIMEFunctions: localeconv();

<math.h>Macros: HUGE_VAL Functions: acos();asin();atan();atan2();ceil();cos();cosh();exp();fabs();floor();fmod();frexp();ldexp();log();log10();modf();pow();sin();sinh();sqrt();tan();tanh();

<string.h>Macros: NULL

<stdarg.h>Macros: va_start();va_arg();va_end();

<stddef.h>Macros: NULLoffsetof();

<stdlib.h>Macros: NULLEXIT_FAILUREEXIT_SUCCESSRAND_MAXMB_CUR_MAXFunctions: abort();abs();atexit();atof();atoi();atol();bsearch();calloc();div();exit();

<stdio.h>Macros: NULL_IOFBF_IOLBF_IONBFBUFSIZEOFFOPEN_MAXFILENAME_MAXL_tmpnamSEEK_CURSEEK_ENDSEEK_SETTMP_MAXstderrstdinstdoutFunctions: clearerr();fclose();feof();ferror();fflush();fgetpos();fopen();fread();freopen();fseek();

L�-�

FLT_MANT_DIGDBL_MANT_DIGLDBL_MANT_DIGFLT_DIGDBL_DIGLDBL_DIGFLT_MIN_EXPDBL_MIN_EXPLDBL_MIN_EXPFLT_MIN_10_EXPDBL_MIN_10_EXPLDBL_MIN_10_EXPFLT_MAX_EXPDBL_MAX_EXPLDBL_MAX_EXPFLT_MAX_10_EXPDBL_MAX_10_EXPLDBL_MAX_10_EXPFLT_MAXDBL_MAXLDBL_MAXFLT_EPSILONDBL_EPSILONLDBL_EPSILONFLT_MINDBL_MINLDBL_MIN

localeconv();setlocale();

<time.h>Macros: NULLCLOCKS_PER_SEC Functions: asctime();clock();ctime();difftime();gmtime();localtime();mktime();strftime(); time();

<signal.h>Macros: SIG_DFLSIG_ERRSIG_IGNSIGABRTSIGFPESIGILLSIGINTSIGSEGVSIGTERMFunctions: signal();raise();

NULL Variables: typedef size_tFunctions: memchr();memcmp();memcpy();memmove();memset();strcat();strncat();strchr();strcmp();strncmp();strcoll();strcpy();strncpy();strcspn();strerror();strlen();strpbrk();strrchr();strspn();strstr();strtok();strxfrm();

exit();free();getenv();labs();ldiv();malloc();mblen();mbstowcs();mbtowc();qsort();rand();realloc();srand();strtod();strtol();strtoul();system();wcstombs();wctomb();

<setjmp.h>Macros: setjmp(); Functions: longjmp();

fseek();fsetpos();ftell();fwrite();remove();rename();rewind();setbuf();setvbuf();tmpfile();tmpnam();fprintf();fscanf();printf();scanf();sprintf();sscanf();vfprintf();vprintf();vsprintf();fgetc();fgets();fputc();fputs();getc();getchar();gets();putc();putchar();puts();ungetc(); perror();