What is the difference between % and % in C?

Asked

Viewed 1,473 times

4

What’s the difference between using the % and the %% in the C language?

  • If any of the answers below helped you, is the case of mark an answer as accepted. Here we do not write "solved" in the question. If you have an answer that really helped you, mark it as accepted. If you came to the solution on your own, put in the solution as an answer. So content is more organized and easier to find in the future by other people with similar problems.

2 answers

4

The symbol % is used in data input and output functions such as printf() and scanf(). When using this symbol indicates that what comes next is something special that has a meaning of how the data should be formatted.

For example %d indicates that the data must be an integer value, or %s must be a string. Without the symbol it would be just a letter to be used normally without a special meaning. It is more or less like the \n which means it’s to skip a line, without the backslash it would just be a letter n.

Just as \\ means he just wants a backslash, %% means he just wants a percentage symbol. Because think about it, if this symbol indicates that it is special, when you want it to stop being special you have to have a different way of using it, then the first symbol says that there is something special to follow and what comes after it is another symbol equal, then especially, it treats that symbol as a normal character to be used in the text used.

#include <stdio.h>
 
int main(void) {
    printf("Strings:\n");
    const char* s = "Hello";
    printf("\t.%10s.\n\t.%-10s.\n\t.%*s.\n", s, s, 10, s);
    printf("Characters:\t%c %%\n", 65); //olha o duplo aqui, veja o resultado
    printf("Integers\n");
    printf("Decimal:\t%i %d %.6i %i %.0i %+i %i\n", 1, 2, 3, 0, 0, 4, -4);
    printf("Hexadecimal:\t%x %x %X %#x\n", 5, 10, 10, 6);
    printf("Octal:\t%o %#o %#o\n", 10, 10, 4);
    printf("Floating point\n");
    printf("Rounding:\t%f %.0f %.32f\n", 1.5, 1.5, 1.3);
    printf("Padding:\t%05.2f %.2f %5.2f\n", 1.5, 1.5, 1.5);
    printf("Scientific:\t%E %e\n", 1.5, 1.5);
    printf("Hexadecimal:\t%a %A\n", 1.5, 1.5);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Documentation.

  • There is something like the arroba before the strings in C# in C?

  • 1

    Not in standard C.

4

If you’re talking about printf or scanf. Here’s a table to help you better:

inserir a descrição da imagem aqui

printf ("Teste %% %%") // "Teste % %"
printf ("%f",40.345) // "40.345"
printf ("Um caractere %c e um inteiro %d",'D',120) // "Um caractere D e um inteiro
120"
printf ("%s e um exemplo","Este") // "Este e um exemplo"
printf ("%s%d%%","Juros de ",10) // "Juros de 10%" 

At another point % also picks up the rest of a division:

The % operator returns the rest of the division of a number by another.

5 % 2 = 1
4 % 2 = 0
  • Thank you all very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.