我遇到了一个我真的不知道如何解决的问题。我主要使用Python编写代码,这是我的第一个C语言程序。
#include <stdio.h>
int ask(void) {
scanf("Var");
return 0;
}
int count(ask) {
scanf("number1");
return 0;
}
int main(void) {
printf("This is my first program!\n");
printf("I hope this program turns out well.");
printf("I don't really know what to do, but i think im progressing.\n");
printf("But yeah, This is my first program.\n");
printf("Type an Number");
ask();
count(ask());
printf("Thanks!");
printf(%count%);
return 0;
}
但是,我不断收到错误消息。
main.c:22:10: error: expected expression
printf(%count%);
^
main.c:22:17: error: expected expression
printf(%count%);
^
2 errors generated.
compiler exit status 1
我想要它做的是,用户键入一个数字,然后打印出该数字。虽然还不完整。我要它写数字1-用户输入,当数字正确时,它会显示“您的数字是:”(数字)
答案 0 :(得分:1)
问题(正如已经指出的那样)是您实际上并没有从scanf()调用中获取和存储值。此外,printf(%count%)
不是有效的C语法。您需要使用printf("%d", count)
。
将所有内容放在一起:
#include <stdio.h>
int ask(void) {
int input_number;
scanf("%d", &input_number);
getchar(); # This is so that the '\n' in is read when you hit Enter
return input_number;
}
int main(void) {
printf("This is my first program!\n");
printf("I hope this program turns out well.");
printf("I don't really know what to do, but i think im progressing.\n");
printf("But yeah, This is my first program.\n");
printf("Type an Number");
int input_number = ask();
printf("Thanks!");
printf("The number you entered is %d\n", input_number);
return 0;
}
为避免犯此类错误,需要阅读一些内容:
printf
教程:https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm
scanf
教程:https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm
答案 1 :(得分:0)
您使用了不正确的格式进行打印。
您正在使用此代码 printf(%count%);
,而应使用此代码printf("%d",count(variablename));
这是您可以使用的代码:
#include <stdio.h>
int ask(void) {
//scanf("Var");
int var; //declaring integer type varible in C
scanf("%d",&var); //taking input
return var; //returning what is being input by user
}
//int count(ask) //its function you are passing
//so it should have parenthesis
int count( int a )
{
return a;
}
int main(void) {
printf("This is my first program!\n");
printf("I hope this program turns out well.");
printf("I don't really know what to do, but i think im progressing.\n");
printf("But yeah, This is my first program.\n");
printf("Type an Number: ");
//ask();
count(ask()); // when you will call it will automatically run ask as well
// and it will pass int value that user will input
printf("Thanks!");
int var1=ask(); // this varible var1 will store value returned by ask()
printf("%d",count(var1) ); //this will print that value
//I dont know what you wanted to do here
//maybe you wanted to print value returned by count function
//so it can be done by this
return 0;
}
printf格式:printf("%formatspecifier",variable name);
的格式说明符是%d
的整数。 %f
为浮动值。 %c
作为字符。 s
表示字符串,依此类推。
答案 2 :(得分:0)
在C语言中,我们使用%
指定输出/输入类型。 %d代表整数,%f代表浮点数,%c代表字符,%s代表字符串,这就是您的基本知识。
对于printf:
printf("%d", varname);
对于scanf:
scanf("%d", &varname);
'&'表示在内存中的位置。
您的程序遇到很多语法错误,下面是一些代码:
#include<stdio.h>
int ask(){
int varin;
scanf("%d", &varin);
return (varin);
}
int count(int countin){
return (countin); //just an example code, or whatever you wanna do here.
}
int main(){
int out;
printf("Whatever\n");
out = ask();
count(out);
printf("%d", out);
return 0;
}