gopi
gopi

Reputation: 25

The return value of function returning long long unsigned int is incorrect

When I call function wordToInt(c) from main, the return value is different from what it's printing inside the function.

main()
{
    long long  unsigned item;
    char c[6] = "ABCDE";
    item = wordToInt(c);
    printf("Main  The item is :%llu\n",item);
}

long long unsigned wordToInt(char *c)
{
    long long unsigned int k,item=0;
    int i,len;
    int j,p=0;
    len = (int) strlen(c);

    for(i = len-1;i>=0;i--)
    {
        if(c[i] == 'c')
            j = 42;
        else if(c[i] == '*')
            j = 99;
        else
            j = (int) c[i];
        j = j%100;
        k = pow(100,p);
        p++;
        item = item + (j*k);
    }

    printf("Function item is :%llu\n",item);
    return item;
}

The output of the program is:

Function item is :6566676869
Main  The item is :18446744071686293893

Can anyone tell me why there is inconsistency in the output?

Upvotes: 2

Views: 11479

Answers (4)

Reaz Murshed
Reaz Murshed

Reputation: 24211

I copied and ran your program and its working. I found the same result in both segments. I don't know why you're getting erroneous answer in the main function. Anyway, here's how i did it.

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>

long long unsigned wordToInt(char *c)
{

  long long unsigned int k,item=0;
  int i,len;
  int j,p=0;
  len = (int) strlen(c);

  for(i = len-1;i>=0;i--)
  {
          if(c[i] == 'c')
                  j = 42;
          else if(c[i] == '*')
                  j = 99;
          else
                  j = (int) c[i];
          j = j%100;
          k = pow(100,p);
          p++;
          item = item + (j*k);
  }
  printf("Function item is :%llu\n",item);
  return item;
}

int main()
{
    long long unsigned item;
    char c[5] = "ABCDE";
    item = wordToInt(c);
    printf("Main The item is :%llu\n",item);
    getch();
    return 0;
}

Then i compiled it with MinGW: gcc -c yourFileName.c And then for executable: gcc -o executable yourFileName.o

Upvotes: 0

neverhoodboy
neverhoodboy

Reputation: 1050

Add a declaration of wordToInt() before main(), or put the body of wordToInt() before main(). Either way, the compiler gets to know the prototype of wordToInt(), and only so can the arguments be passed and return value be returned correctly.

Upvotes: 2

KARTHIK BHAT
KARTHIK BHAT

Reputation: 1420

char array when given a string of characters always adds a NULL character to the end of the string automatically so make the size to 6

char c[6] = "ABCDE";

And the output is consistent when i ran the same code with and without changes.

For char size 6

Function item is :6566676869
Main  The item is :6566676869

For char size 5

Function item is :656667686803780408
Main  The item is :656667686803780408

EDIT: I added a prototype before function call.

Upvotes: 0

mah
mah

Reputation: 39797

First -- pay attention to your compiler's warnings, they matter!

Answer -- since you didn't prototype wordToInt() before using it, the compiler warned you that it had not been prototyped and it assumed the function returned an int (which is smaller than a long long)... so you got an int value returned, rather than a long long. The value printed within the function is correct because the function knew the proper type.

Upvotes: 7

Related Questions