how to combine the function in the one output in Xcode - c

#include <stdio.h>
#define SPACE ' '
void branching_if_judgement(int a, int b){
if (a > b){
printf("a(%d) is larger than b(%d)\n", a, b);
}else {[![enter image description here][1]][1]
printf("a(%d) is smaller or equal to b(%d)\n", a, b);
}
}
char branching_if_judgement_char(int a, int b){
char res;
if (a > b){
printf("a(%d) is larger than b(%d)\n", a, b);
res = 'Y';
}else {
printf("a(%d) is smaller or equal to b(%d)\n", a, b);
res = 'N';
}
return res;
}
int main() {
branching_if_judgement_char(2,3);
}
I follow the example to run the code. And, there are missing the main function in the slide. I add it
So, my question is how to combine all function in the one output, just like the slide.
:

The function signature tells us that
char branching_if_judgement_char(int a, int b)
it returns a thing of type char, and it takes two things of type int.
So when you call it, you're passing the two things to it, but not taking the thing it's returning.
branching_if_judgement_char(2,3);
Correct code should be
char p = branching_if_judgement_char(2,3);
printf("%c\n", p);
Please find some time to read
https://www.tutorialspoint.com/cprogramming/c_functions.htm
https://www.tutorialspoint.com/cprogramming/c_data_types.htm

Related

Max of 4 numbers using another function (max of 2)

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!

how to fix "Function definition is not allowed here" in Xcode

This is my lecture example. And, I follow it.
How to fix the error?
#include <stdio.h>
#define SPACE ' '
int main(){
void branching_if_judgement(int a, int b)
{
if (a > b)
{
printf("a(%d) is larger than b(%d)\n", a, b);
}else
{
printf("a(%d) is smaller or equal to b(%d)\n", a, b);
}
}
}
A function definition/declaration is not allowed in another function.
main is a function and branching_if_judgement is another function.
correct (read compile-able) code:
#include <stdio.h>
#define SPACE ' '
static void branching_if_judgement(const int a, const int b){
if (a > b)
{
printf("a(%d) is larger than b(%d)\n", a, b);
} else
{
printf("a(%d) is smaller or equal to b(%d)\n", a, b);
}
}
int main(){
branching_if_judgement(2, 3);
}

The code does not execute properly. Try to figure out why

int multiply(int a, char *b)
{
return a b;
}
The code does not execute properly. Try to figure out why.
c program language
Thank your kindhearted help !
the question has been solved simply by me, thank everyone!
int multiply(int a, int b)
{
return a*b;
}
There's more than one error in there.
First, you can't return two variables in the same function, you should return a or the content of the pointer that the variable b in pointing to.
So, you could either use:
return a;
to return the variable a.
or you could use
return *b;
to return the content of the adress that b is pointing to.
If you want to multiply, as the name of the function, you should use:
return a*(*b)
Your function need to return an integer
int multiply(int a, char *b)
but you try to return an int (a) and a char* (b)
if you want to return b (and only b) use
char *multiply(int a, char *b)
You must create a variable that the result will be the multiplication of the parameters and then return the value like the created variable, as I showed.
int multiply (int a, int b) {
int result;
result = a * b;
return(result);
}

C language overload

#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

Function Pointer in C

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.

Resources