assigning float into int variable causes no warning

前端 未结 3 811
感情败类
感情败类 2021-01-18 07:27

So, given the following code:

int main(void) {
  int i;
  i = 12.1234;
  i++;
  return 0;
}

I compiled the code and I expected and wanted t

3条回答
  •  -上瘾入骨i
    2021-01-18 07:43

    You may expect that -Wall does enable ALL warnings, but it doesn't! There are a lot of warnings that doesn't even make sense in the wrong environment.

    It is an aggregation of the following flags:

      -Waddress   
      -Warray-bounds (only with -O2)  
      -Wc++11-compat  
      -Wchar-subscripts  
      -Wenum-compare (in C/ObjC; this is on by default in C++) 
      -Wimplicit-int (C and Objective-C only) 
      -Wimplicit-function-declaration (C and Objective-C only) 
      -Wcomment  
      -Wformat   
      -Wmain (only for C/ObjC and unless -ffreestanding)  
      -Wmaybe-uninitialized 
      -Wmissing-braces (only for C/ObjC) 
      -Wnonnull  
      -Wparentheses  
      -Wpointer-sign  
      -Wreorder   
      -Wreturn-type  
      -Wsequence-point  
      -Wsign-compare (only in C++)  
      -Wstrict-aliasing  
      -Wstrict-overflow=1  
      -Wswitch  
      -Wtrigraphs  
      -Wuninitialized  
      -Wunknown-pragmas  
      -Wunused-function  
      -Wunused-label     
      -Wunused-value     
      -Wunused-variable  
      -Wvolatile-register-var
    

    as described here: https://gcc.gnu.org/onlinedocs/gcc-4.8.2/gcc/Warning-Options.html

    What you need is -Wconversion as mentioned above. Another quite useful flag may be -Wextra.

提交回复
热议问题