Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - The difference between Scanford and cin
The difference between Scanford and cin
1.cin is an object of type C++, the type is basic_istream, and scanf is a function with uncertain parameters.

2.2. The class to which CIN belongs is overloaded >; & gt operator, which makes it easier to enter, for example

Double d; CIN & gt; & gtd;

Scanf is different,

Double d; scanf("%lf ",& ampd);

It looks a little troublesome.

In addition, cin is more secure and there will be no memory overflow.

char c; scanf("%lf ",& ampc); //At this time, 8 bytes of space were needed, but only one was provided, and the memory overflowed.

Cin is safer.

char c; CIN & gt; & gtc; //Only enter characters, and it will not overflow.

3. From the bottom, cin in C++ and scanf in C use stdin as input source, but cin and scanf use different buffers. You can set buffer * * * to share through the sync_with_stdio () function in the ios header file.

C++ can also change the input source, but it is very complicated.

In fact, C++ encapsulates stdin, changes the concept of stream with stream and iterator, and C's scanf directly operates stdin.

4.cin

and

Format input function of c language

Precautions for use

(Note: The source code of this article does not contain the header file, so please edit it yourself during the experiment. )

Please watch and operate, so as to really understand the trick:

5.

Scan function ()

Commonly used are

( 1)

scanf("%s ",a); //a

Is a pointer to a string;

Example:

int main(){

char a[20];

scanf("%s ",a);

printf("%s ",a); //

Output symbol sequence

a

Characters in

Returns 0;

}

pay attention to

1, this method of getting strings can be preceded by a carriage return.

\n '

Attention! You can have a try. invest

When you press Enter in front and then enter a set of characters, the output is still the characters you output.

2. After trying to get into the car, run again, and then you add in front.

n

Spaces, and then enter a continuous.

Characters,

See what happens. You will find that he ignored all the spaces in front.

If you like,

Of course you can, or

Use regular expressions, or use

gets();

For example:

Get (a);

In this way, the spaces you enter can also be recorded.

try again

Enter consecutive characters followed by a space, and then enter:

For example, enter someone's name.

When the word "

bill

Gates

"You will find that the output is only

bill

, and

Gates

So go to hell with the name input and input.

Noodles, too, are the best.

gets();

Function.

(2)scanf("%c ",& ampa); //

Pay attention to address symbols;

Example:

int main(){

char a;

scanf("%c ",& ampa);

printf("%c ",a); //

Output character

a

Characters in

Returns 0;

}

note:

If there is a carriage return when getting characters,

Absorb it.

You can try this,

Run the above program,

Get into the car once,

Looking closely, he left a few lines blank.

(You entered the first line, and the second line is

Print function

export

a

The value of)

Understand the mystery.

have you finished?

(3)

scanf("%d ",& ampa);

example

int main(){

int a;

scanf("%d ",& ampa);

printf("%d ",a); //

Output shaping

a

Value in;

Returns 0;

}

Note: This input is the same as (

1

), the carriage return in front will be automatically ignored.

\n '(

It's actually a

American standard code for information interchange code

courtyard

)