简体   繁体   中英

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 int 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;

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);

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;

(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');

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