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.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why does the order of alternatives matter in regex?

분류에서Dev

Why does the order of applying advice matter?

분류에서Dev

Why does the malloc order matter when a struct is sufficiently sized?

분류에서Dev

Why does Order matter in Kwarg parameters in MagicMock asserts?

분류에서Dev

Does the order of var in the javascript context matter?

분류에서Dev

Does the order of entries in the routing table matter?

분류에서Dev

Why does hyper threaded or Multi-threaded CPU matter?

분류에서Dev

Declaring vs Initializing a variable?

분류에서Dev

Why does this SQL order null values last?

분류에서Dev

Why does my C program print out the same output no matter what I put for the input?

분류에서Dev

is there a convention for localhost ports ... does it matter?

분류에서Dev

Why does UISearchDisplayController has to be a class variable?

분류에서Dev

Why MySQL does not return rows in same order as appeared in IN clause?

분류에서Dev

Declaring variable inside functions of a class in C++

분류에서Dev

Grammar Rules for syntax on declaring a variable in Perl?

분류에서Dev

Why both of if statements are executed no matter what the input is?

분류에서Dev

Does it matter that my deb version is not Debian native?

분류에서Dev

What does ... mean after declaring a widget type?

분류에서Dev

Why does the object that this Proc's variable refers to change?

분류에서Dev

Why does using a temporary variable to recycle a java object in XPages work?

분류에서Dev

Why does variable substitution work in this awk one-liner?

분류에서Dev

Why does bash need && to echo a variable on one line?

분류에서Dev

Why does grep ignore the shell variable containing directories to be ignored?

분류에서Dev

Error at declaring a private variable with file_get_contents

분류에서Dev

Declaring initialized C++ static variable (syntax disambiguation)

분류에서Dev

python some case of declaring muti-variable confused me

분류에서Dev

Initialization of a wchar_t array with wmemset. Does encoding matter?

분류에서Dev

Sort variable contents in numeric order

분류에서Dev

Why does the first one work and the second one not work? Global and Private variable declaration

Related 관련 기사

  1. 1

    Why does the order of alternatives matter in regex?

  2. 2

    Why does the order of applying advice matter?

  3. 3

    Why does the malloc order matter when a struct is sufficiently sized?

  4. 4

    Why does Order matter in Kwarg parameters in MagicMock asserts?

  5. 5

    Does the order of var in the javascript context matter?

  6. 6

    Does the order of entries in the routing table matter?

  7. 7

    Why does hyper threaded or Multi-threaded CPU matter?

  8. 8

    Declaring vs Initializing a variable?

  9. 9

    Why does this SQL order null values last?

  10. 10

    Why does my C program print out the same output no matter what I put for the input?

  11. 11

    is there a convention for localhost ports ... does it matter?

  12. 12

    Why does UISearchDisplayController has to be a class variable?

  13. 13

    Why MySQL does not return rows in same order as appeared in IN clause?

  14. 14

    Declaring variable inside functions of a class in C++

  15. 15

    Grammar Rules for syntax on declaring a variable in Perl?

  16. 16

    Why both of if statements are executed no matter what the input is?

  17. 17

    Does it matter that my deb version is not Debian native?

  18. 18

    What does ... mean after declaring a widget type?

  19. 19

    Why does the object that this Proc's variable refers to change?

  20. 20

    Why does using a temporary variable to recycle a java object in XPages work?

  21. 21

    Why does variable substitution work in this awk one-liner?

  22. 22

    Why does bash need && to echo a variable on one line?

  23. 23

    Why does grep ignore the shell variable containing directories to be ignored?

  24. 24

    Error at declaring a private variable with file_get_contents

  25. 25

    Declaring initialized C++ static variable (syntax disambiguation)

  26. 26

    python some case of declaring muti-variable confused me

  27. 27

    Initialization of a wchar_t array with wmemset. Does encoding matter?

  28. 28

    Sort variable contents in numeric order

  29. 29

    Why does the first one work and the second one not work? Global and Private variable declaration

뜨겁다태그

보관