C Language notes, C Language

Source: Internet
Author: User
Tags mathematical functions

C Language notes, C Language

C Language notes

Basic knowledge

Serial number

Type and description

1

Basic Type:

They are arithmetic types, including integer and floating point types.

2

Enumeration type:

It is also an arithmetic type. It is used to define variables that can only use some integer values. When used, the programmer needs to use the eumn keyword to declare the definition.

3

Void type:

Used for a function to specify the return value or parameter of the function. A compilation error occurs when it acts on the variable.

4

Derived type:

Including: pointer type, array type, structure type, consortium type, Function Type

Supplement: 1. The function type refers to the type of the function return value. The array type and structure type are collectively referred to as the gathering type.

2. Apart from the basic types, other types are the custom types that programmers use related keywords or constructed through the basic data types.

 

 

Type reference

 

Integer type

Char, int, short, long

Floating Point Type

Float, double, long double

Enumeration type

Enum typeName {valueName1, valueName2, valueName3 ,......};

Void type

 

Pointer type

Type * name;

Array type

Type name [count];

Structure Type

Struct name {merber1; merber2; merber3 };

Consortium type

Union name {merber1; merber };

Supplement: If you want to describe the symbols of the integer type, add signed/unsigned before the type.

Reference connection:

Enumeration type: http://c.biancheng.net/cpp/html/99.html

Integer, floating point type: http://www.runoob.com/cprogramming/c-data-types.html

Consortium type: http://www.runoob.com/cprogramming/c-unions.html

Struct type: http://www.runoob.com/cprogramming/c-structures.html

Example:

# Include <stdio. h>

# Include <string. h>

Enum week {Mon = 'M', Tues, Wed, Thurs, Fri, Sat, Sun}; // enumeration, the value can only be an integer

Struct stu {char name [8];

Char sex;

Int age;

}; // Struct

Union school {

Char name [20];

Int peopleNumber;

Int phone;

Week day;

}; // Consortium. Only one member can exist at the same time.

Int main (void ){

Int I = 1;

Int * P; // pointer

P = & I;

Union school mySchool;

Struct stu student;

Char name [10] = "john ";

Int number [3] = {1, 2, 3 };

 

 

Strcpy_s (student. name, "john ");

Student. sex = 'M ';

Student. age = 20;

 

Printf ("name: % s, sex: % c, age: % d \ n", student. name, student. sex, student. age );

Printf ("I = % d, p = % d, name = % s \ n", I, * P, name );

 

Strcpy_s (mySchool. name, "***** \ n ");

Printf ("mySchool name: % s \ n", mySchool. name );

MySchool. Maid number = 2000;

Printf ("mySchool peopleNumber: % d \ n", mySchool. peopleNumber );

MySchool. day = Mon;

Printf_s ("day: % c", mySchool. day );

 

Getchar ();

Return 0;

}

 

Variable:

Declaration: dataType name;

In particular, the Declaration of array variables: dataType name [count];

Constant:

Declaration: # define name value // belongs to the preprocessing stage

Const dataType name = value; // belongs to the compilation stage

 

Operator

 

Arithmetic Operations

+,-, *,/, %, ++, And ,--

Relational operation

= ,! =,>, <,> =, <=

Logical operation

&, | ,!

Bitwise operation

&, |, ^, <,> ,~

Value assignment

=, + =,-=, * =,/=, % =, <=, >>=, & =, ^ =, | =

Miscellaneous operations

Sizeof (),*,&,? :

 

Judgment:

If (condition ){

Statement block;

}

Else if (condition ){

Statement block;

}

Else {

Statement block;

}

 

 

Switch (condition ){

Case constant expression: Statement block;

Case constant expression: Statement block;

Default: Statement block;

}

 

Loop:

1.

While (condition ){

Statement block;

}

 

Do {

Statement block;

} While (conditon );

2.

For (int I; I> 10; I ++ ){

Statement block;

}

 

Loop Control:

Break: jump out of the current loop and stop executing

Continue: jump out of the current loop and continue

 

Command

Description

# Include

Contains a source code file

# Define

Definition macro

# Undef

Cancel a defined macro

# Ifdef

Judge the macro definition. If the macro has been defined, return the true

# Ifndef

Judge the macro definition. If the macro is not defined, return the true

# If

If the given condition is true, compile the following code

# Else

 

# Elif

 

# Endif

 

# Error

When a standard error occurs, an error message is output.

# Pragma

Use the standardization method to publish special commands to the Compiler

