Solved

reading whitespace in c

Posted on 2007-03-23
3
1,821 Views
Last Modified: 2011-09-20
Hi,

I remember in C++, you can do a  cin>>ws to read the white space,
can I do the similar thing in C? Thanks!
0
Comment
Question by:yarock
3 Comments
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 500 total points
ID: 18784259

You can certainly do something similar, but it's not quite as elegant.

  char ch;

  while (isspace (ch = getchar ()));
  unget (ch);


And if you're reading an entire line at a time, sscanf() can be controlled to skip whitespace as you parse the entire line.


Kent
0
 
LVL 3

Expert Comment

by:srinims
ID: 18797875

getchar will recieve all keyboard events, untill newline character or eof '\0' character occurs.

char ch;
while(ch=getchar())
{
      printf("%X",ch);
}

will print the hexadecimal equivalent of the key.. will print 20 for space...
0
 
LVL 2

Expert Comment

by:Hurb
ID: 18803751
hi,

char ch;

ch=getchar();           // will store any char include on alphabetic like '\0'
scanf("%c",&ch);    // same as above
scanf(" %c",&ch);  // note space before %c; this will skip any non alpha char including spaces ie. will just store aplhabetic chars
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question