c language printf float error - c

I define a function finding the mean value of an array of int, the function is as follows:
float MeanInt(int x[],int num){
float mean, sum = 0.0f,res=0.0f;
int i;
for(i=0;i<num;i++){
sum += (float)(x[i]);
printf("x[%d] is %d\n", i,x[i]);
printf("sum is %f\n", sum);
}
res = sum/((float)(num));
printf("mean should be %f\n", res);
return res;
}
the printf() within this function all works correctly.
The problem is that when I use it in my project, like follows:
printf("mean number is %f\n", MeanInt(ls_tr_acl,num_round));
I meet with an error saying that:
format %f expects argument of type double, but argument 2 has type int
I'm fully confused because the printf() within the function MeanInt() print out exactly the correct result. I also test MeanInt() on some toy examples and it always works correctly. The problem only happens when I run it in my project.

You are not declaring the function MeanInt before its first invocation so compiler assumes the type of MeanInt is a function returning int taking any number of arguments.
FixInclude appropriate header file (containing the declaration), or move the definition above usage or declare it before usage as:
float MeanInt(int x[],int num);
You can declare it at global scope at the top of the file or in narrower scope also.

Related

redefinition of ‘main’

As I am new to programming, I was trying to write a simple code using functions which will give me the addition of three numbers. Here's the code!
/* Your includes go here */
#include <stdio.h>
int addThreeNumbers(int a, int b, int c)
{
int d;
d = a + b + c;
return(d);
/* Complete this function only
DO NOT write main function.
*/
}
int main()
{
int x, y, z, sum;
printf("Enter the three numbers: ");
scanf(" %d %d %d", &x, &y, &z);
sum = addThreeNumbers(x, y, z);
printf("The sum is %d", sum);
return 0;
}
And the error was as follows:
solution.c:30:5: error: redefinition of ‘main’
solution.c:15:9: note: previous definition of ‘main’ was here
You have another main function in the code somewhere. Post the complete code and I will take a closer look. But that is the only way you can receive this error
In modern C, empty argument parentheses mean that the type and number of arguments is unknown.
Although this segment runs fine with most compilers, yours might be picky. Try declaring main with zero arguments explicitly, like this:
int main(void) {
//code
}
Pretty sure this is one of the online coding sites' question. They put in the main function themselves by appending it to the code, you don't have to explicitly write it. Delete the main function you have written and check if that works out.

error: pointer value used where a floating point value was expected

