Note from C primer plus

Notes after reading C primer plus

Data and C##

Definitions:

**Byte: **The C language defines a byte to be the number of bits used by type char , so one can have a system with a 16-bit or 32-bit byte and char type.
**Char: **C guarantees that the char type is large enough to store the basic character set for the system on which C is implemented.
**Size of Integer Types: **C guarantees only that short is no longer than int and that long is no shorter than int. The idea is to fit the types to the machine.

Suffix
l/L for long type constant
ll/LL for long long type(if the system supports)
Adding u/U before or after l/L indicates that the integer is unsigned.
Suffixes above are for integers, below for floating-point numbers
f/F for float type
l/L for double type

Header File Referred To
limits.h
stdint.h
inttype.h

Portable Types: stdint.h and inttype.h
1.exact-width integer types
Take int32_t as an example.
The type int32_t is an alias for a certain type in the system. The compiler will do it for you.

The header file(stdint.h) on a system that uses a 32-bit int could define int32_t as an alias for int . A different system, one with a 16-bit int and a 32-bit long , could define the same name, int32_t , as an alias for int . Then, when you write a program using int32_t as a type and include the stdint.h header file, the compiler will substitute int or long for the type in a manner appropriate for your particular system.

2.minimum width types
These are designed to save the space. Like the exact-width integer types, these are alias, too.

This set of names promises the type is at least big enough to meet the specification and that no other type that can do the job is smaller. For example, int_least8_t will be an alias for the smallest available type that can hold an 8-bit signed integer value.

3.fatest minimum width types
A example will surffice.

For example, the int_fast8_t will be defined as an alternative name for the integer type on your system that allows the fastest calculations for 8-bit signed values.

Plus: In some cases, only the biggest possible integer type in a system will do. intmax_t and uintmax_t will help you.
C99 and C11 provide assistance with the input and output of these types, which is defined in the header file inttypes.h.Notice that inttype.h includes the header file stdint.h.

#include <stdio.h>
#include <inttypes.h> // supports portable types
int main(void)
{
    int32_t me32; // me32 a 32-bit signed variable
    me32 = 45933945;
    printf("First, assume int32_t is int: ");
    printf("me32 = %d\n", me32);
    printf("Next, let's not make any assumptions.\n");
    printf("Instead, use a \"macro\" from inttypes.h: ");
    printf("me32 = %" PRId32 "\n", me32); //PRId32 is defined
                                         //as a string
    return 0;
}

Question: Why Portable Types?

The same type name doesn't necessarily mean the same thing on different systems, which is not such a good thing.

Floating-point numbers
The constant 4e16 is a double type number, though it can fit in the type long.

new expression of floating-point constants:

Since C99, C has a new format for expressing floating-point constants. It uses a hexadecimal prefix ( 0x or 0X ) with hexadecimal digits, a p or P instead of e or E , and an exponent that is a power of 2 instead of a power of 10. Here’s what such a number might look like:
0xa.1fp10
The a is 10 in hex, the .1f is 1/16th plus 15/256th ( f is 15 in hex), and the p10 is 2^10 , or
1024, making the complete value (10 + 1/16 + 15/256) x 1024, or 10364.0 in base 10 notation.
Not all C compilers have added support for this feature.

Character Strings and Formatted Input/Output

Operators, Expressions, and statements

Assignment:The left side of the = sign must refer to a storage location.
sizeof:C says that sizeof returns a value of type size_t .
++:Be careful to use it.
side efects and sequence point:

Now for a little more C terminology: A side effect is the modification of a data object or file. For instance, the side effect of the statement
states = 50;
is to set the states variable to 50 . Side effect? This looks more like the main intent! From the standpoint of C, however, the main intent is evaluating expressions. Show C the expression 4 + 6 , and C evaluates it to 10. Show it the expression states = 50 , and C evaluates it to 50. Evaluating that expression has the side effect of changing the states variable to 50 . The increment and decrement operators, like the assignment operator, have side effects and are used primarily because of their side effects.
Similarly, when you call the printf() function, the fact that it displays information is a side effect. (The value of printf() , recall, is the number of items displayed.)

ctype.h:This header file contains some function prototypes to analyzing characters.
alternate spellings : iso646.h:
Note that not every language include the symbols used in C. This header file allows you to use and in stead of &&, or instead of ||.
order of evaluation:
Remember the order of evaluation in C is from left to right.

if (expression1 && expression2)
{
    //do something
}

if the expression1 is false the program won't evaluate the expression2. Things go similiarly with ||.
they are not functions!

Typically, getchar() and putchar() are not true functions, but are defined using preprocessor macros, a topic we’ll cover in Chapter 16 , “The C Preprocessor and the C Library.”

buffers:
For buffered input:
The characters you type into the computer is stored in a buffer(a temporary storage) until you press the ENTER key. This enables you to edit the characters before you send them to the program.Functions like scanf() and printf() are buffered input functions.
For unbuffered input:
They are preferred for some interactive programs. Functions like _getch() and getche() are unbuffered input functions. _getch() is unechoed function and getche() is echoed function.
files, stream, and keyboard input
When a program reads a file, it doesn't read the file directly. It reads stream.

Conceptually, the C program deals with a stream instead of directly with a file. A stream is an idealized flow of data to which the actual input or output is mapped. That means various kinds of input with differing properties are represented by streams with more uniform properties. The process of opening a file then becomes one of associating a stream with the file, and reading and writing take place via the stream.

Therefore the differences between the ways of various system can be handled by the specific implementations of C. You can contrate on the code itself.
redirection
Typically, the input is from the keyboard and the output is to the screen. But as mentioned above, the program actually reads the stream, not files themselve and not the keyboard itself. So we can make a program read from a file and output to the screen.
operator: < > >> |

echo_eof < words

The < symbol is a Unix and Linux and DOS/Windows redirection operator. It causes the words file to be associated with the stdin stream, channeling the file contents into the echo_eof program.

The operator > does the opposite thing.
Of course you can conbine these two operators like this:

echo_eof < mywords > savewords;
echo_eof > savewords < mywords;//the order doesn't matter

For >> and |

Unix, Linux, and Windows/DOS also feature the >> operator, which enables you to add data to the end of an existing file, and the pipe operator ( | ), which enables you to connect the output of one program to the input of a second program. See a Unix book, such as UNIX Primer Plus, Third Edition (Wilson, Pierce, and Wessler; Sams Publishing), for more information on all these operators.

Note that these operators are all used in command line or terminal.

C Control Statements:Looping

C Control Statements:Branching and Jumps

Character Input/Output and Input Validation

printf()(Details)
The convertion specification %zd is used to print variables of size_t type.
The * modifier with scanf() and printf() :
For printf()The * is used when you don't want to define the field width.

printf("%*.*f", width, precision, fnum);

The * serves quite a different purpose for scanf()

scanf("%*d %*d %d", &n);
//input:1 2 3
//n = 3, the number 1 and 2 are skipped

scanf()
The return value of scanf() is the number of items it successfully reads.

Functions

compile programs with 2 or more source code files
You can use cc /gcc or other compilers
To compile two files to get one program:
gcc file1.c file2.c
You can also do this:
gcc file1.c ofile2.o
Then the file1.c is conpiled and linked with ofile2.o

Array and Pointers

Varieble-Length Array

They need to have the automatic storage
class, which means they are declared either in a function without using the static or extern
storage class modifiers ( Chapter 12 ) or as function parameters.

To declare a function with VLA arguments

int sum2d(int rows, int cols, int ar[rows][cols]);// ar a VLA
int sum2d(int ar[rows][cols], int rows, int cols); // invalid order
int sum2d(int, int, int ar[*][*]);//valid

The C99/C11 standard says you can omit names from the prototype; but in that case, you need to replace the omitted dimensions with asterisks.
Literal
Literals are constants that aren’t symbolic. For example, 5 is a type int literal, 81.3 is a type double literal, 'Y' is a type char literal, and "elephant" is a string literal.
Compound literal
For arrays, a compound literal looks like an array initialization list preceded by a type name that is enclosed in parentheses.

(int [2]){10, 20}; // a compound literal
(int []){50, 20, 90};
// a compound literal with 3 elements

string literal

Character string constants are placed in the static storage class, which means that if you use a string constant in a function, the string is stored just once and lasts for the duration of the program, even if the function is called several times. The entire quoted phrase acts as a pointer to where the string is stored. This action is analogous to the name of an array acting as a pointer to the array’s location.

/* strptr.c -- strings as pointers */
#include <stdio.h>
int main(void)
{
    printf("%s, %p, %c\n", "We", "are", *"space farers");
    return 0;
}
-------
output:
We, 0x100000f61, s

If the same string literal is used in a program more than once, the compiler has the freedom to store the literal used in more than one place in one or more places. When you declare a string array like this
char nama[] = "charles";
the literal "charles" is stored twice, first the literal is stored in the static storage, and then the program makes a copy of it and stores it in the storage which the array occupies.
Change a literal
What will happen if you do this?

char * p = "hello";
p[1] = 'k';

Your compiler may allow you to do this but under current C standard, the behavior of such action is undefined. Such a statement could, for example, lead to memory access error.(Reason can be found in the "freedom" mentioned before)

Character Strings and String Functions

Storage Classes, Linkage, and Memory Management

File Input/Output

Structures and Other Data Forms

Bits Fidding

The C preprocessor and the C library

Advanced Data Representation

Functions

Versions

1.The C99 and C11 allow you to make the name of an identifier as long as you want, but the compiler will only the first 63 words as significant.
2.The _Bool type is a C99 addition.(If you find that you can use bool type in your program, that's from c++)

C99 also provides for a stdbool.h header file. This header file makes bool an alias for _Bool
and defines true and false as symbolic constants for the values 1 and 0. Including this header
file allows you to write code that is compatible with C++, which defines bool , true , and false
as keywords.

Appendix

Precedence of Operators
. operator has higher precedence than & operator

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容