1
votes

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?

4
got it to work now, thanks guys for the fast response, I'm going to bookmark and use this site more often!!! - dydx
Back when I worked with barcodes (a long time ago), we always treated them as strings, not numbers. - anon
dydx: don't forget to mark an answer as "accepted". - caf
will do, thank you. @Neil, I can't use strings/arrays/functions for my work. - dydx
Why on earth not? And you are using a function - scanf() - anon

4 Answers

4
votes

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;
0
votes

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;
0
votes

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

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