标签: scanf

'%'和格式说明符之间的数字在scanf中的含义是什么?

我知道格式说明符中使用的选项printf(),但我完全不知道%3d可能意味着什么,如下面的代码中所示.

scanf("%3d %3d",&num1,&num2);
Run Code Online (Sandbox Code Playgroud)

一般来说,scanf语句中的数字%format specifier指示之间的数字是什么.

是不是scanf()简单地接收输入并将其存储在参数中指定的地址中?

c scanf

-1
推荐指数
1
解决办法
5256
查看次数

为什么不是isdigit()有效?

我正在尝试创建一个生成随机数的程序,要求用户猜测,然后回答他是否正确.出于某种原因,无论用户是否输入数字,它都会响应,就好像他没有.有任何想法吗?谢谢你帮助初学者:)

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


main()
{
    char iRandomNum = '\0';

    int iResponse = 0;
    srand(time(NULL));

    iRandomNum = (rand() % 10) + 1;

    printf("Guess the number between 1 yand 10 : ");
    scanf("%d", &iResponse);

    if (isdigit(iResponse) == 0)
        printf("you did not choose a number\n");
    else if (iResponse == iRandomNum)
        printf("you guessed correctly\n");
    else 
        printf("you were wrong the number was %c", iRandomNum);
}
Run Code Online (Sandbox Code Playgroud)

c debugging scanf

-1
推荐指数
1
解决办法
1083
查看次数

数字之和程序没有为更大的数字提供正确的o/p

我写了一个程序来查找数字的总和.当我使用数字说10位数时,我得到一些不正确的答案.

例如,如果我输入输入9999999999则输出为46.以下是我目前的代码:

#include<stdio.h>

