zoukankan      html  css  js  c++  java
  • c语言学习(3)

    c语言基础知识网站,多掌握一些库函数, http://www.cplusplus.com/reference/iolibrary/ 

    参考:https://www.runoob.com/cprogramming/c-function-fflush.html

    --------------------------------------------

    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    
       char buff[1050];
    
       memset( buff, '', sizeof( buff ));
    
       fprintf(stdout, "启用全缓冲
    ");
       setvbuf(stdout, buff, _IOFBF, 30);
    
       fprintf(stdout, "这里是 runoob.com
    ");
       fprintf(stdout, "该输出将保存到 buff
    ");
       fflush( stdout );
    
       fprintf(stdout, "这将在编程时出现
    ");
       fprintf(stdout, "最后休眠五秒钟
    ");
       fprintf(stdout, "最后休眠五秒钟1
    ");
       fprintf(stdout, "最后休眠五秒钟2
    ");
       fprintf(stdout, "最后休眠五秒钟3
    ");
       fprintf(stdout, "最后休眠五秒钟4
    ");
       fprintf(stdout, "最后休眠五秒钟5
    ");
    
       sleep(5);
    
       return(0);
    }
    

      

    -----------------------

    --------------------------

    c语言字符串的初始化,

    https://stackoverflow.com/questions/18688971/c-char-array-initialization#:~:text=You%20can%20use%20double%20quotes,the%20NUL%20character%20in%20it.

    Edit: OP (or an editor) silently changed some of the single quotes in the original question to double quotes at some point after I provided this answer.

    Your code will result in compiler errors. Your first code fragment:

    char buf[10] ; buf = ''

    is doubly illegal. First, in C, there is no such thing as an empty char. You can use double quotes to designate an empty string, as with:

    char* buf = "";

    That will give you a pointer to a NUL string, i.e., a single-character string with only the NUL character in it. But you cannot use single quotes with nothing inside them--that is undefined. If you need to designate the NUL character, you have to specify it:

    char buf = '';

    The backslash is necessary to disambiguate from character '0'.

    char buf = 0;

    accomplishes the same thing, but the former is a tad less ambiguous to read, I think.

    Secondly, you cannot initialize arrays after they have been defined.

    char buf[10];

    declares and defines the array. The array identifier buf is now an address in memory, and you cannot change where buf points through assignment. So

    buf =     // anything on RHS

    is illegal. Your second and third code fragments are illegal for this reason.

    To initialize an array, you have to do it at the time of definition:

    char buf [10] = ' ';

    will give you a 10-character array with the first char being the space '40' and the rest being NUL, i.e., ''. When an array is declared and defined with an initializer, the array elements (if any) past the ones with specified initial values are automatically padded with 0. There will not be any "random content".

    If you declare and define the array but don't initialize it, as in the following:

    char buf [10];

    you will have random content in all the elements.

  • 相关阅读:
    备份服务器实战
    LAMP架构编译安装过程详解
    centos .7x service iptables save 错误解决方案
    Linux上安装jdk1.8和配置环境变量
    YUM源使用阿里镜像
    Linux增加swap分区的方法
    elasticsearch5.4集群超时
    职场PPT达人装酷的13条秘诀
    千古绝唱风月事,河山绘尽一人心
    前端集成解决方案
  • 原文地址:https://www.cnblogs.com/oxspirt/p/13511315.html
Copyright © 2011-2022 走看看