#include <stdio.h>
int Add(int a, int b);
int Add(int a, int b, int c);
double Add(double a, double b);
void main()
{
printf("1+2=%d\n",Add(1,2));
printf("3+4+5=%d\n",Add(3,4,5));
printf("1.414+2.54=%f\n",Add(1.414,2.54));
}
int Add(int a, int b)
{
return a+b;
}
int Add(int a, int b, int c)
{
return a+b+c;
}
double Add(double a, double b)
{
return a+b;
}
I wrote with C language and using Xcode. While studying "overload", Xcode keeps show error message that overload cannot be worked. Instead it shows "Conflicting types for 'Add'" message.
With Xcode, would overload cannot be worked?
In Simple words, C doesn't allow function overloading! So, you can't write multiple functions with the same name!
Try to give different function name and try-
#include <stdio.h>
int Add2int(int a, int b);
int Add3int(int a, int b, int c);
double Add(double a, double b);
void main()
{
printf("1+2=%d\n",Add2int(1,2));
printf("3+4+5=%d\n",Add3int(3,4,5));
printf("1.414+2.54=%f\n",Add(1.414,2.54));
}
int Add2int(int a, int b)
{
return a+b;
}
int Add3int(int a, int b, int c)
{
return a+b+c;
}
double Add(double a, double b)
{
return a+b;
}
As mentioned C doesn't support function overloading (like in C++). Neverthless C99 introduced function-like macros with empty arguments, however commas must be preserved with exact number. Here is an example:
#include <stdio.h>
#define Add(a,b,c) (a+0)+(b+0)+(c+0)
int main(void)
{
int a = 1, b = 2, c = 3;
double x = 1.5, y = 2.25, z = 3.15;
printf("%d\n", Add(a, b, c)); /* 6 */
printf("%d\n", Add(a, b, )); /* 3 */
printf("%d\n", Add(, , c)); /* 3 */
printf("%g\n", Add(, y, z)); /* 5.4 */
printf("%g\n", Add(x, , )); /* 1.5 */
return 0;
}
Note that due to due usual arithmetic conversions for arguments with floating-point type 0 would be properly promoted to such type.
This may be only possible if you are using C++.
But in C you can't think of function overloading.
So try to change the function name and then it will work.
Actually C language does not allow function or method overloading
To do method overloading you have to go to C++ or Java
Related
This is about how I can use a function inside another function. I'm trying to get the max of 4 numbers using another function that determines the max of 2. The errors I get are
expected expression before int
and
to few arguments to function max2
I tried to search what they mean however I didn't really understand much... thank you for any help
int max2(int a, int b) {
if(a > b) {
printf("%d is the max\n", a);
}
else {
printf("%d is the max\n", b);
}
}
int max4(int a, int b, int c, int d) {
if(a > b)
{
if(a > c)
{
max2(int a, int d);
}
else
{
max2(int c, int d);
}
}
else
{
if(b > c)
{
max2(int b, int d);
}
else
{
max2(int c, int d);
}
}
}
int main() {
max4(666,853,987,42);
}
You declare functions returning int, but those functions return nothing. Probably you'd want something like this:
#include <stdio.h>
int max2 (int a, int b) { return a > b ? a : b; }
int max4 (int a, int b, int c, int d) { return max2(max2(a, b), max2(c, d)); }
int main (void) {
printf("%d is the max\n", max4(666,853,987,42));
}
Welcome to programming!
You should take a closer look here to see why you are incorrectly calling (important keyword) your function. Additionally you should look into the return keyword. (A quick look at this answer or the more in depth Microsoft article should help)
Also, as mentioned in the comments, your solution is a bit weird. Try finding the max of 20 numbers, by using for or while loops, as well as an array.
Happy learning!
I am looking to fix a CLion library project (C language) with unit tests passing. Although I'm using Googletest but I'm running into compilation problems. It says:
Can't resolve type 'ASSERT_EQ'
My code is below:
arithmetic.h
#ifndef MATH_LIBRARY_H
#define MATH_LIBRARY_H
int add(int a, int b);
int sub(int a, int b);
int mul(int a, int b);
#endif
arithmetic.c
#include "arithmetic.h"
int add(int a, int b) {
return a + b;
}
int sub(int a, int b) {
return a - b;
}
int mul(int a, int b) {
return a * b;
}
tests.c
#include <gtest/gtest.h>
#include <arithmetic.h>
#include "tests.h"
TEST(SuiteName, TestName) {
ASSERT_EQ(add(1, 1), 2); // Can't resolve type 'ASSERT_EQ'
}
I am writing a simple function in c that will return the middle of 2 float values.
My function looks like this:
float mid(float a, float b) {
return (a + b)*0.5;
}
The problem is that this function will always produce 0.000000. I am checking this by using:
printf("%f", mid(2,5))
this will print 0.000000.
However, if i just do:
printf("%f", (2+5)*0.5)
this will print 3.5, which is correct.
How do I fix this problem?
The function is fine. For example, this little snippet outputs correctly.
#include <stdio.h>
#include <string.h>
float mid(float a, float b); //declaration here
int main(void)
{
printf("%f\n", mid(2,5));
return 0;
}
float mid(float a, float b) {
return (a + b)*0.5;
}
The probable problem you have is, you didn't add the declaration of mid() like I did, and the compiler thought it returns implicit int, which causes the problem.
This should work! But I suggest you to pass explicitly double values.
#include <stdio.h>
float mid(float a, float b);
int main() {
printf("%f", mid(2.0, 5.0));
return(0);
}
float mid(float a, float b) {
return (a + b)*0.5;
}
#include <stdio.h>
int main()
{
int a = 4;
int b = 3;
addNumbers(a, b);
}
int addNumbers(int a, int b)
{
return a + b;
}
Why does this not compile, I get a message saying implicit declaration of function addNumbers()?
Either define the function before main() or provide its prototype before main().
So either do this:
#include <stdio.h>
int addNumbers(int a, int b)
{ //definition
}
int main()
{ //Code in main
addNumbers(a, b);
}
or this:
#include <stdio.h>
int addNumbers(int, int);
int main()
{ //Code in main
addNumbers(a, b);
}
int addNumbers(int a, int b)
{ // definition
}
You need to declare the function before you call it in main(). Either move it before main or at least declare it there.
Also, you should prob add return 0 at the end of the main function since it's supposed to return int.
#include <stdio.h>
int addNumbers(int a, int b)
{
return a + b;
}
int main()
{
int a = 4;
int b = 3;
addNumbers(a, b);
return 0;
}
You have to either move the entire addNumber() function above main or provide a prototype. The latter is done the following way:
int addNumbers(int a, int b);
int main()
{
// code of main() here
}
int addNumbers(int a, int b)
{
//code of addNumbers() here
}
Put addNumbers before main
int addNumbers(int a, int b)
{
return a + b;
}
int main()
{
int a = 4;
int b = 3;
addNumbers(a, b);
}
UPDATE:
To print it, printf("%i",addNumbers(a, b)); will display 7 in above case.
You can move the whole function above the point where it is called, or use a function prototype, like this:
#include <stdio.h>
int addNumbers(int a, int b); // function prototype
int main()
{
int a = 4;
int b = 3;
addNumbers(a, b);
}
int addNumbers(int a, int b)
{
return a + b;
}
You'll need a forward declaration of the addNumbers function or its definition moved up before the first usage:
// 2161304
#include <stdio.h>
// forward declaration
int addNumbers(int a, int b);
int main()
{
int a = 4;
int b = 3;
addNumbers(a, b);
}
// alternatively move this up before main ...
int addNumbers(int a, int b)
{
return a + b;
}
Regarding main and return:
Programs will compile without. The signatures of main defined by the standard are:
int main(void)
int main(int argc, char **argv)
There are three portable return values:
0, EXIT_SUCCESS, EXIT_FAILURE
which are defined in stdlib.h.
Declare the function before using it by either adding a prototype before main():
int addNumbers(int a, int b);
or move the whole addNumbers function before main().
I agree with declaration and definition thing but i am not getting any compilation errors with the above code.My gcc version is "4.4.1-4ubuntu9".Any ideas.
I have done a little modification to test the code.
#include <stdio.h>
int main()
{
int a = 4;
int b = 3;
printf("%d", addNumbers(a, b)); // Printf added.
}
int addNumbers(int a, int b)
{
return a + b;
}
You must declare the function before using.
Remember these 4 basic parts while dealing with functions.
Declaration
Call
Definition
Return
if your compiler is C99 standard it throws and error "implicit declaration", since since default promotion is obsolete in C99 standard.
if you try to compile with C89 standard this would be allowable.
In C99 standard function prototype is necessary
Since the compiler executes one line after another,by the time it sees the function call,it has to have the information about that function which the main function is calling.so my friend u need to tell the compiler about the function before you can ever use it.
How can I create a "function pointer" (and (for example) the function has parameters) in C?
http://www.newty.de/fpt/index.html
typedef int (*MathFunc)(int, int);
int Add (int a, int b) {
printf ("Add %d %d\n", a, b);
return a + b; }
int Subtract (int a, int b) {
printf ("Subtract %d %d\n", a, b);
return a - b; }
int Perform (int a, int b, MathFunc f) {
return f (a, b); }
int main() {
printf ("(10 + 2) - 6 = %d\n",
Perform (Perform(10, 2, Add), 6, Subtract));
return 0; }
typedef int (*funcptr)(int a, float b);
funcptr x = some_func;
int a = 3;
float b = 4.3;
x(a, b);
I found this site helpful when I was first diving into function pointers.
http://www.newty.de/fpt/index.html
First declare a function pointer:
typedef int (*Pfunct)(int x, int y);
Almost the same as a function prototype.
But now all you've created is a type of function pointer (with typedef).
So now you create a function pointer of that type:
Pfunct myFunction;
Pfunct myFunction2;
Now assign function addresses to those, and you can use them like they're functions:
int add(int a, int b){
return a + b;
}
int subtract(int a, int b){
return a - b;
}
. . .
myFunction = add;
myFunction2 = subtract;
. . .
int a = 4;
int b = 6;
printf("%d\n", myFunction(a, myFunction2(b, a)));
Function pointers are great fun.
You can also define functions that return pointers to functions:
int (*f(int x))(double y);
f is a function that takes a single int parameter and returns a pointer to a function that takes a double parameter and returns int.