[英]Compiler error in C, Error : Expected Expression
我遇到了一個我真的不知道如何解決的問題。 我主要在 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 - 用戶輸入,當它得到正確的數字時,它會打印“你的號碼是:”(數字)
問題(正如已經指出的)是您實際上並沒有從 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
您使用的打印格式不正確。 您正在使用此代碼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);
integer 的格式說明符是%d
。 %f
用於浮點值。 %c
為字符。 s
代表一個字符串等等。
在 C 中,我們使用%
來指定輸出/輸入類型。 %d 代表 integer,%f 代表浮點數,%c 代表字符,%s 代表字符串,這就是你的基本知識。
對於 printf:
printf("%d", varname);
對於掃描:
scanf("%d", &varname);
'&' 表示 memory 中的位置。
你的程序有很多語法錯誤,下面是一些代碼:
#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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.