Undefined reference to `Function' C [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have researched a solution for this error but I still don't get it.
#include <stdio.h>
float calcF(float a, float b, float c);
int main(void)
{
float mag_flux_den, cur, len;
float result;
printf("What is the magnetic flux density in tesla? : ");
scanf("%f", &mag_flux_den);
printf("What is the current in the conductor in Amperes? : ");
scanf("%f", &cur);
printf("What is the length of the conductor in the magnetic field in metres? : ");
scanf("%f", &len);
result = calcF(mag_flux_den, cur, len);
printf("Force on the current carrying conductor: %f", result);
return 0;
}
float calcf(float a, float b, float c) //calculates force on the current carrying conductor{
float F;
F = a * b * c;
return F;
}
I am using ideone.com and still getting the same error message (undefined reference to 'calcF'). Any help will be much appreciated.

You declared your function as "calcF" with a capital "F", but your definition is "calcf" with a lowercase "f". Make sure they are the same.
Also, your function definition at the bottom of your code block has it's open bracket at the end of the comment.
Move it after the comment.

Try this:
#include <stdio.h>
#include <cs50.h>
float calcF(float a, float b, float c);
int main(void)
{
float mag_flux_den, cur, len;
float result;
printf("What is the magnetic flux density in tesla? : ");
scanf("%f", &mag_flux_den);
printf("What is the current in the conductor in Amperes? : ");
scanf("%f", &cur);
printf("What is the length of the conductor in the magnetic field in metres? : ");
scanf("%f", &len);
result = calcF(mag_flux_den, cur, len);
printf("Force on the current carrying conductor: %f", result);
return 0;
}
float calcF(float a, float b, float c) //calculates force on the current carrying conductor
{
float F;
F = a * b * c;
return F;
}

Related

Why can't I properly do subtraction in this code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
#include<stdio.h>
int subtractNumbers(int n1,int n2);
int main(){
int n1,n2,difference;
printf("Enter Two Numbers: ");
scanf("%i%i",&n1,&n2);
subtractNumbers(n1,n2);
printf("The Difference is %i", n1,n2);
}
int subtractNumbers(int n1,int n2)
{
int result;
result =n1-n2;
return result;
}
Why can't this code properly do a subtraction?
Have to store the return value in 'difference' variable. (As I guess the 'difference' variable is meant for that)
In case of 'printf("The Difference is %i", n1,n2);' I guess the intention is to print the 'difference'. So change to 'printf("The Difference is %i", difference);'
#include<stdio.h>
int subtractNumbers(int n1,int n2);
int main(){
int n1,n2,difference;
printf("Enter Two Numbers: ");
scanf("%i%i",&n1,&n2);
difference = subtractNumbers(n1,n2); // stored the returend result
printf("The Difference is %i", difference); //printing only the result
}
int subtractNumbers(int n1,int n2)
{
int result;
result =n1-n2;
return result;
}
So basically you've mixed up the syntax. It's all right, but when you call your function, it's ok that you return a value, but the in your main you don't read that value.
This fixes it:
#include<stdio.h>
int subtractNumbers(int n1,int n2);
int main(){
int n1,n2,difference;
printf("Enter the first number : ");
scanf("%d",&n1);
printf("Enter the second number: ");
scanf("%d", &n2);
int result = subtractNumbers(n1,n2);
printf("The Difference is %d", result);
}
int subtractNumbers(int n1,int n2)
{
int result;
result = n1-n2;
return result;
}
Primary problem corrected like other answers, but with more commentary and a few "upgrades"...
#include <stdio.h>
// Please spread things out for your readers. Whitespace is free!
// if function PRECEEDS its use, it is its own "function prototype"
int subtractNumbers( int n1, int n2 )
{
int result = n1 - n2; // declare and assign together
return result;
}
int main() {
printf( "Enter Two Numbers: " );
// define variables "close" to when they are first used
int n1, n2;
// ALWAYS check return codes from system functions. Things go wrong
if( scanf( "%i%i", &n1, &n2 ) != 2 )
printf( "Something went wrong\n" );
else
{
// here was where the returned value was not being received. fixed.
int difference = subtractNumbers( n1, n2 );
printf( "The Difference is %i", difference );
}
return 0; // optional with some compilers
}

Program prints 0 as result of multiplication of two positive integers obtained via scanf("%f") [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
My Code:
#include <stdio.h>
int main()
{
int l, b, a;
printf("Enter the length of the rectangle: ");
scanf("%f", &l);
printf("Enter the breadth of the rectangle: ");
scanf("%f", &b);
printf("Area of rectangle is %f", l * b);
return 0;
}
When I give any input it doesn't show me its product, but 0.000000 instead:
As I gave input 2 and 3, it should print Area of rectangle is 6
%f expects its corresponding argument to have type float and you are passing int to it, so changing it to %d would fix the issue, as %d expects its corresponding argument to have type int.
#include <stdio.h>
int main() {
int length, breadth, area;
printf("\nEnter the Length of Rectangle: ");
scanf("%d", &length);
printf("\nEnter the Breadth of Rectangle: ");
scanf("%d", &breadth);
area = length * breadth;
printf("\nArea of Rectangle: %d\n", area);
return 0;
}

Scanf the numbers into function in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
here is the code I wrote.
#include <stdio.h>
#include <stdlib.h>
typedef struct note {
int pitch;
int velocity;
int channel;
} note;
int printnote(int pitch, int velocity, int channel);
int main() {
int size = 100;
note note;
struct note *ptr = malloc(size * sizeof(int));
printf("Input the values for the `pitch`, `velocity`, and `channel`\n");
scanf("%d %d %d", &note.pitch, &note.velocity, &note.channel);
printnote(note.pitch, note.velocity, note.channel);
free(ptr);
return 0;
}
int printnote(pitch, velocity, channel) {
printf("The MIDI Note is:\n");
printf("Pitch -> %d\n", pitch);
printf("velocity -> %d\n", velocity);
printf("channel -> %d\n", channel);
return 0;
};
When I run the code and type the numbers, it shows the wrong answers.
For example, I run the code, and it shows
Input the values for the `pitch`, `velocity`, and `channel`
5 5 5
The MIDI Note is:
Pitch -> 5
velocity -> 0
channel -> -429762432
The three numbers should be the same as the input numbers.
Can anyone help me?
There is a subtle typo in your scanf() conversion string:
scanf("%d %d %d", &note.pitch, &note.velocity, &note.channel);
You used the unicode full width percent sign % (\uff05) instead of the ASCII % character. scanf does not recognise this as a conversion specifier and tries to match the byte sequence used to encode %, (0xEF 0xBC 0x85 in UTF-8) and fails thus only converting the first input into note.pitch and leaving note.velocity and note.channel uninitialized, returning 1. Note how % looks different from % in the fixed font used for code, but identical in the font used for this text: % % % % % %.
Just replace % with the correct character:
scanf("%d %d %d", &note.pitch, &note.velocity, &note.channel);
Also note these remarks:
size and ptr are not used in main(),
you should check the return value of scanf() to detect invalid input. This check would have helped find the error,
the prototype in the definition of printnote is incorrect: the argument types are missing,
the ; after the } is useless,
using the same identifier note for the variable and its type is confusing.
Here is modified version:
#include <stdio.h>
typedef struct note {
int pitch;
int velocity;
int channel;
} note;
void printnote(int pitch, int velocity, int channel);
int main() {
note note1;
printf("Input the values for the `pitch`, `velocity`, and `channel`\n");
if (scanf("%d %d %d", &note1.pitch, &note1.velocity, &note1.channel) != 3) {
printf("invalid input\n");
return 1;
}
printnote(note1.pitch, note1.velocity, note1.channel);
return 0;
}
void printnote(int pitch, int velocity, int channel) {
printf("The MIDI Note is:\n");
printf("pitch -> %d\n", pitch);
printf("velocity -> %d\n", velocity);
printf("channel -> %d\n", channel);
}

How to deliver 2 int,1 float variable input in the input function to main function by using pointers? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm making bill calculater and i need to input 2integer and 1real number at input function and i need to take that 3 number to months function that calculate bill with 3numbers provided by input function.
...
float input(int*,int*,float*);
void months(int, int, int, int, float, float,int,int,float);
...
int main()
{
...
int x1,y1;
float z1;
...
int *x= &x1;
int *y= &y1;
float *z= &z1;
}
float input(int*x,int*y,float*z)
{
...
printf("\nInput your usages of voice : ");
scanf_s("%d", &x);
printf("Input your usages of text : ");
scanf_s("%d", &y);
printf("Input your usages of data : ");
scanf_s("%f", &z);
...
}
void months(...,int*x,int*y,float*z)
{
...
}
I expect if I input x,y,z 150,100,2.11 then
150,100,2.11 is put in months function either,but in months function x,y,z are trash value.
you already get the addresses of the variables in argument in input, so replace
scanf_s("%d", &x);
...
scanf_s("%d", &y);
...
scanf_s("%f", &z);
by
scanf_s("%d", x);
...
scanf_s("%d", y);
...
scanf_s("%f", z);
Out of that you do not need to have the pointer variables in main, you can replace
int x1,y1;
float z1;
...
int *x= &x1;
int *y= &y1;
float *z= &z1;
...
float v = input(x,y,z); /* added */
by
int x1,y1;
float z1;
...
float v = input(&x1,&x2,&z1); /* added */
And if I well understand in function month you can also directly give x1,y1,z1 rather than their addresses. Note the declaration void months(int, int, int, int, float, float,int,int,float); does not match with the definition even partial void months(...,int*x,int*y,float*z)
I also encourage you to check the result of scanf_s to be sure the user enter a valid input
If I change your code getting into account my remarks :
#include <stdio.h>
int input(int*,int*,float*);
float months(int, int, float);
int main()
{
int x1,y1;
float z1;
if (input(&x1, &y1, &z1))
printf("%g\n", months(x1, y1, z1));
return 0;
}
/* return 0 if an input is erronned */
int input(int*x,int*y,float*z)
{
printf("\nInput your usages of voice : ");
if (scanf_s("%d", x) != 1)
return 0;
printf("Input your usages of text : ");
if (scanf_s("%d", y) != 1)
return 0;
printf("Input your usages of data : ");
if (scanf_s("%f", z) != 1)
return 0;
return 1;
}
/* return the bill */
float months(int x,int y,float z)
{
return x + 1.2 * y + z *100; /* just to return a value */
}
Compilation and execution :
pi#raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra i.c
pi#raspberrypi:/tmp $ ./a.out
Input your usages of voice : 150
Input your usages of text : 100
Input your usages of data : 2.11
481

C function not calculating properly [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
In my function when I set *area = 2 or to any other int, my program works as intended. For some reason I cannot calculate the length by the width and have it out put properly, all I get are 0's. What am I missing? Is it something in the call? Also is %f an appropriate conversion for double or is there something better to use? My code is below:
#include <stdio.h>
void area_perimeter(double width, double length, double *area, double *perimeter);
int main(){
double width, length, are, peri;
printf("Enter the width of the rectangle: ");
scanf("%f",&width);
printf("Enter the length of the rectangle: ");
scanf("%f",&length);
area_perimeter(width, length, &are, &peri);
printf("The area of the rectangle is: %f\n",are);
printf("The perimeter of the rectangle is: %f\n",peri);
return 0;
}
void area_perimeter(double width, double length, double *area, double *perimeter){
*area = length * width;
*perimeter = (*area * 2);
}
You invoked undefined behavior by passing a pointer to object having wrong type: %f in scanf()callls for float*, but you passed double*. Use %lf to read double.
Using %f in printf() to print double is good. C99 compiler will also accept %lf.
Try this:
#include <stdio.h>
void area_perimeter(double width, double length, double *area, double *perimeter);
int main(void){
double width, length, are, peri;
printf("Enter the width of the rectangle: ");
if (scanf("%lf",&width) != 1) {
fputs("read error width\n", stderr);
return 1;
}
printf("Enter the length of the rectangle: ");
if (scanf("%lf",&length) != 1) {
fputs("read error length\n", stderr);
return 1;
}
area_perimeter(width, length, &are, &peri);
printf("The area of the rectangle is: %f\n",are);
printf("The perimeter of the rectangle is: %f\n",peri);
return 0;
}
void area_perimeter(double width, double length, double *area, double *perimeter){
*area = length * width;
*perimeter = (*area * 2);
}

Resources