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? Programming in C + – Kiran Karwa 624 views answer comment Share Follow Print See all 2 Comments 2 2 Comments reply MiNiPanda commented Jan 21, 2018 i edited by MiNiPanda Jan 21, 2018 reply Follow flag I am not sure whether this answers your doubt or not.. When your declaration is like char *a="Lola", this means that the string Lola gets stored in the code segment of memory. The 'a' stores the base address of the string i.e. the address of 'L' in Lola. When you use %s specifier, it needs the address of the character starting from which you want to print the rest of the string. When you use %c, it needs to know the value inside the array i.e. the character of the string which you want to print. Tell me if I am wrong.. 1 1 replyShare hs_yadav commented Jan 21, 2018 reply Follow flag %s ...need only starting address and %c need character..... 1 1 replyShare Please log in or register to add a comment.
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. Kamal Pratap answered Jan 21, 2018 Kamal Pratap comment Share Follow 0 reply Please log in or register to add a comment.