Various knowledge of C (small synthesis)

Tip: After the article is written, the catalog can be generated automatically, how to generate the help document to the right

Formatting Characters in C

%c:Single Character
 
%d:Decimal integer(int)
 
%ld:Decimal integer(long)
 
%f:decimal(float)
 
%lf:decimal(double)
 
%o:Octal Number
 
%s:Character string(char)
 
%u:Unsigned decimal number(DWORD),Output in decimal digits unsigned Type data(Unsigned number)
 
%x:Output as unsigned hexadecimal integer,Or the address of the output string when%X Format output, that is, output in uppercase letters)

%e:Output real numbers in exponential form

2. In C&

&Get the address of the variable, for example: scanf("%d",&i),Obtain i Address, and then at this address, talk about storing the input.

3.%p in C

%p In p yes pointer(Abbreviation for Pointer)

printf For%p Normally output the value of a pointer as a hexadecimal integer with a prefix of 0 x. 

Difference between%p and%x

4. In C *

*Is a monocular operator that accesses the address variable represented by the pointer's value

For example:
int a = 0;
int* p; 
p = &a;
*p Namely a Value of 

5. Pointer in C

int* p  Intend p Inside is int The number of types, then because p Is the pointer, it will automatically base itself on p Values inside, to find addresses;

The value of a common variable is the actual value;

The value of a pointer variable is the address of a variable with an actual value

6. Storage of variables in C in memory

C Variables in are stored in memory as stacks, and if you look closely, you can see that each address is four adjacent to each other because int type
 Four bytes, if of another type, occupy different byte units of memory.
(However, the character sizes of the same variable type may be different in operating systems that do not use digits)
#include <stdio.h>
#include <stdlib.h>
#include <float.h>

int main(void)
{
    printf("Data type: char,Storage size:%d Bytes, Minimum:%hhd,Maximum:%hhd\n",
                sizeof(char), CHAR_MIN, CHAR_MAX);
    printf("Data type: unsigned char,Storage size:%d Bytes, Minimum:%hhu,Maximum:%hhu\n",
                sizeof(unsigned char), 0U, UCHAR_MAX);
    printf("Data type: short,Storage size:%d Bytes, Minimum:%hd,Maximum:%hd\n",
                sizeof(short), SHRT_MIN, SHRT_MAX);
    printf("Data type: unsigned short,Storage size:%d Bytes, Minimum:%hu,Maximum:%hu\n",
                sizeof(unsigned short), 0U, USHRT_MAX);
    printf("Data type: int,Storage size:%d Bytes, Minimum:%d,Maximum:%d\n",
                sizeof(int), INT_MIN, INT_MAX);
    printf("Data type: unsigned int,Storage size:%d Bytes, Minimum:%u,Maximum:%u\n",
                sizeof(unsigned int), 0U, UINT_MAX);
    printf("Data type: long,Storage size:%d Bytes, Minimum:%ld,Maximum:%ld\n",
                sizeof(long), LONG_MIN, LONG_MAX);
    printf("Data type: unsigned long,Storage size:%d Bytes, Minimum:%lu,Maximum:%lu\n",
                sizeof(unsigned long), 0LU, ULONG_MAX);
    printf("Data type: float,Storage size:%d Bytes, Minimum:%g,Maximum:%g\n",
                sizeof(float), FLT_MIN, FLT_MAX);
    printf("Data type: double,Storage size:%d Bytes, Minimum:%lg,Maximum:%lg\n",
                sizeof(double), DBL_MIN, DBL_MAX);
    printf("Data type: long long,Storage size:%d Bytes, Minimum:%lld,Maximum:%lld\n",
                sizeof(long long), LLONG_MIN, LLONG_MAX);
    printf("Data type: unsigned long long,Storage size:%d Bytes, Minimum:%llu,Maximum:%llu\n",
                sizeof(unsigned long long), 0LLU, ULLONG_MAX);
    printf("Data type: long double,Storage size:%d Bytes, Minimum:%Lg,Maximum:%Lg\n",
                sizeof(long double), LDBL_MIN, LDBL_MAX);

    return EXIT_SUCCESS;
}


Here are the different types of character placeholders. (int and double)


In addition, the pointer's memory address allocation is different from that of a common variable.

7. include <stdio at the beginning of C. What does H > mean

#Is a preprocessing instruction
include It means "contains".
#include " Something" perhaps #include means to refer to something or a document in this program.
"Something"Or files in are usually provided by the system with an extension .h. This is also known as the header file or the first file.

The header file in C includes prototypes for various standard library functions. The contents of this file are the declarations of the basic input and output functions.
For example, scanf() and printf(), your program contains stdio.h, is equivalent to declaring these functions so that you can use them scanf() or print() in your own programs. Therefore, to call a library function in a program, a header file containing the prototype of the function must be present, typically at the beginning of the program.
#include <math.h> means to include math header file. H is the extension of the header file, which declares math in the standard library to be used in this program. H file. Math. The h header file declares some common mathematical operations, such as multiplication, square operation, and so on.
Each #include statement can contain only one header file. If you have more than one header file, write multiple #include statements, one #include statement per line, and one header file per #include statement.

8. The array in C is actually a pointer (when the parameter is a parameter)

When a function passes an array, it just looks like an array as a parameter, but it is actually a pointer.
Array variables are special pointers when used as parameter parameters.

#include <stdio.h>

void aaa(int a[]);

int main()
{
  int a[10] = {10,20};
  aaa(a);
}

void aaa(int a[])
{
  printf("%d",a[3]);
}

#include <stdio.h>

void aaa(int a[]);

int main()
{
  int a[10] = {10,20};
  aaa(a);
}

void aaa(int* a)
{
  printf("%d",a[3]);
}


Arrays are related to pointers, and here are some of their points of knowledge.

Array variables are special pointers, and array variables are constant pointers. Pointers can be interpreted as arrays with only one bit.

Keywords: C C++

Added by Pyro on Sun, 16 Jan 2022 08:36:36 +0200