본문 바로가기

Language/C++

[C++] introduction to C++ - 컴도리돌이

728x90

 

C++ Structure of Program

 

#include <iostream>

using namespace std; // std namespace 사용

int main()
{
	cout << "hello_world\n";  // hello_worde 출력
    	return 0;
}

 


C++ variables and Data Types

 

- fundamental data types

 

Integer : int (4) , char (1) , short (2) , long (4) , long long(8) + unsigned

Boolean : bool(1)

Floating point numbers : float(4), double(8), long double(8).

 

-variables

 

variables : specific memory locations

declarcation : int a;, double b = 1.0;

Scope : whether the variable is visible (=usable). 

 

<Scope example>

 

void myfunc()
{
	int a=0, b=1;
    {
    	int a=2, c=3;
    	cout << "a = " << a << ", b = " << b << ", c= " << c << endl;
    }
    cout << "a = " << a << ", b = " << b << ", c= " << c << endl;
}
a = 2, b = 1, c= 3
a= 0 , b = 1

 

Scope에 선언한 변수는 local 변수와 이름이 같아도 다른 변수이다. Scope에 선언한 변수들은 휘발성이며 중괄호가 끝나면 사라진다.


C++ Constants

 

Integer : 123(123), 0123(83) ->8진수 , 0x123(291) -> 16진수 / 123u, 123l, 123ul -> unsigned number.

Floating-point : 0.1 (d) , 0.1f (f). / 1e3, 0.3e-9.

Character and string literal : 'c', "a string\n"

Boolean : true , false.