why this program is not showing any error? - c

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

Related

Asking the syntax of Function pointer. - int (int)

I've learned function pointer is used as :
double (*ptr)(double)
ptr = my_func1;
And also, using 'typedef' could be
typedef double (*func1)(double);
func1 my_func1;
But I can't understand why this code is valid below :
int main(void){
test(a);
}
void test(int f(int))
{\
int x;\
(f==a)?(x=1):(x=2);\
printf("%d",f(x));\
}
What's that int f(int)? Is it same syntax with function pointer?
I know the type int (*)int is valid, but I've never seen the type int (int).
And Also I can't understand why the syntax in the main fuction "int f(int) = func_1" is invalid but in the 'test' function's parameter int f(int) = a is valid.
Please tell me TT Thank you.
int f(int) is a function declaration.
In C, two kinds of entities cannot be passed into functions, returned from functions or assigned: arrays and functions.
If parameters of these types are declared, they are silently turned into pointers.
A parameter int f(int) is changed to mean int (*f)(int), similarly to the way a parameter int a[3] is changed to int *a.
You might see a gratuitous warning if you declare a function one way, but define it the other; e.g.:
void test(int (*)(int));
void test(int f(int))
{
}
I've seen some version of GCC warn about this, even though it is correct; the definition and declaration are 100% compatible.
Because inside test, f is declared as a pointer, it is assignable:
f = some_func
However, in a situation where int f(int) declares a function, the identifier is not assignable:
{
int f(int); // "There exists an external f function" -- okay.
f = some_func; // Error; what? You can't assign functions!
}
Outside of function parameter declarations, functions and pointers to functions are distinguished in the usual way, as are arrays and pointers.

Getting Previous declaration is here Error in C

Task: write a function to calculate the area of a square, rectangle, circle.
My code is right. IDK why I am getting this error complete error
#include<stdio.h>
#include<math.h>
int square();
float airclearea();
int rectangle();
int main(){
int s,r1,r2;
float a;
printf("Enter the side of square : ");
scanf("%d",&s);
printf("Enter the side of rectangle");
scanf("%d %d",&r1,&r2);
printf("Enter the radius of circle");
scanf("%f",&a);
printf("%d",square(s));
printf("%d",rectangle(r1,r2));
printf("%f",airclearea(a));
return 0;
}
int square(int s){
return s*s;
}
int rectangle(int r1, int r2){
return r1*r2;
}
float airclearea(float a){
return 3.14*a*a;
}
Before main you declared the function airclearea without a parameter
float airclearea();
But you are calling the function with an argument of the type float
printf("%f",airclearea(a));
In this case the compiler performs default argument promotions and in the case of the function the argument of the type float is promoted to the type double.
So the compiler expects that the function is defined with a parameter of the type double. But the function is defined with a parameter of the type float
float airclearea(float a){
return 3.14*a*a;
}
Either declare the function before main with the parameter of the type float
float airclearea( float );
or in its definition specify the parameter as having the type double.
float airclearea(double a){
return 3.14*a*a;
}
In any case it is always better to provide function prototypes before referencing to functions.
You originally defined float airclearea(); near the top of your code as having no parameters. At the bottom of your code, you redefined it as
float airclearea(float a){
return 3.14*a*a;
}
The first definition defines float airclearea();, without parameters. Replace it with float airclearea(float a);. Your parameter is float a. You haven't shown your other error messages, but I would assume you are getting the same error with int rectangle(); and int square();. Add parameters to the first definitions of both those functions, or just move the main function down to the bottom of your code and remove the top three placeholder definitions.

Implicit function conversion is for double argument to int parameter

#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.

C Why function pointer as parameter instead of just a function?

