1

i'm trying to multiply 2 integers and I always get a negative number, can somebody help me? (with this function, i'm trying to make from this string "3924456639" a integer)

here is my code:

long temp = 0;

fgets(buf, sizeof buf, stdin);

for(int i = 0; i < sizeof buf ;i++){
  if(buf[i] != ' ' && buf[i] != '\n'){
    temp *=  10;// <- gets negative
    temp = temp +  buf[i] - '0';

  }}
bimbam
  • 11
  • 2

3 Answers3

4

You need to check what is sizeof(long) on your system, its probably 4.

32 bit long variable will hold max value of 2147483647, if you store bigger value in it it will go in negative range, so you need to declare temp unsigned long

Pras
  • 4,047
  • 10
  • 20
  • thank you for your answer , i tried it with this code: int m = 3924456639; unsigned long m1 = (long ) 3924456639; printf("%d\n", sizeof m1); printf("%d\n", m1); printf("%d\n", m); and i had this output: 8 -370510657 -370510657 – bimbam Jun 04 '17 at 11:37
  • @bimbam change to `unsigned m = 3924456639u; unsigned long m1 = 3924456639u; printf("%zu\n", sizeof m1); printf("%lu\n", m1); printf("%u\n", m);` – Weather Vane Jun 04 '17 at 11:58
1

You can use strtol. No need to re invent the wheel.

long int strtol(const char *str, char **endptr, int base)
KALALEX
  • 430
  • 1
  • 5
  • 19
  • it works with strtol (so i get long int 3924456639 ) but why can't i just do something like: long var = 3924456639; ? – bimbam Jun 04 '17 at 11:54
  • what do you mean i just did long var = 3924456639; and it works @bimbam – KALALEX Jun 04 '17 at 12:17
  • which compiler do you use ? – KALALEX Jun 04 '17 at 12:18
  • gcc , my problem was : printf("%d\n", m1); =>so i tried to print long with %d , the right argument for long is %ul – bimbam Jun 04 '17 at 12:22
  • hehe np ;D you should always check for std functions do not bother making your own they use them because they are just the fastest way. So not only it already exists but also its the fastest way. – KALALEX Jun 04 '17 at 12:24
1

Your main problem is that your loop steps through all of the buffer buf instead of only stepping through the '\0' terminated string it contains.

For example, if we assume buf was declared as char buf[100]; and we enter the string "3924456639" then buf will contain:

{'3','9','2','4','4','5','6','6','3','9','\n','\0', ... and 88 bytes of garbage}

then your loop will correctly step through the digits and ignore the '\n'. But then it does not stop. Since '\0' is neither ' ' nor '\n', temp gets multiplied by ten and '\0' - '0' is added. And the same goes for every one of those 88 bytes of garbage that is neither ' ' nor '\n'.

To fix this, change:

for(int i = 0; i < sizeof buf ;i++){

to:

for(int i = 0; i < strlen(buf) ;i++){

or:

for(int i = 0; buf[i] != '\0' ;i++){

(Remember to #include <string.h> if you want to use strlen().)

Note:

  • Your minor problem is that long might be too small for your number.
  • Like @KALALEX pointed out, you could use strtol().
SiggiSv
  • 1,219
  • 1
  • 10
  • 20