Speak a little knowledge point---C language three-eye operator __c language

Source: Internet
Author: User
Tags case statement switch case
Preface

This article's small knowledge point is the C language three-mesh operator, this is a very useful operator, you can see in the Kinetis library function has a lot of applications, this article I will use a familiar example to explain.


Knowledge points

。 :
Also known as the conditional operation symbol, is the only three-mesh operator in the C language, and the binding order is from right to left.

The conditional operators have a lower precedence, and are only taller than the assignment operators and the comma operators.


Example Analysis Example 1:

This is the qn908 clock allocation diagram:

From the above figure you can see that there are three sources of SYSCLK, which can be clearly seen in the Clk_ctrl register, as shown in the following figure:

How does the Kinetis clock library function work? See the following program, the perfect application of the triple-mesh operator.
Fsl_clock.c

#define GETSYSCONCLKMUX () (Syscon->clk_ctrl & Syscon_clk_ctrl_sys_clk_sel_mask) >> Syscon_clk_ctrl_sys _clk_sel_shift)


static uint32_t clock_getcoresysclkfreq (void)
{return
    (getsysconclkmux () = 0)? Clock_getrco32mfreq (): (Getsysconclkmux () = 1)?
                              Clock_getxinfreq ():
                                      (Getsysconclkmux () = 2)? Clock_get32kfreq (): 0;
}

Remember that the binding order is from right to left, and it will be analyzed from right to left.

A.
(Getsysconclkmux () = = 2)? Clock_get32kfreq (): 0;

Getsysconclkmux the function of this macro is to get the value of the Sys_clk_sel bit field of the Clk_ctrl register.
So the above statement means that if this bit field value is 2, it returns the frequency value of the 32K clock, which is consistent with the expression in the register above.

B.
(Getsysconclkmux () = = 1)? Clock_getxinfreq ():
(Getsysconclkmux () = = 2)? Clock_get32kfreq (): 0;

Similarly, the bit field is 1, which returns the frequency value of the external clock.

C.
(Getsysconclkmux () = = 0)? Clock_getrco32mfreq ():
(Getsysconclkmux () = = 1)? Clock_getxinfreq ():
(Getsysconclkmux () = = 2)? Clock_get32kfreq (): 0;

Similarly, the bit field is 0, which returns the frequency value of the RCo 32M.

That is, the getsysconclkmux of each value corresponds to a return value. Did you suddenly think of the switch case statement.

    Switch (Getsysconclkmux ())
{case
    0: Return  clock_getrco32mfreq ();
    Case 1: Return  clock_getxinfreq ();
    Case 2: Return  clock_get32kfreq ();
    Default:return 0;
}   

The obvious advantage of three purposes is that a statement is done, direct return. Example 2:

Fsl_clock.c

Static uint32_t clock_getrco32mfreq (void)
{return
    (Syscon->clk_ctrl & Syscon_clk_ctrl_clk_osc32m_ Div_mask)? CLK_OSC_32MHZ/2: Clk_osc_32mhz;
}

As you can see from the expression above, the conditional operator has a lower precedence than the division operator.


Summary

The

Three-mesh operator can also implement switch case statements and be more concise. The
binding order is from right to left. The only three-mesh operator in the
C language.
Precedence is only higher than the assignment operator and the comma operator.

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.