3주차
2025. 4. 8. 21:33ㆍ프로그래밍1및실습
변수와 상수
- 변수와 상수는 program에서 연산/조작하는 객체
- 모든 변수는 사용 전에 초기화되어야 함.
선언
- 변수에 자료형을 연결해 줌.
- compiler에게 변수에 저 정할 수 있는 자료를 위한 적저 한 공간을 제공함.
- Compiler로 하여금 machine이 명시된 여난을 정확하게 수행할 수 있도록 함.
정수
- Integer는 소수 부분이 없는 숫자
표현식
- 상수, 변수, 연산자, 함수 호출의 조합
- 상수, 변수, 함수 호출 자체 또한 표현식이 될 수 있음
- 대부분의 표현식은 값을 가짐
a+b
10.0-sin(9.0/x)
min(10,9)
명령문
- 실행 가능한 최소 단위!
- 세미콜론(;)으로 보통 끝냄
ans=10;
printf("ssjjss");
a+=b;
- 복합 명령문은 중괄호로 묶인 여러 개의 명령문이다.
if(....){
a+=b;
printf(a);
}
대입
- varibale=expr;
- 등호를 활용하여서 변수에 값을 할당해 줌
int type
- 부호가 있는 정수 자료형(음수, 0, 양수)
32-bit 기기에서는 4byte를 활용하여 저장
64-bit 기기에서는 4 혹은 8 byte를 활용하여서 저장
- 2^32개의 고유한 state를 가짐. (4byte 활용 시)
- -2^31 ~ 2^31 - 1의 값을 가짐 (이 범위를 넘어서면 오버플로우 발생)
- 다음과 같이 선언 및 초기화를 해줌
int a=10;
int b=20, c=30;
int d, e=50; //사용 가능은 하지만 좋은 형태는 아님!
Octal and Hexadecimal
- 8진수: 0을 앞에 붙여서 표현 (020 => 16)
- 16진수: 0x를 앞에 붙여서 표현 (0x10 => 16)
- 각각 서식 지정자로 % o, % x를 사용하고 % x는 소문자 % X는 대문자로 출력해 준다.
#include <stdio.h>
int main() {
int num = 255;
printf("10진수: %d\n", num);
printf("8진수: %o\n", num);
printf("16진수 (소문자): %x\n", num);
printf("16진수 (대문자): %X\n", num);
}
10진수: 255
8진수: 377
16진수 (소문자): ff
16진수 (대문자): FF
다른 정수형 자료형
- short / short int: 2byte
- long / long int: 4byte or 8byte
- long long / long long int: 64bit 이상 할당
- unsigned int / unsigned: 음이 아닌 정수 저장
| 자료형 | 크기(Byte) | 값의 저장 범위 | 출력 변환 문자 |
| char | 1 | -128~127 | %c / %d |
| short | 2 | -32768~32767 | %d |
| int | 4 | -2147483648~ 2147483647 | %d |
| long | 4 | -2147483648~ 2147483647 | %ld |
| long long | 8 | -9223372036854775808 ~9223372036854775807 |
%lld |
| unsigned char | 1 | 0~255 | %u |
| unsigned short | 2 | 0~65535 | %u |
| unsigned int | 4 | 0~4294967295 | %u |
| unsigned long | 4 | 0~4294967295 | %lu |
| unsigned long long | 8 | 0~18446744073709551615 | %llu |
char type
- 작은 크기의 정수 저장하는 데 사용
- 대소문자, 숫자, 구두점 그리고 특수문자(%, +)를 포함
- 대부분의 machine은 ASCII 문자 코드로 문자를 표현함
- 다음과 같이 변수의 선언과 초기화를 해준다.
#include <stdio.h>
int main() {
char ch = 'A';
printf("%c\n", ch);
}
- backslash character
이스케이프 문자로 백슬레시로 특수한 의미를 내포한다.
Sequence Meaning Sequence Meaning \a Alert \\ Backslash \b Backspace \' Single quote \f Form feed \" Double quote \n Newline \? Quesion mark \r Carriage return \0oo Octal value \t Horizontal tab \xhh Hexademical value \v Vertical tab #include <stdio.h> int main() { printf("Alert sound: \a\n"); printf("Backslash: \\\n"); printf("Backspace: ABC\bD\n"); printf("Single quote: \'A\'\n"); printf("Double quote: \"Hello\"\n"); printf("Form feed: Hello\fWorld\n"); printf("Newline:\nThis is a new line.\n"); printf("Question mark: \?\n"); printf("Carriage return: Hello\rWorld\n"); printf("Octal value: \101\n"); printf("Hexadecimal value: \x42\n"); printf("Horizontal tab:\tColumn 1\tColumn 2\n"); printf("Vertical tab: Line1\vLine2\n"); }Alert sound: (경고음 발생) Backslash: \ Backspace: ABD Single quote: 'A' Double quote: "Hello" Form feed: Hello□World (일부 환경에서 페이지 넘김) Newline: This is a new line. Question mark: ? Carriage return: World (Hello가 덮어써짐) Octal value: A Hexadecimal value: B Horizontal tab: Column 1 Column 2 Vertical tab: Line1 Line2
floating-point number
- 부동 소수점 숫자는 실수와 어느 정도 일치
- 정수 사이에 존재하는 실수를 포함 (7!=7.00)
- 특정 범위에서 무한 실수가 존재하기에 부동 소수점 숫자로 모든 값을 표현할 수 없음!
- 32비트의 부동 소수 저장 형식을 사용함.

