2013-11-24 35 views
-1

好吧所以即時通訊設法要求用戶輸入一個數字,然後將該數字分配給變量,這顯然沒有發生。我也希望它能夠打印變量。請解釋什麼時候變量被分配,以及當我要求變量時數字的位置。在C程序中使用新變量

蟒蛇即將所以不知道自己還能做些什麼,但要求它這樣

​​

我想做的事情,在C

#include <stdio.h> 

int main() 
{ 
    int this_is_a_number; 

    printf("Please enter a number: "); 
    scanf("%d", &this_is_a_number); 
    printf("You entered %d", this_is_a_number); 
    a=%d 
    printf(a) 
    return 0; 

} 

這是爲什麼人們不工作,我想它解決的問題 的#include

int main() 
{ 
    int this_is_a_number; 
    int a; 

    printf("Please enter a number: /n"); 
    scanf("%d", &this_is_a_number); 
    printf("You entered %d", this_is_a_number); 
    a=this_is_a_number 
    printf("%d/n",&a) 

    return 0; 

} 

下面是最近ç錯誤頌:

Building m.obj. 
C:\Users\Admin\Google Drive\firstCShit\m.c(4): warning #2117: Old-style function definition for 'main'. 
C:\Users\Admin\Google Drive\firstCShit\m.c(12): error #2001: Syntax error: expected ';' but found 'printf'. 
C:\Users\Admin\Google Drive\firstCShit\m.c(14): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'int *'. 
C:\Users\Admin\Google Drive\firstCShit\m.c(14): error #2001: Syntax error: expected ';' but found 'return'. 
*** Error code: 1 *** 
Done. 
+3

「這沒有發生」究竟發生了什麼? – tobyodavies

+0

你問'a =%d'嗎?因爲這應該會引發編譯器錯誤。 – Leonardo

+0

你是指'int a = this_is_a_number; printf(「%d \ n」,a);'? – gongzhitaao

回答

1

你嘗試的線路故障A線:

#include <stdio.h> 

int main() 
{ 
    // Reserve the space in which to store the number 
    int this_is_a_number; 

    // Output a string, note no newline (\n) at the end of 
    // the string means this will probably not be printed 
    // before moving to the next statement 
    printf("Please enter a number: "); 

    //read in an integer (%d) and store it at the address of the variable (this_is_a_number) 
    scanf("%d", &this_is_a_number); 

    // Print the number entered, again no newline, but stdout should be flushed 
    printf("You entered %d", this_is_a_number); 

    // This line is syntactically incorrect and makes no sense 
    a=%d 

    // This line is semantically incorrect, probably a type error 
    // printf requires a format string, like in the example two lines up 
    printf(a) 

    // Exit successfully 
    return 0; 

} 

爲了解決您的編輯,相當於C是:

#include <stdio.h> 

int main() 
{ 
    int h; 
    printf("Please enter a number:\n"); 
    scanf("%d", &h); 
    printf("%d\n", h); 
    return 0; 
} 
+0

爲什麼這不行? – user3026468

+0

@ user3026468 *什麼*不起作用? – tobyodavies

+0

的#include \t INT主() \t { \t INT this_is_a_number; \t \t int a; \t printf(「請輸入數字:/ n」); \t scanf(「%d」,&this_is_a_number); \t printf(「您輸入%d」,this_is_a_number); \t一個= this_is_a_number \t \t的printf( 「%d/n的」,&一個) \t \t \t 返回0; \t} – user3026468

2
  1. 您還沒有聲明的變量a
    與this_is_a_number一起聲明,如int this_is_a_number, a;
  2. 你的任務是錯誤的說法
    使用a = this_is_a_number;
  3. 的printf需要一個格式說明。
    printf("%d", a);