why does the order of variable declaring matter?

lagi

I'm fairly new to C and I have a small function which reads an input of a simple math operation (+,-,*,/) and then calculates the result accordingly and returns -nan if the input is incorrect.

float simple_math(void) {
float a, b;
int char_c;
int ret_a;


ret_a = scanf("%f %c %f", &a, &char_c, &b);
float result;

if (char_c == '+')
    result = a + b;
else if (char_c == '-')
    result = a - b;
else if (char_c == '*')
    result = a * b;
else if (char_c == '/')
    result = a / b;
else
    result = 0.0 / 0.0;

return result;
}

This code works just fine. However, if I change the order of the first two lines the return value is -nan.

int char_c;
float a, b;   // this was originally the first line
int ret_a;

Why does the order of the variable declarations matter?

Gopi
int char_c;

should be

char char_c;

%c is used to scan character and not int so your scanf will lead to undefined behavior.

The side effect of undefined behavior is sometimes things work as expected. So please get rid of undefined behavior it has nothing to do with the ordering of variable definitions.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

why does the order of variable declaring matter?

From Dev

Does the order matter when declaring classes in Java?

From Dev

Does variable order matter for sscanf?

From Dev

Does the order of declaring functions and methods in C++ matter

From Dev

Why does the order of css selectors matter?

From Java

Why does order of mutable borrows matter in Rust?

From Dev

Why does declaration order matter for generic members?

From Dev

Pandas: Why does order of selection matter?

From Dev

Why does the order of template argument substitution matter?

From Dev

Order of calling methods - why does it matter in this example?

From Dev

Why does order in method declaration matter?

From Dev

Why does this library dlopen order matter?

From Dev

why does order of loop nesting matter python?

From Dev

Why does declaration order matter for generic members?

From Dev

Why does the order of alternatives matter in regex?

From Dev

Why does the order of applying advice matter?

From Dev

Why does order in method declaration matter?

From Dev

why does order of loop nesting matter python?

From Dev

Why does the order of prerequisites matter in a makefile?

From Dev

Java : Why does placement of static variable matter?

From Java

Does annotations order matter?

From Dev

Does order of conditions in $and matter?

From Dev

Does the order of instance variable declaration matter in Objective-C?

From Dev

Does variable order matter while parcel read/write operation in Parcelable?

From Dev

Why one child class alone flings error ? does order matter?

From Dev

why does order matter in this escaped characters class in sed?

From Dev

Why does order matter in this usage of Observable.merge?

From Dev

Why does order matter when using "data" and "formula" keyword arguments?

From Dev

Why does order of `Object.include` and `Fixnum.prepend` matter?

Related Related

  1. 1

    why does the order of variable declaring matter?

  2. 2

    Does the order matter when declaring classes in Java?

  3. 3

    Does variable order matter for sscanf?

  4. 4

    Does the order of declaring functions and methods in C++ matter

  5. 5

    Why does the order of css selectors matter?

  6. 6

    Why does order of mutable borrows matter in Rust?

  7. 7

    Why does declaration order matter for generic members?

  8. 8

    Pandas: Why does order of selection matter?

  9. 9

    Why does the order of template argument substitution matter?

  10. 10

    Order of calling methods - why does it matter in this example?

  11. 11

    Why does order in method declaration matter?

  12. 12

    Why does this library dlopen order matter?

  13. 13

    why does order of loop nesting matter python?

  14. 14

    Why does declaration order matter for generic members?

  15. 15

    Why does the order of alternatives matter in regex?

  16. 16

    Why does the order of applying advice matter?

  17. 17

    Why does order in method declaration matter?

  18. 18

    why does order of loop nesting matter python?

  19. 19

    Why does the order of prerequisites matter in a makefile?

  20. 20

    Java : Why does placement of static variable matter?

  21. 21

    Does annotations order matter?

  22. 22

    Does order of conditions in $and matter?

  23. 23

    Does the order of instance variable declaration matter in Objective-C?

  24. 24

    Does variable order matter while parcel read/write operation in Parcelable?

  25. 25

    Why one child class alone flings error ? does order matter?

  26. 26

    why does order matter in this escaped characters class in sed?

  27. 27

    Why does order matter in this usage of Observable.merge?

  28. 28

    Why does order matter when using "data" and "formula" keyword arguments?

  29. 29

    Why does order of `Object.include` and `Fixnum.prepend` matter?

HotTag

Archive