T
T
Timur It doesn't matter2021-08-06 13:39:52
C++ / C#
Timur It doesn't matter, 2021-08-06 13:39:52

What are specifiers for in C?

printf(" %f \n", 1.0 / 10.0). Why is this %f specifier needed in this function? What is the difference between specifiers?
%ld %f %0.f Why are these specifiers needed?

Please explain like a teapot, because I am one.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
alexalexes, 2021-08-06
@alexalexes

To give the printf function additional information in what form you want to print the result of the second and subsequent arguments to this function. Specifiers have a certain relation to such a concept as a data type. In the derivation of the real number type, sign precision can be adjusted, so there is not only %f, but also %0.f, etc.
https://ru.wikipedia.org/wiki/%D0%A1%D0%B8%D1%81%D...

C
CityCat4, 2021-08-06
@CityCat4

Describes the format of the output field, i.e. how printf() should print the argument. The specifiers can be quite spread out - there's border alignment (left-right), placeholder characters and output precision...
man printf(), you can't list everything here. Yes, and not so often they are used, but nevertheless, you need to know about their presence.

M
Mercury13, 2021-08-06
@Mercury13

This is primarily due to the mechanism for passing variable arguments to the C function.
They just fall on the stack one after another - and you need to somehow show how many of these arguments and what types. And secondly, the output format: precision, number system, capital letters, width, and so on.
It is for this reason that the printf specifiers specify the types of the arguments in the first place - the printf function can't do anything if we've mistyped the type. Because %d (%ld, %lld) is primarily a way for her to understand how many bytes to take from the stack and how to interpret them.
C++ uses function overloading for these purposes, Delphi uses tagged data, Java uses object polymorphism. So they know what type of data they are dealing with - and they don't need to distinguish between signed/unsigned int/long/long long. For them, %d/%x/%X means different versions of the integer, %e/f/g means different versions of the fractional, and. etc. If the user writes %d for a fractional, they will either throw an error or interpret it somehow.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question