int64 c ++ debian6

时间:2011-04-23 11:38:23

标签: c++ c gcc

为什么我不能让这个int64工作? 我用g ++ -x c ++ --o program.c编译 它继续以-2147483648重新开始于2147483647 ....

#include <stdint.h>
#include <inttypes.h>
#ifdef __cplusplus
#include <cstdio>
#include <cstdlib>
#include <cstring>
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#endif

int main(int argc, char* argv[])
{
    int64_t i;
    for(i = 0; i < argc; ++i)
        printf("argv[%d]: %s\n", i, argv[i]);

    char string [512];
    int64_t a1 = atoi((const char*) gets(string));
    int64_t limit = a1 + 99999999999

    while(a1 <= limit)
    {
        char command[10000];
        sprintf(command, "%d", a1);
        FILE* pFile = fopen ("myfile.txt","wa");
        fprintf (pFile, "%s\n", command);
        fclose (pFile);

       a1= a1 + 4321;
    }
    return EXIT_SUCCESS;
}

C

2 个答案:

答案 0 :(得分:4)

我认为你应该替换

sprintf(command, "%d", a1);

sprintf(command, "%lld", a1);

使用错误的格式说明符是未定义的行为。 AFAIK,使用%d作为gcc中的格式说明符,只强制打印出32位 - 从而导致输出文件中出现溢出。

答案 1 :(得分:2)

int64_t limit = a1 + 99999999999;

整数常数太大。