C Primer Plus读书笔记 · 4&5

Chapter 2 Introducing C

#2019.7.15#

A Simple Example of C

The Example Explained

在这里插入图片描述
Pass 1: Quick Synopsis
Pass 2: Program Details

#include Directives and Header Files
#include<stdio.h>:

#include

  1. An example of preprocessor directive.
  2. It’s a cut-and -paste operation.

stdio.h file (standard input/output header)

  1. It contains info about I/O functions, such as printf().
  2. Header files help guide the compiler in putting your program together correctly.
    (The actual code of function is in a library file of precompiled code, not in a header file)

The main() Function

int main(void)

  1. A C program always begins execution with the function called main().
  2. The int is the main() function’s return type (return to the OS).
  3. The parentheses generally enclose info being passed along to the function(Nothing is passed along to the function main(), so there is a void in parentheses).

Other format:
main()
void main()
shouldn’t be used even if several compilers tolerate them.

Comments:
/*
*/
the end mark could not be omitted and it allows comments to spread to more than one line.

‘//’ welcome by C++ and Java, using to create comments that are confined to a single line.

Brace, Bodies and Blocks
{

}

  1. Using to delimit functions
  2. Also be used to gather statements within a functions into a unit or block.

#2019.7.16#

Declarations

int num;

  1. The semicolon ay the end of the line identified the lines as a C statement or instruction. The semicolon is part of the statement, not just a separator between statements as it is in Pascal.
  2. The word num in this example is an identifier—that is, a name you select for a variable, a function, or some other entity.
  3. Following C++, C99 and C11 let you place declarations about anywhere in the block as long as you declare them before you use them (traditionally, no other kind of statements are allowed to come before any of the declaration).

Data Type
integers, characters, and floating point.

Name Choice

  1. The compilers only consider the first 63 characters as significant.
  2. The characters at your disposal are lowercase letters, uppercase letters, digits, and the underscore(_). The first character must be a letter or an underscore.
  3. It is better to avoid the usage of one or two initial underscore, for them might be reserved as library identifiers or in the OS.
  4. C names are case sensitive

Four Good Reasons to Declare Variables

Assignment
num=1;
The assignment statement assigns a value from the right side to the left side.

The printf() Function

  1. What lies between the double quotation marks is the actual argument.
  2. \n, the newline character—an example of an
    escape sequence
  3. %d, % means it is a placeholder, and d
    means to print a decimal(base 10) integer.

Return Statement
Return 0;

The Structure of a Simple Program

Tips on Making Your Programs Readable

  1. Choosing meaningful variable names;
  2. use comments;
  3. using blank line to separate one conceptual section of a function
    from one another;
  4. use one line per statement
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值