【问题标题】:What is wrong with sizeof programsizeof 程序有什么问题
【发布时间】:2013-07-31 11:47:30
【问题描述】:

我编写了一个程序来显示使用 sizeof 运算符存储 int 类型和 char 类型的值所需的字节数。但是,它没有编译,它说“Id 返回 1 退出状态”。我该如何解决这个问题?

代码如下:

#include <stdio.h>

main()
{
    int num, num1;

    num = sizeof(int);
    num1 = sizeof(char);
    printf("Bytes required for int:  %d   Bytes required for char: %d\n", num, num1);
    getchar();
}

谢谢

【问题讨论】:

  • getchar(); 之后和} 之前,添加一行return 0;main() 函数应该返回终止状态,即int--因此出现此警告。
  • @GrijeshChauhan 我不认为这是问题所在,但我也不知道这是什么问题
  • @GrijeshChauhan 你可能读错了。 OP 说it does not compile
  • 也不是问题,但您应该将printf() 的格式和参数与printf("… %d … %d\n", (int)num, (int)num1); 匹配(可能有许多其他解决方案)。
  • 权限被拒绝?也许您已经在运行程序,而编译器在编写可执行文件时遇到问题,因为它是打开的。

标签: c char int sizeof


【解决方案1】:

代码是正确的,也许您可​​以尝试复制代码并将其粘贴到新文件中,然后删除旧文件。我希望这有效:)

【讨论】:

    【解决方案2】:

    我可以运行它,在这里试试:http://www.compileonline.com/compile_c_online.php 或者你可以这样编译: $gcc main.c -o demo -lm -pthread -lgmp -lreadline 2>&1 我得到的结果:

    int 所需字节:4 char 所需字节:1

    【讨论】:

      【解决方案3】:

      此代码仅是正确的。它没有问题。 在 ideone 上检查一下。

      # 1:   hide   clone   input   7 seconds ago
      
      result: Runtime error       time: 0s    memory: 2252 kB     signal: -1
      input: no
      output:
      
      Bytes required for int:  4   Bytes required for char: 1
      

      【讨论】:

        【解决方案4】:

        你应该为你的主要功能使用标准格式,可以是:

        int main() {
             //your code
             return 0;
        }
        

        这将导致以下代码:

        #include <stdio.h>
        
        int main() {
            int num, num1;
        
            num = sizeof(int);
            num1 = sizeof(char);
            printf("Bytes required for int:  %d   Bytes required for char: %d\n", num, num1);
            return 0;
        }
        

        这在 GCC4.7.2 (here) 中编译

        【讨论】:

          猜你喜欢
          • 2019-03-12
          • 2015-02-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多