[c] C 언어 기본 문법
1. 변수 선언과 초기화
#include <stdio.h>
int main() {
int x = 10; // 정수형 변수 x를 선언하고 10으로 초기화
float y = 3.14; // 실수형 변수 y를 선언하고 3.14로 초기화
char c = 'A'; // 문자형 변수 c를 선언하고 'A'로 초기화
return 0;
}
2. 연산자
#include <stdio.h>
int main() {
int a = 5, b = 3;
int sum = a + b; // 덧셈 연산
int sub = a - b; // 뺄셈 연산
int mul = a * b; // 곱셈 연산
int div = a / b; // 나눗셈 연산
int mod = a % b; // 나머지 연산
return 0;
}
3. 조건문과 반복문
#include <stdio.h>
int main() {
int age = 20;
if (age >= 18) {
printf("성인입니다.");
} else {
printf("미성년자입니다.");
}
for (int i = 0; i < 5; i++) {
printf("%d\n", i); // 0부터 4까지 출력
}
while (age > 0) {
printf("%d\n", age); // 20부터 1까지 역순으로 출력
age--;
}
return 0;
}
4. 함수
#include <stdio.h>
// 함수 원형 선언
int add(int a, int b);
int main() {
int result = add(3, 5);
printf("3 + 5 = %d", result);
return 0;
}
// 함수 정의
int add(int a, int b) {
return a + b;
}
C 언어의 기본 문법은 변수, 연산자, 조건문, 반복문, 함수 등으로 구성되어 있습니다. 이러한 기본 문법을 사용하여 다양한 프로그램을 작성할 수 있습니다.