1
#include <stdio.h>

 int main(int argc, const char * argv[])
 {

    float apples = 1.1;

    float bananas = 2.2;

    double fruit = apples + bananas;

    printf("%f.\n",fruit);

    return 0;

 }

alright.... so why exactly is %f needed in this? i understand that it is printing the decimal notation of "fruit" for me, but why can i not just ask for the program to print the number "fruit"? is it because i HAVE to declare whether i would want "fruit" printed in decimal %f or scientific notation %e? also I have come across %d on occasion. what does that entail?

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
lfkwtz
  • 1,007
  • 2
  • 11
  • 25
  • Duplicate of this -> http://stackoverflow.com/questions/2274336/c-printf-using-d-and-f – Arpit Jan 23 '13 at 14:48
  • Not a duplicate, Micahael Lefkowitz wants know what the specifiers are, not why printing a float using `%d` produces a different value. – Joe Jan 23 '13 at 14:50
  • `%d` = int, `%f` = float –  Jan 23 '13 at 14:56

7 Answers7

2

well if you take a look at the definition of the printf function, then you would come to know that these values passed as %d : int format specifier %f : float format specifier

are called as format specifiers

printf is also a simple method defined in the library and according to the paramaeter it prints the value

for example

int a = 5;
float b = 5.43 ;

now if you write

printf("%d",a);

it prints the integere value stored in variable a ==>> which is 5

printf("%f",b);

it prints the float value stored in variable b ==>> which is 5.430000 // up to six digits

and if you try with the code

printf("%f",a);
printf("%d",b);

these both lines prints awkward value called as garbage value , taking from an unknown location as you have used wrong format specifier

you can refer to the link

1: http://www.elook.org/programming/c/printf.html to get the list of format specifiers

To Alok Save :

see this is a little complex , if i am not able to explain clearly then let me know i will try to create a diff question with a better answer.

what actually happens is when you write

int a = 5 // 101 in binary

then in memory it allocates 2 bytes = 16 bits like


0000000000000101

but when you try to print it as a float by taking %f as a format specifier it asks for a 4 byte = 32 bit expression but what you get is 16 bit representaion and the most important point the float has its 16th bit reserved for '.' that is decimal so your


0000000000000101 becomes


  1. = 10 in binary // 2 in decimal value

so the value before decimal becomes 2 instead of 5 and since the float still has 16 bits which represents the value after decimal and the int variable provides only 16 bits to represent value before decimal thus the last 16 bits representing the after decimal values pick the garbage values these garbage values representing some random binary value ultimately result in some value

so your output will always be 2.something

this something part will be different for every machine as it picks the garbage value but the value before decimal will always be same following the given explanation

I hope this serves

but if it doesnt let me know

i will try to come up with a better representation

also the link will be very helpful

Community
  • 1
  • 1
  • 1
    **taking from an unknown location as you..*". Why & how is the location unknown? You have a variable, who's address is the location(address). The location is known. How the values at that location are interpreted is what the difference is about. – Alok Save Jan 23 '13 at 15:55
  • 1
    @MichaelLefkowitz : now you do ... LOL – Hussain Akhtar Wahid 'Ghouri' Nov 25 '14 at 22:19
1

The prototype for the printf function is

int printf(const char * restrict format, ...);

The first argument is always a text string that describes the format of the output (hence why you can't just write printf(fruit);). The ... indicates that printf may take zero or more additional arguments after the format string (this is known as a variadic function).

Unfortunately, variadic functions don't automatically know the number or types of arguments passed to them in the variable part of the argument list; they just see a starting address for the next available argument after the last fixed argument. You somehow have to pass that information as part of the fixed arguments. With printf, the number and types of additional arguments are indicated by conversion specifiers in the format string. So when you write

printf("%f\n", fruit);

the conversion specifier %f tells printf that there's one additional argument of type double following the format string. The conversion specifier also tells printf how the value should be formatted for display. For example, in

printf("%7.2f\n", fruit);

the conversion specifier %7.2f tells printf to display the value as a 7-character wide field with 2 digits following the decimal point, or 9999.99.

If the type of the argument doesn't match the conversion specifier, the behavior is undefined. For example, if you write

printf("%d\n", fruit);

you're telling printf to expect an int when you're really passing it a double, which is a logic error. Depending on the underlying architecture any number of different things may happen, so the behavior is left undefined and the compiler doesn't have to do anything in particular about it; any result is considered "correct". Really nice compilers will issue a diagnostic that the argument doesn't match the conversion specifier. Most will simply translate the code as-is, and the result can be anything from unexpected output to an access violation.

Similarly, if you don't pass enough arguments to match the number of conversion specifiers, the behavior is undefined:

printf("%f %d\n", fruit);

If you pass more arguments than conversion specifiers, the additional arguments are evaluated, but otherwise ignored:

printf("%f\n", fruit, apples, bananas);
John Bode
  • 119,563
  • 19
  • 122
  • 198
  • +1 For taking out time to formulate a correct and explanatory answer. It makes my answer redundant But this answer explains much more in detail so I gladly will delete mine.This should be the accepted answer. – Alok Save Jan 23 '13 at 17:00
0

%d is a format specifier used to print integer numbers, %f prints double numbers. You need to specify the type of what you are printing using a format specifier because of the variable number of arguments mechanism used in C, so that the compiler knows, what type is the next argument.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0

The "%f" format specifier is a special string format to indicate to the printf that you are going to print double. (For more details)

The "%d" format specifier is a special string format to indicate to the printf that you are going to print decimal or integer.

Community
  • 1
  • 1
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
0

printf cannot guess what type is your variable. Therefore you give him a hint: the format. Then the function can interpret your argument as a given type.

md5
  • 23,373
  • 3
  • 44
  • 93
0

printf stands for print formatted, which means you supply two kinds of data to it:

  • how to present the data (the format string)
  • what data to present (the rest).

The format string syntax of printf is more or less uniform between languages that support this function. Wikipedia has a nice description: Format placeholders.

Kos
  • 70,399
  • 25
  • 169
  • 233
0

If you do not want a number such as 4.11 to become 4.110000 you can use %g instead of %f. %g uses either %f or %e (smart determine which way compiler finds better in this situation), it works fine for cutting zeros at the end of the number.

Scanf also isn't type safe and you need to tell compiler what type of data you want to be used. There is also the %d and %f with the difference %f refers only to float and not to double. When using scanf the %lf refers to double.

Dydzio
  • 84
  • 1
  • 8