float, double, long double
- Suffix를 붙여서 부동 소수점 상수를 표현
| Suffix | Type | Example |
| f or F | float | 3.7F |
| l or L | long double | 3.7L |
- C compiler는 별도의 표기가 없을 경우 double로 인식
Exponential notation
- 4 e16 = 4*10^16
- 1.234e-3 = 1.234*10^(-3)
- 중간에 공백을 넣으면 오류 발생!
- 부동 소수점 자료형에서 나타낼 수 있는 값
Precision(정밀도)
-Floating value가 가질 수 있는 소수점 이하 자릿수
Range(범위)
-표현하 수 있는 최대 및 최소의 양의 값의 한계
float
-4byte
-Precision of 6 significant figures & Range of 10^(-38) ~ 10^(38)
double
-8byte
-Precision of 15 significant figures & Range of 10^(-308) ~ 10^(308)
Overlow and Underflow
- Overflow: Undefined behavior 또는 무한대를 나타내는 infinity출력!
- Underflow: 유효 숫자에 한계가 오게 되면서 정보를 손실하게 됨.
자료형 크기 확인하기
- sizeof(자료형): 자료형의 크기를 확인하는 연산자(Operator).
- sizeof의 반환되는 자료형은 일반적으로 unsigned
General Conversion Rules
- 이항 연산자의 피연산자들이 서로 다른 자료형을 가지는 경우 lower type은 higher type으로 promote 함
- 대입 연산자에서는 오른쪽에 있는 값이 왼쪽의 자료형으로 promote 됨
Informal Conversion Rules
- Unsigned 피연산자가 없는 경우
1. 둘 중 한 피연산자가 long double이면 나머지는 long double로
2. (1) 이 아니고, 피연산자가 double이면 나머지는 double로
3. (1), (2)이 아니고, 피연산자가 float이면 나머지를 float으로
4. 이외의 경우, char, short를 int로 변환
5. 이후에 둘 중 한 피연산자가 long이면 long으로 변환
Usual Arithmetic Conversions
- 피연산자 중 하나가 long double이면, 다른 피연산자를 long double로
- 반면에 피연산자 중 하나가 double 이면, 다른 연산자를 double로
- 반면에 피연산자가 float이면, 다른 연산자를 float으로
- 반면에 먼저 양쪽 피연산자 모두 정수 승격 수행(작은 정수형 자료형을 더 큰 정수형으로 반환)
정수 승격
-하나라도 unsigned long int이면 unsigned long int
-하나라도 long int이면 long int
-하나라도 long이면 long
-하나라도 unsigned int 이면 unsigned int
-모두 해당 안 하면 int로 변환
long double
↓
double
↓
float
↓
정수 승격 (Integral Promotion)
unsigned long
↓
[long op unsigned] → 시스템에 따라 처리
↓
long
↓
unsigned int
↓
int
- 표현식에서 원래 형식의 모든 값이 int로 표현될 수 있다면 int형으로 변환.
- 그렇지 않으면 unsigned int로 변환
short a,b;
c=a+b; //int형으로 변환
char c='A';
printf("%c\n",c); //char->int
Informal Conversion Rules
- Signed와 Unsigned 값의 비교는 시스템에 따라서 다르게 정의됨.
- 이는 시스템에서 다양한 정수형 타입의 크기가 어떻게 정의되었는지에 따라서 정의됨.
| Declarations | |||
| char c; short s; int i; long l; unsigned u; unsigned long ul; float f; double d; long double ld; |
|||
| Expression | Type | Expression | Type |
| c - s / i | int | u * 7 - i | int |
| u * 2.0 - i | double | f * 7 - i | float |
| c + 3 | int | 7 * s * ul | unsigned long |
| c + 5.0 | double | ld + c | long double |
| d + s | double | u - ul | unsigned long |
| 2 * i / l | long | u - l | system-dependent |