int main()
{
    int sum,value,rem;
    printf("Enter the value:");
    scanf("%d",&value);
    sum= 0;
    while((value/10)>0)
    {
        rem=value%10;
        sum=sum+rem;
        value=value/10;
    }
    sum=sum+value;
    printf("Sum of digits is %d",sum);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c scanf

-1
推荐指数
1
解决办法
103
查看次数

scanf()中%u和%d之间的差异

在C++中,如果我从一个字符串中读取一个整数,那么我是否使用ud作为转换说明符并不重要,因为它们都接受负整数.

#include <cstdio>
using namespace std;

int main() {
    int u, d;
    sscanf("-2", "%u", &u);
    sscanf("-2", "%d", &d);
    puts(u == d ? "u == d" : "u != d");
    printf("u: %u %d\n", u, u);
    printf("d: %u %d\n", d, d);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Ideone.com

我挖得更深,发现是否有任何区别.我找到

int u, d;
sscanf("-2", "%u", &u);
sscanf("-2", "%d", &d);
Run Code Online (Sandbox Code Playgroud)

相当于

int u, d;
u = strtoul("-2", NULL, 10);
d = strtol("-2", NULL, 10);
Run Code Online (Sandbox Code Playgroud)

根据cppreference.com.

是否有任何差异之间的所有ud使用解析这些转换说明,即传递给格式时scanf …

c++ scanf cstdio

-1
推荐指数
1
解决办法
9112
查看次数

C++遇到sscanf模式的问题

我试图"提取"名称和下一个整数.当我运行它时,我得到一个运行时错误.我测试没有字符串,它运行正常.

// Test string
std::string show = "BlahBlah 3";

// Pickup string and int
std::string nameString;
int id;
sscanf(show.c_str(), "%s %i", &nameString, &id);
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

c++ scanf

-1
推荐指数
2
解决办法
142
查看次数

while循环用分号

这个指令做了什么?分号和while循环有什么用?

while(scanf("%s",&s [strlen(s)])== 1);

我正在研究一个程序来检查字符串是否是一个字母组合.这是完整的代码:

int main(){

char s[100];
while (scanf("%s", &s[strlen(s)]) == 1);
char big[26] = {0};
char small[26] = {0};
for (int i = 0; i < strlen(s); i++) {
    if (s[i] >= 'a' && s[i] <= 'z') {
        small[s[i] - 'a'] = 1;
    }
    else if (s[i] >= 'A' && s[i] <= 'Z') {
        big[s[i] - 'A'] = 1;
    }
}

for (int i = 0; i < 26; i++) {
    if (!(big[i] == 1 || small[i] …
Run Code Online (Sandbox Code Playgroud)

c scanf while-loop

-1
推荐指数
1
解决办法
184
查看次数

为什么scanf对大浮点数输错了?

#include <stdio.h>

int main() { 
float k;
    scanf("%f", &k);
    printf("%f", k);
} 
Run Code Online (Sandbox Code Playgroud)

在这个简单的程序中,当我输入一个包含最多8位数的数字时,它会正确显示.

但如果我超过8位,即输入123456789输出123456792.

为什么会这样?那么有趣的事实是,如果我输入的任何数字,123456789并且123456796然后它总是显示123456792.

它是否与浮点数的8位小数精度有关?

c floating-point gcc scanf

-1
推荐指数
1
解决办法
472
查看次数

C - printf显示意外输出

我对编程很陌生,想编写一个程序来读取5个数字并添加它们.这就是它的样子.

#include <stdio.h>

int main(int argc, char *argv[])
{
    int a,b,c,d,e,sum;

    printf("Enter the 5 numbers\n");
    scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);

    sum=a+b+c+d+e;
    printf("Sum of entered integers is %d\n,sum");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

到目前为止我还没到达过循环.那么,为什么我的程序没有给出正确的结果呢?它没有向我显示编译错误.但是当我输入数字1,2,3,4,5时,它给出了一些乱码的结果,如2752264!

c printf scanf

-1
推荐指数
1
解决办法
217
查看次数

为什么scanf会跳过字符串输入?

我试图将多个单词输入和多行输入到一个数组中.但某些地方代码跳过获取输入并跳过结束程序.我已经尝试在'%s'(或'%s')之前和之后添加空格但是它不起作用(也许是因为它在循环中?).非常感谢任何人的帮助!如果我输入两三个以上的单词,它也会开始变得怪异:(我的目标是找到特定字母在所有这些单词和行中出现的次数.

#include <stdio.h> //include standard library

int main(){
  int lineCount, occuranceCount;
  printf("How many lines are you going to enter?");
  scanf("%d", &lineCount);

  char input[lineCount][100], searchChar;

  for(int i=0; i<lineCount; i++){
    printf("Please enter line #%d (100 characters of less):",i+1);
    scanf("%s", &input[i]);
  }

  printf("What letter do you want to check the frequence in those lines? ");
  scanf("%c", &searchChar);

  for(int j=0; j<lineCount; j++){
    for(int k=0; k<100; k++){
      if(input[j][k] != '\0'){
        if(input[j][k]==searchChar)
          occuranceCount++;
      }
    }
  }

  printf("The letter occurs for %d time", occuranceCount);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c scanf char getchar

-1
推荐指数
1
解决办法
841
查看次数

i keep getting type-related errors

I'm writing this program for my homework and i keep getting type errors. To my knowledge %d reads integer which is in this case the variable x and %lf reads double " variable f"

i've tried to remove "\n" in scanf() function as it was requested in other Questions

int x=0;
int l=0;
double f=0;
printf("Geben Sie eine ganze Zahl");
scanf("%d",x);
printf("Geben Sie eine reele Zahl");
scanf("%lf",f);
l=-1;
char r[1]="";
char s[1]="";
  while(l !=1){
    printf("Geben Sie ein Zeichen");
    scanf("%s",r);
    l=strlen(r); …
Run Code Online (Sandbox Code Playgroud)

c scanf

-1
推荐指数
1
解决办法
65
查看次数

标签 统计

scanf ×10

c ×8

c++ ×2

char ×1

cstdio ×1

debugging ×1

floating-point ×1

gcc ×1

getchar ×1

printf ×1

while-loop ×1