when i try to call a function it shows error - c

I am pretty new to coding. I used a simple code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
Sayhi();
return 0;
}
void Sayhi()
{
printf("hi");
}
So when I compile the code it says function "sayhi" was not declared in this scope.
I even tried a different code which used "void" as a function but it didn't work.

This should work - simply declare and define "Sayhi()" before you use it:
#include <stdio.h>
#include <stdlib.h>
void Sayhi();
{
printf("hi");
}
int main()
{
Sayhi();
return 0;
}
A "better" approach would be to create a prototype for "Sayhi()":
#include <stdio.h>
#include <stdlib.h>
void Sayhi(void);
int main()
{
Sayhi();
return 0;
}
void Sayhi();
{
printf("hi");
}
Q: So what's a "prototype"?
https://www.programiz.com/c-programming/c-user-defined-functions
A function prototype is simply the declaration of a function that
specifies function's name, parameters and return type. It doesn't
contain function body.
A function prototype gives information to the compiler that the
function may later be used in the program.
Prototypes should always list the function's parameters. If no parameters, it should list "void".
The value of prototypes shines as your application increases in size and complexity. You'll want to move code OUT of "main()" and into separate .c source files (e.g. "mycomponent.c") and corresponding header files (e.g. "myheader.h").
One additional note: you should always NAME the variables in your prototypes (e.g. void myfunc(int i);.
Q: Do you understand why you were getting the compile error (the function needed to be declared somehow before you used it), and how you can fix it?

Related

How to put a function as an argument in C?

I would like to execute a function given in the paramater of a function. Let me explain with an example.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int object() {
printf("Hello, World!");
return 0;
}
int run_func( int a_lambda_function() ) {
a_lambda_function();
return 0;
}
int main() {
run_func( object() );
return 0;
}
Now, I want to run "object()" in the parameters of "run_func(int a_lambda_function()").
When I run it, It returns an error. How would I achieve this is full C?
Restrictions I have:
Absolutly No C++ allowed.
Functions can be passed as arguments or stored into variables as function pointers.
The definition for a function pointer compatible with your object function is int (*funcp)() ie: a pointer to a function taking an unspecified number of arguments and returning int.
In modern C, functions with an unspecified number of arguments are not used anymore, and functions taking no arguments must be declared with a (void) argument list.
Here is a modified version:
#include <stdio.h>
int object(void) {
printf("Hello, World!");
return 0;
}
int run_func(int (*a_lambda_function)(void)) {
return a_lambda_function(); // can also write (*a_lambda_function)()
}
int main() {
return run_func(object);
}

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.

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;
}

Accessing static function in separate file from main()

I have hello.c containing a function hello():
#include <stdio.h>
static void hello() {
printf("hello.\n");
}
Now, I have main() in main.c, from which I want to call hello() residing in hello.c.
I think I have to pass a function pointer from hello.c to a function in main.c, but I don't know how how to do that exactly. An example with an explanation on how to link the two files would be great! Thanks.
Like this?
hello.h
#ifndef INCLUDED_HELLO_H
#define INCLUDED_HELLO_H
void (*get_hello(void))(void);
// or, better:
// typdef void(*funcptr)(void);
// funcptr get_hello(void);
#endif
hello.c
#include "hello.h"
#include <stdio.h>
static void hello(void) {
puts("Hello!");
}
void (*get_hello(void))(void) {
return hello;
}
main.c
#include "hello.h"
int main(void) {
void(*hello)(void) = get_hello();
hello();
get_hello()();
}
EDIT:
As was quite correctly pointed out to me (sorry, I am new here), this is not exactly self-explaining. Well, the confusing bit is really only the syntax. What we're dealing with here are function pointers. They are exactly what it says on the tin: Pointers to functions, through which functions may be called. The syntax is off-putting, which is why typedefs are your friend, but there you are.
hello in main is a function pointer variable; it's a pointer to a function returning nothing and taking nothing.
get_hello is a function taking no arguments returning a pointer to a function returning nothing and taking no arguments. Similarly,
`int (*foo(double))(float);
would delare a function foo taking a double and returning a pointer to a function taking a float and returning an int.
And, well, that's all there is to it. It is, in principle, very simple, and only the horrible syntax makes it look complicated.

conflicting types for 'outchar'

I am a newbie to C programming. I'm learning by reading the chapters and doing the examples from the book "Teach Yourself C" by Herbert Schildt. I'm trying to run this program in Dev C:
#include <stdio.h>
#include <stdlib.h>
main()
{
outchar('A');
outchar('B');
outchar('C');
}
outchar(char ch)
{
printf("%c", ch);
}
but I get this error when I compile it:
20 1 C:\Dev-Cpp\main.c [Error] conflicting types for 'outchar'
21 1 C:\Dev-Cpp\main.c [Note] an argument type that has a default
promotion can't match an empty parameter name list declaration
15 2 C:\Dev-Cpp\main.c [Note] previous implicit declaration of 'outchar' was here
Please help me with this!
It's because you haven't declared outchar before you use it. That means that the compiler will assume it's a function returning an int and taking an undefined number of undefined arguments.
You need to add a prototype pf the function before you use it:
void outchar(char); /* Prototype (declaration) of a function to be called */
int main(void)
{
...
}
void outchar(char ch)
{
...
}
Note the declaration of the main function differs from your code as well. It's actually a part of the official C specification, it must return an int and must take either a void argument or an int and a char** argument.
In C, the order that you define things often matters. Either move the definition of outchar to the top, or provide a prototype at the top, like this:
#include <stdio.h>
#include <stdlib.h>
void outchar(char ch);
int main()
{
outchar('A');
outchar('B');
outchar('C');
return 0;
}
void outchar(char ch)
{
printf("%c", ch);
}
Also, you should be specifying the return type of every function. I added that for you.

Resources