conflicting types for 'outchar' - c

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.

Related

when i try to call a function it shows error

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?

Unable to return function parameter due to conflicting types for 'function' in C

I'm new to C and I'm trying to understand how there are conflicting types for my function "using_name".
I also don't understand why I have to include a '*' to name the 'using_name()' function. It is because I'm storing a value in the function address?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int using_name(), call_func(char name[20]);
char name[20];
int main ()
{
using_name();
call_func(name);
return 0;
}
char* using_name()
{
printf("What is your name: ");
scanf("%s", name);
printf("\n Your name is %s", name);
return name;
}
int call_func(char name[20])
{
printf("Hello %s", using_name(name));
}
Error: conflicting types for 'using_name'
The return and argument types in the funnction prototype at the beginning of the program has to match the actual types when the function is defined later.
Since using_name() is defined as:
char *using_name()
You need to change the earlier prototype to:
char *using_name();
int call_func(char name[20]);
Another solution is to just put the function definitions at the beginning of the program. You only need prototypes for functions that are used before they're defined, or functions that are defined in another compilation unit (although these prototypes are usually put in a header file).

Same program gives compiler error in one compiler and not the other

I'm studying Learn to Code with C by Simon Long.
https://www.raspberrypi.org/magpi-issues/Essentials_C_v1.pdf
on page 20 there is this simple program:
#include <stdio.h>
void main (void)
{
int a = 0;
while (a < 5)
{
printf ("a is equal to %d\n", a);
a++;
}
printf ("a is equal to %d and I've finished\n", a);
}
But when I compile this I get this compiler error:
while-loop.c:3:1: warning: return type of 'main' is not 'int'
[-Wmain-return-type]
void main(void)
^
while-loop.c:3:1: note: change return type to 'int'
void main(void)
^~~~
int
1 warning generated.
Why is this? It seems that the author's compiler does not give error for this. Why the discrepancy?
When I change
void main (void)
to
int main (void)
it compiles fine.
Per 5.1.2.2.1 Program startup, paragraph 1 of the C standard (bolding mine):
The function called at program startup is named main. The
implementation declares no prototype for this function. It shall be
defined with a return type of int and with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any
names may be used, as they are local to the function in which they are
declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent; or in some other implementation-defined manner.
While void main() does fit in "or in some other implementation-defined manner", in my opinion a particular implementation-specific extension such as that doesn't belong in a beginner's text without at least some explanation as it will only lead to confusion later.
At least find a book that doesn't make the eyes hurt so much.

implicit declaration of function 'enterChar' [-Wimplicit-function-declaration] [duplicate]

