C language IV, typedef keyword, and scope

Source: Internet
Author: User

C language IV, typedef keyword, and scope one, typedef keywords

In the C language, there is a typedef keyword, the function of which is to allow you to define a new name for the type, that is, the individual name

For example:

  

int DWORD

So later, when we define a variable of type unsigned int, this does not have to write unsigned int. Direct DWORD.

For example:

  

DWORD name = 0;

Usually used in the definition of struct, and function pointer.

For example:

  

typedef struct mystruct{   int A;   int b;    } Myintstruct;

Then the new type is myintstruct, and this type eliminates the keyword when you define it every time.

For example:

  

int main{   //struct mystruct is;   Not defined before, to use the above   myintstruct is  ;   IS.A = ten;         return 0;  }
Ii. Scope

In the C language, the code you write has scope limitations.

What is the scope?

In short, when we define variables, this variable is accessed. To be accessible within a certain region.

For example, if you define variable a in the A function, and the B function defines the variable B, then the B function cannot use the A variable.

For example:

  

#include <stdio.h>void  printfb () {   intone;    printf ("%d\r\n", b);   }       void PRINTFA () {  int;  printf ("%d\r\n", a);}

  

The above is the function scope, that is, the {} block statement contains.

Code block scope:

The scope of a block of code is largely differentiated by {}.

For example:

  

int main () {   int a = ten;   {      int a = one;      printf ("%d", a);    Block statement bracketing   }    printf ("%d", a);   return 0;}

If this is defined, then a is not redefined. and a = 11, after the code executes to the end. No longer exists.

File scope:

The file scope is defined outside the code. In other words, global variables. Global scope, which can be accessed at any location in the current file.

For example:

  

  

int a = ten;  Outside the declaration can be. int main () {   printf ("%d", a);   return 0;}

  

C language IV, typedef keyword, and scope

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.