1

When I input a, the output is not a. Condition is true so why is the output not a?. When I use getchar instead of scanf_s, it works fine. What's the issue?

char op;
scanf_s("%c", &op);
if ( op == 'a' )
    printf("the character is a");
else
    printf("not a");
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Fahad Saleem
  • 413
  • 2
  • 13

3 Answers3

2

Try scanf() instead of scanf_s().

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
zulkarnain shah
  • 971
  • 9
  • 20
2

Specifier %c (two more such exceptions %s, %[) requires 3rd argument size-

scanf_s("%c", &op, 1);   // 1 to read single character
ameyCU
  • 16,489
  • 2
  • 26
  • 41
0

The third argument should be sizeof the type. scanf_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including <stdio.h>.

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>

int main()
{
char op;
scanf_s("%c", &op, sizeof(op));
if ( op == 'a' )
    printf("the character is a");
else
    printf("not a");
    return 0;
}