Thread: Character Array

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    69

    Character Array

    Hello,

    The below code is compiling good, but why it's not working?


    Code:
    #include <stdio.h>
    int main(){
        char n[6], i;
        for (i=0; i<=5; i++)
        {
        printf ("Enter you name");
        scanf ("%s", &n[i]);
    }
    
    
        for (i=0; i<=5; i++)
        printf ("%s", n[i]);
    return 0;    
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The below code is compiling good
    Increase your compiler warnings.

    Code:
    /*
    main.c||In function 'main':|
    main.c|7|warning: array subscript has type 'char'|
    main.c|12|warning: array subscript has type 'char'|
    main.c|12|warning: format '%s' expects type 'char *', but argument 2 has type 'int'|
    ||=== Build finished: 0 errors, 3 warnings ===|
    */
    It looks like you're confusing individual characters (%c) and strings (%s). It seems you want to read and print one character at a time (based on the code that loops through the array). Try using %c instead of %s. This won't fix all the problems with the code, but should get you on the right track.

  3. #3
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    You probably meant to do this:
    Code:
    char n[6];
    printf("Enter you name");
    scanf("%5s", n);

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cstryx
    You probably meant to do this:
    Code:
    char n[6];
    printf("Enter you name");
    scanf("%5s", n);
    Note the importance of the '5' in "%5s": without it, your code would be vulnerable to buffer overflow. You should also check the return value of scanf: in this case it should return 1, otherwise you have an input error.

    Furthermore, as Matticus implied in post #2 by observing that you are "confusing individual characters (%c) and strings (%s)", this would replace your for loop as you would be working with a string. Likewise, you do not need a second for loop for print the string once.

    Also, you need to indent your code properly. This is especially important when your code involves loops and the like.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    It's great that we have pretty much all bases covered now that the issues have been pointed out and laserlight was kind enough to provide further detail about them.

    One thing I will add is about the "%5s". Some people get confused about what number should be put there. That 5 is for the actual visible char data that gets put into the string buffer before the null terminator. A format like %s means that the null terminator is appended automatically, so if you use %6s, it would add 6 characters to the buffer + 1 null terminator which would mean you would need a buffer of size 7 bytes, and you only have allocated enough for 6 chars. This is why the proper string is "%5s" and not "%6s".

    I like to use macros to simplify this stuff so that I don't have to change the string manually.

    Code:
    #include <stdio.h>
    
    #define STR_TOKEN(x) #x
    #define STR(x) STR_TOKEN(x)
    #define MAX_CHARS 5
    
    int main(void)
    {
      char n[MAX_CHARS + 1];
      printf("Enter you name");
      scanf("%" STR(MAX_CHARS) "s", n);
      printf("%s\n", n);
      return 0;
    }
    Last edited by cstryx; 08-29-2015 at 01:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-08-2014, 08:52 AM
  2. How do I remove a character from character array?
    By nick753 in forum C++ Programming
    Replies: 25
    Last Post: 12-08-2010, 11:27 AM
  3. Replies: 15
    Last Post: 09-23-2010, 02:19 PM
  4. REmoving a character from a character array
    By Bladactania in forum C Programming
    Replies: 3
    Last Post: 02-11-2009, 02:59 PM
  5. Replies: 8
    Last Post: 11-12-2008, 11:25 AM