Thread: for 8yrs old problem..=)

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    14

    for 8yrs old problem..=)

    Code:
    char num[4];
    int a,b,c,d;
    printf("enter a four digit nuber:");
    scanf("%s",num[4]);
    a=num[0];
    b=num[1];
    c=num[2];
    d=num[3];
    printf("%d",a);
    printf("%d",b);
    printf("%d",c);
    printf("%d",d);
    i input 1234 but it should ouput
    a=1
    b=2
    c=3
    d=4
    ryt? but it doest output the number that im expecting..

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    A nudge in the right direction, consider, the character '1' is represented by the character code for '1' which is not equal to 1. Assuming you are using 8 bit characters of a standard type, look up ASCII coding, and the atoi() function.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by rothj0hn
    Code:
    char num[4];
    scanf("%s",num[4]);
    This does not do what you think it does. It should cause a segmentation fault.
    You're telling scanf() that you want to put a string at the address stored in num[4], which is beyond the end of the array num[], and is also a char, not a pointer.

    I'll leave it to others to explain why making num[] only 4 elements long is a bad idea.
    Insert obnoxious but pithy remark here

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    2
    you can try

    scanf("%s",&num);

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Belzebuts
    you can try

    scanf("%s",&num);

    no need of saying & in front of num. b'cose the num is already a pointer to string to the first element

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM