Study/BoostCourse

[모두를 위한 컴퓨터 과학] Ch2. C언어

쫄보삽질러 2021. 10. 29. 23:27

https://www.boostcourse.org/cs112/joinLectures/41307

 

모두를 위한 컴퓨터 과학 (CS50 2019)

부스트코스 무료 강의

www.boostcourse.org

 

💃  해당 강의를 듣고 스터디용으로 정리한 내용들입니다 💃  

 

👉 이전 강의  

https://ninano1109.tistory.com/185

 

Ch1. 컴퓨팅 사고(Computational Thinking)

https://www.boostcourse.org/cs112/joinLectures/41307 모두를 위한 컴퓨터 과학 (CS50 2019) 부스트코스 무료 강의 www.boostcourse.org 💃  해당 강의를 듣고 스터디용으로 정리한 내용들입니다 💃  1. Comp..

ninano1109.tistory.com

 

 

1. C 기초

source code -> Compiler(Algorithm/ Software) -> machine code

#include <stdio.h>
​
int main(void)
  
{
  printf("hello, world\n");
}

clang: compiler

# Make source code into machine code
$ clang hello.c

 

2. 문자열

#include <cs50.h>
#include <stdio.h>
​
int main(void)
{
  string answer = get_string("What's your name?\n");
  prinft("hello $s\n", answer);
}
​
$ clang -o string string.c -lcs50
​
$ make string

 

3. 조건문과 루프

counter = counter + 1;
counter+=1;
counter++;
​
# Infinite Loop
while (true)
{
  print("hello, world\n");
}
  
int i = 0;
while (i<50)
{
  print("hello, world\n");
  i++;
}
for (int i = 0; i < 50; i++;)
{
  print("hello, world\n");
}
​

 

4. 자료형, 형식 지정자, 연산자

  • Bool
  • char
  • double
  • float
  • int
  • long
  • string
  • ...
#include <cs50.h>
#include <stdio.h>

int main(void)
{
  int age = get_int("what's your age?\n");
  // int days = age * 365;
  // printf("You are at least %i daysd old.\n", days);
  printf("You are at least %i daysd old.\n", age * 365);
}

 

형식 지정자

  • 문자열(string) : %s
  • char : %c
  • float/double : %f
  • int : %i
  • long : %li
#include <cs50.h>
#include <stdio.h>

int main(void)
{
  float price = get_float("What's the price? \n");
  printf("Your total is %f.\n", price*1.0625);
}
#include <cs50.h>
#include <stdio.h>

int main(void)
{
  int n = get_int("n: ");
  
  if (n % 2 == 0)
  {
    printf("even\n");
  }
  else
  {
    printf("odd\n");
  }
}

 

5. 사용자 정의 함수, 중첩 루프

#include <studio.h>

int main(void)
{
  printf("cough\n");
  printf("cough\n");
  printf("cough\n");
}
#include <studio.h>

int main(void)
{
  for (int i = 0; i <= 3; i++)
  {
    printf("cough\n");
  }
}
//함수사용 v1.

#include <studio.h>

void cough(void);

int main(void)
{
  for (int i = 0; i < 3; i++)
  {
    cough();
  }
}

void cough(void)
{
  printf("cough\n");
}
//함수사용 v2.

#include <studio.h>

void cough(int n);

int main(void)
{
  cough(3);
}

void cough(int n)
{
  for (int i = 0; i < n; i++)
  {
    printf("cough\n");
  }
}

 

6. 하드웨어의 한계

#include <cs50.h>
#include <stdio.h>

int main(void)
{
  flat x = get_float("x: ");
  float y = get_float("y: ");
  
  printf("x / y = %.1f\n", x / y);
}
#include <studio.h>
#include <unistd.h>

int main(void)
{
  for (int i = 1; i *= 2)
  {
    printf("%i\n", i);
    sleep(1);
  }
}

32비트를 넘어가면 메모리 에러(오버플로우) 발생.

 


 

 

next 👉  

https://ninano1109.tistory.com/189

 

[모두를 위한 컴퓨터 과학] Ch3. 배열(Array)

https://www.boostcourse.org/cs112/joinLectures/41307 모두를 위한 컴퓨터 과학 (CS50 2019) 부스트코스 무료 강의 www.boostcourse.org 💃  해당 강의를 듣고 스터디용으로 정리한 내용들입니다 💃  https:..

ninano1109.tistory.com

 

반응형