This question already has answers here:
Message "warning: implicit declaration of function"
(10 answers)
Closed 2 years ago.
I have two questions here regarding my C program:
1) In main(), the lines C = enterChar();, N = enterNum();, leftJustifiedPic(C, N);, rightJustifiedPic(C, N); are all giving me implicit declaration of function. What does that even mean? I'm used to Java and is it a little bit different in C with regards to the code?
2) In method enterChar(), Im getting conflicting types for 'enterChar' error and again do not understand what it means and why it happens. I'm working on Eclipse (Cygwin-GCC) if it has anything to do with the problem.
Could smb please detail me on this types of errors and warnings? I appreciate it!
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Welcome to the menu!");
printf("The menu is:\n1. Enter/Change Character\n2. Enter/Change Number\n3. Print Triangle Type 1(Left Justified)\n4. Print Triangle Type 2(Right Justified)\n5. Quit");
printf("\n");
printf("Now enter a number from the menu from 1 through 5: \n");
int num = 0;
scanf("%d", &num);
char C;
int N = 0;
switch(num){
case 1:
C = enterChar();
break;
case 2:
N = enterNum();
break;
case 3:
leftJustifiedPic(C, N);
break;
case 4:
rightJustifiedPic(C, N);
break;
default:
printf("Smth is wrong!");
}
return 0;
}
char enterChar(){
printf("Enter your input as a character. Only 'C' and 'c' are allowed!\n");
char input = 0 ;
scanf("%c", &input);
while(input != 'c' || input != 'C'){
if(input != 'C' || input != 'c'){
printf("You have to enter 'C' or 'c'. Try again!");
}
}
return input;
}
1) You haven't declared the functions before you use them, and the dialect of C that you are using has "implicit function declarations". This means the function are implicitly declared to return int and take any number of parameters of any type.
2) Because you have an implicit function declaration int enterChar(), that clashes with the definition char enterChar().
The solution is to provide function declarations before main().
char enterChar(); // and other function declarations
int main(void) {
....
}
// function definitions
char enterChar() { .... }
Depending on your use-case, it may be worth investigating using a more recent version of C, which doesn't have these implicit function declarations (e.g. C99 or C11)
When the prototype of a function is not declared, the compiler assumes that the return type is an int.
That is what it calls an implicit declaration.
and then, you go to declare enterChar that returns a char. Since the compiler had used an implicit declaration when it was called it in main, it complains about the conflicting types.
You can resolve that problem by providing an explicit declaration of the function before using it in main.
char enterChar();
It's a good practice to provide explicit declarations of all functions before they are used.
Functions must at least be declared before they are called; if possible, they should be defined before they are called.
When the functions are defined in the same source file from which they are called, move the definition of the function to precede the caller, like so:
char enterChar( void ) // void indicates function takes no arguments
{
// body of enterChar
}
int enterNum( void )
{
// body of enterNum
}
int main( void )
{
...
c = enterChar();
...
N = enterNum();
...
}
Yes, this makes the code read "backwards", but it eliminates some headaches.
When functions are defined in a different source file from which they are called, make sure you have a declaration of that function (using prototype syntax!) in place before the call. The declaration may appear at file scope (outside of any function), or within the function from which the call is made:
// enterChar and enterNum are *defined* in a different source file
int enterNum( void ); // declaration at file scope, valid for remainder
// of file
int main( void )
{
char enterChar( void ); // declaration within a block, only valid for
// duration of current block
...
c = enterChar();
...
}
In the case above, the declaration of enterNum is valid over the entire file, while the declaration for enterChar is only valid within the body of main; if any other function in the same source file wants to call enterChar, it must also have a declaration before the call. Either way, the declaration must precede the call.
The usual practice for handling declarations of functions defined in a different source file is to create a header file which contains the function declarations (not definitions!) and include that header in the file that defines the calling function(s):
/**
* main.c
*/
#include <stdio.h>
#include "utils.h" // contains declarations for enterChar and enterNum
int main( void )
{
...
c = enterChar();
...
N = enterNum();
}
Header file:
/**
* utils.h
*/
#ifndef UTILS_H // Include guard; prevents header file from being processed
#define UTILS_H // more than once per translation unit
char enterChar( void ); // void in the argument list indicates the function takes no arguments
int enterNum( void ); // an empty argument list in a declaration specifies that
// the function takes an *unspecified* number of arguments
// which is not the same thing, and not necessarily safe
#endif
Implementation file:
/**
* utils.c
*/
#include <stdio.h>
#include <stdlib.h>
#include "utils.h" // including our own header to make sure our declarations
// and definitions line up.
char enterChar( void )
{
// body of enterChar
}
int enterNum( void )
{
// body of enterNum
}
You'll notice that utils.c also includes utils.h. This is to make sure that our declarations and definitions are in sync.
You have "forward references" to undeclared functions. They need a function prototype, or implementing before being called. Without knowing what the function takes or returns, the compiler assumes int types, although I suspect some compilers will flag an error anyway.
You only posted one function, so I limit my example to that.
#include <stdio.h>
#include <string.h>
char enterChar(); // function prototype
int main(void)
{
char C;
//...
C = enterChar(); // function call
//...
return 0;
}
char enterChar() // function implementation
{
char input = 0;
//...
return input;
}

Assigning function to function pointers

I worte a test program to understand callback functions and function pointers.The program is given below.
My question is while assigning
cb_display = (void*)display_struct;
I have to cast the function to a void*. Why is this required even when the return type of function is void?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int a;
char b[16];
}myst_t;
void (*cb_display)(void*);
void display_struct(myst_t *st){
fprintf(stdout, "a -> %d b -> %s \n",st->a,st->b);
}
int main()
{
myst_t myst;
myst.a = 789432;
strncpy(myst.b,"helloabcd",9);
cb_display = (void*)display_struct;
cb_display(&myst);
return 0;
}
cb_display = (void*)display_struct;
is actually also not valid in C. Don't do it. You cannot assign a void * to a function pointer.
To fix your issue declare your function pointer as:
void (*cb_display)();
It means it matches a function that return no value and takes an unspecified number of parameters. You then don't need any cast. Also please note as it was pointed by Olaf in the comments that a function declarator with () while valid is an obsolescent C feature.
Of course if you will only pass functions like display_struct with a myst_t * parameter, you can also declare cb_display as: void (*cb_display)(myst_t *);
Changing void (cb_display)(void);
To typedef void (cb_display)(void);
May work.

Resources