输出与 c 中的 strcmp() 函数不匹配

Output does not match strcmp() function in c

为了通过 strcmp() 函数比较两个字符串,我通过 fgets()cin 获取了一个输入字符串,另一个在函数中作为默认参数给出。但是当我通过 strcmp() 比较它们时,函数输出不匹配。

    char a[20];
    int b;
    cin>>a;
    b=strcmp(a,"ab");
    cout<<b;

其中我将输入 a 作为 ab 并且 b 的值为 0 完全是 fine.But 当相同的输入被 fgets() 然后 strcmp() 输出与以前不同。

char a[20];
    int b;
    fgets(a,sizeof(a),stdin);
    b=strcmp(a,"ab");
    cout<<b;

其中 a 的值为 abb 的值为 1。为什么?是编译器问题还是其他问题?

fgets() 不去除任何换行符,每节 7.21.7.2 fgets the C standard 的函数:

The fgets function reads at most one less than the number of characters specified by n from the stream pointed to by stream into the array pointed to by s . No additional characters are read after a new-line character (which is retained) or after end-of-file. A null character is written immediately after the last character read into the array.