Results 1 to 3 of 3

Thread: Problem of character array in C programming

  1. #1
    Join Date
    Jun 2009
    Posts
    1,205

    Problem of character array in C programming

    Code:
    # include <stdio.h>
    void main ()
    {
     char chr[50];
     int i,size;
     printf ("Enter the size of the array:\n");
     scanf ("%d",&size);
     printf ("Enter the characters of the array:\n");
     for (i=0; i<size; i++)
     scanf ("%c",&chr[i]); 
     printf ("The characters of the array are\n");
     for (i=0; i<size; i++)
     printf ("%c",chr[i]); 
     }
    I am having the above code and I am really confused about this. In the program if I say size = 5, it should take 5 characters but it actually takes only 2 inputs. Can you help me what is the problem with my character array?

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Problem of character array in C programming

    I think this is because you have not entered any spaces or newline after each entered character and so C compiler and has taken 3 characters in first position and the next 2 characters in second position. This is the reason why you are having only 2 inputs. If you want to differentiate between entries then you have to separate the entries by either spaces or newline character (\n).

  3. #3
    Join Date
    Nov 2008
    Posts
    1,192

    Re: Problem of character array in C programming

    First of all you didn't defined main () as "int". The return type for main (0 should always be int and not void. A main () function always returns something and it is the basic requirement of most of the C compiler. Next after each scanf statement, there should be some delimiters that is needed to separate the inputs. You can use \t for tabbing, \n for new line or spaces. Other than that your code is perfectly OK.

Similar Threads

  1. problem in the sum of optimization on a large array
    By Logan.B in forum Windows Software
    Replies: 4
    Last Post: 25-09-2010, 04:43 PM
  2. Problem with RAID 0 array
    By Shin-Chen in forum Hardware Peripherals
    Replies: 7
    Last Post: 04-07-2010, 06:27 AM
  3. Problem in representing 3 x 3 array of char in c++
    By KAILEY in forum Software Development
    Replies: 5
    Last Post: 20-02-2010, 08:05 PM
  4. Problem in creating array from string values
    By Cedric in forum Software Development
    Replies: 4
    Last Post: 19-01-2010, 05:48 PM
  5. Reading integers from a file into a character array
    By afidelino in forum Software Development
    Replies: 3
    Last Post: 08-08-2009, 11:32 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,520,644.70824 seconds with 16 queries