while compiling i getting error as " error: expected ‘;’ before ‘{’ token"Code:#include<stdio.h>
int main()
{
fun()
{
printf("hai\n");
}
return 0;
}
please any one give explanation on above error
Printable View
while compiling i getting error as " error: expected ‘;’ before ‘{’ token"Code:#include<stdio.h>
int main()
{
fun()
{
printf("hai\n");
}
return 0;
}
please any one give explanation on above error
You are missing a terminating semi-colon.
Hi ,
laselight ,i got ur point .my aim is i want to print the hai from function with int the main with out decaling the function outside.
can anyone modify my program to print hai with in a main funcion?
Eh? What you are asking for seems so simple that you would have done it if you did not have what looks like a call to a function named fun:Quote:
Originally Posted by crocker
Code:#include <stdio.h>
int main(void)
{
printf("hai\n");
return 0;
}
ur logic also right.
but i want to print from a function fun() with in main.
please can u modify my program to print hai
In that case, you should define the function fun() to print "hai\n", then call fun() from within the main function.Quote:
Originally Posted by crocker
i getting out put from above program.Code:int fun(()
{
printf("hai \n");
return 0;
}
int main()
{
fun();
return 0;
}
but i cant understanding why i didt getting output from below program
please give ur suggestionsCode:int main()
{
fun()
{
printf("hai\n");
}
return 0;
}
Yes, that looks right except that you should indent your code, and fun() can have a void return type since it does not appear to need an int return type. (Of course, I am assuming that you #include <stdio.h> as you did earlier, and your actual code does not have a typo error.)Quote:
Originally Posted by crocker
It is simply syntactically wrong. You are not defining fun(), you are calling it, and forgetting a terminating semi-colon. You then introduce a block of scope in which you call printf(). Note that in standard C, functions cannot be defined in the definition of other functions.Quote:
Originally Posted by crocker
"Note that in standard C, functions cannot be defined in the definition of other functions." wt this mean?
u mean in main function we cant define fun()?
Yes. You could forward declare fun() in the main function, but it is more common to place forward declarations outside of function bodies, like how functions are defined.Quote:
Originally Posted by crocker
so af from our discussion , i concluded some points
1. in c function defination is out function is defined out side of function wiher it is called ?
am i right.
its its wrong .please correct
You cannot define a function inside another function. You just call it there. You should also have a prototype declaration after the headers have been included.
The primary reason is that C is a not a block-structured language like Pascal, so you can't have nested function definitions.