0

I am writing a program to enter and display datatypes in C language. I can enter and display int,float,long double,but cannot enter and display char.

int a;
float b;
long double c;
char d;

printf("\nEnter the integer value");
scanf_s("%d", &a);
printf("\nThe integer value is %d", a);

printf("\nEnter the float value");
scanf_s("%f", &b);
printf("\nThe float value is %f", b);

printf("\nEnter the long double value");
scanf_s("%lf", &c);
printf("\nThe double value is %lf", c);

printf("\nEnter the char value");
scanf_s("%c", &d,1);
printf("\nThe char value is %c", d);
  • 1
    You need `scanf_s(" %c", &d, 1);` note the added space before `%c`. Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). Aside: please get into the habit of placing the `\n` newline at the *end* not the start. – Weather Vane Aug 21 '19 at 15:58
  • Try `scanf_s(" %c", &d,1);` instead. Note the extra leading space in the format. And if you print the decimal value of `d` together with the decimal value of `'\n'` (or used a debugger to see the value of `d` directly) then you would hopefully understand what happens. – Some programmer dude Aug 21 '19 at 15:59
  • Also, for `long double` the format should be `%Lf`. Your format `%lf` is for `double` . However, I believe Microsoft implements `long double` as `double` so in the example it "works". – Weather Vane Aug 21 '19 at 16:05
  • You would have got a clue had you put delimiters round your printf, which is always a good idea when debugging: printf("\nThe char value is <%c>", d); – Gem Taylor Aug 22 '19 at 10:37

0 Answers0