Computer Science/C

C ์–ธ์–ด 1๊ฐ• | ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ๊ณ , C ์–ธ์–ด ์—ญ์‚ฌ, Hello World!

ํ† ๋งˆํ† . 2021. 12. 14. 16:59

๐Ÿฑ‍๐Ÿ‰ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์„œ๋ก  ๐Ÿฑ‍๐Ÿš€

์•Œ๊ณ ๋ฆฌ์ฆ˜์ ์ธ ์‚ฌ๊ณ 

ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋งˆ์น˜ ์š”๋ฆฌํ•  ๋•Œ ๋ ˆ์‹œํ”ผ์™€ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค. ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ๊ณ ๋ฅผ ํ•œ๋‹ค๋Š” ๊ฒƒ์€ [1] ์ปดํ“จํ„ฐ๊ฐ€ ์ดํ•ด ๊ฐ€๋Šฅํ•œ ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•˜๊ณ , [2] ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ํฌ๊ด„ํ•˜๋„๋ก ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์„ค๊ณ„ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. 

 

๋ณธ ๋…ธ์ด๋งŒ ์•„ํ‚คํ…์ฒ˜

์ปดํ“จํ„ฐ๋Š” ๋ณธ ๋…ธ์ด๋งŒ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋”ฐ๋ผ์„œ ๋””์ž์ธ๋œ๋‹ค. ๋ณธ ๋…ธ์ด๋งŒ ์•„ํ‚คํ…์ฒ˜๋Š” ์กด ํฐ ๋…ธ์ด๋งŒ(1903~1957, ํ—๊ฐ€๋ฆฌ ์ถœ์‹  ์ˆ˜ํ•™์ž)์ด ์„ค๊ณ„ํ•œ ๊ฒƒ์œผ๋กœ, Stored Program concept๋ผ๊ณ  ํ•œ๋‹ค. Stored Program Concept๋Š” ์ปดํ“จํ„ฐ ๋‚ด๋ถ€์— ๊ธฐ์–ต ์žฅ์น˜๋ฅผ ๊ฐ–์ถ”๊ณ (๋ฉ”๋ชจ๋ฆฌ) ๊ณ„์‚ฐ์˜ ์ˆœ์„œ๋ฅผ ๋ถ€ํ˜ธํ™”ํ•˜์—ฌ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ (CPU), ํ•ด๋…ํ•œ ํ›„์— ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์„ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋กœ ํ•œ๋‹ค. ์ด๋•Œ ์ฒซ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” Fetch : CPU ๋‚ด๋ถ€๋กœ ๋ช…๋ น์–ด๊ฐ€ ์ด๋™ํ•จ(I/O), ๋‘๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” Decode : ๋ช…๋ น์–ด ํ•ด์„(Control Unit), ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„๋Š” Execution : Arithmetic Logic Unit์ด๋‹ค. 

 

ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด

์‚ฌ๋žŒ์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฐœ๋ฐœํ•˜๊ณ , ์ปดํ“จํ„ฐ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‹คํ–‰ํ•œ๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ํ†ตํ•ด ์‚ฌ๋žŒ์˜ ๋…ผ๋ฆฌ๋ฅผ ์ปดํ“จํ„ฐ๊ฐ€ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์œผ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—๋Š” ์ˆ˜์ค€์ด ์žˆ๋‹ค. High-Level Languages, Assembly Languages, Machine Languages๋กœ ๋‚˜๋‰œ๋‹ค. 

 

ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ž˜ ํ•˜๋Š” ๋ฒ•

1. ์ง์ ‘ ํ•œ๋‹ค. 

2. ์—ฐ์Šตํ•œ๋‹ค. 

๐Ÿฑ‍๐Ÿ‰ C ์–ธ์–ด ๐Ÿฑ‍๐Ÿš€

๊ฐœ์š”

๋ณธ ๊ฐ•์˜์—์„œ ๋‹ค๋ฃจ๋Š” C์–ธ์–ด๋Š” 1970๋…„๋Œ€์— ๊ฐœ๋ฐœ๋œ ์–ธ์–ด๋กœ Unix์—์„œ ๊ฐœ๋ฐœ๋˜์—ˆ๋‹ค. C๋Š” ์ž‘๊ณ , ์ด๋™์„ฑ ์žˆ์œผ๋ฉฐ, ๊ฐ•๋ ฅํ•œ ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด ๋„๋ฆฌ ์‚ฌ์šฉ๋œ๋‹ค. C๋Š” C++๊ณผ Java์˜ ๊ธฐ์ดˆ๊ฐ€ ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฌธ๋ฒ•์ด ๋‹ค์†Œ ๋ณต์žกํ•˜๊ณ , array ๋์„ ์ฒดํฌํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉฐ, ๊ธฐํ˜ธ๊ฐ€ ์ค‘์˜์ ์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค. 

 

