简体   繁体   中英

int64 c++ debian6

why cant i get this int64 working? i compile with g++ -x c++ -o program source.c it keeps starting over with -2147483648 above 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

I think you should replace

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

with

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

Using the wrong format specifier is undefined behaviour. AFAIK, using %d as the format specifier in gcc forces only 32 bits to be printed out - thus resulting in what looks like overflows in your output file.

int64_t limit = a1 + 99999999999;

整数常数太大。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM