Practical C Programming - Chapter 5
来源:互联网 发布:软件卸载大师 编辑:程序博客网 时间:2024/05/22 01:59
Arrays, Qualifiers, and Reading Numbers
Strings
C does not allow one array to be assigned to another.
char sam[4];sam = "sam"; // error
The standard function fgets
can be used to read a string from the keyboard. fgets
includes the end-of-line in the string.
fgets(name, sizeof(name), stdin);
Multidimensional Arrays
int matrix[2][4];matrix[1][2] = 10;
Reading Numbers
scanf
provides a simple and easy way of reading numbers that almost never work. The functionscanf
is notorious for its poor end-of-line handling, which makesscanf
useless for all but an expert.
LOL
Instead, we use fgets to read a line of input and sscanf to convert the text into numbers. (The name sscanf stands for “string scanf”. sscanf is like scanf, but works on strings instead of the standard input.)
#include <stdio.h>char line[100]; /* line of input data */int height; /* the height of the triangle */int width; /* the width of the triangle */int area; /* area of the triangle (computed) */int main(){ printf("Enter width height? "); fgets(line, sizeof(line), stdin); sscanf(line, "%d %d", &width, &height); area = (width * height) / 2; printf("The area is %d\n", area); return (0);}
Initializing Variables
C allows variables to be initialized in the declaration statement.
int counter = 0;int code[3] = {0, 1, 2};/*if an insufficient amount of numbers are present, C will initialize the extra elements to zero.*/int another_code[3] = {0}; /*if no dimension is given, C will determine the dimension from the number of elements in the initialization list.*/int some_code[] = {1,2,3,4,5};int matrix[2][4] = { {1,2,3,4}, {10,20,30,40} }// stringschar name[] = {'S','a','m','\0'};// orchar name[] = "Sam";// orchar name[50] = "Sam";// which is equivalent tochar name[50];strcpy(name, "Sam");
Types of intergers
The long
qualifier informs C that we wish to allocate extra storage for the integer. If we are going to use small numbers and wish to reduce storage, we use the qualifier short
.
- int
- long
- short
- signed
- unsigned
signed long int number;
All int declarations default to signed. On the contrary, char declarations do not default to signed, the default is compiler dependent.
Types of Floats
- float
- double
- long double
The qualifier long double denotes extended precision. On some systems, this is the same as double; on others, it offers additional precision. All types of floating -point numbers are always signed.
Hexadecimal and Octal Constants
- Practical C Programming - Chapter 5
- note <practical c programming> chapter 5
- <Practical C Programming> chapter 4
- Practical C Programming - Chapter 2
- Practical C Programming - Chapter 3
- Practical C Programming - Chapter 4
- Practical C Programming - Chapter 9
- Practical C Programming - Chapter 10
- Practical Programming in C
- 6.087 Practical Programming in C, lec6
- 6.087 Practical Programming in C, lec7
- 6.087 Practical Programming in C, lec8
- 6.087 Practical Programming in C, lec9
- 6.087 Practical Programming in C, lec10
- 6.087 Practical Programming in C, lec11
- 6.087 Practical Programming in C, lec12
- 6.087 Practical Programming in C, lec13
- 6.087 Practical Programming in C, lec14
- qmake 命令行参数
- No.2 (code 2.0)UIButton
- linux程序设计第2章末程序1
- poj 3260 The Fewest Coins
- vi编辑只读文档无法保存的解决办法
- Practical C Programming - Chapter 5
- Special的——动态规划入门教程
- Android完全退出应用程序
- [总结]FFMPEG视音频编解码零基础学习方法
- Java中Object类的clone()(克隆)方法
- 《Effective C++》资源管理:条款26-条款27
- No.2 (code 2.1)简易计算器(实现了1个数的连续加减乘除运算)
- Fedora无法播放视频
- 状态模式