I have been encountering this problem when I try to compile in C. When I asked help50 for help, it gave me this message "By "undefined reference," clang means that you've called a function, print, that doesn't seem to be implemented. If that function has,
in fact, been implemented, odds are you've forgotten to tell clang to "link" against the file that implements print. Did you forget to
compile with -lfoo, where foo is the library that defines print?" Because of this, I decided to implement #include <foo.h>, however after I tried to compile, I received a fatal error message. This is my code
#include <cs50.h>
#include <stdio.h>
void print(char c, int n);
//Code
int main(void)
{
int n;
do
{
n = get_int("Height:");
} while(n < 1 || n > 8);
for(int i = 0; i < n; i++)
{
print(' ', n - 1 - i);
print('#', i + 1);
print(' ', 2);
print('#', i + 1);
printf("\n");
}
}
`
You have declared print... But where is the implementation?
The declaration is just a promise to the compiler that you have a function that take some parameters of a given type and return something a given type.
When the compiler sees you calling the function it will how to report errors if the types don't match...
The implementation is where the compiler knows what to do with the parameters in order to return something from that function.
Here is a sample implementation:
void print(char c, int n)
{
printf("My char is %c and my int is %d\n", c, n);
}
There's no standard library print() function in C. You haven't actually defined it anywhere, either.
Related
I'm a beginner in C language and in my previous question I have asked about proper function declaration order in C. I was told that in standard C, it is necessary to declare functions before calling them.
But for the following example code I cannot declare the the function times2p before the main(). I tried to declare it as: void times2(int in_data); right before the main(), but I get errors. Here below is the example code:
#include <stdio.h>
#include <stdlib.h>
void times2p(int *in_data);
void times2(int in_data);
int main()
{
int y = 5;
int s = times2(y);
printf("%d\n", y);
printf("%d\n", s);
printf("-------------\n");
int yp = 5;
times2p(&yp);
printf("%d\n", yp);
return 0;
}
//Multiplies the input argument by two
void times2(int in_data){
in_data = in_data*2;
}
void times2p(int *in_data){
*in_data = *in_data*2;
}
How and where should the times2p function be declared in this case? If I don't declare the code still compiles without error but I was told that I have to declare the functions in C in my previous question.
Here is the error:
||=== Build: Debug in test1 (compiler: GNU GCC Compiler) ===|
C:\Users\nnn\Documents\CodeBlocks\test1\main.c||In function 'main':|
C:\Users\nnn\Documents\CodeBlocks\test1\main.c|16|error: void value not ignored as it ought to be|
C:\Users\nnn\Documents\CodeBlocks\test1\main.c|13|warning: unused variable 'read_x' [-Wunused-variable]|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
Your code in the current version of the question is incorrect, but not for the reason you're asking about.
There's nothing wrong with the void function declarations before main, and the compiler doesn't complain about them. The problem is that a void function doesn't return a value, and you're trying to call them as if they did:
int s = times2(y);
This would be correct only if times2(y) returned an int value (or a value of some type implicitly convertible to int).
Also, looking at the definition of the function:
void times2(int in_data){
in_data = in_data*2;
}
The parameter in_data is a local variable within the function. Changing its value effectively does nothing.
Your times2p function looks OK, and it should work. If you want times2() to be useful, it needs to return a value:
int times2((int in_data) {
return in_data * 2;
}
(The question has changed, so this answer may not be perfect anymore, sorry).
You need to declare at least the prototype of the functions before you use it. Like this:
#include <stdio.h>
#include <stdlib.h>
//How should the function times2p be declared here?
void times2p(int *in_data);
int main()
{
int y = 5;
times2p(&y);
printf("%d\n", y);
return 0;
}
//Multiplies the input argument by two
void times2p(int *in_data){
*in_data = *in_data*2;
}
Or declare the function itself, like this:
#include <stdio.h>
#include <stdlib.h>
//Multiplies the input argument by two
void times2p(int *in_data){
*in_data = *in_data*2;
}
int main()
{
int y = 5;
times2p(&y);
printf("%d\n", y);
return 0;
}
Why does my code work ? I am calling the function generateNumber before declaring it, and I haven't set a prototype at the beginning of the file, so normally it shouldn't work, should it ?
Here is my code :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, const char * argv[]) {
int max = 100;
int min = 1;
int mysteryNumber = generateNumber(min, max);
int enteredNumber = min-1;
do{
printf("Enter a number !\n");
scanf("%d", &enteredNumber);
if (enteredNumber > mysteryNumber) {
printf("It's less !");
}else if(enteredNumber < mysteryNumber){
printf("It's more !");
}
}while (enteredNumber != mysteryNumber);
printf("Congratulations, the mystery number was %d \n", mysteryNumber);
return 0;
}
int generateNumber(int min, int max){
srand(time(NULL));
return (rand() % (max - min + 1)) + min;
}
Thanks by advance !
Surprisingly, this is one of the rare cases when it actually should work with old compilers - specifically, with compilers prior to C99. Still, you shouldn't do it: implicit int has been removed in C99, because it makes code fragile.
When a function lacks prototype, older C compilers used to assume that all its arguments match the types of expressions that you pass, and that their return type is int. Your function happens to match this description: you pass two integers, and treat the return value as an int.
You are right it shouldn't work. My gcc (6.1.0) produces:
test.c: In function ‘main’:
test.c:9:25: warning: implicit declaration of function ‘generateNumber’ [-Wimplicit-function-declaration]
int mysteryNumber = generateNumber(min, max);
^~~~~~~~~~~~~~
It "works" because most compilers are permissive of this by providing an implicit function declaration. However, it's not valid in modern C.
Before C99 removed this implicit function declaration from the standard, it was allowed. But it's not valid anymore since C99.
If your compiler doesn't provide warnings for this, try to increase the warning levels.
Doing K&R using Xcode, in the Functions section I entered the code for their example of the power function as follows.
#include <stdio.h>
int power(int m, int n);
int main()
{
int i;
for (i=0; 1<10; ++i)
printf("%d %d %d\n", i, power(2, i), power(-3, i));
return 0;
}
When I try to run it, the following error appears:
Undefined symbols for architecture x86_64:
"_power", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have read a lot of the answers to this question, but cannot see how it applies to my situation with such a little program. How can I fix this problem?
Why do I get "clang: error: linker command failed with exit code 1"?
You just declared the function. There is not any definition in the code. At the time of the linking process, the compiler (here Clang) cannot link power function to its definition, so the linker throws the error in this kind of situation. If you define
int power(int x, int y)
{
/* Do calculation */
}
Then the linker can link your declaration of power function to its definition, and you will not get any error.
For an integer number I have made a function:
#include <stdio.h>
int power(int base, int exp);
int main()
{
int i;
for (i=0; i<10; ++i)
printf("%d %d %d\n", i, power(2, i), power(-3, i));
return 0;
}
int power(int base, int exp)
{
int result = 1;
while (exp)
{
if (exp & 1)
result *= base;
exp >>= 1;
base *= base;
}
return result;
}
Compile this with gcc file.c.
You missed the definition of function int power (int base, int n) which is given after your main ends on the next page of the book.
When you declare prototype of a function you need to define what it should do you just declared the power function and never defined that, that's why you got error.
Include the following definition, your code will compile the way you wants it to be.
int power (int base, int n) {
int i, p;
p = 1;
for (i=1; i<=n; ++i)
p = p*base;
return p;
}
Pre-edit answer
Now this is not relevant, but useful
I think you want to use function pow() defined in math.h.
double pow(double a, double b)
The C library function pow(double a, double b) returns a raised to the power of b. This function returns a double value so to print that correct specifier will be "%lf".
In this case you just need to include header file
#include <math.h>
In your program.
There is no need to give function declaration int power(int m, int n);
The error you are having is due to giveing I as on of the parameter to pow()
because when you will compile your code (after including math.h and using pow() replacing i with any integer numbers will compile your code and will give proper output.
printf("%lf %lf %lf\n", i, pow(2, 3), pow(3, 2));
This will give you proper result but when you compile it with
for (i=0; i<10; ++i) {
printf("%lf %lf %lf\n", i, pow(2, i), pow(-3, i));
}
It throws same error so I think pow() takes only constants as input so it won't run in for loop.
And if you don't want to include math.h you can simply declare
extern double pow (double base, double exponent);
That will link correctly with the library code without using the math.h include file, here is an example.
int main() {
extern double pow (double base, double exponent);
printf("%lf", pow(8.0, 8.0));
return 0;
}
For more on pow() you can check man page on Linux i.e. man pow.
There is a standard library function which does just that...
#include <math.h>
double pow(double x, double y)
You have to link it explicitly because the default linker, that is ld invoked when no other options given, doesn't link with standard math library.
So you have to do it like...
gcc file.c -lm
You can define the power function
int power(int m, int n) {
// Implement the function body
}
And then your issue will get fixed.
You're getting an error, because there isn't any deceleration for the defined function. So add the deceleration as shown above.
First, you get the error because the compiler can not find the definition of the power function that you are using. Even if you write
int power(int m, int n);
There is an error because you are not defining the function. There is missing {} at the end of the definition and even if you are putting it at the end of the definition, you will get an error because you are not returning nothing for a int function. So, at least if you want to define a function, you have to proceed like this:
int power(int m, int n){return 0};
Then, you will be able to use your function power() in the main function. But, you are doing nothing in the power() function, so you will get nothing back of calling it. If you want to compute the power of a number, you can use the function pow() that is present in the cmath library. A straightforward way of doing it is something like this:
#include <stdio.h> // printf
#include <cmath> // pow()
#include <iostream> // cout
void main()
{
for (int i = 0; i < 10; i++)
std::cout << i << " " << pow(2, i) << " " << pow(-3, i) << std::endl;
}
I included the iostream to use a different way of printing out using the object cout defined in the std namespace. Note that the pow() function has some requirements for its definition, so be careful with the types you are using. You can take a look at pow for more details.
The function should be completed. I met the same question today only because I didn’t define function main in my code.
I have just written a sample program to understand the working of functions in C. I declared a function in C and call it during my programs execution. However my compiler gives me a warning saying unused function. My code looks like this :
#include <stdlib.h>
#include <stdio.h>
int test_function(x);
int main(){
int x;
char letter[] ={"HAAA"};
char cmpre[] = {"AHD"};
int value;
for(int i=0; i<4;i++)
{
if(letter[i] == cmpre[i])
{
x=0;
}
}
int test_function(x)
{
if (x==0)
{
printf("the letters are the same");
}
return value;
}
printf("To check if the letters are the same go to the function");
test_function(x);
return 0;
}
The program seems to execute fine but I get a warning in the fourth line where I declared the function in the start of the program. The warning is :
Multiple markers at this line
- parameter names (without types) in function declaration [enabled by
default]
- Unused declaration of function 'test_function'
I think the way I am calling my function is not right. Could somebody please help me. Thnak you in advance.
Disclaimer: nested functions are non-standard C and I only know (of) the GNU extension for this. As such anything I claim here may well be untrue in another implementation. My recommendation is that you just don't use them at all.
Your nested test_function is shadowing the global declaration. So the test_function you declared above main is never called, because the call inside main refers to the nested function. Hence, you get a warning.
You should declare int test_function outside of main
for example.
int test_function(int x)
and then call the function in main.
value = test_function(x)
This is what your code should look like:
#include <stdlib.h>
#include <stdio.h>
int test_function(x)
{
int value = 0;
if (x==0)
{
printf("the letters are the same");
}
return value;
}
int main(){
int x = 0;
char letter[] ={"HAAA"};
char cmpre[] = {"AHD"};
int value = 0; // unused
for(int i=0; i<4;i++)
{
if(letter[i] == cmpre[i])
{
x=0;
}
}
printf("To check if the letters are the same go to the function");
test_function(x);
return 0;
}
Note that if you dont need a return value you could make the function void.
And initialize your variables. You may search hours to find such a error
#include <stdio.h>
#include <stdlib.h>
void message(char m)
{
print("Hello\n");
}
int main()
{
message(m);
}
Error message when I try to compile
danielc#Ubuntu11:$ gcc Messagef.c -o Messagef
Messagef.c: In function ‘main’:
Messagef.c:11:9: error: ‘m’ undeclared (first use in this function)
Messagef.c:11:9: note: each undeclared identifier is reported only once for each function it appears in
I know that am doing a 'silly' mistake but I just see where am going wrong
Your function takes a char parameter but never uses it. The simplest fix is to remove the unused parameter:
#include <stdio.h>
void message()
{
printf("Hello\n");
}
int main()
{
message();
return 0;
}
Alternatively, change your method to use the parameter, and pass in a character as an argument:
#include <stdio.h>
void message(char m)
{
printf("Hello%c\n", m);
}
int main()
{
message('!');
return 0;
}
See it working online: ideone
Declare m in your main (char m = '?';)
Try "printf" instead of "print"
the variable "m" your passing to the message function has not been defined before its passed.
define the m variable above message() or pass a char literal to the function
Your function expects a char and you are passing m without declaring it. You need to declare m first like this:
char m = 'a';
And then call the function. BTW, you are not doing anything with this variable so it is redundant anyway.
Pick up a book of C language and start following that.
alternately u can initialise m with Hello message. pass the pointer to message to function and then print in message function, somewhat like this:
void message(char *msg)
{
printf("%s", msg);
}
int main()
{
char *m = "Hello";
message(m);
return 0;
}