I have been reading about having functions with functions as parameters, and particulary in C, they use function pointers. Let's suppose I want to implement the newton raphson method (in a simple way) for computing zeros in non linear equations.
double derivative(double f(double), double x)
{
double h = 1e-9;
return (f(x + h) - f(x)) / h;
}
double newton_raphson(double f(double), double x0, double tol)
{
double xk, diff;
do
{
xk = x0 - f(x0) / derivative(f, x0);
diff = fabs(xk - x0);
x0 = xk;
} while (diff >= tol);
return xk;
}
So, to compute an approximation for derivative I need a function that returns a double and takes a double as an argument. Same for computing a root of the function, given the other parameters. My question is, why is this different from declaring function parameters as function pointers? For instance declaring the input parameter f as a function pointer instead of just a function...
The parameter f is a pointer-to-function in both derivative and newton_raphson.
double derivative(double f(double), double x) { ... }
is exactly equivalent to
double derivative(double (*f)(double), double x) { ... }
Only, the former looks nicer - usually when you can omit parentheses, you should probably do so. After all both of them are equivalent to
double ((((derivative)))(double (((*(f))))(double ((trouble))), double ((x)))) { ... }
That I hope will only ever be used in IOCCC.
However, if you're declaring, defining a variable (not a function parameter), you need to use
double (*f)(double);
as
double f(double);
is just a function declaration.
6.7.6.3 Function declarators (including prototypes) of C11 draft n1570 says:
A declaration of a parameter as ‘‘function returning
type
’’ shall be adjusted to ‘‘pointer to
function returning
type
’’, as in 6.3.2.1.
And
6.9.1 Function definitions further says that
[...] the type of each parameter is adjusted as described in 6.7.6.3 for a parameter type list; the resulting type shall be a complete object
type.
additionally it has the following example:
EXAMPLE 2
To pass one function to another, one might say
int f(void);
/* ... */
g(f);
Then the definition of g might read
void g(int (*funcp)(void))
{
/* ... *
(*funcp)(); /* or funcp(); ... */
}
or, equivalently,
void g(int func(void))
{
/* ... */
func(); /* or (*func)(); ... */
}
Like normal data pointers, a function pointer can be passed as an argument and can also be returned from a function. A function’s name holds the address of function.
My question is, why is this different from declaring function parameters as function pointers? For instance declaring the input parameter f as a function pointer instead of just a function...
The answer is that both forms will be treated as same by compiler.
But for readibility of your code, go with the kind of declaration that your example code has, i.e.,
double derivative(double f(double), double x) { ... }
Even in C, the function definitions given below will be interpreted as same-
void foo(int a[]) // or int a[10]
{
...
}
void foo(int *a)
{
...
}

Error in calling function

While compiling this code in the terminal, I am getting an error saying :
newfile1.c:17: error: conflicting types for ‘average’
newfile1.c:2: note: previous declaration of ‘average’ was here
I don't see what is wrong with the code. Could someone help me out?
enter code here
#include<stdio.h>
float average(float);
int main()
{
float marks[4],avg;
int i;
printf("Please enter your marks\n");
for(i=0;i<=3;i++)
{
scanf("%d",&marks[i]);
}
avg = average(marks[4]);
printf("The average marks value is %f",avg);
return 0;
}
float average(float a[4])
{
int i,sum;
float avg_m;
for(i=0;i<=3;i++)
{
sum=sum+a[i];
}
avg_m=sum/3;
return avg_m;
}
Replace
float average(float);
with
float average(float[]);
The function declaration and definition are not matching.
Then call the function like this:
avg = average(marks);
Change line in your file
float average(float);
to
float average(float []);
You have declared the function to take one float instead you want array of floats.
Also, while calling it in main, change to
avg = average(marks);
float average(float);
expects a float variable . You need to pass an array , so add
float average(float[]);. Error happened since your function declaration and definition not matching.
in your main, you should call avg = average(marks); to pass the array to function avg = average(marks[4]); will pass a single variable.
In the prototype of average, you have given float as argument type so compiler is expecting a single float value as argument. If you want to pass an array of values, you have to declare your prototype like this:
float average(float input_marks[]);
You can't give length of an array argument in a prototype or definition. You have to pass array length as a separate argument. So your prototype should look something like
float average(float a[], int a_length);
Your function average takes one float as argument, hence the declaration should be floa avaerage(float). If you do float average(float a[4]) you are telling compiler that your function takes an array of 4 floats as argument.

Resources