main.c
#include <stdio.h>
#include "challenge.c"
static float a = 8.4;
int main() {
printf(“%f\n”, foo(a) + bar(a));
return 0;
}
challenge.c
#include "challenge2.c"
static float a;
float foo(float F) {
a = 3.6;
return bar(F) * a
}
challenge2.c
static float a;
float bar(float F) {
a = 3.8;
return F * a;
}
Given the source code above and that we use "gcc main.c -o challenge" to compile it, what is the output when we type ./challenge?
This code will not be compiled, because of multiple definition of foo() and bar();
This problem can be solved using folowing syntax:
#include <stdio.h>
static float a = 8.4;
float bar(float F) {
a = 3.8;
return F * a;
}
float foo(float F) {
a = 3.6;
return bar(F) * a;
}
int main() {
printf("%f\n", foo(a) + bar(a));
return 0;
}
(in this case you don't need files "challenge.c" and "challenge2.c")
But to run this code must be corrected 2 mistakes:
1) in main() -> in the row with printf() must be edited double quotes to another format( I guess code was written with the use of mobile phone);
2) in row " return bar(F) * a" in function "foo()" must be changed to " return bar(F) * a;" (means adding ";" in the end of this row);
After actions listed above you will get output: 135.735992
Good luck!)
Comments
Leave a comment