我写了以下代码:
#include <stdio.h>
int main()
{
int a, b;
printf("Enter values of a and b\n");
scanf(" %d%d ", &a, &b); // Note the two spaces before and after %d
// ^This^
printf("a = %d b = %d\n", a, b);
return 0;
}
该程序运行如下所示:
aps120797@XENON-PC:/mnt/
例如,请找到以下代码
main()
{
int i;
char s[100];
printf("Enter the string \n");
scanf(" %s ",s);
printf("Enter the string\n");
scanf("%d",&i);
printf("%s\n%d\n",s,i);
}
上述代码的输出为
Enter the string
hai
hai
Enter the string
hai
0
它应该接受一行输入,
我想用C语言做一个循环,当程序要求输入一个整数,而用户输入一个非数字字符时,程序再次要求输入一个整数。
我刚刚找到了下面的代码。但我不明白这是什么意思,scanf("%*[^\n]%*c")。^\n是什么意思?^\n和c之前的*是什么意思?
/*
This program calculate the mean score of an user 4 individual scores,
and outputs the mean and a final grade
Input: score1, score2,score2, score3
Output: Mean, Fin
当我使用ocaml实现read_int函数时(如下所示),
let read_int () = Scanf.bscanf Scanf.Scanning.stdin "%d" (fun x -> x)
如果格式参数为%d(无空格),编译器将返回失败,并显示以下信息:
Exception:
Scanf.Scan_failure
"scanf: bad input at char number 1: ``character '\\n' is not a decimal digit''".
但是如果我使用' '%d(
#include <stdio.h>
int main() {
int a, b;
printf("Enter the values of a and b");
scanf(" %d%d ", &a, &b);
printf("a=%d b=%d", a, b);
return 0;
}
为什么我们必须插入3个输入,因为空格是在双引号前后给定的?
为什么当我在scanf()格式说明符之前使用一个空格时,程序工作得很好。代码如下:
printf("What's your username: ");
scanf(" %s", username);
printf("Do you want to make a deposit or a withdrawal? [d/w]\n");
scanf(" %c", &choice);
当我没有如下代码所示的空格时,它就会退出:
printf("What's your username: ");
sc
例如,当我为num1输入2,为num2输入3时,我希望只要输入第二个数字,输出就会得到8。但是,程序要求我再输入一个整数,而我只是输入一个随机数,比如242,它仍然输出8,这意味着它不会影响结果。所以我的问题是为什么会有第三个输入?谢谢你的帮助!
#include "stdafx.h"
int Power (int num1, int num2);
int main ()
{
int a, b;
puts ("Enter two numbers, a and b:\n");
scanf ("%i\n", &
我想像下面这样解析十六进制网格,并将字母或空格放入char hexadoku[16][16];
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| h | p m o | i k | n f l |
+ + + + + + + + + + + + + + + + +
.....
+---+---+---+---+---+---+---+---+---+---+---+---
我最近偶然发现了一个奇怪的案例(至少对我来说,因为我以前没有遇到过这种情况)..Consider下面的简单代码:
int x;
scanf("%d",&x);
printf("%d",x);
上面的代码接受正常的整数输入,并按预期显示结果。
现在,如果我将上面的代码修改为以下代码:
int x;
scanf("%d ",&x);//notice the extra space after %d
printf("%d",x);
在给出printf语句的结果之前,它接受另一个额外的输入。我不明白为什么空格会导致scan
基本上就像标题说的..。当我的程序从控制台运行时,它会问你想要加密还是解密。当我输入E或E时,它会创建一个新的空行(直到输入某种文本),然后显示“输入文本”和“输入键”行。
因此,在控制台中,它看起来类似于:
你想要地下墓穴还是(D)氪星?E
asdf;<--随机用户输入,以使程序继续。
输入要加密的文本:输入用于加密的密钥:(用户输入)
然后程序退出..。
//message to be encrypted
char text[250];
//word to use as the key
char key[50];
//stores the encrypted word
char e
int main()
{
int num;
int num2;
printf("Enter two numbers\n");
scanf("%i\n",&num);
scanf("%i\n",&num2);
if (num % num2 == 0){
printf("%i is evenly divided by %i",num,num2);
}
else {
printf("%i is not evenly
输入
输入中将有几行以包含单个*的行结尾。不应处理最后一行。每一行都将包含Hajj或Umrah。
输出
对于输入的每一行,在没有引号的情况下,用单独的行输出Hajj-e-Akbar或Hajj-e-Asghar。关于确切的格式,请参考示例。
这是我解决这个问题的代码。
#include <stdio.h>
int main()
{
char str[100];
int i = 1;
while (scanf("%s", &str))
{
if (str[0] == '*')
我编写了下面的C代码,以检查数组中是否存在一个数字,该数组的元素由用户输入。但奇怪的是,它跳过了第三个printf语句,直接获取输入,并在输入之后打印Enter the number you wish to look for。是什么引起的?在代码下面包括输入和输出框。
代码:
#include <stdio.h>
#include <stdlib.h>
void main() {
int arr[30], size, i, num, flag=0;
printf("Enter size of array. \n");
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int intvar=0; //variable to store integer input
double decimalvar = 0; //variable to store double input
char string[30]; //arrary to store string
scanf("%d",&intvar); //scan for int i