[C++] 1.4 입출력 스트림

2021. 3. 17. 22:23Programming Language/.Cpp

1.4 입출력 스트림 - cin,cout,endl

cin → console in

cout → console out

endl → end line

#include <iostream> // cout, cin, endl...
#include <cstdio> //printf

int multiplyNumbers(int a, int b)
{
	int sum = a * b;
	return sum;
}

int main()
{
	using namespace std;

	cout << "a+b = " << multiplyNumbers(1, 2) << endl;

	return 0;
}

 

메모리에서 벌어지는 일

  • mulyiplyNumber(1,2) 함수가 실행되면, 매개변수인 a와 b가 생성되고 1과 2라는 값이 초기화 된다.
  • endl;과 \nBecause of this, use of the ‘\n’ character is typically preferred instead. The ‘\n’ character moves the cursor to the next line, but doesn’t do the redundant flush, so it performs better. The ‘\n’ character also tends to be easier to read since it’s both shorter and can be embedded into existing text.
  • Using std::endl can be a bit inefficient, as it actually does two jobs: it moves the cursor to the next line, and it “flushes” the output (makes sure that it shows up on the screen immediately). When writing text to the console using std::cout, std::cout usually flushes output anyway (and if it doesn’t, it usually doesn’t matter), so having std::endl flush is rarely important.
  • std 참고