Supplement: # pragma comment (linker, "/entry: main2") can be used to change the entry function

Definition:

Return_type function_name (parameter list ){

Statement block;

}

Usage:

Function_name (parameter list );

Supplement: For the source code of an executable file, a main function must always exist to allow the compiler to compile correctly.

The default main function, but not necessarily the main function, can be changed by setting compiler parameters.

Advanced

Standard Library:

It should be known that the standard library is not part of the C language itself, but standardizes some very common functions, so that programmers can better develop and transplant programs. In addition, some system call encapsulation is provided, so that programmers do not have to consider the relationship between the program and the system and the hardware. As long as the relevant library functions are called, they can interact with the hardware.

The standard library contains a large number of macro definitions and usageTypedefThe new type defined by the keyword and the declaration and implementation of the function.

 

Standard Library name

Description

Common Library functions

<Stdio. h>

Standard Input/Output: Provides I/O operations (file read/write and screen input/output) based on common hardware ).

Fclose, fopen, fseek, fwrite, printf,

Scanf

<Stdlib. h>

Defines four variable types, some macros, and a variety of common tool Functions

NULL, atof (string converted to a floating point number), atoi, free, malloc, abort,

Exit, getenv,

<String. h>

Related to the Operation string

Memcmp (compare the first n Bytes of str1 with str2), strcat, strcpy, strlen,

<Stdarg. h>

It is related to the function parameters. When the number of parameters is variable, the parameters in the function are obtained.

 

<Math. h>

Define various mathematical functions

Exp (x): returns the x power of e.

Pow (x, y): returns the y Power of x.

Fabs (x): returns the absolute value of x.

<Time. h>

   

<Stddef. h>

Define various variable types and macros

NULL,

<Assert. h>

Provides a macro function for diagnosis.

Unique library function: assert

<Ctype. h>

Provides functions to test and map characters

Islower, isupper, isxdigit, tolower, toupper...

<Errno. h>

Some macros related to errors are defined.

Errno

<Float. h>

Contains a set of platform constants related to floating point values.

FLT_MAX: The maximum floating point number that the platform can express. FLT_MIN: The minimum floating point number that the platform can express.

<Limits. h>

The macro defined in it limits the values of various variable types.

CHAR_BIT: defines the number of bits in one byte.

<Locale. h>

Defines the settings of a specific region, such as the date format and currency symbol.

LC_CTYPE: affects the character functions.

Setlocale: set or read the hell Information

<Setjmp. h>

The long jump in the program is not limited to the function.

The biggest use of setjmp/longjmp is error recovery, similar to try... catch...

<Signal. h>

Provides macros and functions related to signals

Signal: Set a function to process signals.

Raise (): Generate Signal

<Raise. h>

Used to generate signals

 

 

Shared Library and static library, static call and dynamic call, static link and dynamic link

Static libraries and dynamic libraries are compiled in different ways. Is the Compilation part.

Static links and Dynamic Links belong to links, which tell the linker what type of libraries are used, static libraries are static links, and dynamic libraries are dynamic links.

# Pragma comment (lib, ".. \ debug \ libTest. lib") // notify the connector to use the static library for static Link

Static and Dynamic calls are part of the dynamic library and are about how to load the dynamic library to the memory.

During static calls, the operating system loader first creates a virtual address space for the process, and then maps the executable module to the address space of the process, then the loader checks the import segment of the executable module, tries to locate the required DLL and map them to the address space of the process. (Run the executable module in Chapter 19 of the fifth edition of windows core programming ). # The pragma comment (lib, "dllTest. lib") macro tells the compiler to use static calls.

During dynamic callingLoadLibraryThis API maps the DLL to the address space of the process, and then the programmer obtains the address of the exported function through the GetProcAddress API (before using the returned function pointer to call the function, need to transform to the correct type that matches the function signature) to call the required function.

External functions:

Functions provided by other source files or libraries. When a programmer uses the header file provided by him or uses extern or extern "c" _ declspec (dllimport) for function declaration, indicating that the function is used to external files.

Compile:

Link:

Software running process:

Relationship between operating system and software:

System call:

Relationship between lower-layer c and application-layer c:

 

Relationship between system calls and standard libraries:

System calling is a set of Apis provided by the operating system. These API functions can communicate with the hardware through the system. These API functions need to be obtained by the operating system developers. The standard library of languages is the Standardization Organization of languages. In order to allow programmers to make a small number of program modifications on various platforms or systems, they can compile and run a series of functions. That is to say, the standard library is out of the operating system or platform and belongs to the abstract layer and has platform independence.

