1. ## Median of 3

I'm a newbie in C language so I'm still trying to learn the basic concepts. I have a question about a program regarding the median value of 3 numbers. First I wrote the following piece of code:
Code:
```#include <stdio.h>
double min(double x, double y){
return x<y ? x : y;
}
double med_aux(double x, double y, double z){
return z<x ? x : min(z,y);
}
double med(double x, double y, double z){
return x<y ? med_aux(x, y, z) : med_aux(y, x, z);
}
int main(){
double n1, n2, n3;
n1=5;
n2=22;
n3=1;
printf("The median of the values is %lf");
printf("%lf", med(n1, n2, n3));
return 0;
}```
And it seemed fine to me, but I was getting "The median of the values is 0.0000005.000000" as output, so I changed the printf a bit:
Code:
```#include <stdio.h>
double min(double x, double y){
return x<y ? x : y;
}
double med_aux(double x, double y, double z){
return z<x ? x : min(z,y);
}
double med(double x, double y, double z){
return x<y ? med_aux(x, y, z) : med_aux(y, x, z);
}
int main(){
double n1, n2, n3;
n1=5;
n2=22;
n3=1;
printf("The median of the values is %lf", med(n1, n2, n3));
return 0;
}```
And I got the desired output, which is "The median of the values is 5.000000". But still, I would like to know why it didn't work the first time, since it seemed correct. Also, if you could better explain the %d(decimal integer) and %f(floating point), when to use which, I would greatly appreciate it. I know these questions may seem trivial but I really want to understand properly. Thank you!! 2. You've been thrown into an exercise in writing functions before you understand printf. You need to be able to use printf() to get the results from your other exercises.

printf() is a function in C. However it isn't an ordinary function, it's a variable arguments function. As a beginner, it's probably best to think of it as an output statement rather than a function.

It takes a format string as its first argument. This tells it the types of subsequent arguments and, when you get more advanced, things like the number of decimal places to output. For now, you don't need to worry about that. What you need to know is that '%' is the escape character. "%" means 'treat the next character as a placeholder for an argument rather than a character to output. What you need to know at first is that %c means "output a character", "%d" means "output an integer" and "%f" means "output a floating point value", actually a double (if you pass a float it gets promoted to a double". %s means "output a string" and matches a char *.

With these simple formatting commands, you can get data out of the program and onto the screen. You need to experiment a bit just printing out a few values.

You must supply an argument to match the formatting character. If you don't do this, printf misinterprets the argument you supply ot grabs garbage if you don't supply anything, and you get strange results. 3. If you turn the compiler warnings on, the errors will be pointed out at compiler time.

For 'gcc' I use "-Wall -pedantic" as my default compile options - enable all warnings, and be pedantic (complain about even the smallest, trivial things). 4. Originally Posted by hamster_nz For 'gcc' I use "-Wall -pedantic" as my default compile options - enable all warnings, and be pedantic (complain about even the smallest, trivial things).
High five, fellow pedant!  5. Originally Posted by laserlight High five, fellow pedant! Sorry to hijack this thread, but is `-pedantic` just useful if you're worried about porting? Are there any benefits if the code it staying put? 6. Originally Posted by Yonut Sorry to hijack this thread, but is `-pedantic` just useful if you're worried about porting? Are there any benefits if the code it staying put?
It's mainly for porting. It means "reject anything that isn't strictly ANSI-compliant". However in most beginners' exercises, you won't be doing anything that is platform-specific. So -pedantic can act as a warning that you are going wrong. Popular pages Recent additions double, median, numbers, piece, wrote 