Thread: Implicit int rule in Function Parameters

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    113

    Implicit int rule in Function Parameters

    Hello All,

    My question is about the old implicit int rule. I do not use that one and as far as I know it is encouraged not to use and it is obselete now. However I want to learn it just in case I can see it in old codes.

    I understand that if a function is defined without a return type it is assumed to return to int according to this rule.

    Also not because of this rule but there is a different function parameter definition from the modern style. The parameters can be just typed as names and they can be declared inside the function, just like that:

    Code:
    int function(x,y){
    int x;
    char y;
    ...
    }
    My question is about function definitions which include only names as parameters and not declared in the function, like this:

    Code:
    int function(x,y){
    ...
    }
    Is it assumed that x and y are ints according to the implicit int rule? My book has an example similar to the one below:

    Code:
    int function(register x, register y){
    ...
    }
    I know what register is. But I don't know why the author wrote this example with a storage specifier. Is a specifier or qualifier required in parameter so that the implicit int rule works? Or does my example work too?

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    359
    Implicit int's and K&R syntax are still valid, but not recommended... You'll get warnings, but it works.

    A reminder about the register keyword: This is a hint to the compiler telling that the address of the identifier cannot be taken... It's NOT a hint about allocating the object on registers, instead of memory.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    359
    What don't work anymore is to declare a local or global variable without the 'int' keyword. This is a syntax error, if the identifier isn't previously declared:

    x;
    Last edited by flp1969; 4 Weeks Ago at 02:27 PM.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    359
    About K&R syntax, the function definition should be:
    Code:
    int f(x,y)
    int x;
    char y;
    {
    ...
    }
    Otherwise, you are declaring local objects.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Quote Originally Posted by flp1969 View Post
    About K&R syntax, the function definition should be:
    Code:
    int f(x,y)
    int x;
    char y;
    {
    ...
    }
    Otherwise, you are declaring local objects.
    Thanks for the correction. But what if I don't declare them at all, does implicit int rule makes them int automatically?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,374
    You'll get warnings, but it works.
    Actually using modern C with a properly configured compiler should generate an error not a warning. Implicit parameter and return types are no longer supported with C11.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Quote Originally Posted by jimblumberg View Post
    Actually using modern C with a properly configured compiler should generate an error not a warning. Implicit parameter and return types are no longer supported with C11.
    To be honest I don't think I will use this. The question is mostly because of curiosity and to be able to understand if I see them in an old code.

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    359
    Quote Originally Posted by jimblumberg View Post
    Actually using modern C with a properly configured compiler should generate an error not a warning. Implicit parameter and return types are no longer supported with C11.
    Are you saying gcc and clang aren't "properly configured compilers"?
    Code:
    $ gcc -xc -std=c11 -include stdio.h - <<EOF
    > f(x,y) { return x+y; }
    > int main(void) { printf( "%d\n", f(1,2)); }
    > EOF
    <stdin>:1:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
    <stdin>: In function ‘f’:
    <stdin>:1:1: warning: type of ‘x’ defaults to ‘int’ [-Wimplicit-int]
    <stdin>:1:1: warning: type of ‘y’ defaults to ‘int’ [-Wimplicit-int]
    $ ./a.out
    3
    $  clang -xc -std=c11 -include stdio.h - <<EOF
    f(x,y) { return x+y; }
    int main(void) { printf( "%d\n", f(1,2)); }
    EOF
    
    <stdin>:1:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
    f(x,y) { return x+y; }
    ^
    1 warning generated.
    $ ./a.out
    3

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,374
    Are you saying gcc and clang aren't "properly configured compilers"?
    No, I'm saying you haven't properly configured the compilers, the compilers are fine if properly configured. Here is what my compiler reports:

    ||=== Build: Debug in chomework (compiler: GCC 8-1) ===|
    main.c|1|error: return type defaults to ‘int’ [-Wimplicit-int]|
    main.c|1|warning: no previous declaration for ‘f’ [-Wmissing-declarations]|
    main.c||In function ‘f’:|
    main.c|1|error: type of ‘x’ defaults to ‘int’ [-Wimplicit-int]|
    main.c|1|error: type of ‘y’ defaults to ‘int’ [-Wimplicit-int]|
    main.c||In function ‘main’:|
    main.c|5|error: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]|
    main.c|5|warning: incompatible implicit declaration of built-in function ‘printf’|
    main.c|5|note: include ‘<stdio.h>’ or provide a declaration of ‘printf’|
    ||=== Build failed: 4 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
    This is the code I used.
    Code:
    f(x,y) { return x+y; }
    
    
    int main(void)
    { printf( "%d\n", f(1,2)); }

  10. #10
    Registered User
    Join Date
    Feb 2019
    Posts
    359
    Quote Originally Posted by jimblumberg View Post
    No, I'm saying you haven't properly configured the compilers, the compilers are fine if properly configured.
    All you did was enable -Werror or -Werror=implicit-int options to make warnings into errors... This is not "properly configure"... What options your IDE is using?

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,374
    Actually I added quite a few flags. You need at a minimum, IMO, to use -Wall -Wextra along with the -g flag for debug mode. I also recommend -pedantic-errors, along with the standard you wish to use (I recommend -std=c11) to stop your compiler from using compiler specific hacks.

    Lastly I normally use -Wvla when working with forum based code because most of the time the users don't know that they are using them or even what they are and what the limitations are.

    Edit: Using a compile line without any warning flags enabled is using an improperly configured compiler IMO.
    Last edited by jimblumberg; 3 Weeks Ago at 06:30 PM.

  12. #12
    Registered User
    Join Date
    Feb 2019
    Posts
    359
    Quote Originally Posted by jimblumberg View Post
    Actually I added quite a few flags. You need at a minimum, IMO, to use -Wall -Wextra along with the -g flag for debug mode. I also recommend -pedantic-errors, along with the standard you wish to use (I recommend -std=c11) to stop your compiler from using compiler specific hacks.

    Lastly I normally use -Wvla when working with forum based code because most of the time the users don't know that they are using them or even what they are and what the limitations are.

    Edit: Using a compile line without any warning flags enabled is using an improperly configured compiler IMO.
    Ahhh... here we are! -pedantic-errors... Ok, this will cause obsolete features do be errors, but this is not "properly configure" the compiler, but asking it to distrust old (but still valid) syntax... The standard, as far as I know, says these kind of syntax is obsolete is discouraged, not they SHALL NOT be used (I could be wrong)...

    Anyway, using implicit 'int's and K&R syntax is NOT recomended, but still can be used... For example, I think ncurses source almost entirely uses K&R syntax...

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,374
    Ahhh... here we are! -pedantic-errors... Ok, this will cause obsolete features do be errors, but this is not "properly configure" the compiler,
    That is your opinion. In my opinion using obsolete methods is an error and should be flagged as such. The pedantic flag also prevents the compiler from using other implementation hacks as well.

    Using inadequate warning levels when developing code is using a poorly configured tool.

    For example, I think ncurses source almost entirely uses K&R syntax...
    So? If you need to compile this library then you should compile this library to an earlier standard that allows this "feature". But the application that is using this library should not be using these obsolete/removed features and having the compiler properly configured to disallow these "features" is beneficial. But compiling code with no added warning messages is asking for trouble.

    Edit: Compiling the code above to the C90 standard just produces warnings even with the -pedantic-errors flag.
    Last edited by jimblumberg; 3 Weeks Ago at 06:53 AM.

  14. #14
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    I have understood what works in functions and what not with more trial and error. For variable declaration, the first of below works with the aid of implicit int rule but the second is not converted to int. Why does not the second one work?

    Code:
    register var1;
    var2;

  15. #15
    Registered User
    Join Date
    Feb 2019
    Posts
    359
    Quote Originally Posted by GokhanK View Post
    I have understood what works in functions and what not with more trial and error. For variable declaration, the first of below works with the aid of implicit int rule but the second is not converted to int. Why does not the second one work?

    Code:
    register var1;
    var2;
    Because the second isn't an identifier declaration, is a null expression.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simpson's rule and Trapezoidal rule from fixed array
    By timwonderer in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2010, 03:14 PM
  2. Implicit Declaration Of Function
    By nathanpc in forum C Programming
    Replies: 1
    Last Post: 01-26-2010, 08:46 PM
  3. implicit declaration of function?
    By BoneXXX in forum C Programming
    Replies: 2
    Last Post: 04-27-2007, 11:04 PM
  4. Implicit declaration of function ...???
    By soothsayer in forum C Programming
    Replies: 8
    Last Post: 08-07-2006, 06:49 AM
  5. Implicit declaration of function
    By soothsayer in forum C Programming
    Replies: 5
    Last Post: 02-28-2006, 02:56 PM

Tags for this Thread