ํŠน์ง•

(๋งˆ์ง€๋ง‰ ์Šฌ๋ผ์ด๋“œ๋„)

 

๐Ÿฑ‍๐Ÿ‰ Hello World! ๐Ÿฑ‍๐Ÿš€

๊ธฐ๋ณธ ์Šคํ…

file ์ƒ์„ฑ > Compile > Linking > Debugging > Run

 

ํ”„๋กœ๊ทธ๋žจ ์ถœ๋ ฅ

#include <stdio.h>

int main(void){
    printf("From sea to shining C\n");
    return 0;
}

 

์ปดํŒŒ์ผ

์†Œ์Šค ํŒŒ์ผ('.c')๋ฅผ objective file('.o')๋กœ ๋ฐ”๊พผ๋‹ค. ์ด๋•Œ objective file์ด๋ž€, ์ปดํ“จํ„ฐ๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ํ‘œํ˜„์„ ๊ฐ€์ง„ ํŒŒ์ผ์„ ๋งํ•œ๋‹ค. 

 

๋งํ‚น Linking

๋งํ‚น์€ Objective file์„ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. 

(objective file) sea.o, sea.obj

(executable program) sea.out, sea.exe

 

์‹คํ–‰

./sea๋กœ ์‹คํ–‰

๐Ÿฑ‍๐Ÿ‰ ์˜ˆ์ œ 1 ๐Ÿฑ‍๐Ÿš€

#include <stdio.h>
int main(void){
    int miles, yards;
    float kilometers;
    miles = 26;
    yards = 385;
    kilometers = 1.609 * (miles + yards / 1760.0);
    printf("\nA marathon is %f kilometers.\n\n", kilometers);
    return 0;
}

* assignment statement *

variable = expression;์˜ ํ˜•ํƒœ๋กœ variable์— expression์„ ํ• ๋‹นํ•œ๋‹ค. 

* expression *

์šฐ๋ณ€์— ์˜ค๋Š” ๋‚ด์šฉ. ์ƒ์ˆ˜, ๋ณ€์ˆ˜, ์—ฐ์‚ฐ์ž์™€ ๊ฒฐํ•ฉํ•œ ์ƒ/๋ณ€์ˆ˜ ๋“ฑ

* conversion rule *

7/2 == 3 // int / int = int

7.0/2 == 3.5 // double / int = double

* format *

conversion specification์ด ์ด๋ฃจ์–ด์ ธ์•ผ ํ•œ๋‹ค. 

 

๐Ÿฑ‍๐Ÿ‰ ์˜ˆ์ œ 2 ๐Ÿฑ‍๐Ÿš€

#include <stdio.h>
int main(void){
    int score1, score2, score3, avg;
    int num_score;
    score1 = 87;score2=93;score3=100;
    num_score = 3;
    avg = (score1+score2+score3) / num_score;
    printf("Average score: %d\n", avg);
    return 0;
}

* conversion rule *

๋งŒ์•ฝ์—, score1, score2, score3, avg์„ (float)score1, (float)score2, (float)score3, (float)avg๋กœ ๋ฐ”๊พธ๋ฉด, 

๊ฒฐ๊ณผ๊ฐ€ ์™„์ „ํžˆ ๋‹ฌ๋ผ์ง„๋‹ค. 

๐Ÿฑ‍๐Ÿ‰ Flow of Control ์ œ์–ด ํ๋ฆ„ ๐Ÿฑ‍๐Ÿš€

Alternative actions : ์„ ํƒ

if ๋ฌธ

#include <stdio.h>
int main(void){
    int a = 1;b=3;
    if (b==3){
        a=5;
    }
    printf("%d", a);
    return 0;
}

if else

#include <stdio.h>
int main(void){
    int cnt = 1;
    int a, b, c;
    if (cnt==0){
        a = 2;b=3; c=5;
    }
    else{
        a=-2;
        b=-3;
        c=-5;
    }
    return 0;
}

 

loop : ๋ฐ˜๋ณต

while statement

#include <stdio.h>
int main(void){
    int i=1, sum = 0;
    while (i<=5){
        sum += i;
        ++i;
    }
    printf("sum = %d\n", sum);
    return 0;
}

 

for statement

#include <stdio.h>
int main(void){
    int i=1, sum = 0;
    for (i=1;i<=5;++i)){
        sum += i;
    }
    printf("sum = %d\n", sum);
    return 0;
}

 

Function