Implicit function conversion is for double argument to int parameter - c

#include <stdio.h>
int main() {
foo(22.3);
return 0;
}
void foo(int x) {
printf("%d", x);
}
Prints 1. Why isn't it printing 22 instead? I believe that when I call foo without declaring an explicit prototype, c generates an implicit prototype that converts short and char to int, and float to double in the function arguments. So 22.3 isn't touched in the call to foo(22.3) and then in the callee function, the parameter int x gets assigned to 22.3: int x = 22.3 which results in numeric conversion wherein the fractional part gets dropped, leaving just 22 in x. So why isn't it printing 22 instead of 1 ?
I think I've got it wrong as to what happens when a caller calls a callee, with mismatched parameter types?

You're calling a function with parameters that are not compatible with the arguments. Because the function doesn't have a prototype, the double-to-int conversion doesn't occur. This results in undefined behavior.
If you were to pass an int, or a smaller integer type that would be promoted to int, then the function would work.

You forgot about function prototype:
#include <stdio.h>
void foo(int); // <--- here!
int main() {
foo(22.3);
return 0;
}
void foo(int x) {
printf("%d", x);
}
Without this, the compiler probably takes bytes from the beginning of your float value 22.3 and treats them as integer value.

Related

Cannot print called float variables

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);
}

Function declaration before and after main()

#include <stdio.h>
int add2nums( int, int);
void main(void)
{
int y,a,b;
printf("Enter 2 numbers\n");
scanf("%d%d", &a, &b);
y = add2nums(a,b);
printf("a is %d\n", a);
printf("b is %d\n", b);
printf("y is %d\n", y);
}
int add2nums( int num1, int num2)
{
int sum;
sum = num1 + num2;
return(sum);
}
So usually, when I create new functions in C the definition of the function is created before the main() function.
In my lecture, there is this example of how to create a function prototype and how they are created by declaring it before the main() function and then defining it after the end of the main() function.
When running the program above, the following error comes up:
Line5: warning: return type of 'main' is not 'int' [-Wmain]|
What is happening? And why is the declaration of the function add2nums() occur twice once before main() and with no parameters?
int add2nums( int, int);
and then again after the end of main() with parameters num1 and num2
int add2nums( int num1, int num2)
There are two valid signatures for the main() function:
int main( void )
int main( int argc, char *argv[] )
Notice that both valid signatures have a return type of int. Any other return type, such as void is not valid and causes the compiler to output a warning message.
When the code calls a function, the compiler needs to know the signature of that called function. There are two ways to tell the compiler what the signature of the called function is:
have the whole function listed before where it is called
have a prototype (aka forward reference) of the function signature before where the function is called. In a prototype the compiler only needs the returned type and the types of the parameters. However, it is a good programming practice to have the names of the parameters listed in the prototype as a courtesy to the humans reading the code.
In C language, the main function has a signature int main(), which lets you to return a value back to the OS, but you made the main() with no void return type. Because of that you've got the warning.
int add2nums( int, int); is just a declaration of the function which lets the compiler make a reference to the function. That's because the compiler reads the file only once.
Names of the parameters aren't required, because they aren't part of the signature.
Declaration of the function is the information for the compiler (not linker as another answer states) - what type the parameters of the function are and what type of the return value is.
So the names of the parameters are not needed.

What does `int (*)(int)` in c mean with regard to type?

I have this C code (inspired by a test)
#include <stdio.h>
int foo (int n)
{
static int s = 0;
return s += n;
}
int main()
{
int y;
int i;
for (i=0; i<5; i++) {
y= foo(i);
}
printf("%d\n", foo);
return 0;
}
and I am specifically interested in the value of foo and what type it has. The compiler gives me this warning
test.c:18:16: warning: format specifies type 'int' but the argument has type
'int (*)(int)' [-Wformat]
but I'm not really sure what that means. What is an int (*)(int) and how does calling the function name with no arguments give me something of this type?
Without the function call, foo evaluates to a pointer to a function that takes an int and returns an int. That is the long description of int (*)(int).
You are not calling foo. To call foo, place (argument) after the name. As it is, you take the address of foo, which is of type int (*)(int) (pointer to function taking one integer argument and returning an int) and send it to printf.
printf then complains because you are trying to tell it to print an int, but are giving it a function pointer.

expected expression before double

Keeps saying that there is an expected expression before double int pt function
#include <stdio.h>
int pt (int a, int b)
{
int c, result;
c = (a * a) + (b + b);
result = double sqrt (double c);
return result;
}
int main (void)
{
int d, e, f;
int pt (int a, int b);
printf("type enter after input of the two legs");
scanf("%i", &d);
scanf("%i", &e);
f = pt (d,e);
printf("the hypotenuse is %i", f);
return 0;
}
Change
result = double sqrt (double c);
to
result = sqrt(c);
You don't have to cast c into a double when you pass it into the sqrt function because of implicit conversion. If you still wanted to do the cast, the correct way would be sqrt((double) c).
Also, #include <math.h> for use of the sqrt function.
Note: It's not required to cast the return type of sqrt to int (relevant since result is of type int) - however some compilers may give warnings about implicit conversion (Credit to #MattMcNabb). It can also be good practice to put the cast in to signal to other coders that the precision loss is intentional.
double sqrt (double c); is a function declaration. (It is also a function prototype). This is how you announce that a function exists, and what parameters it takes and what it returns. The word c here does not mean anything, it can be omitted.
When you want to call a function you do not repeat this info. You just give the function name, followed by a list of values, e.g. sqrt(c) .
In your code , sqrt has not been declared. Functions must be declared before they are called. It's not possible to simultaneously declare a function and call it; you have to declare it first.
(Historical note: this is true from C99 onwards; in C89 you could call an undeclared function, and the compiler would assume you wanted the function declared to return int ; this would cause undefined behaviour for any function that does not actually return int).
It is possible for you to provide your own declaration, so long as it matches the standard declaration:
double sqrt(double);
However it is a better idea to just use the standard declaration by going:
#include <math.h>
which works because the file math.h includes the line double sqrt(double); (or something equivalent).
Then you can write:
result = sqrt(c);
Note that you do not need to use any casts in relation to this function call. Since a function prototype exists, the compiler knows that even if you supply an int, it should convert that int to a double (which it knows how to do), and vice versa.
It is talking about the casting that you are doing?
your casting is complaining it should be like this:
your result is an integer, so you should be casting it to int..
but if you want to cast anything to double it should be as follow
result = (double)sqrt((double) c)
or another way is double(sqrt((double) c)
but if you want to cast it as int then
result = (int)sqrt((double) c)
or
result = int(sqrt((double) c))
hope this helps good luck

why this program is not showing any error?

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);

Resources