16 C language compiler of Warning of the type of solutions

  • 2020-04-02 02:41:25
  • OfStack

When the compiler finds a problem somewhere in the program, it gives a warning that something might go wrong. The warning message may indicate a big bug lurking in the program, or it may be correct. The right way to handle warnings is to eliminate them as much as possible. Every warning given by the compiler should be carefully analyzed to see if something is really wrong. Only those warnings that are not problematic can be ignored.

Description:

Because of the compilation of warnings of all kinds, can not be listed, the following is a more typical list of some warnings, there are some warnings, as long as you take the literal meaning, you can quickly find out and solve it.

Type 1:
Warning: implicit declaration of function 'Example()'.

Reason for warning:

You call the function Example() in your.c file, but you don't include the corresponding.h file that declares the function.
It's possible that you define the body of the function in a.c file, but you don't declare it in.h.
Solutions:

You can add: extern Example() at the beginning of the.c file that calls this function.
You can include the header file declaring the function Example() in the.c file that calls this type of function.
If you define the body of the function in a.c file, but you don't declare it in.h, you can also generate an.h file and add your function declaration.
Similar warning:

Warning: type mismatch with previous implicit declaration
Warning: type mismatch with previous implicit declaration
Warning: previous implicit declaration of 'Example()'
 
Type 2:
Warning: unused variable 'param'

Reason for caution: obviously, you define the variable 'param' and don't use it at all.

Solution: delete it if you don't need it.

Type 3:
Warning: statement with no effect.

Reason for warning: it's possible that in your file, you do this with #define MACROPRINT

And then at some point you define #define MACROPRINT printf. Then you quote everywhere

MACROPRINT(" HELLO "), so that there is no error, but the warning "this declaration is useless" occurs.

Solution: delete #define MACROPRINT.

Type 4:
Warning: int format, long int arg (arg 3)

Warning reason: printf("%s%d, szDebugString, ulGwId); Your ulGwId is an unsigned long, and the output you choose for it is "%d" (this format is -int for integer type).

Solution: for this type of error, you only need to make the parameter types consistent. As shown above, you only need to change "%d" to "%d".

Warning :comparison between pointer and integer

Type 5:
Warning: comparison is always 0 due to limited range of data type

Warning reason: you may have defined unsigned int uParam; But you did if(uparam) < The judgment of 0),

Data of type unsigned int is always > =0, so this comparison gives a warning because the data type limits its scope.

Solution: remove this judgment.

Type 6:
Display: warning: control reaches the end of a non - void function

Why it's a warning: you may have written one of these warnings


unsigned long FuncA()
{
  if()
  {
    return ulValue;
  }
  if()
  {
    return ulValue;
  }
}

In a function like this, you might not enter in either of the if statements, and then you have no value to return at all before exiting the function.

Solution: if a function has a return value, make sure that the function has a return value in all cases.

Warning :'return' with no value, in function returning non-void

Type 7:
Warning: overflow in implicit constant conversion

Reason for caution: transformations of variables can lead to numerical overbounds.


#define RET_PRODUCTID 0x10000000
#define ERR_RET_GLOBAL RET_PRODUCTID+5000
#define RET_USER ERR_RET_GLOBAL+5000
#define USER_OK RET_USER+0
#define USER_FAIL RET_USER+1

If defined this way, short Func(){return USER_OK} is encountered, which warns that the bounds have been crossed.

Solution: determine the range of good values.

Type 8:
Warning: 'ulParam' might be used uninitialized in this function

Reason for caution: when ulParam is the right value of an expression, you have not initialized this parameter before.

Such as:


void Func()
{
  ulong ulParam;
  ulong ulRetCode;
if( ... )
{
  ulParam =  ... ;
}
if( ... .)
{
  ulParam =  ... ;
}
  ulRetCode = ulParam;
}

In this case, ulParam is never assigned at all when neither if() can be executed, which makes it dangerous to assign ulRetCode.

Solution: pay more attention and be more careful.

Type 9:
Warning: passing arg 1 of 'free' makes pointer from integer without a cast

Warning reason: you are free(a), but a is an unsigned long, you may have a pointer value in a.

Solution: when free(a), cast a as pointer type. Namely: free (a) (char *).

Warning: assignment from an incompatible pointer type

Warning: initialization from incompatible pointer type

Warning :passing arg 2 of 'AOS_MemCopy_X' makes pointer from integer without a cast

Type 10:
Display: warning: 'MY_DEBUG' redefined

Warning: this is the location of the previous definition

Reason for warning: two warnings in a row, one possibility is that you include two.h's in your.c file

And both.h files declare MY_DEBUG.

Solution: only declare these things in one file.

Type 11:
Warning: value computed is not used

Reason for caution: the values involved in the operation are not valid. Let's say you do this:

Char * p;

* p++;

It doesn't affect p at all.

Solution: determine exactly what operation to perform.

Type 12:
Warning: '#ifdef' argument starts with a digit

Reason for warning: an error like #ifdef 0 has occurred

Solution: #if 0

Type 13:
Warning: unknown escape sequence '\R'

Warning reason: the compiler does not recognize '\R'.

Solution: a slip of the pen should be '\r'.

Type 14:
Warning :too few arguments for format

Warning reason: you might do this: printf(" %d%s ",uParam);

Solution: keep what you want and get rid of what you don't want.

Type 15:
Warning: 'Func' defined but not used

Reason for caution: the Func function is defined, but you don't use it at all.

Solution: get rid of what you don't want.

Type 16:
Display: warning: suggest parentheses around && within | |

Reason for warning: someone used it

If ((* p > = 'a') && (*p < Is equal to 'z') |, | times p > = 'A') && (*p < Is equal to 'Z') |, | times p > = '0') && (*p < = '9'))

Solution: you'd better


if( ( ( *p >= 'a' ) && ( *p <= 'z' ) )  ||  ( ( *p >= 'A' ) && ( *p <= 'Z' ) )  ||  ( ( *p >= '0' ) && ( *p <= '9' ) ) )


Related articles: