在C程序中使用C全新的变量

时间:2013-11-24 04:15:13

标签: c

好吧所以我试图让用户输入一个数字,然后将该数字分配给变量,这显然不会发生这种情况。我还希望它然后打印变量。请解释何时分配变量以及当我要求变量时数字的位置。

来自python,所以不知道还能做什么但是这样问一下

h=raw_input('please enter number')
print h

我想在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.

2 个答案:

答案 0 :(得分:2)

  1. 您尚未声明变量a
    与{_ 1}}
  2. 之类的this_is_a_number一起声明它
  3. 您的转让声明错误 使用int this_is_a_number, a;
  4. printf需要格式说明符 a = this_is_a_number;

答案 1 :(得分:1)

您的尝试逐行细分:

#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;
}