In the context that you define the %
is not an operator but a format specifier .
In C the format specifiers are the ones that tell the variadicas functions the type of argument with which they will work.
In the simplest case:
printf("%s\n", "hola mundo");
Tells the compiler to optimize the call to the function printf
for an argument of type char *
.
The most common format specifiers can be:
- %d o %i: Especifican un entero con signo.
- %u : Especifican un entero sin signo.
- %p : Direccion de un puntero u dato.
- %lld : Especifica un entero largo (long long). [*]
- %llu : Entero largo sin signo (unsigned long long) [*]
- %s : Especifica que el parametro es un puntero a un arreglo de caracteres.
- %c : Un caracter.
- %x : Especifica un valor hexadecimal.
- %% : Muestra un literal de porcentaje.
- %f : Imprime un float o double.
*: These specifiers may be out of standard.
Out of this context, it is a module operator and serves to obtain the remainder of a division.
EDIT:
Additionally, as I mentioned in the beginning, it is a format specifier, it can also be used to fill spaces with zeros or as you wish:
printf("%X es lo mismo que %02X", 0x0a, 0x0a);
This will result in:
A es lo mismo que 0A
Or also:
printf("%.03f es un float!\n", 0.250554);
The latter limits to printing the first 3 places after the decimal point in float
.
EDIT 2:
I felt that some information was missing in this answer, which is now present in this edition and is that the format specifiers have some magic inside.
Let's organize this by types:
-
char *
or "C-String" : Your specifier is %s
, but what happens if we do: %.2s
?
Suppose we have the following string: "Hola Mundo"
and we do:
printf("%.2s\n", "Hola Mundo"); // Resultado: Ho
The answer is: It is limited to printing only 3 characters of the current parameter; similarly, it can be used to space the chains:
printf("'%*s'\n", 20, "Hola Mundo"); // Resultado: ' Hola Mundo'
What if we mix them?
printf("'%*.2s'\n", 20, "Hola Mundo"); // Resultado: ' Ho'
As you can see, all the format of a chain is possible in C.
The *
character within a format specifier must include an additional parameter in the call to the function to specify its value, as in the examples mentioned above.
Here below a table with the possible types of format that can be given to a parameter, see last reference (Wikipedia):
Especificador Lo que hace:
--------------------------------------------------------------------------------------------------
%07i justificado a la derecha, 7 dígitos de largo, sin relleno
%.7i largo mínimo de 7 dígitos, justificado a la derecha, rellena con ceros
%8.2f tamaño total de 8 dígitos, con dos decimales
%.*f”',x,d) tamaño predeterminado,x numeros decimales
%*.*f”,x,y,d) tamaño igual a x, y numeros decimales
%s cadena terminada en null
%5s primeros cinco caracteres o delimitador
%.5s primeros cinco caracteres, sin tener en cuenta el delimitador
%20.5s primeros cinco caracteres, justificados a la derecha, con 20 caracteres de largo
%-20.5s primeros cinco caracteres, justificados a la izquierda, con 20 caracteres de largo
The format is practically the same for all types, but you still have to be careful.
Some references (in English):