identifier
Identifiers must be declared and can be variables, functions, or other entities.
Is Int an identifier?
No, int is a C language keyword and is not named arbitrarily.
C language keywords are as follows:
constant
Does not need to be declared and cannot be assigned or changed.
printf function
printf is composed of print printing and format format, placeholder printing
The definition is written in <stdio.h>.
integer data type
The reason for defining different integer types: the memory occupied is different, indicating that the data range is different.
char, short, int, long, longlong occupy byte and numerical range:
The C language does not specify the size range of data types, and the specific implementation is left to the compiler and platform to decide.
sizeof (measures the byte size occupied by the entity)
The larger the occupied bytes, the larger the range.
Do not use the highest bit as the sign bit unsigned.
1~5 to read. floating point type
floating point type float
The %d placeholder is used for integer types, and the %f placeholder is used for floating point types.
float can represent at least 6 significant digits.
Floating point type double
A type with higher precision than float, double precision floating point type double.
Bytes occupied by floating point type
For floating point types, the higher the precision, the larger the range, and the larger the bytes it occupies.
float 4; double 8 bytes.
variable constant
Variable: Something that can change and has the potential to change.
Constant: something that does not change and cannot be changed.
Declare variables:
Identifier: A self-named flag that represents the name of a variable, function, or other entity.
Identifier naming rules: 1. It can only consist of uppercase and lowercase English letters, numbers or underscores.
2. The identifier cannot start with a number.
3. The identifier cannot be an existing keyword.
Keywords: C language standards stipulate that they have special meanings and uses and can be used directly in programs. For example: short, int, long, float, double.
Declaring variable formula: [data type + identifier name + semicolon]. Declare it in use first! ! !
Variable initialization and assignment methods:
1. After the variable is declared, it is initialized immediately.
int a=100; printf("%d\n",a);
2. Variables are declared first before variable assignment.
int a; a=100; printf("%d\n",a);
Note: Variables can be assigned multiple times, but cannot be initialized multiple times.
constant
Literal constants do not need to be declared, and the compiler can determine the type.
Symbolic constants:
#define symbolic constant value
Character type variables and constants
Character constants are enclosed in single quotes.
For example: 'a'
Placeholder
Integer type %d Floating point type %f Character type %c
Character type takes up space:
Character variable: char
There is a one-to-one mapping relationship between characters and numerical values, which is called the American Standard Code for Information Interchange or ASCII code.
Character types require only one byte to be stored properly.
The character type is the integer type Character type only takes up one byte The character type is named char
\n is a newline character, \n means to end printing on one line and start printing on the next line.
example:
Define a character variable letter and initialize it to the uppercase letter A. Through the relationship in ASCII, the uppercase letter A is changed into a lowercase letter A, and the lowercase letter A is printed.
#include <stdio.h> int main () { char letter ='A'; letter =letter+32; printf("letter =%c",letter); return 0; }
Value 0: used to identify the end of the string.
Escape character: \
\numeric value (octal): escape character printf("hello\0world"); print hello printf("\110\145\154\154\157"); Also print hello printf("hello\12world"); print hello world The effect is the same as\n
printf
Unsigned integer placeholder: %u
Accuracy
Minimum field width
Use minimum field width If flag 0 is specified, the minimum width will be padded with 0s.
6~10
scanf (for input)
_CRT_SECURE_NO_WARNINGS
scanf converts the input string according to the corresponding conversion specification.
The binary data after conversion will be stored in the variable addresses of subsequent parameters.
input string #include <stdio.h> int main() { char str[10]; scanf("%s",str); printf("%s",str); return 0; }
Enter characters #include <stdio.h> int main() { char c; scanf("%c",&c); printf("%d %c\n",c,c); return 0; }
Operator 12
pointer
Get address operator &
&data object Get the first address of the data object and the required storage space
pointer type
Target data type* variable name declaration pointer
The value of pointer type is the first address of the target data object.
Where is the size of the data object stored?
The first address can be copied, but the pointer type changes, causing the data length to change, so it cannot be copied correctly.
The pointer type stores the first address of the target data object through a value, and marks the space size of the target data object through the type itself.
Value operator *
*pointer Find the target data object based on the first address and space size stored in the pointer.
The size of bytes occupied by the pointer is also related to the compiler or compilation configuration.
Pointer access to array
The first element gets the first address of the array.
Value operators have higher precedence than arithmetic operators.
Array name gets the first address of the array.
When an array name appears in an expression, the array name is converted into a pointer to the first element of the array.
For example: arr+1 is equivalent to &arr[0]+1 Exceptions: 1. When using sizeof on the array name 2. When using the address operator & on the array name
The subscript operator eventually expands to pointer form.
Pointers are passed as parameters
Pointers to pointers (multilevel pointers)
The pointer of int * data object is called [secondary pointer]
Multidimensional Arrays
Pointer array int* pB[10] Array pointer target type (*variable name)[number of elements]
Moving and taking values of array pointers
35
String processing functions
#include "string.h"
strlen: Get the length of the string in the character array
strcat: String splicing function, splicing the source string to the target string
strcpy: String copy function, copies the source string to the target string
strcmp: String comparison function, compares two strings, returns 0 if consistent, 1, -1 if different
37
Pointer implements string processing function
#include <stdio.h> size_t mstrlen(const char *str) { if(str=NULL) { return 0; } size_t len =0; while(*str !='\0') { len++; str++; } return len; } int main() { size_t len; len =mstrlen(NULL); printf("%d\n",len); len =mstrlen(""); printf("%d\n",len); len =mstrlen("HELLO"); printf("%d\n",len); return 0; }
#include "stdio.h" char * mstrcat(char * destination ,const char * source) { if(destination == NULL) { return NULL; } if(source == NULL) { return destination; } char *ret =destination;//Save the first address of the string while (*destination !='\0') { destination++; } while(*source !='\0') { *destination =*source;//Append source to destination. destination++; source++; } *destination ='\0'; return ret; }
int mstrcmp(const char *str1,const char *str2) { if(str1==NULL && str2 == NULL) { return 0; } if(str1 !=NULL && str2 ==NULL) { return 1; } if(str1 == NULL && str2!==NULL) { return -1; } int ret =0; while (1) { if(*str1 !=*str2) { if(*str1 > *str2) { ret = 1; } else { ret =-1; } break; } else { if(*str1 == '\0' && *str2 == '\0') { break; } str1++; str2++; } } return ret; }
Getting to know structured data
pointer to structure
union
Structures and unions
Memory alignment!
Joint sharing.
enumeration enum
The enumeration will start from 0 and increase sequentially.
If you want to increase from 1 enum msgType{ eInteger=1; eFloat, eString };
scope of identifier
The inner scope will override the outer scope.
preprocessing directives
Cancel macro definition #define NUM 1 #undef NUM #define NUM 3
typedef keyword
Define data type aliases
often used in structures
typedef does not create any new types, it just adds a convenient alias for an existing type
The difference between typedef and #define
#define can set an alias for the value, but typedef cannot For example: #define PI 3.1415926 #define is processed by the precompiler and the replacement code is modified. Typedef is not affected by preprocessing and is processed by the compiler at compile time. #define can also define aliases for types, but in some cases, it is more appropriate to use typedef For example: typedef char *STRING STRING name1,name2;
You do not need to define the alias of the integer type yourself. The compiler will set the corresponding alias according to the integer range of the platform. Header file: stdint.h
How does printf's conversion specification ensure portability?
Header file inttype.h
Branch structure in preprocessing
#if constant expression Before compilation, it is processed by the processor. According to the branch direction, the code that needs to go to the branch is retained and the code that has been skipped branch is deleted. #if #else #elif #ifdef #ifndef
#ifdef #ifndef
You can also use #if defined (macro) or #elif defined (macro)
#include
#include <folder> Search for files in the include directory of the compiler. < >The compiler comes with files in the include directory of the compiler. #include "filename" First search for files in the current directory, then search for files in the compiler's include directory.
Multiple file code
storage class
Any variable declared within a code block belongs to the automatic storage category.
Indicates that a variable belongs to the automatic storage category auto
The life span of n ---- the period from creation to destruction of the data object. The lifetime of the data object.
The scope of n - the identifier refers to the area where the relationship exists for the data object, which is an association relationship.
Automatic variables have block scope and lifetime. ---Local variables
File operations
fopen