c - expected expression before 'long' -
i'm new programming, , i'm using book me write code solve ln(1+x) after user inputs "x". syntax way book has in example keep getting error: expected expression before 'long'
on line 28. line 28 line reads long double y = log(1+(x));
.
#include <stdio.h> #include <math.h> #define log(x) _generic((x),\ long double: log(1+(x))\ ) int main() { double x; printf("please enter number -1 +1: "); scanf("%f", &x); long double y = log(1+(x)); printf("from c math library: ln(1+x) = %f\n", y); }
if new programming, , trying make example work, not use macros @ stage focus on function , data type. start double
typical working type of library interfaced math.h
. example shows how calculate natural ln log()
function.
as others have commented, scanf()
needs format specifier of %lf
double
, printf()
needs format specifier of %f
double
. note have tested return value scanf()
function as as number input, check input behaved.
lastly, invite numbers in range -1 <= x <= 1
yet ln(0)
undefined, hence have restricted input range exclude -1
.
#include <stdio.h> #include <math.h> int main(void) { double x = 0, y; printf("please enter number -1 +1: "); if (1 != scanf("%lf", &x) || x <= -1 || x > 1) { printf("i need -1 < number <= +1\n"); return 1; } y = log(x + 1); printf("from c math library: ln(1+x) = %f\n", y); return 0; }
here sample outputs:
please enter number -1 +1: -1 need -1 < number <= +1 please enter number -1 +1: 0 c math library: ln(1+x) = 0.000000 please enter number -1 +1: 1 c math library: ln(1+x) = 0.693147
Comments
Post a Comment