inline functions in case function is returning something - inline

If my function is returning some value then making it inline should give error?
like inline
int fun(int x)
{
return x*x)
}
is function in main it should open like
main()
{
int a=fun(3);// compiler will resolve it as a=return 3*3
}

No it will not give any error
probably small functions made inline
for more information Click here

Related

void affecting output of the program

I am calling one function in which values are printed, but when void is added before the call, the function is not giving correct (or any) output.
I tried various methods
#include <stdio.h>
void func1();
void func2();
void func1()
{
printf("Inside func1()\n");
}
void func2()
{
printf("Inside func2()\n");
}
int main()
{
void func1();
void func2();
printf("Inside main()\n");
return 0;
}
Output is :-
Inside main
When void is removed before calling func1 and func2, the output is changed.
#include <stdio.h>
void func1();
void func2();
void func1()
{
printf("Inside func1()\n");
}
void func2()
{
printf("Inside func2()\n");
}
int main()
{
func1();
func2();
printf("Inside main()\n");
return 0;
}
Output is :-
Inside func1
Inside func2
Inside main
Can anyone explain how void is affecting desired output?
The statement void func1(); within main is a function declaration. This is a no-op at runtime.
func1(); actually calls the function.
It's how the language grammar works, that's all. It's rather clever if you think about it.
In this case, void func1(); is not a call to the function, it just means: somewhere there may be a function with this signature, I want to use it even if it is not declared forward. Usually we call this a function declaration and we don't use it inside main but at the beginning of the file.
A function declaration is the action to tells the compiler about a function's name, return type, and parameters.
eg:-void func1();
A function definition provides the actual body of the function.
eg:-
void func1()
{
printf("Inside func1()\n");
}
To call a function, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value.
eg:-func1();
A declaration always begins with a type name. When you write
int func();
you are declaring to the compiler that you have a function that takes an unspecified number of parameters and returns int (so the compiler knows about it).
When you write
func();
you are telling the compiler to execute function func(); and discard its result, in a normal execution statement.
Always you have a C statement beginning with a type identifier
, that's not to be executed, but to inform the compiler of the existence of such thing (to declare it)
In your example, you changed two declarations at the beginning of main() into to execution statements. As such, the compiler generated code to execute those, resulting in the output you got on execution.

Why does the function call work even though it is not defined before the call?

I have done this quiz, and do not understand the output
#include <stdio.h>
int main()
{
void demo();
void (*fun)();
fun = demo;
(*fun)();
fun();
return 0;
}
void demo()
{
printf("GeeksQuiz ");
}
Expected: Compiler error because I thought that normally demo() would need to be initialized before the call in main()?
Actual results: GeeksQuiz GeeksQuiz
Is my assumption wrong that functions generally need to be defined before they can be called?
functions generally need to be defined before they can be called
Well, not actually, compiler just needs to see a prototype before the call (usage). A forward declaration would be enough.
In your case, inside main(),
void demo();
is serving that purpose. Note that, this is not a function call.

C Global Variable Code giving an compilation error

So I'm new to c and wrote some code but i'm not sure why i'm getting an error when i try to run it.
int GlobalVariable=0;
int main()
{
int LocalVariable=0; //can be used within main()
dis();
printf(GlobalVariable);
return 1;
}
int dis()
{
GlobalVariable=5; //Can be accessed in any functions and made changes to it
}
Here is the prototype of printf function:
int printf(const char * restrict format, ...);
And look what you are typing:
int GlobalVariable=0;
printf(GlobalVariable);
The problem is that you used a function without first telling the compiler about it.
In this case you must provide function prototype as the function definition itself is provided after main
int dis( void ); // function prototype
int main()
{
...
}
int dis() // function definition
{
...
}
Alternatively, you can put the function definition before main(). But usually it would be better to have function prototypes before main() and (usually) even better to put the prototypes in a separate header file - that way it'd be easier to look straight into the main program without being bother about other function details.

Declaring a function?

in C I am writing some of my very first exercises. Earlier on, I tried to declare a simple function inside of main and it comes with an error: "function definition is not allowed here". But I thought a function could be declared inside of main or outside, the only difference being the scope?? I have also read, in here, of other people writing functions inside of main, so why won't it let me do it?
thanks
You can declare a function inside another function:
int main(void) {
int foo(int); // declaration
...
}
But you can't define a function inside another function:
int main(void) {
// Doesn't work.
int foo(int x) {
return x * 2;
}
...
}
Also, declaring functions inside other functions is a really unusual thing to do, and essentially never necessary.

Why does the following code show an error?

#include <stdio.h>
void m();
void n() {
m();
}
void main() {
void m() {
printf("hi");
}
}
On compiling, an error
"undefined reference to m"
is shown. Which m is being referred to?
First, let me declare clearly,
Nested functions are not standard C. They are supported as GCC extension.
OK, now, in your code, m() is a nested function inside main(). It is having block scope for main() only. Outside main() other functions cannot see the existence of m() ,neither can call m() directly. m() can be called only inside main().
In your case, the call to m() inside n() is causing the issue. Even if you provided the forward declaration as void m();, linker won't be able to find the definition of m() and throw error.
Solution: Move the definition of m() outside main(), then you can use it from any other function.
Also note, the recommended signature of main() is int main(void).
As has been explained elsewhere, C doesn't support nested functions as a rule (gcc does as an extension, but almost no other compiler that I know of does).
You need to move the definition of m outside of main. Preferably you should define m before it is used by n:
#include <stdio.h>
void m()
{
printf("hi\n");
}
void n()
{
m();
}
int main( void ) // void main() is not a valid signature for main
{
n(); // call n, which calls m, which prints "hi"
return 0;
}

Resources