tags:

views:

228

answers:

4

What is the C equivalent to the C++ cin statement? Also may I see the syntax on it?

+7  A: 

You probably want scanf.

An example from the link:

int i, n; float x; char name[50];
n = scanf("%d%f%s", &i, &x, name);

Note however (as mentioned in comments) that scanf is prone to buffer overrun, and there are plenty of other input functions you could use (see stdio.h).

sje397
If you _must_ use `scanf`, at least use it in a non-buffer-overflow sort of way :-) See http://stackoverflow.com/questions/3723044/get-scanf-to-quit-when-it-reads-a-newline/3723071#3723071 for a better way.
paxdiablo
Please don't recommend using `scanf`. http://c-faq.com/stdio/scanfprobs.html
jamesdlin
I think my warning is clear enough. The OP is obviously fairly new to C, and scanf provides the perfect start to learning about command line input without having to be concerned with additional complexities. I'm sure he/she will encounter the idea of buffer overrun again before any production code gets written. More experienced users reading the question should heed the warning.
sje397
Your warning talks about buffer overruns but not about the various other problems that make `scanf` hard to use (hardly a "perfect start"). Moreover, I strongly disagree that introducing new programmers to bad habits is a good idea.
jamesdlin
The 'various other problems' in your link are actually just one fairly simple one, and arguably a real problem anyway. I think the warning covers the bad habits call. New programmers do need to learn what bad habits are (hence the warning), but more importantly they can't learn everything at once.
sje397
Arguably a real problem? They doesn't span 3 questions in the comp.lang.c FAQ for no reason; they're frequently asked. They're frequent problems that people run into on SO too. And IMO it's simpler to learn to use `fgets` and `sscanf` instead of learning the little gotchas with `scanf`. (Also, sure, people should be aware of what bad habits *are*, but they shouldn't be encouraged to *adopt* them.)
jamesdlin
Yes, arguably. I didn't say it wasn't a problem, but it's arguable because it is a design 'feature' (and possibly a bad one). And again I think the warning is enough *discouragement*: I added it in response to the first 'anti-scanf' comment, which is fair. The links provide avenues to explore *when the OP is ready*. I also still think it best answers the question - fgets and others are not as functionally similar to the cin operator>>.
sje397
+1  A: 

In c++, cin is the stdin input stream. There is no input stream in C, but there is a file object, called stdin. You can read from it using a lot of ways, but here's an example from cplusplus.com:

/* fgets example */
#include <stdio.h>

int main()
{
  char buffer[256];
  printf ("Insert your full address: ");
  if (fgets(buffer, 256, stdin) != NULL) {
    printf ("Your address is: %s\n", buffer);
  }
  else {
    printf ("Error reading from stdin!\n");
  }
  return 0;
}

UPDATE

I've changed it to use fgets, which is much simpler since you have to worry about how large your buffer is. If you also want to parse the input use scanf, but make sure that you use the width attribute.

Mike Axiak
-1 for even *mentioning* `gets`. It's far too prone to buffer overruns, and should never, ever, *ever* be used in new code.
cHao
Alright I'll fix it. DO you think scanf is much better? :P
Mike Axiak
I'm personally not a fan of it. `fgets` does a similar job to `gets`, though, and lets you specify the buffer length. The only catch is that iirc it keeps the newline rather than chomping it.
cHao
That's what I was updating it to use -- it's what I normally use.
Mike Axiak
One more (tiny) nitpick, Mike then I'll give you an upvote. STDIN should be lowercase.
paxdiablo
+2  A: 

There is no close equivalent to cin in C. C++ is an object oriented language and cin uses many of its features (object-orientation, templates, operator overloading) which are not available on C.

However, you can read things in C using the C standard library, you can look at the relevant part here (cstdio reference).

jbernadas
+1 for providing a linked reference rather than mentioning/illustration a single function as if that were the only C option. @Anteater: <cstdio> is header for using the C-style I/O routines within C++ programs, if you're writing a C program just #include <stdio.h>
Tony
You're right, I'm so used to C++ that forgot that in C the headers names are different.
jbernadas
+8  A: 

cin is not a statement, it's a variable that refers to the standard input stream. So the closest match in C is actually stdin.

If you have a C++ statement like:

std::string strvar;
std::cin >> strvar;

a similar thing in C would be the use of any of a wide variety of input functions:

char strvar[100];
fgets (strvar, 100, stdin);

See an earlier answer I gave to a question today on one way to do line input and parsing safely. It's basically inputting a line with fgets (taking advantage of its buffer overflow protection) and then using sscanf to safely scan the line.

Things like gets() and certain variations of scanf() (like unbounded strings) should be avoided like the plague since they're easily susceptible to buffer over-runs. They're fine for playing around with but the sooner you learn to avoid them, the more robust your code will be.

paxdiablo
I think it would be fair to interpret the question to mean 'equivalent to a statement using cin'
sje397