1

I have the following code to just implement the pow() as in math.h library. Well I have two questions:

  1. Why the scanf() statement is taking input before it prints "power:" from the printf() function?
  2. How can I calculate the power of big integers, like suppose the power calculated is like : 22235645654789787978797797 (just for example). How do I calculate and print it.

    #include <stdio.h>
    unsigned long long int pow(unsigned long long int n,unsigned long long int d);
    int main(){
    
    unsigned long long int a,x,n;
    printf("Number:");
    scanf("%u\n",&a);
    printf("power:");
    scanf("%u\n",&n);
    x = pow(n,a);
    printf("%u\n",x);
    }
    unsigned long long int pow(unsigned long long int n,unsigned long long int d){
    if(n+1==1)
        return 1;
    return d*pow(n-1,d);
    }
    

Image

In the image you can see the input of variable n is taking input before printing "power:" from printf(), so no matter what input do you type.

Please help me understand it. Open for any suggestions and comments.

bhansa
  • 7,282
  • 3
  • 30
  • 55
  • The first question has been answered, for the second you should find (or write) a bigint library. – Weather Vane Jun 25 '16 at 11:02
  • See [\[ this \]](http://stackoverflow.com/a/21860015/1620779) answer. – sjsam Jun 25 '16 at 11:19
  • 1
    Possible duplicate of [\[ this \]](http://stackoverflow.com/questions/21859277/behaviour-of-scanf-when-newline-is-in-the-format-string) question. – sjsam Jun 25 '16 at 11:19
  • when compiling, always enable all the warnings, then fix those warnings. (for `gcc`, at a minimum use: `-Wall -Wextra -pedantic` I also use: `-Wconversion -std=gnu99` ) Then the compiler will output messages about a bunch of problems with the posted code. – user3629249 Jun 27 '16 at 00:45
  • do NOT use 'well known' system function names (like `pow`) rather, use a unique name, (like `myPow`) – user3629249 Jun 27 '16 at 00:46
  • variable names should indicate 'usage' or 'content' or better, both. Variable names like 'a', 'n', and 'x' are meaningless, even in the current context. – user3629249 Jun 27 '16 at 00:48
  • when calling any of the `scanf()` family of functions, always check the returned value (not the parameter value) to assure the operation was successful. – user3629249 Jun 27 '16 at 00:52

4 Answers4

5

Your printfs and scanfs are executed in order. The output looks messed up because of buffering.

When "power:" gets printed, it ends up in the buffer that does not get flushed until a number gets entered. Adding \n solves this for console output. You can also call fflush(STDOUT) explicitly to print buffered strings to the screen.

Note: You are not reading/printing unsigned long long correctly. Use llu, or see this Q&A for proper format specifiers for 64-bit unsigned integers:

unit64_t a, x;
...
scanf("%"SCNu64, &a);
printf("n: %"PRIu64"\n", x);
Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

You should not use scanf("%u\n",&n), use scanf("%u",&n); without the \n. It will work flawlessly. For the second question, those kind of calculations are not usually seen in the C programming language. It will lead to a hidden buffer overflow, maybe someone else will answer better then me.

Giulio Paoli
  • 85
  • 11
1

the problem is in the format strings for the calls to scanf() and printf() functions.

The format string should not contain the '\n'

all the scanf() format strings and theprintf() format string are using '%u' but they should be using '%llu' so they match the variable type

Note: Even with the corrected code, if the resulting number is sufficiently large, it will overflow the 'unsigned long long int' variable, and the program will fail.

Here is the corrected code, examine carefully

#include <stdio.h>
#include <stdlib.h>

unsigned long long int myPow(unsigned long long int n, unsigned long long int d);

int main( void )
{
    unsigned long long int a,x,n;

    printf("Number:");
    if( 1 != scanf("%llu",&a) )
    {
        perror( "scanf for first number failed");
        exit( EXIT_FAILURE );
    }

    printf("power:");
    if( 1 != scanf("%llu",&n) )
    {
        perror( "scanf for second number failed" );
        exit( EXIT_FAILURE );
    }

    printf( "n=%llu, a=%llu\n", n, a);
    x = myPow(n,a);
    printf("%llu\n",x);
}

unsigned long long int myPow(unsigned long long int n,unsigned long long int d)
{
    if(n+1==1)
        return 1;
    return d*myPow(n-1,d);
}
user3629249
  • 16,402
  • 1
  • 16
  • 17
0

I modified your code slightly to format the number correctly, and I think that the program now does what you want.

#include <stdio.h>
unsigned long long int pow(unsigned long long int n,unsigned long long int d);
int main(){

    unsigned long long int a,x,n;
    printf("Number:");
    scanf("%llu",&a);
    printf("power:");
    scanf("%llu",&n);
    x = pow(n,a);
    printf("%llu\n",x);
}
unsigned long long int pow(unsigned long long int n,unsigned long long int d){
    if(n+1==1)
        return 1;
    return d*pow(n-1,d);
}

Test

Number:2
power:10
1024
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • It is working fine with your example but suppose I take a value like Number: 256 and power: 25 it is giving me the result `0` How I didn't understand? – bhansa Jun 25 '16 at 11:02
  • 1
    256 has its 8 least significant bits equal to 0. 256 squared has 16 of them, 256 cubed has 24 and so on. So 256 power 25 will have so many they take up the whole variable, and the rest of the number is truncated. – Weather Vane Jun 25 '16 at 11:08
  • Nice, but this doesn't answer the question as mentioned in its title. – sjsam Jun 25 '16 at 11:23