본문 바로가기

스터디 로그/Computer Science

[C++] Lecture 1-2. C++ 시작하기

오늘부터 최대한 빠른 시일 내에 이 crash course를 다 끝내는 게 목표이다. 이 시리즈의 글들은 거의 모든 부분이 아래 링크에서 발췌한 것일 예정이다. 따로 참고한 링크가 있다면 하단에 reference 남기도록 하겠다. Thank you freeCodeCamp!

 

https://www.youtube.com/watch?v=8jLOx1hD3_o

 


 

Lecture 1.

 

we need compiler to generate executable binary file

clang, gcc 모두 c/c++/objective-c 위한 compiler (gcc는 다른 언어 컴파일도 가능한 듯)

clang 은 Apple의 default compiler (llvm이 backend) , gcc는 homebrew 통해서 따로 설치 가능

clang --version과 gcc --version 모두 default compiler version을 알려줌

gcc version은 brew info gcc 커맨드 통해서 알 수 있음

 


 

Lecture 2.

 

궁금했던 점: std::endl and "\n" both show the same new line, both insert a newline character into the output stream

Then what is their difference? --> Buffer Flushing

std::endl also flushes the output buffer, which means it ensures that all buffered output is written to the output device immediately --> For immediate output visibility: std::endl, for lightweight and efficiency: "\n"

 

Convention for indicating success/failture in C/C++

--> rely on specific numeric values: return 0 meaning success, non-zero (often 1) meaning failure/error

--> unlike Python's convention where it returns values and exceptions to indicate success/failure

 

Comments

One line comment -- using // (<-> Python: #)

Mutiple lines comment -- using /* comments */ (<-> Python: """ comments """)

 

Errors

  • compile time error
  • runtime error (errors from your logic, sometimes it cause the program to be crashed)
  • warnings

Statements and Functions

#include <iostream>

int main(){
    int v1 {100};
    int v2 = 200;

    std::cout << "v1: " << v1 << std::endl;
    std::cout << "v2: " << v2 << std::endl;
    
    return 0;
}

 

 

Data Input and Output

Stream Purpose
std::cout Printing data to the console (terminal)
std::cin Reading data from the terminal
std::cerr Printing errors to the console
std::clog Printing log messages to the console
#include <iostream>

int main(){
    std::string name;
    int age;

    std::cout << "Type your name and age: " << std::endl;
    std::cin >> name >> age;
    std::cout << name << " is " << age << " years old." << std::endl;

    return 0;
}

 

#include <iostream>

int main(){
    std::string full_name;

    std::cout << "Type your name :" << std::endl;
    std::getline(std::cin, full_name); // for input including spaces
    std::cout << "Nice to meet you, " << full_name << "!" << std::endl;

    return 0;
}

 

 

C++ core languages vs. Standard library vs. STL

Standard library는 <iostream>, <string> 같은 거고

STL은 a part of C++ standard library but a collection of container types (collection of things)