Thread: characters array

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    4

    characters array

    Hello everybody,so my question is i want to print characters,no string just an array of characters,i do this but it s not working,maybe i have to put the '\0' at the end?

    Code:
    int main()
    {
        int i;
        char ch[5];
    
    for(i = 0; i < 5; i++)
    {
        scanf("%c",&ch[i]);
    }
    
    for(i = 0; i < 5; i++)
    {
       printf("%c\n",ch[i]);
    }
        
        
        
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    How is it "not working"? Seems okay to me:

    Code:
    hello
    h
    e
    l
    l
    o
    The way you're doing it, you don't need the '\0' at the end. That marks the end of a string. Technically, you're working with single characters at a time.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    4
    oh, ok, now i get it, i was accidentally typing in a character and then pressing enter, so it was also recognizing ENTER as a character. I should have typed ALL 5 characters together and then press ENTER.,thanks,i have one more question

    Code:
    int main(void)
     {
     int count;
      
     printf("Enter a  line of text:\n");
     printf("EOF to  stop.\n");
      
     count = getchar();
      
     while(count !=  EOF)
     {
          putchar(count);
          count = getchar();
     }
          return 0;
     }

    1)how can i enter characters instead of integers when the variable is INT?

    2)why does it insert GETCHAR() outside the while and then go back into the while?

    3)i thought the EOF[end of file] is the last character of the string or '\0', what does CTRL -D have to do with it?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    oh, ok, now i get it, i was accidentally typing in a character and then pressing enter, so it was also recognizing ENTER as a character.
    Well done on coming to that conclusion yourself! If you're interested in more reading about that, there's a link in the FAQ: FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com

    1)how can i enter characters instead of integers when the variable is INT?
    Characters can typically be thought of as small integers. They both basically represent numbers - it's just that many systems use the value of a character to determine which "character symbol" to be printed. Regardless if you're storing the result of "getchar()" into an int or a char, it is still recognized as a character. For instance, if you enter the number one, the result is the character '1' and not the number 1 being stored.

    A common implementation is ASCII - if you look at the chart, you'll see that the character '1' actually has a value of 49 in that encoding.

    As to why a function called "getchar()", that gets a character, returns a data type "int" (and not "char")? It's because a char is not guaranteed to be large enough to hold the value of EOF. More on that here: FAQ > Definition of EOF and how to use it effectively - Cprogramming.com

    2)why does it insert GETCHAR() outside the while and then go back into the while?
    It doesn't. The program is executed sequentially, so the first "getchar()" is called before the loop, and all subsequent "getchar()" calls occur within the loop.

    3)i thought the EOF[end of file] is the last character of the string or '\0', what does CTRL -D have to do with it?
    Cntl-D (on some systems) triggers EOF.

    >> i thought the EOF[end of file] is the last character of the string or '\0'

    Read the link on EOF I posted above - it explains what EOF is and is not.

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    4
    Thank you for your valuable help, I appreciate it,,have a nice day

  6. #6
    Registered User
    Join Date
    Mar 2014
    Posts
    14
    The code of yours works great but it could perhaps be written a bit cleaner. Consider the re-written code.

    Code:
    int main(void)
     {
     int count;
    
    
     printf("Enter a  line of text:\n");
     printf("EOF to  stop.\n");
    
    
     while( ( count = getchar() ) != EOF )
     {
          putchar(count);
     }
          return 0;
     }
    Its the exact same thing only yours except I put the input and comparison for EOF inside the while condition.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Kotik View Post
    Its the exact same thing only yours except I put the input and comparison for EOF inside the while condition.
    No, it's not the same at all. Your code prints characters out while reading them, whereas the OP's code reads the input, and only outputs characters when all desired input as been received. Your code also reads until end of file is encountered, whereas the OP's is specifically limited to reading 5 characters (although the user is not prevented from entering more, the effect might not be what is expected).

    Also, naming the variable "count" is not exactly "cleaner". Nothing is being counted.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-13-2013, 12:01 AM
  2. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  3. Replies: 7
    Last Post: 05-11-2008, 10:57 AM
  4. Characters in an Array
    By Xenmordoc in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2002, 11:19 PM
  5. characters into array??
    By mik in forum C++ Programming
    Replies: 13
    Last Post: 10-04-2001, 08:54 AM