For example, I have a string containing "184553", how do I go about extracting the 3rd character "4" from string string and assigning it to another variable?

I have tried the following code but it does not work, the system crashes:

char string[6] = "184553";
printf("%s",string[2]); // Expecting it to print "4"

Any help??

Dani AI

Generated

In C, strings are arrays of char terminated by '\0', and indexing is zero-based. The crash you saw happens because %s expects a pointer to a NUL-terminated string, but string[2] produces a single character value, not a pointer; passing that to %s is undefined behavior. See the printf family format rules in cppreference.

If you need the 3rd character, validate the index first and then extract it. If an API requires a C string, build a 1-character string explicitly.

#include <string.h>

void use_third_character(const char *s) {
    size_t i = 2; // 3rd character
    if (i < strlen(s)) {
        char ch = s[i];             // the character itself
        char one[2] = { ch, '\0' }; // a 1-character C string if needed
        /* use ch with %c, or one with %s in other APIs */
    } else {
        /* handle out-of-range index */
    }
}

Additional tips:

  • Ensure the storage for any string literal you copy into includes space for the terminator. For length N text, you need N+1 bytes. Functions like strlen and sizeof (on arrays with known size) help avoid off-by-one errors; see strlen.
  • If you want the numeric value of a digit character, check the range and convert: int digit = s[i] - '0'; only after verifying s[i] is between '0' and '9'.
  • Indexing bytes in UTF-8 text may split a multibyte character; the above is safe for plain ASCII digits.

Recommended Answers

All 7 Replies

Like so

char my_ch = string[2];

hi there,

As you want to print a single character please use %c as the format specifier instead of %s. Also the size of string variable should be 7.
The code becomes as:

char string[7] = "184553";
   
      printf("%c",string[2]); //prints "4" now

Anirudh

Yes, %c should be used instead of %s. This isn't technically a C-string, just a simple array. A string would require 7 spaces, to accommodate for the NULL value at the end.

Yes, %c should be used instead of %s. This isn't technically a C-string, just a simple array. A string would require 7 spaces, to accommodate for the NULL value at the end.

No, technically it is a C-string. It has quotes which by definition ends in a NULL value. The definition actually overflows the array.

My mistake

No, technically it is a C-string. It has quotes which by definition ends in a NULL value. The definition actually overflows the array.

Thank-you WaltP

I appreciate your technical prowess with these questions..and I hope you realize this is sincere...

When you want to declare an array of the same size as a const string literal then don't specify the array size so that the compiler can figure out the size itself char string[] = "184553";

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.