Dark Horse programmer--c Language Foundation---the easy omission in C language learning (Part I)

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

Knowledge Point ①: output, input function formatting placeholders and formatting modifiers use

%c one character

%d decimal integer%ld: Long integer%lld:longlongint%hd:shortint

%i Decimal Integer

%o Eight-binary integers

%x,%x hexadecimal integer

%s A string that ends with a space, tab, or newline character.

%f,%f,%e,%e,%g,%g is used to enter real numbers, which can be entered in decimal or exponential form.

%p A pointer

%u reads an unsigned decimal integer%zd: unsigned long integer unsignedlong

%n the equivalent number of characters that have been read into the value

Percent Read% sign

The printf () function format modifier describes:

Minimum wide Field m (integer):

Specifies the number of columns that the output is accounted for; if M is a positive integer, when the Output data width field is less than M, in the domain to the right, the left redundant bits fill the space, when the output data width is greater than M, the actual width of all output; if M has a leader 0, the left redundant bit is 0;

Display precision. N (an integer greater than or equal to 0):

The precision modifier, after the minimum width field, is made up of a dot and subsequent integers, for floating-point numbers that specify the number of floating-point decimal digits for the output, and for strings that specify the number of substring characters to intercept from the left of the string.

*:

The minimum wide field m and precision. N is substituted to indicate that their value is not constant, and the output items of the printf () function are specified sequentially

#: Decorated with the format character F, E, G, used to ensure that the output of the data has a decimal, even if there is no decimal place, when the format character x is also used to ensure that the output hexadecimal number preceded by a leader 0x.

scanf () function format modifier:

Ignore input modifier *: Indicates that the corresponding input is not assigned to the corresponding variable after reading it.

Knowledge Point ②: often use code block {} to free up memory that is used by unnecessary variables.

Knowledge Point ③: (for interview) a wonderful way to exchange two integers.

Wonderful one:

[CPP]View Plaincopy
    1. A = b-a;
    2. b = b-a;
    3. A = B + A;

Wonderful two:

[CPP]View Plaincopy
    1. A = A^b;
    2. b = a^b;
    3. A = A^b;

Knowledge Point ④:scanf ("%d%d"); If there are spaces in the format string, you can use a Space, tab, carriage return

Knowledge Point ⑤: The result of the remainder operation% is the same as the result of the symbol except the two positive number after the remainder is added.

Knowledge Point ⑥:sizeof operator

sizeof is an operator that is not taken for granted as a function

sizeof can be used without adding ()

sizeof can add variables, constants, data types

With data type is required plus ()

Problems caused by directly defining variables behind knowledge points ⑦:if

[CPP]View Plaincopy
    1. if (> 6)
    2. int a = 5;//Error * * * * scope
    3. If you define a new variable after the IF, you must use curly braces

The following is also the same error

[CPP]View Plaincopy
  1. char c = ' + ';
  2. int a = 10;
  3. int B = 20;
  4. Switch (c)
  5. {
  6. Case ' + ':
  7. int sum= A + b;//error ****** scope
  8. printf ("and is%d\n", sum);
  9. Break
  10. Case '-':
  11. Intminus = a-b;//Error
  12. printf ("Difference is%d\n", minus);//Error
  13. Break
  14. }


Modify if you want to define a new variable after the case must be wrapped with {}

[CPP]View Plaincopy
  1. Switch (c)
  2. {
  3. Case ' + ':
  4. {
  5. int sum = a + b;
  6. printf ("and is%d\n", sum);
  7. Break
  8. }
  9. Case '-':
  10. {
  11. int minus = A-B;
  12. printf ("Difference is%d\n", minus);
  13. Break
  14. }
  15. }

Similarly, the For loop does not have {} and cannot define a variable after

[CPP]View Plaincopy
    1. for (int i = 0; i<; i++, a++)//This has an error scope
    2. {
    3. INTA = 10;
    4. }

Knowledge Point ⑧:c language can initialize the count variable in for//performance good, how I remember learning not to C + + can

[CPP]View Plaincopy
    1. for (int i=0; ...; ...)

Contrast

[CPP]View Plaincopy

int i = 0;//This case makes I stay in memory for too long

    1. while (I. ...)

⑨:for initial parameter scope problem of knowledge point

[CPP]View Plaincopy
    1. for (int i = 0; i<; i++)//scope This is the right loop body to perform 10 times
    2. {
    3. Inti = 10;
    4. }

Equivalent

[CPP]View Plaincopy
    1. for (int i = 0; i <; i++)
    2. {
    3. INTA = 10;
    4. }
[CPP]View Plaincopy
    1. for (int i = 0; i<; i++, a++)//This has an error scope
    2. {
    3. INTA = 10;
    4. }

There is an error scope below

[CPP]View Plaincopy
    1. for (int i = 0; i<; i++, a++)//This has an error scope
    2. {
    3. int a = 10;
    4. }

Knowledge Point ⑩: The trap of a comma-expression

For example:

[CPP]View Plaincopy
    1. for (int i=0,j=0; ...; ...) cannot be written for (int i=0,int j=0; ...) Use of a comma-expression

Knowledge point?: C language does not allow two function names there is no overloaded concept

Functions cannot be defined repeatedly but can be declared repeatedly

Knowledge point?: #include编译预处理命令与多文件团队开发

#include编译预处理命令可以认为是: Copy the contents of the following file into the current file