However, the APIs (called by the System) provided by each operating system are not the same, that is, the function name, function parameters, and return value types are different.

But in order for the program to run on a specific system, it is necessary to implement the library functions (Standard Functions) in the standard library on the system ). This section is provided by the compiler, that is, the compiler developer will provide the standard library part of the language that the compiler can compile (the implementation of the standard library ). They compile the implementation code of the standard library by reading the API documentation provided by operating system developers.

Now we can understand that programmers can get rid of the operating system by calling the corresponding library functions and write their own portable program code (the standard library eliminates platform differences, provides programmers with a unified coding method for user experience to facilitate program transplantation ).

Library code parsing:

Supplement: the Development in vs2013 does not use pure c, but only the source code of c ++ is used. However, c ++ is a c upgrade and backward compatible with c. There is little difference in all aspects, but there are some differences in syntax.

Vc in windows:

Time. h

# Include <time. inl> // adopts the c ++ inline syntax.

 

Time. inl // inline

Static _ inline time_t _ CRTDECL time (time_t * _ Time) // 32-bit

{

Return _ time32 (_ Time );

}

 

Time. c // Implementation of library functions

# Include <time. h>

# Include <windows. h> // header file of the System Call provided by the operating system

_ Time32_t _ cdecl_ Time32(

_ Time32_t * timeptr

)

{

_ Time64_t tim;

FT nt_time;

 

GetSystemTimeAsFileTime (& (nt_time.ft_struct); // system call

 

Tim = (_ time64_t) (nt_time.ft_scalar-EPOCH_BIAS)/2017000i64 );

 

If (tim> (_ time64_t) (_ maxcompute time32_t ))

Tim = (_ time64_t) (-1 );

 

If (timeptr)

* Timeptr = (_ time32_t) (tim);/* store time if requested */

 

Return (_ time32_t) (tim );

}

We can see that the implementation of the warehouse picking function time actually calls the GetSystemTimeAsFileTime System Call in windows. h.

Supplement: Since library implementation is not a concern of application programmers, programmers only care about what is provided. HFile function declaration (that is, the interface provided by the standard library ), therefore, compiler developers can use superb technologies for various optimizations and Data Operations (that is, implementation of libraries in a way that we cannot understand ).Therefore, you do not need to care about the implementation of the database.

Glibc

 

Time. h

# Include <bits/time. h>

Extern time_t time (time_t * _ timer) _ THROW; // use the time provided by liunx

 

Time. c

Time_t

Time (timer)

Time_t * timer;

{

_ Set_errno (ENOSYS );

 

If (timer! = NULL)

* Timer = (time_t)-1;

Return (time_t)-1;

}

 

<Sys/time. h>

Extern int gettimeofday (struct timeval * _ restrict _ TV,

_ Timezone_ptr_t _ tz) _ THROW _ nonnull (1 ));

 

Due to limited capabilities, you cannot find specific function implementations. However, you can learn through the network that the implementation of the c standard library on liunx uses the time System Call provided by linux, function ing is used.

The system call to obtain the time on liunx is gettimeofday in # include <sys/time. h>.

Supplement: Generally, the operating system only provides a small part of system calls to consider the difficulty of implementation and the convenience of management, these system calls are generally implemented by a mix of C and assembly, and their interfaces are defined by C, while the specific implementation is assembly. The advantage of this is that the execution efficiency is high and, this greatly facilitates upper-layer calls.

Written at the end: a programming language is only an abstraction layer. To enable computers to recognize programs written in this language, a compiler or interpreter is required, converts a character-type source code file compiled by a programmer into a command that can be understood by a computer (cpu. Because computers are divided into many platforms and architectures (different operating systems and different CPUs), various compilers or interpreters must be compiled to implement the language of the platform, implement the syntax and semantics in the language, and provide the standard part set by the Language Standard Organization (which standard library and other programming aspects should be provided) (but not necessarily, you can also add or delete some syntaxes and libraries ). Therefore, the compiler or interpreter is the final boss, which has the right to decide the language implementation. To put it bluntly, a programmer is a hard worker working for a compiler or interpreter. In the end, the result of your work is still determined. Whatever the language standard, it's empty talk to say that the fucking language can be transplanted is not implemented. There is also that Rome was not built in one day, and now all the programs are made up of the initial cpu commands (people are not the prehistoric single cell slowly evolved ?).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.