c-language : chapter 2
DESCRIPTION
C-Language : Chapter 2. 2013, Fall Pusan National University Ki- Joune Li. Chapter 2: Type, Operator and Expression. Contents Constants and Variable Types Operators Expression. 1. Constants 와 Variables. Program 기본적인 과정 : Data 의 처리 Data 의 표현 Constant 와 Variable Constant - PowerPoint PPT PresentationTRANSCRIPT
C-Language : Chapter 22013, Fall
Pusan National University
Ki-Joune Li
Chapter 2: Type, Operator and Expression
• Contents Constants and Variable Types Operators Expression
1. Constants 와 Variables
• Program • 기본적인 과정 : Data 의 처리• Data 의 표현
• Constant 와 Variable
• Constant• 주어진 값 , 변하지 않는 값• 예 . 123, 24.0, ‘a’, “abc”
1. Constants 와 Variables
• Variable• 변하는 값• 반드시 주기억 장소에 값을 저장
• 예 . x = 124;
x 의 값을 저장하는 주기억 장치에 123 이라는 값이 저장
• 변수 이름• 영문으로 시작 , 소문자 / 대문자 구별 , 특수문자 사용 금지
변수 대입 상수
주기억 장치
x123
1. Constants 와 Variables
• 변수의 값 지정 (Assignment)float x, y, z;
x = 124;
y = x;
z = y = x;
왼쪽의 변수와 오른쪽의 변수가 서로 다른 의미
• Main Memory 주소• von Neumann Machine
2. Data Type
• Data Type• 연산의 방법을 명시• 표현 및 저장 방법의 명시• 예 .
• 5 / 2 = 2• Integer : 2 bytes 로 표시
• Type 의 종류• Built-in Type : C 가 제공하는 Types• User-Defined Type : , 프로그래머가 정의하는 Types
2. Data Type – Built-In Types
• Number• Integer : int, short, long, unsigned
unsigned short, unsigned long• Float : float, double
• Character• char : 1 character• char* : character string
• Enumeration
2. Data Type – Type Declaration
• 변수의 다음 사항을 결정• 표현방법 및 주기억 장치의 저장 크기 및• 연산 방법
• SyntaxType VariableName ; 또는Type VariableName = InitialValue ;
2. Data Type – Array
• 여러 개의 데이터를 저장하는 변수• Syntax
Type VarableName [numberOfElements ];• 여러 개의 데이터를 반복적으로 처리할 경우 사용
• 예int sum=0;int i, values[10];
/* ... value[0], ... value[9] */for(i=0;i<10;i++)
sum=sum+value[i];
2. Data Type – 그 외• Enumeration Type
• 단순히 값을 순서를 정하여 구별하기 위한 Type• Syntax
enum VariableName {value1, value2, …, valuek};
• Register Type• 값을 주기억 장치가 아닌 Register 에 기록• 속도를 증가시키기 위한 방법• Syntax
register Type VariableName ;
3. Operators
• Operator 의 종류• Arithmetic Operators• Increment/Decrement Operators• Relational and Logical Operators• Bitwise Operators• Assignment Operators
3. Operators – Arithmetic Operators
• Arithmetic Operators• 일반적인 산술연산• +, -, *, /, % ( 나머지를 계산 : 예 . 5 % 2 = 1)
• Incremental Operators• a=a+1; a++; 또는 a=a-1; a--;• Post-increment vs. pre-increment
a=10;b=++a; b=a++;printf(“%d %d\n”, a, b); printf(“%d %d\n”, a, b);
• a+=4;
3. Operators – Relational Operators
• Relational Operators (Comparison Operators)• 값의 비교를 위한 연산자• <, >, <=, >=, !=, ==• 연산의 결과 : False(0) 또는 True(0 이 아닌 수 )
a=10; b=11;printf(“%d\n”, (a<b)); printf(“%d\n”, (a>b));
3. Operators – Logical Operators
• Logic Operator• 논리적 연산을 위한 연산자• &&, ||, !• 연산의 결과 : False(0) 또는 True(0 이 아닌 수 )
a=10; b=11;printf(“%d %d %d\n”, (a<b)&&(a>b), (a<b)||(a>b), !(a>b));
3. Operators – Bitwise Operators
• Bitwise Operator• 값을 bit 단위로 처리하는 연산자• &, |, ^, ~, <<, >>
a=10; /*1010*/ b=11; /*1011*/printf(“%d %d %d\n”, a|b, a&b, a^b); printf(“%d %d\n”, a<<1, a>>1);
3. Operators – Type Casting Operators• Type Casting Operator
• 변수의 Type 을 변경하는 연산자 .• Syntax: (TypeName) variable
float x=12.5, y=3.4;printf(“%f %d %d\n”, x/y, x/y, (int)x/(int)y);
3. Operators – Assignment Operators
• Assignment 도 하나의 Operationfloat x=20.5, y;y=x;
결과는 Assign 된 값 .
printf(“%f \n”, y=x);
4. Expression
• Expression 은 operator 와 operand 로 구성• Operator 의 우선순위 (Precedence)
• 연산의 순서를 결정
• Conditional Expression• X = cond ? value 1 : value 2; • 예
X = a > 0 ? 10 : 20;