【C语言学习】《C Primer Plus》第4章 字符串和格式化输入/输出
学习总结
1、String str=”hello world!”;(Java),char[20]=” hello world!”;(C)。其实Java字符串的实现,也是字符数组。
2、字符串的尾部都会以空字符(\0)结束,所以” hello world! “这个字符数组的长度是13。<string.h>函数库有个strlen()函数可以计算机字符串的字符数组长度(不包括空字符\0)。
3、scanf(“%s”,name)和gets的差别:
#include <stdio.h>
int main(void){
char name[];
printf("what's your name?\n");
scanf("%s",name);
printf("hello %s!\n",name);
return ;
}
运行结果:
what's your name?
tom green
hello tom!
#include <stdio.h>
int main(void){
char name[];
printf("what's your name?\n");
gets(name);
printf("hello %s!\n",name);
return ;
}
运行结果:
what's your name?
tom green
hello tom green!
4、#define可用于常量定义,格式:#define NAME value,编译器在编译前会把所有NAME替换为value后在编译,相当于单纯“面值”的替换。如果value是组合元素的话,最好括号,如#define SUM (x+y),以防这种“文字游戏”引起的错乱。
5、C常量的另一种表示可以使用const关键字,如const int months=12。那么months就是一个常量了。总之被const修饰的对象就是常量,不可以修改。
6、编程练习(题7):
#include <stdio.h>
#define J2S 3.785
#define Y2G 1.609
int main(){
double mile,gallon;
printf("enter your mile:");
scanf("%lf",&mile);
printf("enter your gallon:");
scanf("%lf",&gallon);
printf("your oil wear is %.1f\n",mile/gallon);
printf("your CHN oil wear is %.1f\n",mile*Y2G/(gallon*J2S));
}
运行结果:
enter your mile:100
enter your gallon:50
your oil wear is 2.0
your CHN oil wear is 0.9
【C语言学习】《C Primer Plus》第4章 字符串和格式化输入/输出的更多相关文章
- C Primer Plus 第4章 字符串和格式化输入/输出 编程练习
1. #include <stdio.h> int main(void) { ]; ]; printf("请输入您的名字: "); scanf("%s&quo ...
- c语言学习笔记第四章——字符串和格式化输入、输出
B站有视频演示 本章学习printf函数的输入输出,字符串的定义与实用. 字符串 字符串(character string)是一个或多个字符的序列,如下所示: "Zing went the ...
- C Primer Plus学习笔记(三)- 字符串和格式化输入/输出
从一个简单的例子开始 #include <stdio.h> int main() { char name[10]; printf("Input Your Name:\n" ...
- 重学C语言---04字符串和格式化输入/输出
1.程序示例 //talkback.c一个能为你提供一些信息的对话框 #include <stdio.h> #include <string.h> //提供strlen函数原型 ...
- GO语言学习笔记1-输入带空格的字符串
最近开始学习GO语言,并做了一些编程练习.有道题要输入带空格的字符串,这在C/C++中很容易实现,但GO中好像并不那么容易.学过C/C++的可能都知道,在C中可以使用gets()函数,在C++可以使用 ...
- c语言学习之基础知识点介绍(二):格式化控制符和变量的补充
上节简单介绍了c语言中的一些基础知识点,本节将对之前介绍的不够详细的知识点进行补充. 格式化控制符的消息介绍: %d的其他控制符: 1.%md:m代表这个整数位占用多少位,m是一个整数.实际数字不足的 ...
- C语言学习之我见-strncat()可调整的字符串拼接函数
strncat()函数,用于两个字符串的拼接. (1)函数原型 char * strncat(char * Dest,const char * Source,size_t _Count)` (2)头文 ...
- 【C语言学习】-05 二维数组、字符串数组、多维数组
⼆二维数组.字符串数组.多维数组
- R语言学习笔记(二十一):字符串处理中的元字符(代码展示)
元字符有自己的特殊含义 [ ]内的任意字符将被匹配 grep(pattern = "[wW]", x = states, value = T) grep(pattern = &qu ...
随机推荐
- C# 用POST提交json数据
public void GetResponse(string url, string json) { Encoding encoding = Encoding.UTF8; byte[] data = ...
- Python中获取异常(Exception)信息
异常信息的获取对于程序的调试非常重要,可以有助于快速定位有错误程序语句的位置.下面介绍几种python中获取异常信息的方法,这里获取异常(Exception)信息采用try...except...程序 ...
- cassandra的源代码的入口
参考 http://ju.outofmemory.cn/entry/115864 cassandra自带服务端,这和leveldb不一样. 入口就从服务端程序说起. 具体的入口程序在 Cassand ...
- Wind7系统下 wifi设置
netsh wlan set hostednetwork mode=allow ssid=pass:123456789 key=123456789 netsh wlan set hostednetw ...
- nodejs的url参数获取
express封装了多种http请求方式,我们主要使用get和post两种,即qpp.get和qpp.post.qpp.get和qpp.post的第一个参数都为请求的路径,第二个参数为处理请求的回调函 ...
- C# this和base的使用
namespace THISORBASE { //参考地址:http://blog.sina.com.cn/s/blog_7300c7d90100rs20.html /*这个时候,派生类和基类的_st ...
- ubuntu14.04设置terminal配色方案以配合使用vim的Solarized插件
在安装vim插件之前,首先安装Vundle插件,用来管理vim插件,安装方法查看Vundle在github上的指南.在安装vundle的时候出现了一个错误E117:unknown function v ...
- 4580: [Usaco2016 Open]248
Description Bessie likes downloading games to play on her cell phone, even though she does find the ...
- Java学习笔记 01 基本数据类型、标识符、关键字和运算符
一.基本数据类型 基本数据类型 数据类型 内存空间(8位等于1字节) 取值范围 备注 byte 8位 -128~127 short 16位 -32768~32767 int 32位 -2147 ...
- oracle exists
公司项目中有用到exists,感觉挺有用的,拷贝一些感念的东西. “exists”和“in”的效率问题 1) select * from T1 where exists(select 1 from T ...