변수와상수 1
변수란무엇인가? 변수 : 정보 (data) 를저장하는컴퓨터내의특정위치 ( 임시저장공간 ) 메모리, register 메모리주소 101 번지 102 번지 변수의크기에따라 주로 byte 단위 메모리 2
기본적인변수형및변수의크기 변수의크기 해당컴퓨터에서는항상일정 컴퓨터마다다를수있음 short <= int <= long signed vs. unsigned signed 가 default 3
변수형 형태 bool unsigned short int short int unsigned long int long int int(16비트 ) int(32비트 ) unsigned int(16비트 ) unsigned int(32비트 ) char float double 크기 1바이트 2바이트 2바이트 4바이트 4바이트 2바이트 4바이트 2바이트 4바이트 1바이트 4바이트 8바이트 값참 / 거짓 0 ~ 65,535-32,768 ~ 32,767 0 ~ 4,294,967,295-2,147,483,648 ~ 2,147,483,647-32,768 ~ 32,767-2,147,483,648 ~ 2,147,483,647 0 ~ 65,535 0 ~ 4,294,967,295 256 개의문자값 (ASCII) 1.2e-38 ~ 3.4e38 2.2e-308 ~ 1.8e308 4
#include <iostream> int main() { using std::cout; cout << "The size of an int is:\t\t" << sizeof(int) << " bytes.\n"; cout << "The size of a short int is:\t" << sizeof(short) << " bytes.\n"; cout << "The size of a long int is:\t" << sizeof(long) << " bytes.\n"; cout << "The size of a char is:\t\t" << sizeof(char) << " bytes.\n"; cout << "The size of a float is:\t\t" << sizeof(float) << " bytes.\n"; cout << "The size of a double is:\t" << sizeof(double) << " bytes.\n"; cout << "The size of a bool is:\t" << sizeof(bool) << " bytes.\n"; } return 0; 5
변수정의하기 예 int myage; int myage, myweight, myheight; 의미있는단어사용 int main() { unsigned short x, y, z; z = x*y; return 0; } 대소문자구별 int age, Age; // No syntax error 작성법 int my_age; int myage; int imyage; 예약어사용금지 int, if, while, for, main int main() { unsigned short Width; unsigned short Height; unsigned short Area; Area = Width * Length; return 0; } 6
변수에값할당 vs. 초기화 할당 ( 값대입 ) unsigned short Width; Width = 5; 정의할때초기화 unsigned short Width =5; long width = 5, length = 7; int myage=39, yourage, hisage=40; 7
// list 3.3 #include <iostream> int main() { using std::cout; using std::endl; unsigned short int Width = 5, Length; Length = 10; // create an unsigned short and initialize with result // of multiplying Width by Length unsigned short int Area = Width * Length; } cout << "Width:" << Width << "\n"; cout << "Length: " << Length << endl; cout << "Area: " << Area << endl; return 0; 8
typedef 변수별칭 : typedef typedef unsigned short int USHORT 9
// list 3.3 #include <iostream> typedef unsigned short int USHORT; //typedef defined int main() { using std::cout; using std::endl; } USHORT Width = 5; USHORT Length; Length = 10; USHORT Area = Width * Length; cout << "Width:" << Width << "\n"; cout << "Length: " << Length << endl; cout << "Area: " << Area <<endl; return 0; 10
변수사용 short vs. long unsigned vs. signed float vs. int unsigned 정수형올림초기화 signed 정수형올림초기화 11
변수사용 (cont.) 이진표현 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 unsigned 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 signed 0 1 2 3 4 5 6 7-8 -7-6 -5-4 -3-2 -1 -N is represented by 2 s complement, N* N*=2 n -N 12
//list 3.4 #include <iostream> int main() { using std::cout; using std::endl; } unsigned short int smallnumber; smallnumber = 65535; cout << "small number:" << smallnumber << endl; smallnumber++; cout << "small number:" << smallnumber << endl; smallnumber++; cout << "small number:" << smallnumber << endl; return 0; small number : 65535 small number : 0 small number : 1 13
//list 3.5 #include <iostream> int main() { short int smallnumber; smallnumber = 32767; std::cout << "small number:" << smallnumber << std::endl; smallnumber++; std::cout << "small number:" << smallnumber << std::endl; smallnumber++; std::cout << "small number:" << smallnumber << std::endl; return 0; } small number : 32767 small number : -32768 small number : -32767 14
문자 (character) 1 바이트 256개의문자가능 0~255까지의숫자또는 ASCII 문자형으로해석 97 a 53 5 문자초기화 char mychar= a ; 특수출력문자 \a 벨소리 \ 작은따옴표 \b backspace \ 큰따옴표 \f form feed \` 홑따옴표 \n 개행문자 \? 의문부호 \r Carriage return \\ 역사선 \t 탭 \000 8 진법표시 \v 수직탭 \xhhh 16 진법표시 15
//list 3.6 #include <iostream> int main() { for (int i = 32; i<128; i++) std::cout << (char) i; return 0; } 16
상수 (Constant) 문자상수 : 입력값 int myage = 39 ; 기호상수 : 이름에의해표시 ( 유지보수의편이 ) student = classes * 15; students = classes * studentperclass; #define 을가지고상수를정의하는법 형 (type) 을가질수없음 #define studentperclass 15 const 로상수 형을가질수있음 const unsigned short int studentperclass = 15; 17
나열형상수 특정값으로제한된변수명 Ex) COLOR -> RED, BLUE, GREEN enum COLOR {RED, BLUE, GREEN, WHITE, BLACK} 정수값과일치 0 1 2 3 4 enum COLOR {RED=100, BLUE, GREEN=500, WHITE, BLACK=700} 101 501 18
// list 3.7 #include <iostream> int main() { enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; Days today; today = Monday; if (today == Sunday today == Saturday) std::cout << "\ngotta' love the weekends!\n"; else std::cout << "\nback to work.\n"; } return 0; 19
// list 3.8 #include <iostream> int main() { const int Sunday = 0; const int Monday = 1; const int Tuesday = 2; const int Wednesday = 3; const int Thursday = 4; const int Friday = 5; const int Saturday = 6; int today; today = Monday; if (today == Sunday today == Saturday) std::cout << "\ngotta' love the weekends!\n"; else std::cout << "\nback to work.\n"; } return 0; 20