开发者

call to undefined function in function main()

开发者 https://www.devze.com 2023-01-11 17:41 出处:网络
whenever i use a function which is called in main function, it gives this error: Call to undefined function in function main()

whenever i use a function which is called in main function, it gives this error: Call to undefined function in function main() i am using turbo c++ compiler version 4.5 and windows vista ultimate service pack 2 Can you tell which header file or something else is to be used. I am beginner in C language.

An example which produces this error:

#include<stdio.h> 

main( ) 
{ 
  int i ; 
  int marks[ ] = { 1, 2, 3, 4, 5, 6, 7 } ; 

  for ( i = 0 ; i <= 6 ; i++ ) display ( marks[i] ) ; 
} 

display开发者_如何学C ( int m ) 
{ 
  printf ( "%d ", m ) ; 
}


You need to define (or at least declare) any function before use. You can do that by including a header that includes a declaration (or prototype) for the function, or the declaration or definition can be contained directly in the source file at hand. For example:

#include <stdio.h>

void doit() { 
    // call function declared in <stdio.h>
    printf("Function called from main");
}

int main() {
    // call function defined above.
    doit();
    return 0;
}


make sure you create a prototype for your functions if these are global function then prototype them in a header file then include this file in your C file where you would like to use these functions.

hope this helps

#include<stdio.h> 

// this is the prototype
void display ( int m );

void main( ) 
{ 
  int i ; 
  int marks[ ] = { 1, 2, 3, 4, 5, 6, 7 } ; 

  for ( i = 0 ; i <= 6 ; i++ ) display ( marks[i] ) ; 
} 

void display ( int m ) 
{ 
  printf ( "%d ", m ) ; 
}


Post your code for us to help you.

You need to make sure your functions are defined before you implement your main function. That is there defintion is known before main.

Here is an example:

#include<stdio.h>

void someFunc()
{
 //define this function
 //notice it is before the main function
}

int main(void)
{
 someFunc();
 return 0;
}

The other way you could do it is to define the protocol or signature of the function before main and then you should be able to define this function after main.

If this involves a function that you may want to be included in your code, maybe from some 3rd party or library, you will need ot include the header file at the top of your file (just like the stdio.h header I've entered in my example).

**Your version edited**

Change your code to this:

#include<stdio.h> 

void display(int m)
{
  printf ( "%d ", m ) ; 
}

int main(void) 
{ 
  int i ; 
  int marks[ ] = { 1, 2, 3, 4, 5, 6, 7 } ; 

  for ( i = 0 ; i <= 6 ; i++ ) display ( marks[i] ) ; 
  return 0;
} 


Just add the below code above the main()

     void display(int);
     main() {
     // code
     }


You probably need to declare the prototype of that function before the definiton of main().

0

精彩评论

暂无评论...
验证码 换一张
取 消