0

I've been trying different compilers available on the Codeforces submission page but none of the different compilers is giving an output such as Code::Block's

Problem link: https://codeforces.com/problemset/problem/282/A

Here is my code:

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

int main() {
int count = 0, i = 0, final;
int x = 0;
char strg[3];

scanf("%d", &count);

for (i = 0; i < count; i++){
    scanf("%s", &strg[0], &strg[1], &strg[2]);
    if ((strcmp(strg,"x++") == 0) || (strcmp(strg,"++x") == 0)){
        x = x+1;
    } if ((strcmp(strg,"x--") == 0) || (strcmp(strg,"--x") == 0)){
        x = x-1;
    }
}
printf("%d", x);

}

Submission page output: "wrong answer 1st numbers differ - expected: '1', found: '0'" However, Code::Blocks prints the correct value which is "1".

Codeforces submission:

Codeforces compiler output

Program running output:

Running the code from codeblocks

7
  • A C string is limited by a final end-of-string character '\0'. But since your variable has only space for 3 characters, the used test checker might put anything after the third character. -- Another quirk: Why are you providing three target arguments for just one format specifier at your call of scanf()? Additionally, you should check its return value. Commented Jun 21, 2020 at 17:04
  • I checked the return value and it's just the same as the printf() output, could you please further clarify what I should do? Commented Jun 21, 2020 at 19:58
  • Well, the return value of printf() has a completely different meaning than the return value of scanf(). Please read its documentation. -- Another thing to consider: Are you sure that "x" is lower case? Commented Jun 21, 2020 at 20:16
  • You might like to print the read instruction to see what you got, for example like this: printf("\"%s\"\n", strg); Commented Jun 21, 2020 at 20:18
  • I tried printing the read line as using the statement you mentioned, it showed the inputted result but it was between quotation marks, does that make a difference? and has "x" is lower case when inputted Commented Jun 21, 2020 at 20:52

1 Answer 1

1

The strg[3] is too short to accommodate the 2 chars string. It is an Undefined Behaviour.

As it is undefined it works in some environment and does not in another.

Sign up to request clarification or add additional context in comments.

1 Comment

I changed the 3 to a 10 and still no difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.