tags:

views:

67

answers:

3
#include<stdio.h>

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>(TOP)
#include<fstream.h>

#define MAX 5

int top = -1;
int stack_arr[MAX];

main()
{
int choice;
while(1)
{
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

push()
{
int pushed_item;
if(top == (MAX-1))
printf("Stack Overflow\n");
else
{
printf("Enter the item to be pushed in stack : ");
scanf("%d",&pushed_item);
top=top+1;
stack_arr[top] = pushed_item;
}
}/*End of push()*/

pop()
{
if(top == -1)
printf("Stack Underflow\n");
else
{
printf("Popped element is : %d\n",stack_arr[top]);
top=top-1;
}
}/*End of pop()*/

display()
{
int i;
if(top == -1)
printf("Stack is empty\n");
else
{
printf("Stack elements :\n");
for(i = top; i >=0; i--)
printf("%d\n", stack_arr[i] );
}
}/*End of display()*/
+2  A: 

You should either forward declare these functions before main:

void push();
void pop();
void display();

or move the actual declarations above main.

ChrisF
now the error "Type mismatch in redeclaration of push() pop() and display() occurs.
Alesha Aris
@Alesha - add `void` to the actual declarations. It would be better to move `main` to the bottom of the file.
ChrisF
A: 

add void push(); before main

Veer
A: 

It would really help if you told us what compiler you were using and the exact error message it gave you. It would also help if you formatted your code a bit (although this particular snippet is small and simple enough to read).

So, there several things going on here. First is that if you don't explicitly type a function definition, that function is assumed to return int (i.e. the function is implicitly typed int). Thus, main, push, pop, and display are all assumed to return int. This is a very archaic and frowned-upon style, and IINM is prohibited as of the C99 standard. Since push, pop, and display don't return a value to the caller, they should be explicitly typed void. main should explicitly be typed int (discussed further at the bottom).

Secondly, if the compiler doesn't see a function declaration or definition before it sees a function call, then it will assume the function returns int. This can result in a type mismatch error if you later define the function with a different return type, as in:

int main(void)
{
  ...
  foo();
  ...
}

void foo(void) { ... }

When the compiler sees the call to foo() in main(), it assumes that foo() will return an int, but then the later definition contradicts that assumption, so it issues a diagnostic.

The lesson here is that you should at least declare your functions before you call them, like so:

int main(void)
{
  void push();
  void pop();
  void display();
  ...
  push();
  ...
  pop();
  ...
  display();
  ...
}

void push(void) { ... }
void pop(void) { ... }
void display(void) { ... }

and make sure that the declaration and definition match up. Personally, I follow the "define-before-use" rule, like so:

void push(void) { ... }
void pop(void) { ... }
void display(void) { ... }

int main(void)
{
  ...
  push();
  ...
  pop();
  ...
  display();
}

That way you don't have to worry about keeping declarations and definitions synced up.

On the definition of main: The C standard defines exactly two interfaces for main:

int main(void); 
int main(int argc, char *argv[]); 

Individual implementations may define additional interfaces for main; several add a third parameter like so:

int main(int argc, char *argv[], char *env[]);

Your definition of main must conform to one of the two forms specified in the standard, or one of the forms specified in the documentation for your particular compiler, or the behavior of the program will be undefined (which can mean anything from running as expected, crashing immediately, or running to completion but leaving the system in a bad state, or something else completely). There are several authors who insist that if you don't need the return value from main() you can type it void, but that's only true if the compiler documentation explicitly says that it supports void main().

John Bode