dydx
dydx

Reputation: 35

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?

Upvotes: 1

Views: 281

Answers (4)

philcolbourn
philcolbourn

Reputation: 4122

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

Upvotes: 0

Dan Story
Dan Story

Reputation: 10155

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;

Upvotes: 4

Henri
Henri

Reputation: 5113

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

Upvotes: 0

t0mm13b
t0mm13b

Reputation: 34592

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;

Upvotes: 0

Related Questions