search
HomeBackend DevelopmentC#.Net TutorialWhat are the input functions in C language?

What are the input functions in C language?

Sep 01, 2020 am 09:27 AM
c languageinput function

The input functions of the c language are: 1. scanf() function, which reads formatted input from the standard input stdin; 2. getchar() function, which obtains a character from the standard input stdin; 3. gets() The function reads a line from the standard input stdin; 4. The getch() function reads a string from the stdin stream and stops when a newline character or EOF is received.

What are the input functions in C language?

The operating environment of this tutorial: Windows 7 system, c99 version, Dell G3 computer.

The input functions of c language are:

1. The return value of scanf

scanf() function returns a successful assignment The number of data items. If an error occurs when reading the end of the file, EOF will be returned.

For example:

scanf("%d%d", &a, &b);

If both a and b are successfully read, then the return value of scanf is 2

If only a is successfully read, the return value is 1

If neither a nor b is successfully read, the return value is 0

If an error is encountered or end of file is encountered, the return value is EOF

and returns The value is int type

Blank character: A blank character will cause the scanf() function to omit one or more blank characters in the input during the read operation.

Non-whitespace character: A non-whitespace character will cause the scanf() function to eliminate characters that are the same as the non-whitespace character when reading.

Things you should pay attention to when using the scanf function

(1) For string arrays or string pointer variables, since the array name and pointer variable name themselves are addresses, when using the scanf() function , there is no need to add the "&" operator in front of them.

(2) An integer can be added between each format specifier of "%" in the format string to represent the maximum number of digits in any read operation.

(3) There is no precision control in the scanf() function.

For example: scanf("%5.2f",&a); is illegal. You cannot attempt to use this statement to enter a real number with 2 decimal places.

(4) Scanf requires a variable address. If a variable name is given, an error will occur.

For example, scanf("%d",a); is illegal and should be changed to scnaf ("%d",&a); is legal.

(5) When inputting multiple numerical data, if there are no non-format characters in the format control string to separate the input data, spaces, TAB or carriage returns can be used as intervals.

C compiler will consider the data to end when it encounters a space, TAB, carriage return or illegal data (for example, when "12A" is entered for "%d", A is illegal data).

(6) When inputting character data (%c), if there are no non-format characters in the format control string, all entered characters are considered to be valid characters.

(7) If there are non-format characters in the format control string, the non-format characters must also be entered when inputting.

2. getchar

getchar has an int return value. When the program calls getchar, the program waits for the user to press the key. The characters entered by the user are stored in In the keyboard buffer. Until the user presses Enter (the carriage return character is also placed in the buffer). When the user types Enter, getchar begins to read one character at a time from the stdin stream. The return value of the getchar function is The ASCII code of the first character entered by the user. If an error occurs, -1 is returned, and the characters entered by the user are echoed to the screen. If the user enters more than one character before pressing Enter, the other characters will be retained in the keyboard buffer area. , waiting for subsequent getchar calls to read. In other words, subsequent getchar calls will not wait for the user to press the key, but directly read the characters in the buffer. They will not wait for the user to press the key until the characters in the buffer have been read.

3. getch

The basic functions of getch and getchar are the same. The difference is that getch obtains the key value directly from the keyboard and does not wait for the user to press Enter. As long as the user presses a key, getch It returns immediately. The return value of getch is the ASCII code entered by the user. If an error occurs, -1 is returned. The entered characters will not be echoed on the screen. The getch function is often used in program debugging. During debugging, relevant results are displayed in key locations. To be viewed, then use the getch function to pause the program. When any key is pressed, the program continues to run.

4, gets

Function: Read a string from the stdin stream , until it stops when a newline character or EOF is received, and stores the read result in the character array pointed to by the buffer pointer. The newline character is not used as the content of the read string. The read newline character is converted into a null value and ends the string.

Return value: If the reading is successful, the same pointer as the parameter buffer is returned; if EOF (End-of-File) or an error occurs during the reading process, a NULL pointer is returned. Therefore, when encountering a return value of NULL, use the ferror or feof function to check whether an error occurred or EOF was encountered.

If you want to know more about programming learning, please pay attention to the php training column!

The above is the detailed content of What are the input functions in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
C# .NET Development Today: Trends and Best PracticesC# .NET Development Today: Trends and Best PracticesApr 28, 2025 am 12:25 AM

The latest developments and best practices in C#.NET development include: 1. Asynchronous programming improves application responsiveness, and simplifies non-blocking code using async and await keywords; 2. LINQ provides powerful query functions, efficiently manipulating data through delayed execution and expression trees; 3. Performance optimization suggestions include using asynchronous programming, optimizing LINQ queries, rationally managing memory, improving code readability and maintenance, and writing unit tests.

C# .NET: Building Applications with the .NET EcosystemC# .NET: Building Applications with the .NET EcosystemApr 27, 2025 am 12:12 AM

How to build applications using .NET? Building applications using .NET can be achieved through the following steps: 1) Understand the basics of .NET, including C# language and cross-platform development support; 2) Learn core concepts such as components and working principles of the .NET ecosystem; 3) Master basic and advanced usage, from simple console applications to complex WebAPIs and database operations; 4) Be familiar with common errors and debugging techniques, such as configuration and database connection issues; 5) Application performance optimization and best practices, such as asynchronous programming and caching.

C# as a Versatile .NET Language: Applications and ExamplesC# as a Versatile .NET Language: Applications and ExamplesApr 26, 2025 am 12:26 AM

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

C# .NET for Web, Desktop, and Mobile DevelopmentC# .NET for Web, Desktop, and Mobile DevelopmentApr 25, 2025 am 12:01 AM

C# and .NET are suitable for web, desktop and mobile development. 1) In web development, ASP.NETCore supports cross-platform development. 2) Desktop development uses WPF and WinForms, which are suitable for different needs. 3) Mobile development realizes cross-platform applications through Xamarin.

C# .NET Ecosystem: Frameworks, Libraries, and ToolsC# .NET Ecosystem: Frameworks, Libraries, and ToolsApr 24, 2025 am 12:02 AM

The C#.NET ecosystem provides rich frameworks and libraries to help developers build applications efficiently. 1.ASP.NETCore is used to build high-performance web applications, 2.EntityFrameworkCore is used for database operations. By understanding the use and best practices of these tools, developers can improve the quality and performance of their applications.

Deploying C# .NET Applications to Azure/AWS: A Step-by-Step GuideDeploying C# .NET Applications to Azure/AWS: A Step-by-Step GuideApr 23, 2025 am 12:06 AM

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

C# .NET: An Introduction to the Powerful Programming LanguageC# .NET: An Introduction to the Powerful Programming LanguageApr 22, 2025 am 12:04 AM

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NET Framework vs. C#: Decoding the Terminology.NET Framework vs. C#: Decoding the TerminologyApr 21, 2025 am 12:05 AM

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function