質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Visual C++

Microsoft Visual C++はWindowsのCとC++の統合開発環境(IDE)であり、コンパイラやデバッガを含んでいます。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

5回答

10993閲覧

scanfってどうやって使うのですか?

cand

総合スコア65

Visual C++

Microsoft Visual C++はWindowsのCとC++の統合開発環境(IDE)であり、コンパイラやデバッガを含んでいます。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

1グッド

0クリップ

投稿2018/04/07 06:37

情報オリンピックの過去問の答えを見て思ったのですが、scanf関数ってどんなものですか?

Tokeiya3👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答5

0

scanf でぐぐればいろいろ解説が出てきます

投稿2018/04/07 06:41

y_waiwai

総合スコア87774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yumetodo

2018/04/08 03:44

man scanf を見るべし
guest

0

ベストアンサー

回答になっているかどうかはわかりませんがC++で入力ならこっちのほうがおすすめです。

C++

1#include<iostream> 2#include<string> 3using namespace std; 4 5int main() 6{ 7 string input; 8 cout << "文字列を入力してください。"<<endl; 9 cin >> input; 10 cout << input << "と入力されました。" << endl; 11 return 0; 12}

ちなみにscanf関数はエラー要素が多く、とても危険な関数です。(scanf使用禁止令を出す会社があるくらい)

投稿2018/04/07 07:02

編集2018/04/07 07:05
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

こんにちは。

直接的な回答ではないです。すいません。

scanfは大昔からあるC言語の標準ライブラリ関数です。設計が古いため、問題点も多いです。
C++でもscanfを使うことは可能ですが、scanfに比べると設計が新しいstd::istreamを使うことがお薦めです。

投稿2018/04/07 06:59

Chironian

総合スコア23272

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

c

1/* $Id: scanf.c,v 1.2 2002/08/09 20:56:57 pefo Exp $ */ 2 3/* 4 * Copyright (c) 2000-2002 Opsycon AB (www.opsycon.se) 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Opsycon AB. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 21 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 */ 33#include <stdio.h> 34#include <stdarg.h> 35#include <string.h> 36#include <ctype.h> 37 38#include <pmon.h> 39 40/* 41 * ** fscanf --\ sscanf --\ 42 * ** | | 43 * ** scanf --+-- vfscanf ----- vsscanf 44 * ** 45 * ** This not been very well tested.. it probably has bugs 46 */ 47static int vfscanf __P((FILE *, const char *, va_list)); 48static int vsscanf __P((const char *, const char *, va_list)); 49 50#define ISSPACE " \t\n\r\f\v" 51 52/* 53 * scanf(fmt,va_alist) 54 */ 55int 56scanf (const char *fmt, ...) 57{ 58 int count; 59 va_list ap; 60 61 va_start (ap, fmt); 62 count = vfscanf (stdin, fmt, ap); 63 va_end (ap); 64 return (count); 65} 66 67/* 68 * fscanf(fp,fmt,va_alist) 69 */ 70int 71fscanf (FILE *fp, const char *fmt, ...) 72{ 73 int count; 74 va_list ap; 75 76 va_start (ap, fmt); 77 count = vfscanf (fp, fmt, ap); 78 va_end (ap); 79 return (count); 80} 81 82/* 83 * sscanf(buf,fmt,va_alist) 84 */ 85int 86sscanf (const char *buf, const char *fmt, ...) 87{ 88 int count; 89 va_list ap; 90 91 va_start (ap, fmt); 92 count = vsscanf (buf, fmt, ap); 93 va_end (ap); 94 return (count); 95} 96 97/* 98 * vfscanf(fp,fmt,ap) 99 */ 100static int 101vfscanf (FILE *fp, const char *fmt, va_list ap) 102{ 103 int count; 104 char buf[MAXLN + 1]; 105 106 if (fgets (buf, MAXLN, fp) == 0) 107 return (-1); 108 count = vsscanf (buf, fmt, ap); 109 return (count); 110} 111 112 113/* 114 * vsscanf(buf,fmt,ap) 115 */ 116static int 117vsscanf (const char *buf, const char *s, va_list ap) 118{ 119 int count, noassign, width, base, lflag; 120 const char *tc; 121 char *t, tmp[MAXLN]; 122 123 count = noassign = width = lflag = 0; 124 while (*s && *buf) { 125 while (isspace (*s)) 126 s++; 127 if (*s == '%') { 128 s++; 129 for (; *s; s++) { 130 if (strchr ("dibouxcsefg%", *s)) 131 break; 132 if (*s == '*') 133 noassign = 1; 134 else if (*s == 'l' || *s == 'L') 135 lflag = 1; 136 else if (*s >= '1' && *s <= '9') { 137 for (tc = s; isdigit (*s); s++); 138 strncpy (tmp, tc, s - tc); 139 tmp[s - tc] = '\0'; 140 atob (&width, tmp, 10); 141 s--; 142 } 143 } 144 if (*s == 's') { 145 while (isspace (*buf)) 146 buf++; 147 if (!width) 148 width = strcspn (buf, ISSPACE); 149 if (!noassign) { 150 strncpy (t = va_arg (ap, char *), buf, width); 151 t[width] = '\0'; 152 } 153 buf += width; 154 } else if (*s == 'c') { 155 if (!width) 156 width = 1; 157 if (!noassign) { 158 strncpy (t = va_arg (ap, char *), buf, width); 159 t[width] = '\0'; 160 } 161 buf += width; 162 } else if (strchr ("dobxu", *s)) { 163 while (isspace (*buf)) 164 buf++; 165 if (*s == 'd' || *s == 'u') 166 base = 10; 167 else if (*s == 'x') 168 base = 16; 169 else if (*s == 'o') 170 base = 8; 171 else if (*s == 'b') 172 base = 2; 173 if (!width) { 174 if (isspace (*(s + 1)) || *(s + 1) == 0) 175 width = strcspn (buf, ISSPACE); 176 else 177 width = strchr (buf, *(s + 1)) - buf; 178 } 179 strncpy (tmp, buf, width); 180 tmp[width] = '\0'; 181 buf += width; 182 if (!noassign) 183 atob (va_arg (ap, u_int32_t *), tmp, base); 184 } 185 if (!noassign) 186 count++; 187 width = noassign = lflag = 0; 188 s++; 189 } else { 190 while (isspace (*buf)) 191 buf++; 192 if (*s != *buf) 193 break; 194 else 195 s++, buf++; 196 } 197 } 198 return (count); 199}

scanf.c

投稿2018/04/10 05:16

編集2018/04/10 05:21
arch_

総合スコア158

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

投稿2018/04/08 16:14

編集2018/04/08 16:15
haruniku

総合スコア527

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問