Recalling the C language learning first round: 1 basis Grammar

C is a universal language, for procedural computer program design language.

C language was originally used for system development work, especially procedures of the operating system. Since the C language code generated by the speed written in assembly language code is almost the same speed, so as the system using C language development language. Examples of the application of the following characteristics may also be seen as a system developed in C language:

  • operating system
  • Compiler
  • Assembler
  • text editor
  • printer
  • Network Drive
  • Modern procedures
  • database
  • Language interpreter
  • Entity tool

 

Hello, world code analysis

#include <stdio.h>

int main{
   /* 我的第一个 C 程序 */
   printf("Hello, World! \n");
   
   return 0;
}
  • The first line of the program  #include <stdio.h>  is the command preprocessor telling the compiler before the actual C compiler to include stdio.h (stdio header) header. If you do not find  stdio.h  header file, the compiler error occurs.
  • All C language programs need to include  main () main function. The code from the  () main  function begins execution. int is the return type
  • / * ... * /  for explanatory notes.
  • printf ()  for formatting the output to the screen. printf ()  function in the  "stdio.h"  declaration header file.
  • return 0;  statement is used to indicate to exit the program and then will be value type must match the type declaration before the main, otherwise compilation errors.

 

Compile & C program execution

  1. Text editor to write the code above.
  2. Save the file as  hello.c, .c are C source code files .
  3. Open a command prompt, enter the directory to save the file.
  4. Type  gcc hello.c, compile the code.
  5. Generate  a.out  executable file, type  a.out  to execute the program.
  6. Printout on the screen "the Hello World" .

Single C source file compilation execution command examples:

gcc hello.c
./a.out
输出:Hello, World!

A plurality of source files are compiled C Run Example:

$ gcc test1.c test2.c -o main.out
$ ./main.out

 

C token (Tokens)

C program composed of various tokens, a token may be keywords, identifiers, constants, a string value, or a symbol.

 

semicolon

In a C program, the semicolon is a statement terminator. That is, each statement must end with a semicolon. It indicates the end of a logical entity.

 

Note

Single-line comments: //

Multi-line comments: / * * / comment this format can be single or multiple rows.

 

Identifier

C is an identifier used to identify the variable, function, or any other user-defined name of the item. Identified by letters, digits, and underscores, and can only begin with the letters and underscore. C is case-sensitive programming language.

 

Keyword

These words can not be retained as a constant and variable names or other identifier names.

Keyword Explanation
auto Automatic variable declaration                                                                           
break Out of the current cycle
case A switch statement branch
char Declaration character variable or function return value type
const Read-only variable declaration
continue End the current cycle, the next cycle start
default Switch statement "other" branch
do Loop circulation statement
double Double precision floating point variable declaration or function return value type
else The negative branch of conditional statements (and if used in conjunction)
enum Declare enumerated types
external Declaring a variable or function is defined in another file or elsewhere in this document
float Declared float variable or function return value type
for Iterative statements
goto Unconditional jump statement
if Conditional statements
int Declare integer variable or function
long Long declared variable or function return value type
register Declare register variables
return Subroutine return statement (can take parameters, or without arguments)
short Declare short integer variable or function
signed Statement signed type variable or function
sizeof Computing data type or variable length (i.e., number of bytes occupied)
static Declare a static variable
struct Declaration of structure type
switch A switch statement
typedef Alias ​​for the data type
unsigned Declare an unsigned type variable or function
union The union type declaration
void Function has no return value declared or no parameter declaration untyped pointer
volatile Explanatory variables during program execution may be implicitly changed
while                            Loops of cycling conditions

Newly added keywords 

  _Bool        _Complex        _Imaginary   inline       restric      
  _Alignas   _Alignof   _Atomic     _Generic     _Noreturn  
  _Static_assert   _Thread_local  

 

C in space

The line contains only spaces, blank lines are called, will be completely ignored by the compiler

Various parts of the space-delimited statements, so the compiler can identify the statement an element (such as int) where it ends. To enhance readability between the identifier of expression, operators and the like to add a space

 

C data type

Type of the variable determines the variable storage space occupied, and how to interpret the bit pattern stored. C is divided into the following types:

No. Type and Description
1 Basic types:
arithmetic types, including two types: type integer and floating-point type.                                                        
2 Enumerated types:
arithmetic type, defined in the program is used only to impart a discrete variable is a certain integer value.
3 void type:
type specifier  void  indicates that the value is not available.
4 派生类型:
它们包括:指针类型、数组类型、结构类型、共用体类型和函数类型。

数组类型和结构类型统称为聚合类型。函数的类型指的是函数返回值的类型。

