FMUSER Wirless Transmit Video And Audio More Easier !

[email protected] WhatsApp +8618078869184
Language

    Explore C language entry foundation buffers

     

    01 - C Standard Library Buffer Exploration In a computer, it is a very important concept. The C Standard library uses a large number of caches. The most typical is the cache of standard input and standard output. About the input and output of the C language, you can use this article. Take a great improvement of the program performance, first we look at the following code will output? #include "stdio.h" #include "unistd.h" int main () {printf ("Hello World!"); // The standard output output string // The program stays in the While loop, the program exits will force refresh Buffer Data While (1) {Sleep (1);} Return 0; We call the Printf function in the program intended to output "Hello World!" In the standard, the following While (1) loop is to allow the program to stop without exiting the program, each time sleep 1S avoids occupying a large number of CPU resources, including Unistd in Linux .h header file can use the SLEEP function. Now we compile the following to see what will output? We see that there is no output. But from the program, we have called the PrintF function to output a string to standard output strings, which is a role. The Printf function is default line buffer, and the data output will be output when the output string is filled with or the buffer is filled or manually called the FFLUSH function. Now you only need to add a statement to output a newline, you can output a string in the standard output output. Printf (""); // Remove, the default standard output will immediately output a refresh buffer Or we manually call FFLUSH or to force refresh buffers and output strings. Fflush (stdout); // Forced refresh standard output buffer What will happen after compiling the statement of the standard error output string? FPRINTF (stderr, "error information"); // 往 标准 错 错 输 输 输 输 输 信息 The FPRINTF function outputs the information to the first File pointer type parameter, where we pass the Stderr, and then output the string "error information" on the console after compiling running. Standard error output and standard output Run test results compare We know that the FPRINTF function does not need to be added to the standard error output information, nor does it need to force the refresh buffer to output information immediately. This is because the standard error output is a buffered mode, what data is output immediately when writing data. Let's take a look at the input code. #include "stdio.h" int main () {char Arr [100] = {0}; scanf ("% s", arr); return 0;} In this code, the program is run from the standard input data from the standard until the carriage is pressed to enter the data into the array arr. After pressing the Enter, it is actually refreshed in the input buffer to write the data at once into the array arr. 03 - Buffer In the computer, the application calls a system call from the user state into the kernel state and then returns to the user state overhead. If we call the Printf function, you have to switch from the user state to the kernel each time, then the continuous output of multiple character overhead costs will be very large. At this time, the cache has played a very big role, the output string First cache in the application, caching to a certain number of times, call the system calls at once, output the cache data to the standard output. Since only one system call is called, it is much higher than the continuous call multiple system call performance. In life, we can also feel the efficiency of the cache, and make a trash can in your office, there is a place where you have fallen garbage, if you throw a garbage to the trash can, take it down, will There is a lot of time between the office and downstairs, and waste a lot of time. If the trash can be filled, then get down the downstairs, you only need to run all the garbage, save time, improve efficiency. 04 - Buffer mode and way of use There is a row buffer mode, full buffer mode, and no buffer mode in the C language. Row buffer mode: Fill the buffer or a flipline '' or call the FFLUSH function to force the refresh buffer to output it immediately. Full buffer mode: Fill the buffer or call the FFLUSH function to force the refresh buffer to output it immediately. No buffer mode: What data is output immediately, such as standard error output default buffer mode. Below we use the actual code to demonstrate how to use three buffer patterns, set the buffer mode to use the setvbuf function, let's take a look at the setvbuf function declaration. . / * Make STREAM use buffering mode MODE If BUF is not NULL, use N bytes of it for buffering; else allocate an internal buffer N bytes long * / extern int setvbuf (FILE * __restrict __stream, char * __restrict __buf, int __modes. , SIZE_T __N) __THROW; The first parameter is the FILE type pointer, the second parameter is the external buffer pointer, the third parameter is buffer mode, the fourth parameter is buffer size, if the external buffer is not used, the function will call Malloc application. Memory is used as an internal buffer. Conversion Mode provides three parameters to set different buffer modes, respectively. _Iolbf line buffered _iofbf flly buffered _iofbf flly buffered Unbuffed mode instance code #include "stdio.h" #include "unistd.h" int main () {setvbuf (stdout, null, _ionbf, 0); // The standard output is set to have no buffer, not using the external buffer Printf ("Hello World! "); Returnograph;} Compilation operation will be output immediately Hello World! Row buffering mode instance code #include "stdio.h" #include "unistd.h" int main () {setvbuf (stdout, null, _iolbf, 0); // Standard output set to line buffer mode, not using external buffer Printf ("How Are YOU "); // Do not immediately output string fflush (stdout); // Force refresh buffer, output string Return 0;} After compilation, since FFLUSH will force the data to be refreshed to the standard output. Full buffer mode instance code #include "stdio.h" #include "unistd.h" int main () {setvbuf (stdout, null, _iofbf, 0); // Standard output set to full buffer mode, not using external buffer Printf ("Hello World ! "); // Do not immediately output Printf (" how are you "); // Do not output Printf (" ") immediately; while (1) {Sleep (1);} return 0;} After the compilation is running, there is no output. Now we add the following statement in front of the While cycle, compile and run. Fflush (stdout); // Forced refresh buffer The string is output immediately after compiling! The same way of use can be used for standard input and standard error output, just cache stdone or stderr. EDTOR: JQ, read full article, original title: C language entry foundation buffer Article Source: [Micro Signal: Androidpush, WeChat public number: QNX] Welcome to add attention! Please indicate the source of the article.

     

     

     

     

    List all Question

    Nickname

    Email

    Questions

    Our other product:

    Professional FM Radio Station Equipment Package

     



     

    Hotel IPTV Solution

     


      Enter email  to get a surprise

      fmuser.org

      es.fmuser.org
      it.fmuser.org
      fr.fmuser.org
      de.fmuser.org
      af.fmuser.org ->Afrikaans
      sq.fmuser.org ->Albanian
      ar.fmuser.org ->Arabic
      hy.fmuser.org ->Armenian
      az.fmuser.org ->Azerbaijani
      eu.fmuser.org ->Basque
      be.fmuser.org ->Belarusian
      bg.fmuser.org ->Bulgarian
      ca.fmuser.org ->Catalan
      zh-CN.fmuser.org ->Chinese (Simplified)
      zh-TW.fmuser.org ->Chinese (Traditional)
      hr.fmuser.org ->Croatian
      cs.fmuser.org ->Czech
      da.fmuser.org ->Danish
      nl.fmuser.org ->Dutch
      et.fmuser.org ->Estonian
      tl.fmuser.org ->Filipino
      fi.fmuser.org ->Finnish
      fr.fmuser.org ->French
      gl.fmuser.org ->Galician
      ka.fmuser.org ->Georgian
      de.fmuser.org ->German
      el.fmuser.org ->Greek
      ht.fmuser.org ->Haitian Creole
      iw.fmuser.org ->Hebrew
      hi.fmuser.org ->Hindi
      hu.fmuser.org ->Hungarian
      is.fmuser.org ->Icelandic
      id.fmuser.org ->Indonesian
      ga.fmuser.org ->Irish
      it.fmuser.org ->Italian
      ja.fmuser.org ->Japanese
      ko.fmuser.org ->Korean
      lv.fmuser.org ->Latvian
      lt.fmuser.org ->Lithuanian
      mk.fmuser.org ->Macedonian
      ms.fmuser.org ->Malay
      mt.fmuser.org ->Maltese
      no.fmuser.org ->Norwegian
      fa.fmuser.org ->Persian
      pl.fmuser.org ->Polish
      pt.fmuser.org ->Portuguese
      ro.fmuser.org ->Romanian
      ru.fmuser.org ->Russian
      sr.fmuser.org ->Serbian
      sk.fmuser.org ->Slovak
      sl.fmuser.org ->Slovenian
      es.fmuser.org ->Spanish
      sw.fmuser.org ->Swahili
      sv.fmuser.org ->Swedish
      th.fmuser.org ->Thai
      tr.fmuser.org ->Turkish
      uk.fmuser.org ->Ukrainian
      ur.fmuser.org ->Urdu
      vi.fmuser.org ->Vietnamese
      cy.fmuser.org ->Welsh
      yi.fmuser.org ->Yiddish

       
  •  

    FMUSER Wirless Transmit Video And Audio More Easier !

  • Contact

    Address:
    No.305 Room HuiLan Building No.273 Huanpu Road Guangzhou China 510620

    E-mail:
    [email protected]

    Tel / WhatApps:
    +8618078869184

  • Categories

  • Newsletter

    FIRST OR FULL NAME

    E-mail

  • paypal solution  Western UnionBank OF China
    E-mail:[email protected]   WhatsApp:+8618078869184   Skype:sky198710021 Chat with me
    Copyright 2006-2020 Powered By www.fmuser.org

    Contact Us