624 views
2 2 votes
#include <stdio.h>

int main(void) {
    char *a="lola";
    printf("%s\t",a);
    printf("%c",*a);
    return 0;
}

Output: lola l

Here can anyone explain why does it print a string without dereferencing and to print a character it needs dereferencing?

1 Answer

1 1 vote

The %c format specifier is used whenever we want to specific that the variable that we are going to print or scan is of type char thats why we need to specify the address and it will pick the character at that location. The variable need not strictly be of character type. For example if printf("%c",65) is given then 65 is considered not as an integer but as an ASCII code of some character ( which is 'A') so you get to see A on the output console.

On the other hand the %s format specifier is used to specify to the printf or scanf functions that contents of the address that is specified as the next parameter are to considered as string, i.e, a homogenous collection of characters that is terminated by the null character '/0'. So the printf function prints the contents starting from the address specified in the next field till a null character is found and the scanf function stores the string that was received from the console input character by character starting from the address specified in next part and it automatically appended a null character at the end of the string.

Position:
Show:

No related questions found