Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 26-08-2009
Member
 
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?
Reply With Quote
  #2  
Old 26-08-2009
Member
 
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).
Reply With Quote
  #3  
Old 26-08-2009
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 11:09 AM.