Mina Basem
Mina Basem

Reputation: 11

Codeforces compiler shows a different output than Code::Blocks (Question 282A - Bit++)

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

Upvotes: 0

Views: 334

Answers (1)

0___________
0___________

Reputation: 67476

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.

Upvotes: 1

Related Questions