scanf("입력형식", &변수이름);
%d, %f, %c, %s
등 데이터 타입에 따라 달라짐&
는 변수의 메모리 주소를 나타냄&
를 반드시 사용#include <cstdio>
int main() {
int score;
printf("숫자를 입력해 주세요\n");
scanf("%d", &score); // 10 입력 하면
printf("%d", score); // 10 이 출력
}
%d
: 10진수(정수) 를 입력받음&score
: 변수 score 의 주소(메모리 위치) 에 입력받은 데이터를 저장함#include <cstdio>
int main() {
int score;
printf("숫자를 입력해 주세요\n");
scanf("%d", &score); // 10 입력
printf("%d", score); // 10 출력
printf("%X", &score); // 메모리 주소 출력
}
int score;
이렇게 선언하면, 컴퓨터가 메모리에 4바이트 공간을 마련해둠
주소 (예시) | 값 | 변수이름 |
---|---|---|
6DBCF1DC | (빈 상태) | score |
scanf("%d", &score); // 10 입력
여기서 &score
의 주소 (6DBCF1DC) 에 입력된 값을 저장
주소 (예시) | 값 | 변수이름 |
---|---|---|
6DBCF1DC | 10 | score |
printf("%d", score);
메모리에서 값을 불러와 출력 (결과: 10)
printf("%X", &score);
메모리 위치값이 출력 (6DBCF1DC)
함수 | 형태 | 설명 |
---|---|---|
scanf | scanf("%d", &변수) | 변수의 주소로 데이터를 입력받아 저장 |
printf | printf("%d", 변수) | 변수의 값을 출력 |
int score;
scanf("%d", &score);
printf("%d", score);
score 를 n 으로 바꾸면?
int n;
scanf("%d", &n); // 여기도 바뀌어야 함
printf("%d", n); // 여기도 바뀌어야 함