好好爱自己!

c语言学习(3)

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

 

 

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

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <string.h>
 
int main()
{
 
   char buff[1050];
 
   memset( buff, '\0', sizeof( buff ));
 
   fprintf(stdout, "启用全缓冲\n");
   setvbuf(stdout, buff, _IOFBF, 30);
 
   fprintf(stdout, "这里是 runoob.com\n");
   fprintf(stdout, "该输出将保存到 buff\n");
   fflush( stdout );
 
   fprintf(stdout, "这将在编程时出现\n");
   fprintf(stdout, "最后休眠五秒钟\n");
   fprintf(stdout, "最后休眠五秒钟1\n");
   fprintf(stdout, "最后休眠五秒钟2\n");
   fprintf(stdout, "最后休眠五秒钟3\n");
   fprintf(stdout, "最后休眠五秒钟4\n");
   fprintf(stdout, "最后休眠五秒钟5\n");
 
   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 = '\0';

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 '\040' and the rest being NUL, i.e., '\0'. 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.

 

posted @   立志做一个好的程序员  阅读(165)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2018-08-16 vim 多窗口,多tab编辑
2018-08-16 【转】php中的会话机制(2)
2017-08-16 【转】三年后再反思我的" Java Web项目管理得失谈"
2017-08-16 java构造函数重载this(true)
2017-08-16 为什么用clojure作为storm 的主要开发语言
2017-08-16 php模拟并发
2017-08-16 【转】storm 开发系列一 第一个程序

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示