0

I searched everything I tried, but I couldn't find an answer I need.

I was solving a problem called "Big Number A+B", with C language. The maximum value of A and B is 10^10000 so I couldn't solve it with unsigned long long, so I tried using char array to add it.

#include <stdio.h>

int main(void) {
    char tem1[10001], tem2[10001];
    char a[10001], b[10001], res[10002] = "";
    int carry = 0, sum = 0;

    scanf("%s%s", tem1, tem2);
    sprintf(a, "%010000s", tem1);
    sprintf(b, "%010000s", tem2);

    for (int i = 9999; i >= 0; i--) {
        sum = (a[i] - 48) + (b[i] - 48) + carry;
        carry = sum / 10;
        sum = sum % 10;
        res[i + 1] = sum + 48;
    }
    res[0] = carry + 48;

    int start = 0;

    while (res[start] == 48) {
        start++;
    }

    puts(res + start);
    return 0;
}

I declared 5 char arrays, two for save number temporary, two for numbers for adding, but I put zeros at front to make length exactly 10000, and last for to save result.

Because input was two integers at single line, I used scanf to read them. And whith sprintf, I put zeros to make length to 10000. Then inside for loop, I added a[i], b[i], and carry, and mod 10 to save char to array res. and after breaking out of the loop, I copied last carry to res[0].

Actual add is done, but there could be zeros at front of result, so I found where character wasn't '0', and printed it from there.

I put numbers to this program, and everything worked fine. But when I submited this code to judge, it says Output Limit Exceeded, why does it?

I'm sorry if my english was bad, or question was too stupid. Thank you.

  • 1
    Please don't use [*magic numbers*](https://en.m.wikipedia.org/wiki/Magic_number_(programming)). If by `48` you mean the ASCII value for the digit zero, use the actual character `'0'` instead. – Some programmer dude Dec 29 '20 at 05:49
  • I assume the "judge" is some kind of an online platform, which one? Is there some particular format it expects the answer to be printed out? – vmt Dec 29 '20 at 05:51
  • Thanks for tip. I'll try to use actual character value. – Junhyuk Han Dec 29 '20 at 05:51
  • @vmt It's korean judge named [Baekjoon](https://acmicpc.net), it says on it's decription, if output is longer then 2*(correct answer)+1024byte, result became **Output Limit Exceeded**. – Junhyuk Han Dec 29 '20 at 05:56
  • 1
    My compiler complains that *"Flag '0' results in undefined behavior with 's' conversion specifier"*. The C standard agrees with that. So the judge may not be zero padding the strings the way you expect. Find a different method to zero pad the strings. – user3386109 Dec 29 '20 at 06:34
  • 1
    I suggest you read your `scanf` documentation, and (a) ensure your target buffers aren't breached (`%10000s`) and use two `scanf` calls, not one, to exploit the value of using `%n` (which you're currently not using). There's a lot of work here that is frankly unnecessary. – WhozCraig Dec 29 '20 at 06:44
  • `sprintf(a, "%010000s", tem1);` is not doing what you think. Did you even try to print `a` ? – Support Ukraine Dec 29 '20 at 07:31
  • Consider this: How many chars is needed for holding `10^2` ? How many chars is needed for holding `10^3` ? How many chars is needed for holding `10^4` ? .... How many chars is needed for holding `10^10000` ? – Support Ukraine Dec 29 '20 at 07:33
  • @4386427 Yes, and I don't know why, it worked as I thought. – Junhyuk Han Dec 29 '20 at 07:34
  • `scanf("%s` is something you should never ever do. Always put in a number to prevent buffer overflow. Like `scanf("%10000s` – Support Ukraine Dec 29 '20 at 07:36
  • Please see https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users and compare what is described there to your "I searched everything I tried, but I couldn't find an answer I need.". I am open to discussing whether your post demonstrates the expected level of research effort with that quoted part. Consider deleting it to avoid drawing attention to the amount of demonstrated effort, in case you think it risks drawing the wrong kind of attention. – Yunnosch Dec 29 '20 at 08:46

1 Answers1

-1

padding specifier %0 only works for numeric values, not for string. You may hack it as below:

sprintf(a, "%0*d%s", 10000-(int)strlen(tem1), 0, tem1);
sprintf(b, "%0*d%s", 10000-(int)strlen(tem2), 0, tem2);

printf("%*d", w, v) is to print v with width w.

delta
  • 3,778
  • 15
  • 22
  • This won't work when `tem1` has 10000 (or more) chars – Support Ukraine Dec 29 '20 at 07:30
  • 1
    @4386427 yes, it is, but it is irrelevant from OP. – delta Dec 29 '20 at 08:25
  • @4386427 In this case, you should post something helpful. From the OP, it is clear that it is from some intro to C courses, but you are just trying to spread your some C safe coding guidelines. It won't be helpful for beginners either. IMHO, the first thing for someone starting to learning C, it to know where the bug is, how to get it to work, how it works. When you are more experienced, performance, safeness, etc are considered. – delta Dec 31 '20 at 00:57