ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • main, header files, commands | CS 50 Week 1
    Computer Science/CS 50 Harvard 2021. 11. 6. 18:49

    CS50 2021년 가을 강의를 한글로 정리한 것입니다.

     

     

    Main

    Scratch를 이용할 때, 우리는 위에 보이는 when flag clicked라는 버튼을 사용해서 프로그램을 동작시켰다. (물론 이 이외에도 다른 방식으로 프로그램을 동작시키는 방법도 있다). C언어에서는 이 블록 대신, main 이 이 역할을 대신한다.

    int main(void)
    {
    
    }

    위 코드가 바로 main인데, curly braces, 즉 {}는 코드를 감싸준다. 

     

    Header files

    #include <cs50.h>
    #include <stdio.h>
    
    int main(void)
    {
        printf("hello, %s\n",  get_string("What's your name? "));
    }

    이 코드를 보자. 위쪽에 #include <cs50.h> 그리고 #include <stdio.h>와 같은 줄은 컴파일러에게, 지금 내가 그 코드를 직접 작성하지 않았고, 이러한 이름을 가진 library를 사용할 것이니까, 내 프로그램에 불러달라고 말하는 것이다. stdio.h 는 printf와 같은 함수를 우리 코드에서, 우리가 직접 개발하지 않고 사용할 수 있고, cs50.h 덕분에 get_string과 같은 함수를 사용할 수 있게 된 것이다. 사실 studio.hcs50.h는 음식점의 메뉴와 같다. 이 헤더 파일은 실제 implementation을 불러오는 게 아니라, 목록(menu)만 가지고 있는 것이다.

     

     

    Linux Commands

    터미널을 사용할 때, 아래와 같은 commands(커맨드들)을 사용하면 다양한 것들을 마우스 없이 할 수 있다.

    • cd, for changing our current directory (folder)
      • cd를 이용해 현재에서 다른 폴더 또는 디렉터리로 이동할 수 있다.
      • # Use cd to go innder folder
        > cd week1_c
        > pwd
        /Users/jieunchon/git_repo/cs50/week1_c
        
        # Use cd to go upper folder
        > cd ..
        > pwd
        /Users/jieunchon/git_repo/cs50
        
        # Use cd without any other arguments to go to default directory
        > cd
        > pwd
        /Users/g471000
    • cp, for copying files and directories
      • 파일이나 디렉터리(폴더)를 복사할 수 있다.
      • # Use cp command to copy README.md file and save it as README2.md
        > cp README.md README2.md
        > ls
        README.md      README2.md     libcs50-10.1.1 week1_c
        
        # Use cp command to copy libcs50-10.1.1 directory and save it as libcs50-10.1.1_copy, 
        # using -r option
        > cp -r libcs50-10.1.1 libcs50-10.1.1_copy
        > ls
        README.md           README2.md          libcs50-10.1.1      libcs50-10.1.1_copy week1_c
    • ls, for listing files in a directory
      • 어떤 파일이 있는지 보여줄 수 있다.
      • > ls
        README.md           README2.md          libcs50-10.1.1      libcs50-10.1.1_copy week1_c
    • mkdir, for making a directory
      • 디렉토리(폴더)를 생성할 수 있다.
      • > mkdir testfolder
        > ls
        README.md           README2.md          libcs50-10.1.1      libcs50-10.1.1_copy testfolder          week1_c
    • mv, for moving (renaming) files and directories
      • 파일이나 폴더(디렉터리)를 다른 장소로 옮기거나 이름을 바꿀 수 있다.
      • # Renamed testfoler as a_test_folder
        > mv testfolder a_test_folder
        > ls
        README.md           README2.md          a_test_folder       libcs50-10.1.1      libcs50-10.1.1_copy week1_c
        
        # Moved README2.md file into a_test_folder
        > mv README2.md a_test_folder
        > ls
        README.md           a_test_folder       libcs50-10.1.1      libcs50-10.1.1_copy week1_c
        > ls a_test_folder
        README2.md
    • rm, for removing (deleting) files
      • 파일(또는 옵션을 추가하면 폴더까지도)을 삭제할 수 있다. 이 커멘드는 굉장히 신중하게 사용해야 한다.
      • # Move incide of copied lib folder
        > cd libcs50-10.1.1_copy
        > ls
        LICENSE   Makefile  README.md build     docs      post      postinst  postrm    postun    src       tests
        
        # Remove LICENSE file
        > rm LICENSE
        > ls
        Makefile  README.md build     docs      post      postinst  postrm    postun    src       tests
        
        # Remove tests folder(directory)
        > rm -r tests
        > ls
        Makefile  README.md build     docs      post      postinst  postrm    postun    src
        
        # Remove entire directory
        > cd ..
        > pwd
        /Users/jieunchon/git_repo/cs50
        > ls
        README.md           libcs50-10.1.1      libcs50-10.1.1_copy week1_c
        > rm -rf libcs50-10.1.1_copy
        > ls
        README.md      libcs50-10.1.1 week1_c
    • rmdir, for removing (deleting) directories only if it is empty
      • 디렉터리(폴더)를 삭제할 수 있다.
      • # Make a_test_folder empty and then remove the directory
        > ls
        README.md           a_test_folder       libcs50-10.1.1      libcs50-10.1.1_copy week1_c
        > rmdir a_test_folder
        rmdir: a_test_folder: Directory not empty
        > rm a_test_folder/README2.md
        > rmdir a_test_folder

     

    여기에서 . 은 현재의 directory를 뜻하고 .. 는 위쪽 디렉터리를, 한번 더 위로 가려면 ../.. 를 이용하면 된다.

     

    Command Line에 대해서는 추가 설명 영상이 준비되어있었다.

     

     

    Reference

     

    댓글

Designed by Tistory.