LoginSignup
6
6

More than 5 years have passed since last update.

入力を安全に受け取る

Last updated at Posted at 2014-04-05

scanfやgetsを使うのは危険だと聞いた。
scanfと同じ用な感じで安全に受け取るには、
fgetsとsscanfを使えば良さそう。

main.c
#include <stdio.h>

int main()
{
    char buf[80];
    int a, b;

    fgets(buf, 80, stdin);
    sscanf(buf, "%d %d", &a, &b);

    printf("%d + %d = %d\n", a, b, a+b);

    return 0;
}
6
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6