Ok I have been trying to figure this out for 3 hours and I have search a bunch of stack overflow questions and answers but I have this same error:
/usr/bin/ld: /tmp/ccXbXNRV.o: in function 'main':
main.c:(.text+0x5a): undefined reference to 'plus'
/usr/bin/ld: main.c:(.text+0x67): undefined reference to `minus'
collect2: error: ld returned 1 exit status
And I cant figure this out because my code doesn't seem that he is the problem
main.c:
#include <stdio.h>
#include "funcs.c"
int main()
{
int z = 0;
int wh = 1;
while (wh == 1)
{
printf("What you want?\n1-Plus\n2-Minus\n");
scanf("%d", &z);
if (z == 1)
{
plus();
}
if (z == 2)
{
minus();
}
}
printf("The program ended\n");
return 0;
}
funcs.c
#include <stdio.h>
inline void plus(void)
{
int a = 0;
int b = 0;
printf("Pls insert a numb\n");
scanf("%d", &a);
printf("Pls insert a numb\n");
scanf("%d", &b);
a = a + b;
printf("The result is: %d\n", a);
}
inline void minus(void)
{
int a = 0;
int b = 0;
printf("Pls insert a numb\n");
scanf("%d", &a);
printf("Pls insert a numb\n");
scanf("%d", &b);
a = a - b;
printf("The result is: %d\n", a);
}
help.h
extern int a;
extern int b;
extern int z;
extern int wh;
inline void minus(void);
inline void plus(void);
I have try to compile it with this command gcc funcs.c main.c
I know that is a simple program but I really want to learn c
If you could help I would be very thankful!
You can fix this by doing three things:
Don't include a .c file. You're already providing it to gcc on the command line.
Include help.h in files where you use those functions.
Not using inline. You can't use inline when the caller and callee are in different translation units.
main.c:
#include <stdio.h>
// #include "funcs.c"
#include "help.h"
int main()
{
int z = 0;
int wh = 1;
while (wh == 1)
{
printf("What you want?\n1-Plus\n2-Minus\n");
scanf("%d", &z);
if (z == 1)
{
plus();
}
if (z == 2)
{
minus();
}
}
printf("The program ended\n");
return 0;
}
funcs.c
#include <stdio.h>
void plus(void)
{
int a = 0;
int b = 0;
printf("Pls insert a numb\n");
scanf("%d", &a);
printf("Pls insert a numb\n");
scanf("%d", &b);
a = a + b;
printf("The result is: %d\n", a);
}
void minus(void)
{
int a = 0;
int b = 0;
printf("Pls insert a numb\n");
scanf("%d", &a);
printf("Pls insert a numb\n");
scanf("%d", &b);
a = a - b;
printf("The result is: %d\n", a);
}
help.h:
extern int a;
extern int b;
extern int z;
extern int wh;
void minus(void);
void plus(void);
Compile and run like so:
$ gcc -Wall -Werror funcs.c main.c
$ ./a.out
What you want?
1-Plus
2-Minus
^C
Other thoughts:
extern int a;
extern int b;
extern int z;
extern int wh;
You're already declaring these variables locally. This is unneeded. The extern keyword tells the compiler that these variables are defined in another translation unit that it can't see. This isn't true, so you should just remove these.
Related
#include<stdio.h>
void add(int a,int b)
{
int c=a+b;
printf("\nSum=%d",c);
}
void hello(char *name)
{
printf("Hello %s",*name);
}
int main()
{
int a,b;
char name[20];
void (*ptr)(int,int)=&add;
void (*hello)(char*)=hello;
printf("Enter your Name:");
scanf("%s",&name);
hello(&name);
printf("Enter the two values\n");
scanf("%d%d",&a,&b);
ptr(a,b);
return 0;
}
i want to take input from user then pass it to function but i am unable to do so.
Here is what my complier shows as error: https://i.stack.imgur.com/DVYL6.png
You don't need to access the array address, it will be implicitly converted to char* when you pass it to the functions (both to scanf and hello).
I don't see the use of the functions pointers, so in order to simplify the code, I would rewrite it like this:
#include <stdio.h>
void add(int a, int b)
{
printf("Sum = %d\n", a + b);
}
void hello(char *name)
{
printf("Hello %s\n", name);
}
int main()
{
int a = 0, b = 0;
char name[20];
printf("Enter your Name:\n");
scanf("%s", name);
hello(name);
printf("Enter the two values\n");
scanf("%d%d", &a ,&b);
add(a, b);
return 0;
}
If you insist of using the pointers, this is how main should be written:
int main()
{
int a = 0, b = 0;
char name[20];
void (*add_ptr)(int, int) = &add;
void (*hello_ptr)(char *) = &hello;
printf("Enter your Name:\n");
scanf("%s", name);
hello_ptr(name);
printf("Enter the two values\n");
scanf("%d%d", &a, &b);
add_ptr(a, b);
return 0;
}
I have to create a program that has a function charster() that accepts three arguments: a character and two integers. It will print he character input x number of times per y number of lines. I am getting syntax error LNK2019 and LNK1120 Unresolved external symbol referenced in main. And IDK what I have done to cause this or what to do to correct it.
Here is my code:
#include <stdio.h>
void charster(char n, int n_char, int n_lines);
int main(void)
{
char n;
int n_lines, n_chars;
printf("Please enter a character to be printed, the amount of times it should appear, and the amount of lines that should appear: ");
while (scanf("%c, %d, %d", &n, &n_chars, &n_lines) == 3)
{
charster(n, n_chars, n_lines);
}
return 0;
void charster(char n, int n_char, int n_lines);
{
int count;
for (count = 0; count < n_lines; count++)
{
putchar(n);
}
}
}
Firstly C language does not support nested functions. So you have to write the function charster() outside the main function.
You can write it above or below the main function as prototype is already declared.
Kindly have a look at the following code:
#include<stdio.h>
void charster(char n, int n_char, int n_lines);
int main(void)
{
char n;
int n_lines, n_chars;
printf("Please enter a character to be printed, the amount of times it should appear, and the amount of lines that should appear: ");
while (scanf("%c %d %d", &n, &n_chars, &n_lines) == 3)
{
charster(n, n_chars, n_lines);
}
return 0;
}
void charster(char n, int n_char, int n_lines)
{
int times;
int lines;
for (lines = 0; lines < n_lines; lines++)
{
for(times=0;times<n_char;times++){
printf("%c ", n);
}
printf("\n");
}
}
I am learning about functions and how to call upon them and use them in class. I don't quite understand where I've gone wrong here. I know that there are some mistakes around the int main part. I have asked my teacher and he is reluctant on giving me an example that would solve my problems or help me out. I think my main problem is at factorial_result = factorial();
#include <stdio.h>
void mystamp(void)
{
printf("My name is John Appleseed\n");
printf("My lab time is 12:30 on Sunday\n");
return;
}
int getnum(void)
{
int local_var;
printf("Please enter an integer: ");
scanf("%d%*c", local_var);
return(local_var);
}
int factorial(void)
{
int x,f=1,local_var;
for(x=1; x <= local_var; x++)
f = f * x;
return(f);
}
int main(void)
{
int result;
int factorial_result;
mystamp();
result = getnum();
factorial_result = factorial();
printf("You typed %d\n", result);
printf("The factorial is %d\n", factorial_result);
return;
}
Declare local_var as a global variable and do:
local_var = getnum();
OR
Change main() to:
int main(void)
{
int result;
int factorial_result;
mystamp();
result = getnum();
factorial_result = factorial(result);
printf("You typed %d\n", result);
printf("The factorial is %d\n", factorial_result);
return;
}
And factorial() to:
int factorial(int n)
{
int x,f=1,local_var=n;
for(x=1; x <= local_var; x++)
f = f * x;
return(f);
}
Your factorial should be calculated based on the input( i.e in your case int result ).
So, your method factorial() should looks as follows :
int factorial( int number )
{
int factorial_value = 1;
while( number > 0 )
{
factorial_value *= number;
number--;
}
return factorial_value;
}
Then, the correct factorial would be returned and printed accordingly ! Regarding the scope of the variables that you have used, see the comments under your question.
#include <stdio.h>
int factorial(int);
int main()
{
int num;
int result;
printf("Enter a number to find it's Factorial: ");
scanf("%d", &num);
if (num < 0)
{
printf("Factorial of negative number not possible\n");
}
else
{
result = factorial(num);
printf("The Factorial of %d is %d.\n", num, result);
}
return 0;
}
int factorial(int num)
{
if (num == 0 || num == 1)
{
return 1;
}
else
{
return(num * factorial(num - 1));
}
}
This is a simple factorial program using recursion calling function !
include
int main()
{
int c, n, fact = 1;
printf("Enter a number to calculate its factorial\n"); scanf("%d", &n);
for (c = 1; c <= n; c++) fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
so i have this C code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
int b;
int c;
scanf("%d", &b);
scanf("%d", &a);
c = a + b;
printf(c);
return 0;
}
but, after i insert number for a and b, the program stop working. please help me
C noob here
In your code you have the following line is wrong:
printf(c);
as the printf() syntax would be like what i've written below
printf("%d",c);
so your now code would be:
#include <stdio.h>
int main()
{
int a;
int b;
int c;
scanf("%d", &b);
scanf("%d", &a);
c= a + b;
printf("%d",c); //this is the correct printf() syntax
return 0;
}
printf(c);
should be
printf("%d\n", c); /* `\n` at the end of the string flushes the `stdout` */
because printf expects a const char* as its first argument, not an int.
guys I'm trying to compile my program in c but I'm getting this error (conflicting types for allocArray)?
Here is my code:
#include <stdio.h>
#include <stdlib.h>
int number(int);
char *allocArray(int);
int main ()
{
printf("Enter a number: ");
int userNumber;
scanf("%d", &userNumber);
int m= number(userNumber);
printf("\nThe number is %d", m);
printf("\n");
printf("*****************************************************\n");
printf("The array is %s", alloArray(5));
}
int number(int n)
{
int num = n;
return num;
}
char *alloArray(int num)
{
char *addr;
addr = (char *) malloc(num);
//addr = char[num];
return addr;
}
You've misspelt allocArray as alloArray (twice, in fact).