-5
#include<stdio.h>
#include<conio.h>

void main()
{
  void display();
  clrscr();
  printf("main function");
  getch();
}

void display()
{
  printf("user function");
}

the output of the above program is

main function

but I want the display() function to be executed before the main function.

the output should be:

user function main function

Jason Jong
  • 4,310
  • 2
  • 25
  • 33
Vasanth Kumar
  • 150
  • 1
  • 9
  • 1
    remove the word **void** in the main function and to display the user function output, don't call the clrscr – Monah Mar 04 '15 at 07:02
  • Unless you are writing a bootloader etc for a bare metal embedded system, executing a function before main doesn't make any sense. If you ever find yourself needing that, there's a 100% chance that your program design is bad. Furthermore, C doesn't specify where static storage duration variables are initialized, so should you somehow manage to run a function before main, it cannot rely on variables with static storage duration. Which in turn means that calling standard library functions at that point, might very well cause your program to crash and burn. – Lundin Mar 04 '15 at 07:43
  • Also, I don't predict a bright future for the MS DOS operating system, so I would strongly recommend to get rid of Turbo C in favour for a C standard compliant compiler. – Lundin Mar 04 '15 at 07:47

5 Answers5

4

Your problem is here:

void main()
{
    void display(); <--- here
    ....

This declares a function but does not call it. Call it like this

display();

Do note that your display function accepts arbitrary parameters. And your main signature is wrong. Your program should be:

#include<stdio.h>
#include<conio.h>

void display(void); 

int main(void)
{
    clrscr(); // clear screen first
    display();
    printf("main function");
    getch();
    return 0;
}

void display(void)
{
    printf("user function");
}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

Finally I got the answer without calling the function inside the main,

#include<stdio.h>
#include<conio.h>
void display(void);
# pragma startup display 0
void main()
{
printf("\nmain function");
getch();
}
void display()
{
clrscr();
printf("\nuser function");
}

output: user function main function

Vasanth Kumar
  • 150
  • 1
  • 9
0

The Main function is executed only, in the main you can all other function, but all that you do in your program is in the main. You cannot call other function out of the main

By the way, the main uses to be

int main(int argc, char *argv);

If you want to call your function in 1st you can just do

int main(int argc, char *argv) {
yourFunction(awesomeArguments);
//rest of main
}
0

You may mean "main function" in the printf statement.The actual int main function is the first to execute. Switch the printf's to reverse the display if that is what you mean.

0

you are declaring the function inside the main function and not calling it. Hence the execution order isn't according to you expectation.Just place the function definition above the main function and call it as

display();

or declare it as

void display() ;

above main method and call it without 'void'