-1

Hi~I'm just a beginner in C. I have some code like:

#include<stdio.h>
#include<math.h>

...
scanf("%lf %lf", &A[I], &B[I])
...

I need to input data like e or e^2 from keyboard. I tried "e" or "exp(1)" but they didn't work. Could anyone give me some suggestion? Any help is appreciated!

Claire Sun
  • 23
  • 2
  • 2
    Unfortunately "*they didn't work*" is not a very meaningful trouble report. Which behaviour did you observe, that did not satisfy which of your expectations? – alk Oct 07 '18 at 13:09
  • And BTW, how are `A` and `B` defined? – alk Oct 07 '18 at 13:10
  • If with "*e*" you are after the [Euler-Number](https://en.wikipedia.org/wiki/E_(mathematical_constant)) you need to enter it explicitly: `2.71828182845904523536028747135266249775724709369995[...]` – alk Oct 07 '18 at 13:20
  • @alk - althouth a `double` is only good for 20 significant figures, so entering all thos digits would serve no purpose ofver just `2.7182818284590452354`. – Clifford Oct 07 '18 at 13:23
  • 2
    Your conversion specifiers do not match your input. You appear to be entering an expression (e raised to the power 2), but scanf is not a calculator. It allows for numerical input in *scientific notation* using e-notation allowing for 20e+2 to mean *20 times 10 to the power of 2*, but beyond that you should parse and do the calculations yourself. Your caret (^) notation is certainly not supported by scanf directy. –  Oct 07 '18 at 13:30
  • don't use `scanf()`, use `fgets()` instead and parse the input yourself https://linux.die.net/man/3/fgets – cat Oct 07 '18 at 14:24

2 Answers2

4

The input to scanf() are strings, C is a general purpose and low-level programming language and will not interpret 'e' as Euler's number - 2.7182818... or any other common mathematical constant for that matter.

Moreover, scanf() is not an expression evaluator, so expressions such as e^2 or calls such as exp() will not be evaluated.

To do what you expect would require you to write an expression evaluator that recognised specific constants and operators. That is not a trivial task. An example you might adapt as well as an excellent explanation of the process can be found at http://www.arstdesign.com/articles/expression_evaluation.html. For other advice on expression-evaluator implementation see c expression Evaluator

Clifford
  • 88,407
  • 13
  • 85
  • 165
1

I am also new to programming. However, I may suggest the following:

int main() {
    float n;
    scanf("%f",&n);
    n = pow(2.718281,n);
    printf("%f\n",n);
}

Edit: We can't use ^ in C as power operator, instead we use pow() function.

Please suggest edits and corrections if I am wrong.

Ajay Yadav
  • 151
  • 1
  • 2
  • 11
  • I am not going to downvote this but `^` operator in C is `XOR` operation not for calculating power - use `pow` function instead from `math.h` header and use `-lm` option to compile if you are coding on `gcc`. – Gaurav Oct 07 '18 at 16:06
  • sure, thanks @Observer for the correction. We learn from our mistakes. – Ajay Yadav Oct 07 '18 at 17:02
  • @AjayYadav -- we do learn from our mistakes, but when you post an answer you are suggesting to someone with a question, and to future searchers with questions that you know what you are talking about. May I suggest at the very least that you try to compile and run the code you provide in your answers. This will alert you to some problems before publicly posting mistakes (it won't save you from all such mistakes), and you will learn from this process as well. Otherwise, welcome to SO. – ad absurdum Oct 07 '18 at 17:09
  • @DavidBowling i am honored sir. thank you. and i will definitely compile my code whenever I post it. – Ajay Yadav Oct 07 '18 at 17:42