Talk C Chestnut Bar (179th: C-language Instance-character and string output function II)

Source: Internet
Author: User

Hello, crossing, all right, last time we were talking about examples of character and string output functions, let's go on to this example. Idle
The words go forward, and the words return to the positive. Let's talk C chestnuts together!

Crossing, we introduced in the last time the use of character and string output functions, manufactoring is the theory, the old saying cloud: The end of the paper, it is known that the matter to preach. In this case, we have to apply these paper theory to practice, let's practice it together.

Next, we'll show you how to use character output functions with specific code, and what to consider when using them:

int main(){    char‘a‘;    char"hello world";    if (EOF == fputc(ch,stdout) )    {        puts("fputc running failed");        return1;    }    fputs("\n",stdout);    return0;}

We use the FPUTC function in the above code to output a character to the standard output, at the same time to determine whether the function is successful, if the function is successful, then output a newline character, so that we can see in the terminal program running results, if the function fails, then output error in the terminal, Then quit the program and finish running the program. Here is the result of the program running:

@talk8$ ./test    //在终端中运行编译好的程序a                 //程序在标准输出,也就是当前终端中输出字符:a@talk8$           //程序正常结束,返回终端,终端等待其它命令或者程序运行

The other two character output functions: PUTC and Putchar also have the same function, we do not give examples, please do their own to verify that they are not and FPUTC have the same result.

After describing the character output function, we'll show you how to use the string output function with specific code, and what to consider when using it:

intMain () {Char*buf[]= {"One","both","three"};intindex =0; while(Index <sizeof(BUF)/sizeof(Char*) )    {if(0>fputs(Buf[index],stdout)) {puts("Fputs running Failed");return 1; }fputs("\ n", stdout);//Special output line break hereindex++; } index =0; while(Index <sizeof(BUF)/sizeof(Char*) )    {if(0>puts(Buf[index])) {puts("puts running failed");return 1; } index++;//Do not require special output line break}return 0;}

The following is the results of the operation of the program, please refer to:

Onetwothree   //依次使用fputs输出数组中的内容,每行后面的换行符都是专门输出的Onetwothree   //依次使用puts输出数组中的内容,每行后面的换行符是puts输出的

In combination with the above code and the running results of the program, we can see that puts can automatically output line breaks at the same time as the output string, which is more intelligent than the function fputs. Of course, the ruler is short, inch, in the actual program, we can choose a function according to the program to output the string.

Crossing, I put the code in the example into a file, and put the file in my resources, you can click here to download the use.

You crossing, for examples of character and string output functions Let's talk about this. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (179th: C language Instance-character and string output function II)

Related Article

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.