C language--gets, puts, scanf, printf function detailed usage and differences

Table of contents

I. Introduction

 2. In-depth analysis of functions gets() and scanf()

     (1) Header file

     (2) scanf () function

     (3) gets() function

    Key points: (4) The difference between gets(a); and scanf("%s", a);

    (5) The difference between puts(a) and priintf("%s",a);

Three, mutual encouragement 


I. Introduction

    Before writing this article, I always had a half-understood feeling about these basic functions and didn’t care too much until I encountered a full screen of hot hot hot hot hot hot hot hot hot hot hot hot hot hot hot Hot times or input characters always do not meet the format requirements of the topic . So far, I have read some articles by big guys and made what I have to understand.

 2. In-depth analysis of functions gets() and scanf()

     (1) Header file

    The header files of the gets() and scanf() input functions are both #include <stdio.h>

    (2) scanf () function

   The scanf() function is an input function that can input integers, floating-point numbers, characters, integer arrays, and string arrays

   1. Use %d to input scanf("%d", &x);

    2. Use %f to input scanf("%f",&x);

                       Use %lf in double to input scanf("%lf",&x);

    3. Use %c in the character char to input scanf("%c",&x);

    3. Use %d in the integer array to input scanf("%d",&a); Note: the array name must have the address character &

    Note: At this time &a transmits the first address

    4. Use %s in the string array to input scnaf("%s", a); Note: the array name cannot have the address character &

     (3) gets() function

   gets() is an input function that specifically inputs string arrays

    gets(a); a is the array name

    Key points: (4) The difference between gets(a); and scanf("%s",a);

    gets(a) is similar to scanf("%s",a), but not identical.

    There is a problem when the scanf("%s", a) function inputs a string, that is, if a space is entered, the string will be considered as the end, and the characters after the space will be processed as the next input item.

    The gets(a) function will receive the entire input string until a newline is encountered.

 Code example: scanf("%s",a);

#include <stdio.h>
#include <string.h>
int main()
{
	char a[10] = { 0 };
	scanf("%s", a);   //1234
	printf("%s", a);  //1234
	return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
	char a[10] = { 0 };
	scanf("%s", a);   // 1 2 3 4
	printf("%s", a);  // 1
	return 0;
}

Code example: gets(a);

#include <stdio.h>
#include <string.h>
int main()
{
	char a[10] = { 0 };
	gets(a);        //1234
	printf("%s", a);  //1234
	return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
	char a[10] = { 0 };
	gets(a);        //1 2 3 4
	printf("%s", a);  //1 2 3 4
	return 0;
}

    (5) The difference between puts(a) and priintf("%s",a);

    The usage of puts and printf is the same, the function of puts() is the same as that of the statement "printf("%s\n", a);

    Note: puts will automatically output a carriage return after outputting the string.

Three, mutual encouragement 

 The following is my understanding of the input function. If there are friends who don’t understand or find problems, please tell them in the comment area. At the same time, I will continue to update my understanding of the getchar() function , please continue to pay attention to me! ! ! ! ! ! ! ! 

Guess you like

Origin blog.csdn.net/weixin_45031801/article/details/127231645