Character Input and symbol constant EOF in C Language

Source: Internet
Author: User

My system is centos5.5 and the compiler is GCC 2.5.

The procedure is as follows:

// Function of the program: verification expression getchar ()! = Whether the value of EOF is 0 or 1.

# Include <stdio. h>

Main ()

{

Int C;

While (C = getchar ()! = EOF )//! The = Priority is higher than the = priority, so the expression is combined from right to left.

Printf ("% d \ n", C );

Printf ("% d-at EOF \ n", C );

}

After GCC compilation, generate the. Out file.

./A. Out: Enter the following data:

2 press ENTER

Output:

1

1

How can two 1 s be output? Think about it, the carriage return is also a character, so it is actually two inputs, so the output is two 1. It also indicates that when the input is not equal to EOF, the expressionGetchar ()! = EOFThe value is 1.

Enter:

Enter

Output:

1

Enter-1 and press Enter.

Output:

1

1

1

'-', 1, and 'Carriage return 'are three characters in total, SO 3 are output.

How can I end the input at this time? In my system, press Ctrl + D.

Press Ctrl + D, which is equivalent to the input EOF, and the output is:

0-at EOF

This indicates that when the input is equal to EOF, the expressionGetchar ()! = EOFThe value is 0.

Because EOF is defined in the header file <stdio. h>, you can add the following code to the end of the program to view the value of EOF:

Printf ("% d \ n", EOF );

The output result is-1.

Well, make a little progress every day.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.