Memory bread C language

Input character string
in the C language, there are two functions allows the user to input character string from the keyboard, they are:
Scanf (): the format control character input string by% s. In addition to the string, scanf () can also enter other types of data.
gets (): direct input character string, and only the input string.

However, scanf () and gets () is differentiated:
scanf () is a space-separated string when reading encountered space is considered the current string is over, so I can not read the string contains spaces.
gets () string is considered part of the space, experiencing the Enter key only when considered end of the input string, so, no matter how many spaces are entered, they do not press the Enter key on gets (), it is a the complete string. In other words, gets () to read a whole line string.

#include <stdio.h>
int main(){
    char str1[30] = {0};
    char str2[30] = {0};
    char str3[30] = {0};
    //gets() 用法
    printf("Input a string: ");
    gets(str1);
    //scanf() 用法
    printf("Input a string: ");
    scanf("%s", str2);
    scanf("%s", str3);
   
    printf("\nstr1: %s\n", str1);
    printf("str2: %s\n", str2);
    printf("str3: %s\n", str3);
    return 0;
    }

The output string
in the C language, there are two functions can be output on the console strings (display), which are:
the puts (): output string and wrap function can only output string.
printf ():% s by the output format control character string, do not automatically wrap. In addition to the string, printf () can output other types of data.

#include <stdio.h>
int main(){
    char str[] = "http://c.biancheng.net";
    printf("%s\n", str);  //通过字符串名字输出
    printf("%s\n", "http://c.biancheng.net");  //直接输出
    puts(str);  //通过字符串名字输出
    puts("http://c.biancheng.net");  //直接输出
    return 0;
}
Original articles published 0 · won praise 0 · Views 11

Guess you like

Origin blog.csdn.net/weixin_39475542/article/details/104551872