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
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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:Originally Posted by crocker
Code:#include <stdio.h> int main(void) { printf("hai\n"); return 0; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.Originally Posted by crocker
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.)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.Originally Posted by crocker
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
"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.Originally Posted by crocker
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
The primary reason is that C is a not a block-structured language like Pascal, so you can't have nested function definitions.