1
#include <stdio.h>
#include <string.h>
void main()
{
char array[]="hello";
printf("%s",array[0]);  
printf("%c",array[0]);  
}

couldn't able to access array[0] when %s is used but able to access array[0] when %c is used, help me to find a solution for this.

user11
  • 197
  • 2
  • 8

2 Answers2

0

you should use address while using with %s ==> &array[0]

because %s requires a pointer as argument.

usually we use

printf("%s",character_array_name);

here character_array_name is the address of first element

character_array_name == &character_array_name[0];

and if you want to print only one character , you need to use

printf("%.1s",character_array_name);  

Example Code:

#include<stdio.h>
int main()
{
char *str="Hello World";
printf("%s\n",str); //this prints entire string and  will not modify the  pointer location  but prints till the occurence of Null.

printf("%.1s\n",str); //only one character  will be printed
printf("%.2s\n",str); //only two characters will be printed
//initially str points to H in the "Hello World" string, now if you increment str location
str++;  //try this str=str+3;
printf("%.1s\n",str); //only one character
printf("%.2s\n",str); //only two characters will be printed

str=str+5; //prints from the W
printf("%s\n",str); //this prints upto Hello because scanf treats space or null as end of strings
printf("%.1s\n",str); //only one character
printf("%.2s\n",str); //only two characters will be printed

return 0;
}
Gangadhar
  • 10,248
  • 3
  • 31
  • 50
  • thanx @gangadhar then one more question can u explain with some eg. prgm to show how the %s holds the address for the correspondng string – user11 Sep 10 '13 at 10:45
  • @Dineshdk %s did not hold the address. this is a format specifier in standard library functions , the address is holds with the argument which is passed when %s used in the functions like printf , scanf etc. See the added example. – Gangadhar Sep 10 '13 at 11:12
0

Although you have accepted an answer , I thought my answer could help you in a certain way.

String literals are a sequence of characters and you can visualize your array like this :

         +---+---+---+---+---+----+
  array: | h | e | l | l | o | \0 | 
         +---+---+---+---+---+---+-
           ^
           | 
          array[0]

printf is a variadic function and it doesn't know anything about its arguments until you specify them , so when it sees %c format specifier it assumes that the next argument will be a variable storing a character ,in this case its array[0] that is the character h is stored at the index 0 of the array .

Now, when printf sees a %s it assumes that the next argument will be a pointer pointing the string literal ("hello") that you want it to print , in this case array[0] is not a pointer, you should put array instead in the printf , please note that array names are not pointers but array name decays to pointer

Besides , you should use int main(void) in place of void main its standard

Community
  • 1
  • 1
0decimal0
  • 3,884
  • 2
  • 24
  • 39
  • somewhat yur ans help me out to explore diff in carrying out ptr operations using %s but culdn't get wi shuld i use int main(void)? – user11 Sep 11 '13 at 06:13
  • @Dineshdk You should use it because its in C standard :- http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf .... but please make sure you have the latest one ... C11 standard has already come. Check out this post on SO :-http://stackoverflow.com/questions/12225171/difference-between-int-main-and-int-mainvoid – 0decimal0 Sep 11 '13 at 09:48
  • @Dineshdk To elaborate myself --- the return type of `main` is always `int` not `void` , void in the argument's place denotes that `main` won't take any arguments, **You are beginning programming and you will be making software one day so its important to make sure that your program is portable and that is possible only if you stick to the standards** , regards :) – 0decimal0 Sep 11 '13 at 09:52