【问题标题】:C program not compiled properly on Objective-C Compiler [closed]C程序未在Objective-C编译器上正确编译[关闭]
【发布时间】:2013-02-08 19:10:34
【问题描述】:

我对 Objective-C 比较陌生 我知道它是 C 的超集。但是当我尝试在 GCC 中编译这个 C 代码时,它给了我一个错误,我无法执行代码。

  #include <stdio.h>  
  int main(){  
   int x;
   printf("Input int:\n");
   scanf("%d", x);
   printf("%d", x);
    return 0;             
}

当我执行其他代码时,没有错误,但输出错误。 注意:唯一的变化是“int *x”。

   #include <stdio.h>  
   int main(){  
   int *x;
   printf("Input int:\n");
   scanf("%d", x);
   printf("%d", x);
    return 0;             
}

  INPUT: 3
  OUTPUT: 2147307520

【问题讨论】:

  • 两种语言都错了。

标签: c objective-c pointers int scanf


【解决方案1】:

你需要像第一个那样为整数分配空间,但是你需要像第二个那样传递一个指向scanf()的指针——有点;您需要将整数的地址传递给scanf()。你应该写:

#include <stdio.h>  

int main(void)
{  
    int x;
    printf("Input int:\n");
    scanf("%d", &x);
    printf("%d\n", x);
    return 0;             
}

这是现在工作的 C 代码;它也有可能像 Objective C 一样工作。在打印输出的末尾也包含一个换行符是个好主意。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    • 1970-01-01
    相关资源
    最近更新 更多