What is the use of the operator% in printf of variables in C language?

14

I am starting a course on YouTube from C.

And they show that to print the values of the variables it is necessary to use this operator % .

example:

int suma, a, b;
a=2;
b=3;
suma=a+b;
printf("El valor de la suma es %i", suma);

In case of integer %i , float %f and char %c

I would like to know what other uses this operator has and what attributes the value depends on, since I saw that I could delimit the decimals shown using %.1f or %.2f

    
asked by Victor Alvarado 31.03.2017 в 21:03
source

2 answers

22

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

answered by 31.03.2017 / 21:09
source
2

It is also used for the Module operator, which is not more than the rest of a division. For example, here is a small piece of code in C that says if a number is even or odd:

int numero = 4;

if(numero % 2 == 0)
    printf("El número es par");

else
    printf("El número no es par");   

This should show the screen "The number is even", because if you divide an even number between 2, the rest will always be 0. I hope it has helped you.

    
answered by 31.03.2017 в 21:09