c - Can't printf ASCII value for scanf_s value of variable -
trying user-input value character, display ascii integer number. doesn't want it--as keep getting blank output. however, if manually assign character letter let
works fine.
#include "stdafx.h" #include <stdio.h> #define eps 8.85e-12 int main() { int x = 1, y = 2, z; float p = 2.5, q = 4.8; double = 2.5e-9, b; char let; z = 2 * y; printf(" x/(y+z) = %.1f \n", (float)x/(y + z)); printf(" alphabet "); scanf_s("%c", &let); printf("ascii value of alphabet %d\n", let); printf("insert value of b :"); scanf_s("%lf", &b); printf("value of eps/(a*b)= %8.2e\n", eps / (a*b)); return 0; }
as per microsoft's documentation:
unlike
scanf
,wscanf
,scanf_s
,wscanf_s
require buffer size specified input parameters of typec
,c
,s
,s
, or string control sets enclosed in[]
. the buffer size in characters passed additional parameter following pointer buffer or variable.
so need is:
scanf_s("%c", &let, sizeof(let));
although, of these safe functions, they're no safer "unsafe" ones if know you're doing.
note i'm not including truly unsafe variants gets(str)
or scanf ("%s",str)
know , control how input you'll consume fine.
Comments
Post a Comment