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 24: Static variable, global variable declaration but not initialized with initial value (Int0 char)

But the local variable declaration is not initialized and there is no initial value.

Knowledge Point 25: The overall initialization of a struct must be made at the time of declaration

[CPP]View Plaincopy
    1. struct person P1 = {"Zhangsan", ' F '};//this is correct

The following is the wrong

[CPP]View Plaincopy
    1. struct person p2;
    2. P2 = {"Zhangsan", +, ' F '};

Initialization can also be written like this

[CPP]View Plaincopy
    1. struct person p1 = {. Name = "Zhangsan",. Age =,. Gender = ' F '};

Knowledge Point 26: The memory occupied by the struct is a multiple of the maximum member's memory (completion algorithm), as

[CPP]View Plaincopy
    1. struct student{
    2. int AGE;//4 bytes
    3. char*name;//pointer type of eight bytes
    4. };
    5. struct Student stu;
    6. Stu.age = 20;
    7. Stu.name = "Jack";
    8. int s = sizeof (STU);
    9. printf ("%d\n", s);//output is 16 principle: 4+8=12--->16



Knowledge Point 27: Several ways to define structure variables:

Way One:

[CPP]View Plaincopy
    1. struct student{
    2. int age;
    3. Char *name;
    4. };
    5. struct Student stu1;

Way two:

[CPP]View Plaincopy
    1. struct student{
    2. int age;
    3. Char *name;
    4. }stu2;//This is defined as a variable at the same time as a type, can only be written once, otherwise the type is defined repeatedly

Way three:

[CPP]View Plaincopy
    1. struct {
    2. int age;
    3. Char *name;
    4. }stu3;//This method cannot again define this type of variable

One of the best ways for a person to think:

[CPP]View Plaincopy
    1. typedef struct student{
    2. int age;
    3. Char *name;
    4. }student;
    5. Student Stu4;



Knowledge Point 28: Preprocessing directives (three types):

1. Macro definition

2. Conditional compilation

3. The file contains

1. Macros defined by pairing and using macros with parameters

[CPP]View Plaincopy
    1. #define
    2. ...
    3. #undef

Macros with parameters

[CPP]View Plaincopy
    1. #define SUM (V1,V2) ((v1) + (v2))//parentheses are required

For example:

[CPP]View Plaincopy
    1. #define Pingfang (a) a*a
    2. #define Pingfang (a) (a*a)

When called

[CPP]View Plaincopy
    1. Pingfang (Ten)/pingfang (2)//Incorrect
    2. Pingfang (5+5)//Incorrect

Macro efficiency with parameters is higher than function

2. Conditional compilation (usually judging the value of a macro)

[CPP]View Plaincopy
    1. #if conditions
    2. ...
    3. #elif conditions
    4. ...
    5. #else
    6. ...
    7. #endif (very important) otherwise all subsequent code is invalid

[CPP]View Plaincopy
    1. #ifdef is equivalent to
    2. #if defined ()

[CPP]View Plaincopy
    1. #ifndef is equivalent to
    2. #if!defined ()

3. The file contains

1.<> represents the file that comes with the system, "" represents a custom file

2. Do not allow loop inclusions, such as A.H contains B.h,b.h and contains A.H

Knowledge Point 29:typedef is just an alias for a type, not a new type.

[CPP]View Plaincopy
    1. struct student{
    2. int age;
    3. Char *name;
    4. };
    5. typedef struct STUDENT Student;

Equivalent to

[CPP]View Plaincopy
    1. typedef struct student{
    2. int age;
    3. Char *name;
    4. }student;

is also equivalent to

[CPP]View Plaincopy
    1. typedef struct {
    2. int age;
    3. Char *name;
    4. }student;

Similar to naming enumeration types

[CPP]View Plaincopy
    1. typedef enum SEX
    2. {
    3. Man,women
    4. }sex;

The following scenario is more specific

The following is the custom data type for the function pointer type, and the return value type and parameter type to match

[CPP]View Plaincopy
  1. #include <stdio.h>
  2. typedef int (*typefuncpointer) (int, int);
  3. int add (int a, INTB)
  4. {
  5. return a + B;
  6. }
  7. int minus (int a, INTB)
  8. {
  9. return a-B;
  10. }
  11. int main ()
  12. {
  13. Typefuncpointer p = add;//using a custom type
  14. Typefuncpointer P2 = minus;//using a custom type
  15. printf ("Add =%d\n", p (1, 2));
  16. printf ("Minus =%d\n", p2 (1, 2));
  17. return 0;
  18. }

Here is the pointer type that defines the struct body

[CPP]View Plaincopy
    1. typedef struct student{
    2. int age;
    3. Char *name;
    4. }*ptrstu;
    5. How to use
    6. Student Stu ={18, "Zhangsan"};
    7. Ptrstu p = &stu;

A macro definition is also a type that can be named

[CPP]View Plaincopy
    1. #define Integer INT

Equivalent

[CPP]View Plaincopy
    1. typedef int Integer

Attention and the difference between typedef

For example:

[CPP]View Plaincopy
    1. typedef char * String
    2. #define STRING2CHAR *
    3. Use
    4. String s1,s2;//s1 and S2 are char * types
    5. String2 s3,s4;//s3 is of type char*, but S4 is of type Char

Knowledge point 30:static and extern difference is the possibility of cross-file access

1. Functions

2. Variables

Effects on functions

External functions: Defined functions can be accessed by this file and other files

internal functions; Defined functions can only be accessed by this file

By default, all functions are external (equivalent to the keyword extern), so you can omit the extern effect:

Complete definitions and references an external function is added extern

Reference is also the default external function, so also omit extern

Static function: Define an intrinsic function

Use: Static return type function name (parameter list)

cannot be called by another file

In a project, the external function name of this file cannot be the same as the external function of other files (error)

The intrinsic function (static) in this file can be the same as the function name of other files.

Effect on a variable:

There are two categories of global variables:

External variables: Defined variables can be accessed by other files

1. By default, all global variables are external variables

2. External variables of the same name in different files represent the same

3. Define an external variable without extern and add extern to the declaration

The same statement is not wrong.

Internal variable: The defined variable cannot be accessed by another file

Different files with the same name internal variables do not affect each other

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.