redefinition of ‘main’ - c

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.

Related

Compiler shows "error lvalue required as left operand of assignment". What does it mean and how to fix it?

I'm new to programming. So, details are appreciated.
#include<stdio.h>
#include<math.h>
int main()
{
float x, f(x);
printf("Enter x=");
scanf("%f", &x);
f(x)= 3*pow(x, 5)- 5*sqrt(x)-6*sin(x); /*in this line compiler shows error*/
printf("f(x)= %f", f(x));
return 0;
}
Excuse me for assuming that you are a C beginner looking for the way to write a function in C. There are many C tutorials out there, I recommend finding one for further learning.
Here is an example of using an actual C function for what you are doing.
#include<stdio.h>
#include<math.h>
/* first define the function */
float f(float x) /* function head */
{ /* start of function body */
return 3*pow(x, 5) - 5*sqrt(x)-6*sin(x);
} /* end of function body and definition */
int main(void)
{
float x; /* definition of function has already happened, so no f(x) here */
printf("Enter x=");
scanf("%f", &x);
printf("f(x)= %f", f(x) /* call the function */);
/* Note that some coding styles do not like calling a function
from within a more complex statement. Using a variable to
take the result of the function is preferred.
I chose this way to stay more similar to your own code.
*/
return 0;
}
The following could work.
#include <stdio.h>
#include <math.h>
int main()
{
float x, f;
printf("Enter x=");
scanf("%f", &x);
f = 3 * pow(x, 5) - 5 * sqrt(x) - 6 * sin(x);
printf("f(x)= %f", f);
return 0;
}
In your code, f(x) is a valid identifier, but not a variable. It's a very poorly written (and now invalid, as per the latest C standard) function prototype. You cannot assign to it, it's not a modifiable lvalue.
That is why in case of
f(x)= 3*pow(x, 5)- 5*sqrt(x)-6*sin(x);
compiler screams.
On why your code did not throw an error for the invalid format, it's the legacy support in compiler. In your case
float x, f(x);
is treated the same as
float x, float f ( int x ) ; //missing data type is treated as 'int',
// DON'T rely on this "feature"

C program involving call to functions(beginner) - I don't understand

currently I have a question at hand that I don't understand due to a few things. Here is the question:
Que. 1. Given the following program, show the values of the variables a, b, c, x, y, z in the main function after each function call to FindSum. Also, show the values of a, b, c in FindSum immediately after executing each function call to FindSum.
And here is the code:
#include <stdio.h>
/* function prototype declaration for FindSum */
void FindSum(int, int, int *);
int main(void)
{
int a=2, b=5, c=1, x=3, y=4, z=7;
FindSum (a, b, &c); /* a first call to FindSum */
printf(“first call in main %d %d %d %d %d %d \n”, a, b, c, x, y, z);
FindSum (x, y, &z); /* a second call to FindSum */
printf(“second call in main %d %d %d %d %d %d \n”, a, b, c, x, y, z);
return 0;
}
/* definition of FindSum */
void FindSum (int a, int b, int *c)
{
a += (b * 2);
b += (b * 2);
*c += (b * 2);
printf(“in FindSum: %d %d %d \n”, a, b, *c);
My questions:
1) Which one is the main function?
2) Is the problem asking me to finish up the code so that it produces the desired result?
3) Also, show the values of a, b, c in FindSum immediately after executing each function call to FindSum" Where is the function call to findsum
I'm currently in a C programming class where the professor doesn't explain well/english is subpar. Due to this, I am struggling slightly on these issues. Was hoping someone could shed me light
1) Which one is the main function?
The one named main.
2) Is the problem asking me to finish up the code so that it produces the desired result?
No, it's asking you to act like the computer and "run the program" on paper and say what it would output.
3) Also, "show the values of a, b, c in FindSum immediately after executing each function call to FindSum" Where is the function call to findsum
They're marked with comments in the code. Search for "call to FindSum".
It's important to understand the basic structure of a program. Including how functions work and the difference between pass by value and pass by reference. Understanding this is key towards answering these questions. The "int main(void)" line is the main function declaration. The left and right brackets define it's scope. Note the values of the variables a,b,c,x,y,&z at the beginning. Write them on a piece of paper. The FindSum function takes in two parameters and outputs one parameter. Perform the calculation and write down your results. This answers the first part of the question. Then step through line of code and write down the results after each call to FindSum. This is not asking for you to modify the code, but simply execute what the computer would compute.

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.

Using pow() in C

I have this problem with this homework I'm supposed to do.
[ It says Create a program that's able to calculate and show the sum of S]
Like S=1+1/4+1/8+1/16 ... till 1/ [2 pow n]
So I worked on it and came up with this code
#include <stdio.h>
void main()
{
int n,i;
float p,s;
printf("Enter the maximum power n :");
scanf("%d",&n);
s=0;
p=0;
for (i=0;i<n;i++)
{
p+=1/pow(2, i);
s+=p;
printf("s = %f\n",s);
}
printf("The sum of this equation is :%f",&s);
}
But when I execute it is always like S=0.
What am I doing wrong?
You are printing an address ('&s) with%f` variable. Using a wrong specifier invokes undefined behavior. You may get anything.
Also, No need of variable s. Remove the line
s+=p;
It should be like:
#include <stdio.h>
int main(void)
{
int n,i;
float p;
printf("Enter the maximum power n :");
scanf("%d",&n);
p=0;
for (i=0;i<n;i++)
{
p+=1/pow(2, i);
printf("p = %f\n",p);
}
printf("The sum of this equation is :%f",p);
}
You need to include <math.h> to get the proper prototype of pow().
You might need to link to the math library too gcc main.c -Wall -lm
#include <math.h>
....
for (i=0;i<n;i++)
{
p=1/pow(2, i);
s+=p;
printf("s = %f\n",s);
}
printf("The sum of this equation is :%f",s);
Your program has multiple problems. Enabling compiler warnings should tell you about some of them.
You should include the C header which contains the declaration of the pow function.
You add each addend twice.
In your second printf statement, you pass a float. But the %f format specifier expects a double argument. In your third printf statement, you pass a pointer to a float.
Another cosmetic problem is that your main function should return an int.
just a guess, may you replace the line
p+=1/pow(2, i);
with
p+=1.0f/(float)pow(2, i);
and
printf("The sum of this equation is :%f",&s);
with
printf("The sum of this equation is :%f",s);
Typo may be.. but you will have say %f and s (not &s)
printf("The sum of this equation is :%f",s);
On side note:
Once you include <math.h> you will get compiler warning for using correct pow(..) prototype. Below code would be relevant.
p+=1.0f/(float)pow(2.0f, i);

Resources