728x90
printf
와 scanf
는 string 형태로 format을 정하고 형식 지정자로 값을 대입하거나 입력받을 수 있도록 만든 표준 입출력 함수
표준 라이브러리 함수
#include <stdio.h>
처럼#include
로 미리 작성된 프로그램을 불러옴.h
,.c
,.txt
모두 사용할 수 있고#include
로 불러온 위치에 해당 내용이 실행됨< >
와" "
로 불러올 수 있는데,< >
는 표준 라이브러리에서 파일을 찾고," "
는 먼저 작업 폴더에서 찾고 없다면 표준 라이브러리에서 찾음
printf
- C의 표준 출력 함수
printf("출력할 서식", 변수1,..., 변수n)
형태로 사용할 수 있고 출력할 서식에 형식 지정자formatspecifierformatspecifier를 포함시켜 변수를 함께 출력할 수 있음- 변수를 하나도 포함시키지 않고 출력할 수도 있고 서로 다른 type의 값을 출력할 수 있음
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
void mainvoidvoid { int a = 1, b = 2; char c = 'c'; printf"novariables\n""novariables\n"; //"no variables" printf"a="a=; //a = 1, b = 2, c = c } - 위의 예시에서
printf
출력 서식에%
가 형식 지정자의 자리임을 나타내고 데이터 타입에 따라 다른 형태를 가짐. 맨 아래 표 참고
scanf
- C의 표준 입력
scanf("입력 받을 서식", 변수1의 주소, ..., 변수n의 주소)
scanf
에 값을 입력할 때, 변수명이 아니라&
로 변수의 주소를 전달This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersvoid mainvoidvoid { int i; printf"Input:""Input:"; scanf""; //i가 아니라 &i로 주소를 전달 printf"YourInputwas"YourInputwas; }
scanf
가 window에서 보안 문제가 있어scan_f
를 사용하라는 경고가 나올 수 있음 (scanf
과scanf_s
의 용법이 다름)#pragma warning (disable:4996)
or#define _CRT_SECURE_NO_WARNINGS
를 가장 위에 입력해주면 경고 무시하게 됨
- format만 적절히 설정해주면 한 번에 여러 개의 입력을 받을 수 있음
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void mainvoidvoid | |
{ | |
int i, j, k; | |
printf"Input:""Input:"; | |
scanf""; //space로 구분 | |
printf"YourInputwere"YourInputwere; | |
} |
- 입력되어야 할 값보다 더 많이 입력 받을 경우 버퍼에 데이터가 남아 문제가 발생하기 때문에 더 많은 입력을 받아선 안됨.
Format specifier | Data type |
---|---|
%d |
10진 정수형 |
%o |
8진 정수형 |
%x |
16진 정수형 |
%u |
unsigned 10진수 |
%f |
실수형 |
%lf |
double 타입 실수형 |
%c |
문자 |
%s |
문자열 |
'프로그래밍 언어 > C' 카테고리의 다른 글
[C++] pair class 사용법 00 | 2022.09.18 |
---|---|
함수 00 | 2022.07.30 |
C 연산자 00 | 2022.07.30 |
C 언어에서 수 체계와 변수 00 | 2022.07.30 |