I want to pass a float number to another function.
int main()
{
float start=0;
float step=0.1;
int number=10;
fun(start,step,number)
}
fun(float star, float ste, int numbe)
{
//here I get "star = 0", "numbe = 10", but "ste = -1.084264e-19"
}
what is wrong here?
Thank you
It is most certainly possible to pass a float to a function, as long as you provide a prototype or move the definition ahead of the first use of the function.
A prototype (also called a forward declaration) looks like this:
void fun(float star, float ste, int number);
In larger projects, prototypes go into header files.
Note: do not forget to add void in front of the function definition as well. Otherwise, the compiler treats your function as returning an int.
If you omit the prototype, the compiler will default to using the old K&R C rules to decide how to pass arguments to your function. The result is the unusual behaviour you are seeing, and hopefully a compiler warning as well.
You need a correct function declaration before your function call:
Add:
void fun(float star, float ste, int numbe);
before your main declaration and also add void return type in your fun function definition.
you need to declare the function before main function.
Try this:
void fun(float star, float ste, int number);
int main()
{
float start=0;
float step=0.1;
int number=10;
fun(start,step,number);
}
Related
I cannot print float variables when calling my functions. int variables print but floats won't print their proper value when passed to my function.
I tried to change the float to int and that worked
int main() {
int foo = 6;
call(foo);
}
void call(int bar) {
printf("%d", bar);
}
This worked and it does indeed print 6.
But, doing the same but with floats only prints out 0.00000:
int main() {
float foo = 6;
call(foo);
}
void call(float bar) {
printf("%f", bar);
}
How do I correctly call and then print float variables?
you need a forward declaration of call
void call(float integerrrr);
int main(){
float integerrrr=6;
call(integerrrr);
}
void call(float integerrrr){
printf("%f", integerrrr);
}
your compiler probably warned you about this
You could simply define call above main instead of below it. The compiler must have seen the declaration of functions when they are used, so a forward declaration like pm100 suggests is one way. Moving the whole definition above main is the another (that does not require a forward declaration).
#include <stdio.h>
void call(float integerrrr){
printf("%f", integerrrr);
}
int main(void){
float integerrrr = 6;
call(integerrrr); // now the compiler knows about this function
}
INT type variables i can print but float just does not
If your program actually compiles as-is it will use an old (obsolete) rule that makes an implicit declaration of undeclared functions when they are used. The implicit declaration would be int call(); - which does not match your actual function. The program (even the one that seems to be working) therefore had undefined behavior.
the compiler of c work from top to bottom so line by line,
so u will have to call the first function void call() then your main function:
void call(float integerrrr){
printf("%f", integerrrr);
}
int main(){
float integerrrr=6;
call(integerrrr);
}
hey i try a call by reference from an int array:
void function2(int stapel,int colour){
stapel[1]=stapel[0]
stapel[0]=colour;
}
void function1(int stapel){
int colour=2;
function2(stapel,colour);
}
int main(){
int *stapel;
stapel=malloc(sizeof(int)*2);
function1(stapel);
}
whats wrong? :O i want to use stapel now in my main function.
You had wrong function declarations, your functions are receiving pointers.
You need to use
void function2(int *stapel,int colour){...
void function1(int *stapel){...
Instead of just int stape. This is the complete code:
void function2(int *stapel,int colour){
stapel[1]=stapel[0]
stapel[0]=colour;
}
void function1(int *stapel){
int colour=2;
function2(stapel, colour);
}
int main(){
int *stapel;
stapel=malloc(sizeof(int)*2);
function1(stapel);
free(stapel); // Also free the memory
}
As pointed out in the comment, also remember to free the memory at the end (here it makes no actual difference because the program will be terminated, but is always a good practice).
In the main function you are passing a pointer to int as argument, but the functions does not take pointer to int. Change the functions to take pointers. The errors you get should be pretty obvious in this regard.
You also have a ordering-problem in our function2 where you use uninitialized data to initialize stapel[1].
stape1 is a pointer to int and you can not pass it as argument to a function that expects a integer value. However from the use in the function bodies it seems you should change the function declaration to expect int* instead of ints.
check this
Edit this
function1(int *stapel)
and
function2(int *stapel,int colour)
You are not "calling-by-reference" you just say int stape1 in the function signature, if you wish to use a reference (or an array) in C pass a pointer.
void function1(int * stapel){
//...
}
Then you can pass the pointer from main
function1(stapel);
Don't forget to free it afterwards
free(stape1);
#include<stdio.h>
#include<malloc.h>
void function2(int *stapel,int colour){
stapel[0]=colour;
stapel[1]=stapel[0];
}
void function1(int *stapel){
int colour=2;
function2(stapel,colour);
}
int main(){
int *stapel;
stapel=malloc(sizeof(int)*2);
function1(stapel);
printf("stapel[0]=%d stapel[1]=%d",stapel[0],stapel[1]);
return 0;
}
try this code it works correct.......
in your program you are passing a pointer but defined it as integer
and there are other problem that i corrected ........
if you have any confusion you can ask............
I think you may have to use int *stapel in the function parameter list. I think int stapel is a value of type int pushed on the stack of the function. I think int *stapel is a pointer to an address pushed on the stack. Finally, I seem to remember using &stapel to dereference the pointer. It has been a long time for me. I am sure one of the bright folks will give you a better answer ;)
in the code below .
i have defined function prototype with no argument
in definition as well as in function call i have used one parameter.
i would like to know why i am not getting any error ?
# include <stdio.h>
float circle(); /* no parameter*/
int main()
{
float area;
int radius =2;
area=circle(radius);
printf("%f \n",area);
return 0;
}
float circle( r) /* with one parameter even no parameter type */
{
float a;
a=3.14*r*r;
return (a);
}
The
float circle();
is not a function with zero parameters. It's a function with an unspecified number of parameters.
The
float circle( r) {
is a K&R-style definition in which the type of r defaults to int. See https://stackoverflow.com/a/18433812/367273
This is because compiler treat r as int by default when no parameter is defined for circle. Try to run your code after declaring function prototype as
float circle(void);
and you will get error.
That's because function
float circle();
declaration doesn't declare function that takes no arguments.
It's implicitly declared as a function that takes undefined number of integer variables as arguments.
Just like
function();
is valid function declaration. Implicitly this function will be treated as function taking int as arguments and returning int.
If you want to declare function function taking no arguments or not returning any value, you do it with void keyword:
void funct(void);
I know this question sound stupid but please answer it.
Let us say we have int A(int num); as a function prototype.
What would we refer to as the name of the function? int A(int num) or A?
Also does the body of the function the part at the top of the definition?
int A(int num){ <-- Does the body include this?
return num * num;
}
Thank you.
Normally you'd go with just "A" as the name of the function; as you said, int A(int num) is its prototype; the body is what the function does, so here just return (num * num);. When referencing a function in a language that allows overloads, you might use the prototype instead of the name, however.
The name of int A(int num) is A.
The first part, int, describe the return type of the function, then we have the name of the function, A, and finally a list of parameters the function accepts (int num). In this case it is a single parameter of type int
The body of a function, is the code which actually performs the functionality of the function. Basically its the code within the curly brackets: { }
int A(int num){
return(num * num); //this is the body
}
So my problem is, I am passing a variable by value (it is a float) from one function in my C code to another function. For some reason the variable is 0 after the pass. I have multiple other floats being passed by value, that are not 0 (all in the same function call), so I can't understand why this one is. It might be as simple as some typo that I am just not seeing:
int cuda_call(float *h_DataA, float *h_Kernel, int numSmooths, float kernelSum, int KERNEL_R, int KERNEL_W, int DATA_W){
printf("\n What am I here?: %f \n", kernelSum);
convolutionProgram(h_DataA, h_Kernel, numSmooths, kernelSum, KERNEL_R, KERNEL_W, DATA_W);
return 1;
}
extern "C" void convolutionProgram(float *h_DataA, float *h_Kernel, int numSmooths, float kernelSum, int KERNEL_R, int KERNEL_W, int DATA_W);
void convolutionProgram(float *h_DataA, float *h_Kernel, int numSmooths, float kernelSum, int KERNEL_R, int KERNEL_W, int DATA_W){
printf("\n what am I now? %f \n", kernelSum);
float
*d_DataA,
*d_DataB,
*d_Temp,
*d_Kernel;
.......
}
By the time I get to that second print in the called function, it is 0.
Maybe you have somehow ended up mixing calling conventions (ABIs). For instance, the first compilation unit may be performing a cdecl call, while the second one is compiled with fastcall.
http://en.wikipedia.org/wiki/X86_calling_conventions
Is it possible that you have not rebuilt the object file for one of the c files? This looks like a signature mismatch which can arrise from a function signature change without rebuilding both object files.