Beginner question about scanf/printf

This is a discussion on Beginner question about scanf/printf within the C Programming forums, part of the General Programming Boards category; Obviously I am a noob, and just trying to get a grasp on C. I wrote the following, thinking it ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    SF, CA
    Posts
    5

    Beginner question about scanf/printf

    Obviously I am a noob, and just trying to get a grasp on C. I wrote the following, thinking it would be simple, but it truncates the output, and I cannot see why.

    Code:
      #include <stdio.h>
      int main()
      {
          char dummy[80];
          printf("Enter a string:\n");
          scanf("%[^a]",dummy);
          printf("%s\n",dummy);
          return 0;
      }
    When I run it:
    $ ./a.out
    Enter a string:
    This is a test
    This is
    $

    Where am i going wrong?
    thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What do you expect %[^a] to do?

  3. #3
    Registered User
    Join Date
    Apr 2010
    Location
    SF, CA
    Posts
    5
    Im not sure. I don't quite get it. I saw it in another code example. Even if i change it to %s it still truncates the output.

  4. #4
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Quote Originally Posted by mickeyd View Post
    Im not sure. I don't quite get it. I saw it in another code example. Even if i change it to %s it still truncates the output.
    I hope you didn't malloc() anything....

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by mickeyd View Post
    Im not sure. I don't quite get it. I saw it in another code example. Even if i change it to %s it still truncates the output.
    I wonder where you would have seen that. I would have expected you to see "%[^\n]", which is what you want.

    To find out what it does, you should read the manual on scanf (type "man scanf" into your terminal, into Google, or into your C textbook).

  6. #6
    Registered User
    Join Date
    Apr 2010
    Location
    SF, CA
    Posts
    5
    Awesome! It worked as you suggest. Thank you.

  7. #7
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    You should look up the use of regular expressions and the documentation on scanf.
    -- Will you show me how to c++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 07:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 05:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 02:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 07:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21