Multi-File Team development: The modules are compiled into. O are not linked successfully, together the link is delivered into the final program.

Link: merges all associated. o Target files and C-language libraries in the project to generate executables

1. function definition put. c file, function declaration put. h file

2. If you want to use a function defined in a. c file, you only need to include the. h file that corresponds to the. c file.

The role of 3..h files: copied by others. You do not need to pipe. h files when compiling links.

Knowledge point?: Array initialization: I've never seen one of these.

[CPP]View Plaincopy
    1. int ages[5] = {[3] = 19, [4] = 20};

This is possible.

[CPP]View Plaincopy
    1. int ages[] = {19,20};

The following is not true:

[CPP]View Plaincopy
    1. INTAGES[5];
    2. ages = {19, 20, 21, 22, 23};//array name is address

Here's what you can do.

[CPP]View Plaincopy
    1. int count = 5;
    2. int ages[count];//
    3. Ages[0] = 10;
    4. AGES[1] = 11;
    5. AGES[2] = 12;//But the value of ages[3]ages[4] is random

The following is not true:

[CPP]View Plaincopy
    1. int count = 5;
    2. int ages[count]= {10,11,12};//error

Conclusion: If the array is initialized when it is defined, the array element must be a constant, or do not write

Knowledge points?: Calculating the length of an array

[CPP]View Plaincopy
    1. sizeof (array name)/sizeof (array name [0])
    2. sizeof (array name)/sizeof (array type)

Knowledge points?: The length of an array cannot be evaluated inside a function when the array is a function parameter

Because the parameters are passed

Arrays are treated as pointers, and the length of a pointer variable is always 8 bytes in a C language in a 64-bit compiler environment

So in passing the parameter to pass the array length

Knowledge Point 16: String initialization

The first four of the following are strings

[CPP]View Plaincopy
    1. Char name[8] = "it";
    2. Char name2[8] ={' i ', ' t ', ' n '};
    3. Char name3[8] ={' i ', ' t ', 0};
    4. Char name4[8] ={' i ', ' t '};

That's not possible.

[CPP]View Plaincopy
    1. Char name4[] = {' I ', ' t '};//is not a string

Understanding the storage of strings in memory

[CPP]View Plaincopy
    1. Char name[] = "it";
    2. Char name2[] ={' o ', ' K '};
    3. printf ("%s\n", &name2.[ 1]);//kit

Knowledge Point 17:strlen function

1. Number of characters calculated: "Ha haha" length is 7

2. The calculated characters do not include ' + '

3. Start from the address of the incoming value until the first ' x '

Knowledge Point 18: When defining a pointer variable, you should initialize it at the same time

[CPP]View Plaincopy
    1. Int*p = &a;


Or

[CPP]View Plaincopy
    1. Int*p;
    2. p = &a;


Int*p; p = &a;

Try not to define after emptying such as: int *p;

Knowledge Point 19: Pointers are often used for functions where multivalued returns are used in C + + is reference

Knowledge Point 20: The difference between an array and a character pointer definition string

Define strings in two ways

1 Using arrays

Char name[] = "it";

Name[0] = ' T ';//This is OK, string variable

Features: Character of string can be modified

Usage Scenario: The contents of a string change frequently

2. Using pointers

Char *name2 = "it";

Name[0] = ' T ';//This is not possible, string constants

Features: The contents of the string can not be modified

Usage Scenario: The contents of the string do not need to be modified, and this string is often used

Knowledge Point 21: The same piece of memory when holding the same value in the constant area

[CPP]View Plaincopy
    1. #include <stdio.h>
    2. Intmain ()
    3. {
    4. Char *str1 = "ABCD";
    5. Char *str2 = "ABCD";
    6. printf ("Str1 's address is%p\n", str1);
    7. printf ("Str2 's address is%p\n", str2);
    8. return 0;
    9. }

The result of the execution is:

[CPP]View Plaincopy
    1. STR1 's address is 0x10373bf66.
    2. STR2 's address is 0x10373bf66.

Knowledge Point 22: Constant area, heap, stack

The programmable internal presence is basically divided into the following parts: Static storage, heap, and stack areas. They have different functions and different ways of using them.

Static storage: Within the time the program is compiled, it is already allocated, and the entire running period of the program exists in this block. It mainly stores static data, global data, and constants.

Stack area: When executing a function, the storage units of local variables within the function can be created on the stack, which are automatically freed when the function is executed at the end. The stack memory allocation operation is built into the processor's instruction set and is highly efficient, but allocates limited memory capacity.

Heap area: Also known as dynamic memory allocation. The program uses malloc or new to request any size of memory at run time, and the programmer is responsible for freeing the memory with free or delete when appropriate. The lifetime of dynamic memory can be determined by us, and if we do not release the memory, the program will release the dynamic memory at the end. However, good programming habits are: If a dynamic memory is no longer used, it needs to be released, otherwise, we think there is a memory leak phenomenon.

Knowledge Point 23: pointers to functions

[CPP]View Plaincopy
      1. void Test (int a)
      2. {
      3. printf ("%d\n", a);
      4. }
      5. void (*p) (int) = test;//fixed notation
      6. void (*p) ();p = test; ibid.
      7. (*p) (3);//function call, can also direct P ()

Dark Horse programmer--c Language Foundation---the easy omission in C language learning (Part I)

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.