본문으로 건너뛰기
Advertisement

C 언어 - 변수와 자료형

1. 변수와 상수

C 언어에서 변수는 메모리 공간의 이름이며 데이터 타입과 함께 선언되어야 합니다.

int age = 20; // 변수 선언 및 초기화
age = 21; // 값 변경 가능

const float PI = 3.14159f; // 상수 선언
// PI = 3.14; // 컴파일 에러 발생

2. 데이터 타입 (Primitive Data Types)

  • int: 정수형 (기본 4바이트, %d)
  • float: 실수형 (단정밀도, 4바이트, %f)
  • double: 실수형 (배정밀도, 8바이트, %lf)
  • char: 문자형 (1바이트, %c)
  • void: 타입이 없음을 의미하는 키워드

3. 조건문 (If, Switch)

int score = 85;

if (score >= 90) {
printf("A 등급\n");
} else if (score >= 80) {
printf("B 등급\n");
} else {
printf("C 등급 이하\n");
}

int choice = 2;
switch (choice) {
case 1:
printf("옵션 1 선택\n");
break;
case 2:
printf("옵션 2 선택\n");
break;
default:
printf("기본 옵션\n");
}

4. 반복문 (For, While, Do-While)

for (int i = 0; i < 5; i++) {
printf("Iteration %d\n", i);
}

int j = 0;
while (j < 3) {
printf("While %d\n", j);
j++;
}
Advertisement