开发者

a newbie gcc compiler and c language question

when I'm trying to compile my c program it gives me this error warning: integer constant is too large for 'long' type

which refers to these lines

int barcode, a, b, c;
scanf("%d", &barcode);
a = barcode / 1000000000000;
b = barcode / 100000000000 % 10;
c = barcode / 10000000000 % 10;

and the rest is fine. I know I'm not supposed to use in开发者_如何学Got for such a large number, any suggestions on what I should use? if I replace int with double what should the '%d' part be replaced with then?


Use long longs instead of ints for integer values of that size, with the LL literal suffix:

long long barcode, a, b, c;
scanf("%lld", &barcode);
a = barcode / 1000000000000LL;
b = barcode / 100000000000LL % 10;
c = barcode / 10000000000LL % 10;


Change the datatype to long as shown here

long barcode, a, b, c;
scanf("%ld", &barcode);
a = barcode / 1000000000000L;
b = barcode / 100000000000L % 10;
c = barcode / 10000000000L % 10;


You should replace your int with long int, and prefix the constants with L

Like this:

long long unsigned int barcode;
barcode = 100000000LL;
printf("Barcode is %li", barcode);


(if you like an answer, perhaps you could select one?)

Also, you might consider placing the barcode in a string instead. This way you can access the first 3 digits that you need more easily (if that is what you roughly want - note: you do want c to be the first two digits after the first digit, correct?)

char barcode[14];
int a, b, c;
scanf("%s", &barcode);
a = (int) (barcode[0] - '0');
b = (int) (barcode[1] - '0');
c = b * 10 + (int)(barcode[2] - '0');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