0

I was just printing some characters in C. I have declared char c and assigned its characters. then using a for loop i try to print the characters one by one. I have used pointers, of course.

#include <stdio.h>
#include <string.h>

int main()
{ 
  char c[4] = {"hia"};
  int i; 
  for(i=0;i<4;i++)
  { 
    printf(&c[i]);
  }
  return 0; 

}

However when I compile my code using turbo, i get output "hiaiaa" instead of "hia"! What am i doing wrong here?

8 Answers 8

3

Your printf() call is broken. You are using the string (from the point you specify) as the formatting string. This will not print single characters. Instead each call will print from where its formatting string starts, to end of the string.

This means the first call will print all of c, the next will print from c[1] and onwards, and so on. Not at all what you wanted.

If you want to print single characters, use the %c format specifier:

printf("%c", c[i]);

No pointer necessary, since the character is passed by value to printf().

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

2 Comments

@Bathsheba I like to think that's how I roll. :) At least most of the time.
i know my question was kinda silly, but pointer is something i have always had difficulty with, its one of the things of C i dont really have a firm grasp on
1

This is what happened in your loop:

0. hia
1. ia
2. a
3. \0

However, you want to print exactly one char at a time, not a null terminated string, so you should pass it as char not a char*:

printf( "%c", c[i] )

Also, you are looping four times, but string length is just three. You should use:

for( i = 0; i < strlen( c ); i++ )
   ...

Comments

1

The printf function have an char* as first argument, that's correct. However, it prints a string (that is, a zero-terminated sequence of char) so it will always do that.

If you want to print one character at a time, then you have to use that format, like in:

printf("%c\n", c[i]);

You also have another problem, and that is that you try to print the zero terminator as well. This character is not printable so will not show. Use e.g. i < strlen(c) as the loop condition to overcome this.

Also, instead of printing character-by-character, print it all as one string:

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

Comments

1

1) For loop size should i<3 , not i<4 (i=3 refers to the null character at the end of the string)

2) use printf("%c",c[i]);

Comments

0

Explanation of what you're seeing: In each loop, printf is printing a null-terminated string. This string starts in every loop one char later inside your array. How it should be done, depends on what you're intending. If you want to print the string char by char via pointer you may use:

char *p=&c[0];
while (*p) {
  printf("%c", *p);
  p++;
}

Comments

0

Your question is to print string using pointer. You could use

printf("%s", c);

or character by character as (include library string.h for this)

for(i=0;i<strlen(c);i++)
{ 
   printf("%c", c[i]);
}

Comments

0

in C strings are stored as character arrays and are terminated by a zero-value, so called zero-terminated strings. Btw, this is why you have to make the array size of 4 for thee real chars.

In your example, you are passing pointers th each char to the printf function and printf prints the strings from your pointer to the next null-value . The 1st pass prints "hia", the 2nd ia and the 3rd a.

To print a single char in each pass, you have to use

printf ("%c", c[i]);

Comments

0

Your loop will call printf with the following parameter:

printf("hia");  // first loop iteration
printf("ia");   // second loop iteration
printf("a");    // third loop iteration
printf("");     // fourth loop iteration

You probably meant to print one character at a time:

  for(i=0;i<3;i++)  // No need to print the string termination character.
  { 
    printf("%c", c[i]);  // "%c" is the printf format code to print a single character 
  }

2 Comments

I am interested to know how and why printf(&c[i]) is a valid syntax?
@haccks printf expects a char* as its first argument. &c[i] (the address of a char) is a char*. Since the string doesn't contain any format specifiers, no additional arguments are expected.

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.