Answer to Question #182286 in C for anonymous456

Question #182286




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?



1
Expert's answer
2021-04-17T08:46:38-0400

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!)


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS