Thread: Doubt on scanf function

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    26

    Doubt on scanf function

    Hi,

    I have a query wrt character array. Follg is the basic code

    Code:
    void main()
    {
      char a[10];
      for(i=0;i<10;i++)
      {
          scanf("Enter the array you wanna store in a", %c, a[i]);    
       }
    }
    Suppose user enters: ABCDEFGHIJK
    is this stores as string as "ABCDEFGHIJK" in char array or as 'A', 'B',.......'K' i mean character constants...
    Also the diff between string and character though i am aware of the fact in C that there is no string !

    It would be very helpful if someone throws light on this...thks in advance!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    First of all, that code doesn't even compile. Second, read up on your scanf documentation - it appears you're trying to use it as a printf/scanf hybrid sort of thing (which it isn't, of course). Finally, remember that scanf take the address of (eg: pointer to) the object (indeed, how else could it write to it otherwise?), and you should always check the return value of scanf.

    And although C has no formal string object, it does use the convention of null-terminated character arrays, which are more often than not referred to as 'strings'.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    26
    Ya thats right. I have now corrected it. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 07:42 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM