C language collection (2): Symbol Overloading

Source: Internet
Author: User

A very important feature of C language is conciseness. But sometimes it is too concise. Simply add, modify, or delete a character, the original program can be completely different.

A concise C language allows a symbol to indicate different meanings in different contexts. This is called the symbol "overload ".

For exampleVoidSymbol, which has the following common meanings:

1> as the return type of the function, it indicates that no value is returned.

2> in Pointer declaration, it indicates a universal pointer

3> In the parameter list of the function, it indicates that no parameter exists.

Here is a multiplication number.*For more information, see the "bad" symbol overload.

Code:

1 # include <stdio. h> 2 3 int main () 4 {5 int p, N = 5, * q = & N; 6 7 p = N * sizeof * q; 8 printf ("p = % d. \ n ", p); 9 10 p = N * sizeof (int) * p; 11 printf (" p = % d. \ n ", p); 12 13 p = N * sizeof (int) * q); 14 printf (" p = % d. \ n ", p); 15 16/* error: binary operator * operand ('unsigned int' and 'int *') invalid */17 // p = N * sizeof (int) * q; 18 // printf ("p = % d. \ n ", p); 19 20 return 0; 21}

Do not look at the definition of the variable first (because the type of the variable will prompt the function), directly look at the line7, 10, 13, 17 expressions, do you know what they mean?

Okay. Let's see the results,

Randy @ ubuntu :~ /C_Language $./a. out
P = 20.
P = 400.
P = 20.

Explanation:

Line7: we have encountered sizeof again. Remember, sizeof is an operator, not a function. When sizeof is of type, it must be called parentheses, such as sizeof (int ). N = 5, q is the pointer, pointing to the N address,

So * q = 5, is int type, sizeof (int) = 4, so p = 5*4 = 20.

Line10: p = 5*4*20 = 400.

Line13: It's actually the same as line7.

Line17: this expression has been injected because it fails to be compiled. Should I know why? (The answer is annotated)

 

Summary: In this example, if a small * is not a little human bypass, there are also many other symbol overload examples.

For exampleStatic, extern, &, <,().

Is this the beauty of the C language concise? Discussion!

--- End ---

 

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.