공부/c++

c 유용한 함수들

Lectinua 2019. 5. 26. 03:06

scanf, printf 대신 쓰일 수 있는 함수

 

char ch;
ch = getchar();
putchar(ch);

 

char string[100];
gets_s(string); //콜 바이 레퍼런스, 한 줄 기준으로 받아짐
puts(string);

 

scanf("%s" string)는 gets_s와 달리 공백 기준으로 받아짐

 

EX & 설명)

#include  <stdio.h>
#include  <stdlib.h>
#include  <time.h>

void A(); //sprintf
void B(); //sscanf
void C(); //난수
void D(); //exit(0);

int main() {
C();
}

void A() {
int n = 450;
char str[100];

sprintf_s(str, "%d", n);
printf("s: %s\n", str);
}

void B() {
char str[] = "450";
int n;

sscanf_s(str, "%d", &n);
printf("n: %d\n", n);
}

void C() {
//rand()를 쓰려면 #include <stdlib.h>를 포함시켜야 함
//time()을 쓰려면 #include <time.h>를 포함시켜야 함
//time(NULL); // 1970/01/01 00:00:00로부터 지난 시간을 초단위로 나타냄
srand(time(NULL)); //시드 설정
for (int i = 0; i < 10; i++) {
printf("%d\n", rand() % 10 + 1);
}
}

void D() {
exit(0); //메인 함수에서의 return 0;과 같음
 //그 자리에서 프로그램을 종료시킴
}