81

I was confused with usage of %c and %s in the following C program:

#include <stdio.h>
    
void main()
{
    char name[] = "siva";
    printf("%s\n", name);
    printf("%c\n", *name);
}

Output:

siva
s

Why we need to use pointer to display a character %c, and pointer is not needed for a string

I am getting error when I run

printf("%c\n", name);

I got this error:

str.c: In function ‘main’:
str.c:9:2: warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘char *’

7 Answers 7

130

If you try this:

#include<stdio.h>

void main()
{
 char name[]="siva";
 printf("name = %p\n", name);
 printf("&name[0] = %p\n", &name[0]);
 printf("name printed as %%s is %s\n",name);
 printf("*name = %c\n",*name);
 printf("name[0] = %c\n", name[0]);
}

Output is:

name = 0xbff5391b  
&name[0] = 0xbff5391b
name printed as %s is siva
*name = s
name[0] = s

So 'name' is actually a pointer to the array of characters in memory. If you try reading the first four bytes at 0xbff5391b, you will see 's', 'i', 'v' and 'a'

Location     Data
=========   ======

0xbff5391b    0x73  's'  ---> name[0]
0xbff5391c    0x69  'i'  ---> name[1]
0xbff5391d    0x76  'v'  ---> name[2]
0xbff5391e    0x61  'a'  ---> name[3]
0xbff5391f    0x00  '\0' ---> This is the NULL termination of the string

To print a character you need to pass the value of the character to printf. The value can be referenced as name[0] or *name (since for an array name = &name[0]).

To print a string you need to pass a pointer to the string to printf (in this case name or &name[0]).

Sign up to request clarification or add additional context in comments.

1 Comment

This is a nice explanation.
25
%c

is designed for a single character a char, so it print only one element.Passing the char array as a pointer you are passing the address of the first element of the array(that is a single char) and then will be printed :

s

printf("%c\n",*name++);

will print

i

and so on ...

Pointer is not needed for the %s because it can work directly with String of characters.

Comments

9

You're confusing the dereference operator * with pointer type annotation *. Basically, in C * means different things in different places:

  • In a type, * means a pointer. int is an integer type, int* is a pointer to integer type
  • As a prefix operator, * means 'dereference'. name is a pointer, *name is the result of dereferencing it (i.e. getting the value that the pointer points to)
  • Of course, as an infix operator, * means 'multiply'.

1 Comment

* in reference to a pointer, always means the same thing. you are just overthinking it (a sign of intelligence) . look at it like this, int *myintptr; means that the address the pointer myintptr is referencing will store an int, and the *myintptr = 5; means that the address the pointer myintptr is referencing should contain a 5. the * always means "the value at".
5

The name of an array is the address of its first element, so name is a pointer to memory containing the string "siva".

Also you don't need a pointer to display a character; you are just electing to use it directly from the array in this case. You could do this instead:

char c = *name;
printf("%c\n", c);

1 Comment

More to the point *name is derefencing the pointer (ie returning the thing that the pointer points to)
2

If you want to display a single character then you can also use name[0] instead of using pointer.

It will serve your purpose but if you want to display full string using %c, you can try this:

#include<stdio.h>
void main()
{ 
    char name[]="siva";
    int i;
    for(i=0;i<4;i++)
    {
        printf("%c",*(name+i));
    }
} 

Comments

0

The thing is that the printf function needs a pointer as parameter. However a char is a variable that you have directly acces. A string is a pointer on the first char of the string, so you don't have to add the * because * is the identifier for the pointer of a variable.

1 Comment

Welcome to StackOverflow! This question is several years old and has a lot of well-explained, detailed answers. At the moment, your answer doesn't really add anything more than what these other answers do. If you are able to provide more detail and add some new aspect to the issue, please edit your post and include it. If you can't, don't worry! There are plenty of other questions on this site; go ahead and delete this answer and try solving something more recent. Happy answering!
0

Or you can use %c like in the below code:

#include <stdio.h>

void main()
{
    char name[]="siva";
    //prints as string form
    printf("%s\n",name);
    
    //print each letter on different line
    int size= sizeof(name);
    int i;
    for(i=0;i<size;i++){
        printf("%c\n",name[i]);
    }
    
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.