# 헤더파일
#include <stdio.h>
# gcc 컴파일러
$ pwd
/Library/Developer/CommandLineTools/usr/bin
$ ls
gcc
$ vim main.c
$ ls
main.c
$ gcc main.c
$ ls
a.out main.c
$ ./a.out
Hello World
# 표준입출력 라이브러리
컴파일러가 설치된 경로의 include
디렉토리에 존재한다.
#include <stdio.h>
# printf()
void main() {
int age = 32;
char* name = "Paul";
printf("Hello World \n");
printf("My name is %s \n", name);
printf("My age is %d \n", age);
return;
}
# scanf()
#include <stdio.h>
#include <stdbool.h>
void main() {
int a;
float b;
scanf("%d %f", &a, &b);
printf("%d %f", a, b);
return;
}
# 변수
# 정수
#include <stdio.h>
void main() {
short a = 30;
int b = 30;
long c = 30;
printf("%d \n", a);
printf("%d \n", b);
printf("%d \n", c);
return;
}
# 실수
#include <stdio.h>
void main() {
float height = 176.3;
double weight = 70.3;
printf("%f \n", height);
printf("%f \n", weight);
return;
}
# 문자
#include <stdio.h>
#include <stdbool.h>
void main() {
char alphabet = 'a';
printf("%c \n", alphabet);
return;
}
# 문자열
# bool
stdbool.h
를 포함시켜야 한다.
#include <stdio.h>
#include <stdbool.h>
void main() {
bool isMarried = false;
if (isMarried) {
printf("is married.");
} else {
printf("is not married.");
}
return;
}
# 형변환
void main() {
double height = 176.3;
int _height = (int)height;
printf("%d \n", _height); // 176
return;
}
# 상수
#include <stdio.h>
void main() {
const int DB_CONNECTION = 100;
printf("%d \n", DB_CONNECTION);
return;
}
# 반복문
# while
#include <stdio.h>
#include <stdbool.h>
void main() {
int idx = 0;
while (idx < 10) {
printf("Hello \n");
idx ++;
}
return;
}
# for
#include <stdio.h>
#include <stdbool.h>
void main() {
for (int i=0; i<10; i++) {
printf("Hello \n");
}
return;
}
# 조건문
#include <stdio.h>
#include <stdbool.h>
void main() {
bool isMarried = true;
if (isMarried) {
printf("She is married. \n");
} else {
printf("She is not married. \n");
}
return;
}
#include <stdio.h>
#include <stdbool.h>
void main() {
bool age = 40;
if (age < 20) {
printf("She is teen. \n");
} else if (age > 20 && age < 65){
printf("She is adult. \n");
} else {
printf("She is senior. \n");
}
return;
}
# 함수
#include <stdio.h>
#include <stdbool.h>
int sum(int a, int b) {
return a+b;
}
void main() {
int result = sum(30, 50);
printf("%d", result);
return;
}