This is not homework, but my last assignment made it clear that I didn't clearly understand pointers when coding C.
Therefore, I tried to type a simple program using pointers just to invert an integer in a function, so I could figure out where the gap in my understanding is.
Apparently, I've arrived at it, but I still cannot figure out what I am doing wrong.
My source code is below.
#include <stdio.h>
float invert(int *num);
void main (void)
{
int num;
float a;
printf("enter an integer \n");
scanf("%i", &num);
printf("Number entered %i \n", num);
a=invert(&num);
printf("This is the invse from main %f \n", a);
}
float invert(int *num) /* function inverts integer */
{
float invse;
printf("num is %i \n\n", *num);
invse = 1/(float)num;
printf("invse is %f \n\n", invse);
return(invse);
}
My thinking was that I used the pointer to direct the computer to use the value stored at the address for num in the function invert(). The pointer appears in the variable declaration. I cast the value stored at that pointer as a float, so I could invert it, and store it in a local variable.
The problem appears to be in the local variable assignment. My compiler returns "invert.c:29:2: error: pointer value used where a floating point value was expected
invse = 1/(float)num;
^
Apparently my code indicates a pointer value for inverse, but I declared it as a float, which I find confusing.
Any help is appreciated. This will save me on completing my larger set of code for my assignment, which I did not post here.
Thanks.
Judging by the printf call inside invert
printf("num is %i \n\n", *num);
you already know that in order to access the value passed to invert for inversion you have to dereference num pointer: *num.
If so, then why aren't you dereferencing num when you perform the inversion itself?
invse = 1/(float)num;
I mean, if you are the one who wrote that printf, you should also realize that the actual inversion should be done as
invse = 1 / (float) *num;
or, alternatively, as
invse = 1.f / *num;
On top of being incorrect your original variant is illegal: you are not allowed to convert pointers to floating-point types in C, which is the reason for the error message.
P.S. From the bigger picture point of view, there's no real reason to pass that the number to invert by pointer. Passing the immediate value would make more sense
float invert(int num)
{
...
In that case you, of course, don't have to dereference anything inside invert.

Newbie Getting error: expected ')' before ',' token

I am writing a program that calculates the square of two values (I must use a function.) I am sure that there are many mistakes but, I just can't seem to pick them out:
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
int integer1, integer2, total = 0;
int squared(int integer1, int integer2);
int main(void)
{
printf("Enter two numbers to be Squared\n");
scanf("%d%d",&integer1,&integer2);
printf("Square of entered numbers = %d\n", squared(integer1,integer2));
return 0;
}
int squared(int integer1, int integer2)
{
int total;
total = integer1 + integer2;
return total *= total;
}
Header file:
#ifndef HEADER_H
#define HEADER_H
#define squared
int squared(int integer1, int integer2);
#endif
While you don't really need to specify the type int for the arguments that the function squared takes, because int is assumed whenever type is not specified, it is never bad to just put them down, as in:
int squared(int integer1, int integer2, int total);
// instead of
// int squared(integer1, integer2, total);
// both at the prototype and the definition of the function
Then again, you may just leave that out.
There are real problems in your function squared's definition. You aren't using semicolons ;, and you have written a return for the first statement, where I think you don't really want to return anything yet. You probably just wanted:
int squared(int integer1, int integer2, int total)
{
total = integer1 + integer2;
return total *= total;
}
One another important thing is, you are giving 2 less arguments to the squared function call from your main function. squared awaits for 3, you give it just 1. You probably wanted to call it as following:
...
printf("Square of entered numbers = %d\n", squared(integer1, integer2, total));
...
Lastly, you shouldn't be using variables that you haven't given a value to. integer1 hopefully will have a value assigned, integer2 also, hopefully. But total won't be assigned a value by the time you call the squared from main. You can just initialize it with a 0 or something, like this:
...
int integer1, integer2, total = 0;
...
Actually, you don't even need to have a total inside main, your squared function doesn't need a 3rd argument that holds total, as soon as you just declare an int total inside squared. But I won't get to that...
The main issue throughout is that you don't specify the type of each function parameter. You need to tell the compiler that integer1 is an int, for example. Also, see haccks' answer.
Two return statements one after the other are useless. The second return statement is unreachable. And you missed semicolons at the end of both the lines. May be this is the function you wanted:
int squared(int integer1,int integer2,int total)
{
total = integer2+integer2;
return total*total;
}
This calculates the sum of the two values and returns the square of the sum.
in addition to the function definition you need to change even the function call,
printf("Square of entered numbers = %d\n",squared(total));
to
printf("Square of entered numbers = %d\n",squared(integer1,integer2,total));
so that function call gets the value to add. read some basic c programs to know the format and syntax.
With all above answers, you seem to have missed ; in the return statement of your function.

Please help me to understand these error

#include<stdio.h>
float func (float t, float y){
return y ;
}
int main (){
float t0,y0,t,y;
printf ("the value of t: ");
scanf ("%f",&t0);
printf ("the value of y: ");
scanf ("%f",&y0);
t=t0;
y=y0;
static int n=0;
// t[0]=t0;
// y[0]=y0;
for (n=0;n<=3;n++){
y[1]=y[0];
printf ("value of y %f %f \n",t,y);
}
return 0;
}
The error is:
Building prog.obj.
D:\master\c language\ch3\prog.c(166): warning #2117: Old-style function definition for 'main'.
D:\master\c language\ch3\prog.c(182): error #2144: Type error: pointer expected.
D:\master\c language\ch3\prog.c(182): error #2144: Type error: pointer expected.
*** Error code: 1 ***
You cannot array index something that is not an array, or a pointer into an array.
Your y and t floats are not pointers into arrays in your program.
You should make them float *y, *t into pointers so you can point them into array.
Change float t0,y0,t,y; to float t0,y0,*t,*y;
and
t=&t0; //assign address of t0 to t
y=&y0;
Change printf ("value of y %f %f \n",t,y); to
printf ("value of y %f %f \n",*t,*y); //note to dereference t and y here, to get their values
Here's a example of your program I fixed to work
The 'Old-style function definition for main()' message means that you've not given a prototype definition. The correct forms are:
int main(void) { ... }
int main(int argc, char **argv) { ... }
The version int main() is fine in C++, but not strictly a prototype in C, and hence gets the 'old-style' tag.
The other messages are more inscrutable; the line numbers do not correspond to the code you show. However, as Tony The Lion notes in his answer, the line
y[1] = y[0];
is erroneous since y is not an array. There is room to think that should be:
y = y0;
and you'd need a companion:
t = t0;
in order to have defined values printed in the printf() statement.
Even with these changes, the code does not make a lot of sense. However, given that you removed 150-odd lines, we can suppose that the missing code would make more sense.
There is no need to make n into a static variable; it is better not to do so.
Please make sure, in future, that your error messages correspond to the source code you post, not to some variant version of the code you post. The line numbers should not be as large as 166 or 182; they should be single digit numbers or small double digit numbers. But even more importantly, they should match the code!

C function prototypes

I'm pretty new to C programming and I had a question as to why a sample code I was given runs the way it does. I'm learning about function prototypes. Can someone give me a run down on the order in which this compiles?
//TwoFunctions - All code split into two user-defined functions
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
//function prototypes
//Calculates and displays the total and avergae of two numbers
void CalcAvg(double tot);
double CalcTotal();
int main()
{
double totl;
totl = CalcTotal();
CalcAvg(totl);
printf("Your total is %.2f\n", totl);
return 0;
}
CalcTotal()
{
double val,
num,
total;
printf("Please enter a number: ");
scanf(" %lf", &num);
printf("Please enter another number: ");
scanf(" %lf", &val);
total = val + num;
return total;
}
void CalcAvg(double tot)
{
double avg;
avg = tot/2;
//printf("Your total is %.2f\n", tot);
printf("The average of the two numbers is %.2f\n", avg);
return;
}
If it makes any sense, for the most part I understand and can write a program like that, however I am a little unclear as to the the steps involved, the calls, and the order in which the compiler compiles the program. Can someone shed a little light on this for? Greatly appreciate it!
Suggested changes:
/*
* Calculates and displays the total and avergae of two numbers
*/
#include <stdio.h>
//function prototypes
double CalcAvg(double tot);
double CalcTotal();
int main (int argc, char *argv[])
{
double tot = CalcTotal();
printf("Your total is %.2f\n", totl);
printf("The average of the two numbers is %.2f\n", CalcAvg(totl));
return 0;
}
double
CalcTotal()
{
double val, num, total;
printf("Please enter a number: ");
scanf(" %lf", &num);
printf("Please enter another number: ");
scanf(" %lf", &val);
total = val + num;
return total;
}
double
CalcAvg(double tot)
{
return tot / 2.0;
}
Function declaration and function prototypes have their differences. A declaration simply means introducing the function (its name) to the compiler before it's usage, similar to a variable; just the function return type is specified. A prototype is where you specify every type the function is associated with i.e. argument type(s) and the return type.
Say you've an add that adds two ints and returns an int. This is a declaration
int add();
while this is a prototype
int add(int, int);
In C89 having a declaration isn't necessary at all. You may just use (call) add(3, 5) and the compiler should infer argument types from the function call and since no return type is known int is assumed. From C99 onwards declaration before usage was made mandatory. Still declaring a prototype isn't necessary. Thus the declaration would be
Note that the argument types isn't part of the declaration. C99 didn't allow the return type to be assumed implicitly as int, it has to be specified in the declaration. Still a function prototype isn't mandatory. Even today C11 doesn't expect a prototype but just a declaration.
Function prototypes were introduced in ANSI C (1989) where a function's declaration can have the argument and return types specified. Though it was never made mandatory for all functions. Only for variadic functions it's mandatory since C89 to declare a function with prototype.
As for your specific program, you declared two functions (one with prototype and one without [see Kerrek SB's comment]) and the used them in main; after main's definition, you've defined the two functions earlier declared.
your questions expects a lot of different topics to be discussed.
1)how compilation takes place? Check This Link For compilation Process
2) how are functions called ? Function Call
read these two links if still you have doubts be more specific on what you want.
Please follow the changes as suggsted by #FoggyDay.
The execution of any program is started from main. so compiler will first find out main and start execution from there.
int main()
{
double totl;
totl = CalcTotal(); /*As a function call to CalcTotal(); function is made here, the
execution is main is suspended and compiler tries to find
CalcTotal()'s definition and runs the whole function and return
value is store in "totl" variable. (Here the return value of
CalcTotal() is double as defined by function's return value.)*/
/*As soon as CalcTotal() function returns, the compiler again comes back to main()
where it suspended the execution and resumes the execution. saves return value of
CalcTotal() in "totl" and jumps to next statement.*/
CalcAvg(totl); /*Again in this statement, a function call to CalcAvg(totl) function
is made here so main()'s execution will be suspended and execution
jumps to CalcAvg(totl) function. The compiler continues to execute
this function until it returns*/
/*When CalcAvg(totl) returns, the execution of main() is again reumed from here*/
printf("Your total is %.2f\n", totl); //This line prints totl
return 0; //main returns and this defines end point of the program.
}
Generally, execution of a program starts from main and goes step by step until a function is called inside main. As soon as any function is called, execution is redirected to that function until that function returns.

Resources