1. scanf The function has a return value , Its return value can be divided into three cases

1) Positive integer , Indicates the number of correct input parameters . For example, to perform scanf("%d %d", &a, &b);

If user input "3 4", You can input it correctly , return 2( Two variables are correctly entered );

If user input "3,4", You can input it correctly a, Unable to input b, return 1( A variable is entered correctly ).

2) 0, Indicates that the user's input does not match , Unable to enter any values correctly . As in the above example , If the user enters ",3 4", return 0.

3) EOF, This is stdio.h Constant defined in it ( Usually the value is -1), Indicates that the input stream has ended . stay Windows Next , The user presses CTRL+Z( You'll see a ^Z character ) Press enter again ( It may need to be repeated 2 Time ), It means the end of input ;Linux/Unix Next use CTRL+D End of input .

So you can use the following code to handle the input :

while (scanf("%s %c %c", str, &oldchar, &newchar) == 3)

{

; // Handle

}

2. Why the front scanf In the format string of ,%s and %c You need a space in the middle ?

That's because if there are no spaces ...oldchar The input is the space = =.

3. By the way ,printf The return value of is the number of characters output , for example ,printf("1234") The return value of 4, and printf("1234\n") The return value of 5.

c Language scanf More articles on return values

  1. scanf Return value problem

    Recuperate at home , I have nothing to watch C primer plus, See the book for scanf Input judgment , The following methods are commonly used : At this point it will return 1 Consider my input successful , If not for 1 It is regarded as input failure . In the end scanf What does the return value of refer to ? example ...

  2. VS2019 in scanf The problem of return value being ignored and its solution

    Catalog [ problem ](# Yesterday I was using Visual Studio 2019 To write C Language program encountered scanf The return value is ignored ) Question why Method ① Method ② Method ③ Method ④ Yesterday I was using Visual Studio 20 ...

  3. C Language functions return values and variable types

    Preface I've been writing questions recently , When writing fast powers of matrices , For programs whose return value is an array , The writing is very miscellaneous . Take this opportunity to , Rearrange C The relationship between the return value of a function and the type of a variable . According to the lifetime of the variable , It can be divided into three types 1. Static variables Life starts with the program ...

  4. C Language return The return value is well understood

    C Language use return Keyword returns the function value , It's a good way to encapsulate functions , The question here is : Variables created inside functions are local variables , It's private , The scope is within the function , Why can I pass a value to the calling function ? To explain this problem, we need to start from C Language call ...

  5. Shell Script get C Language executables return values

    #!/bin/sh #./test yes c Program , This procedure return 0 ./test OP_MODE=$? echo $OP_MODE # $? Display the exit status of the last command .0 No mistakes , Any other value indicates an error .

  6. C The return value of the language program is int When , Different values represent different meanings

    This is the standard I set for my own code , Easy to read and code . Other people's code can't be applied 1 Successful implementation 0 There is an error , Does not affect program execution -1 Execution failure -2 Fatal error inside the program , Exit procedure

  7. C Language scanf Function return value note

    scanf scanf yes C Standard library stdio The functions defined inside are used to obtain user input , Specific introduction can be made in CppReference See above .scanf The return value of is the number of variables that have been assigned successfully , That is to say scanf(&quo ...

  8. C Language scanf The return value of

    #include <stdio.h> int main(void) { long num; long sum = 0L; int status; printf("Please e ...

  9. Shell Introductory tutorial :Shell The return value of the function

    shell The return value of a function usually has 3 Ways of planting : 1.return sentence ( The default return value ) shell The return value of a function can be the same as that of other languages , adopt return Statement returns . such as : #!/bin/bash functio ...

Random recommendation

  1. SharePoint Web service and template

    SharePoint Web service Corresponding mapping list WSS Web   Services Web Reference Administration   Service http://<s ...

  2. Reinforcement learning ( 11、 ... and ) Prioritized Replay DQN

    Strengthening learning ( Ten )Double DQN (DDQN) in , Here we are DDQN Use two Q The Internet , Use current Q Network computing is the biggest Q The action corresponding to the value , Using goals Q The network calculates the target corresponding to the largest action Q value , And then eliminate the bias of greedy method . Today I ...

  3. python ordinary ftp Program

    Server side '''1. Read file name 2. Check whether the file exists 3. Open file 4. Detect file size 5. Send file size to client 6. Wait for the client to confirm 7. Start sending data while reading 8. send out md5'''import socket,os,time ...

  4. Inherit polymorphic java Related basic knowledge

    1: Static statement block . Construct statement blocks ( It's the one with braces ) And the order in which constructors are executed Example : class HelloA { public HelloA() { System.out.println("H ...

  5. hihocoder 1638: Multi level and search set

    Topic link And the search set can be used for clustering . import java.io.FileInputStream; import java.io.FileNotFoundException; import java.ut ...

  6. reCaptcha for ASP.NET

    Use reCaptcha for asp.net library, It used to work , Recently I found that it is not working properly ,google There's a solution , Do not apply control And use it directly api, The code is as follows : markup: ...

  7. stay Ubuntu Environment qemu-kvm Configuration of network bridge

    In the file /etc/network/interfaces To add the following auto lo iface lo inet loopback #auto eth0 #iface eth0 inet manual ...

  8. Threads 、 process 、daemon、GIL lock 、 Thread lock 、 Recursive lock 、 Semaphore 、 timer 、 event 、 queue 、 Multi process

    # This code is based on Python3 What is a process ? Programs don't run on their own , Only load the program into memory , The system allocates resources for it to run , And this kind of program is called process . The difference between a program and a process is : A program is a collection of instructions , It's a process run ...

  9. cg course

    Unity Yes shader Our key support is cg Language , Because of the cross platform nature Cg Language and CPU Upper C Language is very similar , Just have their own set of keywords and function library Cg The authority and introduction to language is in NVID1A On the official website of , If the ...

  10. ASP.NET SignalR And LayIM coordination , Easy to realize the website customer service chat room ( Four ) Add expression 、 Group chat function

    Two days off , Or decided to take this tail away . This is the last one , It's also a hasty ending . Today we will add facial expression function and group chat . It's almost there , Other features , Readers can expand or optimize . As for the code I wrote , I didn't reconstruct myself . well , We drive ...