[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

implicit routine definition

i have 2 routines say,

void one() {
  ...
  two();
  ...
}

void two() {
  ...
  one();
  ...
}


when compiling, it gives me the warning of the implicit declaration of two() to return int.  
is there something i can do to avoid this warning??

0
rawdrib
Asked:
rawdrib
  • 2
2 Solutions
 
van_dyCommented:
just do

void two(void);   // add this declaration before void one()

void one() {
  ...
  two();
  ...
}

void two() {
  ...
  one();
  ...
}

it shall compile without warning
hope this helps,
van_dy
0
 
PaulCaswellCommented:
van_dy,

That should move the problem to 'implicit ... one() ...'. Caller should also prototype:

void one(void);   // add this declaration too.

The reason for this is that if you allow the compiler to intuit the type of a function then it will assume the function returns an 'int'. Prototyping all functions is a good practice as it will avoid many potential issues.

Paul



0
 
van_dyCommented:
Hi Paul,

      I dont think it will be necessary to add void one(void)
if we keep
---------------------
void two(void);   // add this declaration before void one()

void one() {
  ...
  two();
  ...
}

void two() {
  ...
  one();
  ...
}
-------------------- in one single  file.
It is possible to declare and define the  functions (which call  each other)
at the same time,  with one exception, we need to break the loop somewhere
and a single declaration like 'void two(void)' should work  nicely.
like for an example.

varun@pc ~# cat > test.c
void two(void);

void one(void)
{
        two();
}

void two(void)
{
        one();
}
varun@pc ~# cc -c test.c   //compiles nicely without warnings.
varun@pc ~#

However your suggestion to prototype void one(void) at the
beginning isnt bad either since it lets the reader know all the
functions that are being defined later in the file.   But not giving
the prototype  'void one(void)' shouldnt cause warnings

0
 
kledbetterCommented:

You should always prototype ALL of your functions -- never let the compiler do implicit definitions.  It's just asking for trouble.
Bottom line, to fix your problem just do function prototypes in either a .h file or at the top of the .c file holding these routines:


void one(void);
void two(void);

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now