Fabulous Fabulous - 11 days ago 5
C Question

Typedef parameters and functions

I have the following piece of code:

#include <stdio.h>

typedef char km_ph;
typedef double nnew;

int printme(km_ph b){
printf ("%c %d\n", b,b);
return 0;
}

int main() {

km_ph a = 'o';
nnew b = 100.09;
printme(9);
printme(a);
printme(b);
return 0;
}


I expected
printme(b)
and
printme(9)
to throw some type of warning or error since the type of
b
is not the type of parameter that can be passed to
printme
.
printme
expects
km_ph
type, or at least
char
type. I get the following output:


9
0 111
d 100



why does this happen?

Answer

Depends on the compiler, running FlexLint on it gives this (see significant prototype coercion)

FlexeLint for C/C++ (Unix) Vers. 9.00L, Copyright Gimpel Software 1985-2014 
--- Module: diy.c (C)
     1  #include <stdio.h> 
     2  
     3  typedef char km_ph;
     4  typedef double nnew;
     5  
     6  int printme(km_ph b){
     7          printf ("%c  %d\n", b,b);
     8          return 0;
     9      }
    10  
    11  int main() {
    12  
    13      km_ph a = 'o';
    14      nnew b = 100.09;
                      _
    15      printme(9);
diy.c  15  Warning 534:  Ignoring return value of function 'printme(char)' (compare with line 6)
                      _
    16      printme(a);
diy.c  16  Warning 534:  Ignoring return value of function 'printme(char)' (compare with line 6)
                     _
    17      printme(b); 
diy.c  17  Warning 524:  Loss of precision (arg. no. 1) (double to char)
diy.c  17  Info 747:  Significant prototype coercion (arg. no. 1) double to char
diy.c  17  Warning 534:  Ignoring return value of function 'printme(char)' (compare with line 6)
    18      return 0;
    19  }
    20  

see

That is why it is always good to have some kind of static code analyzer in your projects.