Reputation: 123
I am coding in Code-Blocks Editor with GNU GCC Compiler.I tried to use function strtod
which the below prototype:
double strtod(const char *a, char **b);
If I use the below code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
char *a;
a="99.5HELLO";
char *b;
printf("%.1lf\n%s", strtod(a, &b), b);
return 0;
}
I expect the Console Terminal to represent something like this after running the code:
99.5
HELLO
But what I actually got was something strange:
99.5
@
What's happening? Where did I make a mistake?
Upvotes: 2
Views: 347
Reputation: 477140
The order of evaluation of subexpressions is unspecified, and so the last function argument may be evaluated first and you end up reading the uninitialized value b
, which is undefined behaviour.
Order the evaluations:
const char *a = "99.5HELLO";
char *b;
double d = strtod(a, &b);
printf("%.1f\n%s", d, b);
Upvotes: 8