整数类型和浮点类型

标准整数类型的存储大小和值范围的细节(32位和64位):

float,单精度浮点值。单精度是这样的格式,1位符号,8位指数,23位小数。

double,双精度浮点值。双精度是1位符号,11位指数,52位小数。

 使用sizeof 运算符。表达式 sizeof(type) 得到对象或类型的存储字节大小。

示例:

#include <stdio.h>
#include <float.h>
#include <limits.h>
 
int main()
{  //%lu 为 32 位无符号整数
   printf("int 存储大小 : %lu \n", sizeof(int));
   printf("float 存储最大字节数 : %lu \n", sizeof(float));
   //%E 为以指数形式输出单、双精度实数
   printf("float 最小值: %E\n", FLT_MIN );
   printf("float 最大值: %E\n", FLT_MAX );
   printf("精度值: %d\n", FLT_DIG );
 
   return 0;
}

void 类型

void 类型指定没有可用的值,表示类型的缺失。它通常用于以下三种情况下:

序号 类型与描述
1   函数返回为空
C 中有各种函数都不返回值,或者您可以说它们返回空。不返回值的函数的返回类型为空。例如 void exit (int status);
2 函数参数为空
C 中有各种函数不接受任何参数。不带参数的函数可以接受一个 void。例如 int rand(void);
3 指针指向 void
类型为 void * 的指针代表对象的地址,而不是类型。例如,内存分配函数 void *malloc( size_t size ); 返回指向 void 的指针,可以转换为任何数据类型。

类型转换

1、数据类型转换:C 语言中如果一个表达式中含有不同类型的常量和变量,在计算时,会将它们自动转换为同一种类型;在 C 语言中也可以对数据类型进行强制转换;

2、自动转换规则:

  •  a)浮点数赋给整型,该浮点数小数被舍去;
  •  b)整数赋给浮点型,数值不变,但是被存储到相应的浮点型变量中;

3、强制类型转换形式: (类型说明符)(表达式)

 

C变量和常量

变量其实只不过是程序可操作的存储区的名称。常量是指程序在运行时其值不能改变的量。常量不占内存,在程序运行时它作为操作对象直接出现在运算器的各种寄存器中,又称作字面量。变量是指在程序运行时其值可以改变的量。变量的功能就是存储数据。标识符应做到‘顾名思义’。

变量定义

语法:

//仅定义
type variable_list;
//或定义并初始化(指定一个初始值)
type variable_name = value;

对于不带初始化值的定义:带有静态存储持续时间的变量会被隐式初始化为 NULL(所有字节的值都是 0),其他所有变量的初始值是未定义的。

变量声明

变量的声明有两种情况:

  • 1、一种是需要建立存储空间的。例如:int a 在声明的时候就已经建立了存储空间。
  • 2、另一种是不需要建立存储空间的,通过使用extern关键字声明变量名而不定义它。 例如:extern int a 其中变量 a 可以在别的文件中定义的。
  • 除非有extern关键字,否则都是变量的声明加定义。

若要一个源文件中引用另外一个源文件中定义的变量,我们只需在需要引用的文件中将变量加上 extern 关键字的声明。

整数常量用0x 或 0X 表示十六进制,0 表示八进制,不带前缀则默认表示十进制。后缀是 U 和 L 的组合,U 表示无符号整数(unsigned),L 表示长整数(long)。后缀可以是大写,也可以是小写,U 和 L 的顺序任意。

浮点常量由整数部分、小数点、小数部分和指数部分组成。

字符常量是括在单引号中,例如,'x' 可以存储在 char 类型的简单变量中。

字符串字面值或常量是括在双引号 "" 中的。一个字符串包含类似于字符常量的字符:普通的字符、转义序列和通用的字符。使用空格做分隔符,把一个很长的字符串常量进行分行。下列字符串字面值是显示相同的:字符串字面值或常量是括在双引号 "" 中的。一个字符串包含类似于字符常量的字符:普通的字符、转义序列和通用的字符。使用空格做分隔符,把一个很长的字符串常量进行分行。下列字符串字面值是显示相同的
:

"hello, dear"

"hello, \
dear"

"hello, " "d" "ear"

常量的定义

C 中,有两种简单的定义常量的方式:

  1. 使用 #define 预处理器。 #define identifier value;
  2. 使用 const 关键字。: const type variable = value;

常量常常更应该定义为大写字母形式。

发布了161 篇原创文章 · 获赞 90 · 访问量 5万+

Guess you like

Origin blog.csdn.net/qq_42415326/article/details/104017628