Alex Rivera | Logout

What is the C equivalent to the C++ cin statement?

Asked 2010-09-16T03:55:52.727
19

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

c++ c
Edit
Report

1 Answer

1

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.

answered 2010-09-16T03:59:00.240

Your Answer