In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what operators there are in C language, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.
1. monocular operator
Before we learned about the triple eye operator (?:), It means there are three operands.
For example: 3 + 5
where + is an operator
3 is the left operand.
5 has an operand.
+ is a binocular operator
So what is a monocular operator, that is, one operand?
Our common operators are:
Here we will introduce sizeof,~,++ and--in detail
1.sizeof
Sizeof is often used to calculate the length of types, such as array types, int, char, short, and so on. Also, sizeof is an operator, not a function, so the parentheses can be omitted. However, the type length can not be saved, this is a syntax requirement.
Here are some common uses of sizeof
#includeint main(){ //sizeof int a = 10; printf("%d\n", sizeof(a)); printf("%d\n", sizeof(int)); printf("%d\n", sizeof(float)); printf("%d\n", sizeof(double)); int arr[10] = { 1,2,3,4,5,6,7 }; printf("%d\n", sizeof(arr)); printf (" % d\n ", sizeof (int [10]));//Calculate the length of the entire array printf("%d\n", sizeof a); //printf (" % d\n ", sizeof int); this is incorrect return 0;}
The calculation results are as follows:
2.~
The main function of~is to reverse the binary of a number, but again, the final printed code is the original code.
#includeint main(){ int a = 0; //~negated by (complement 2 in memory) bit //00000000000000000000000000000000 //111111111111111111111111111111-complement //11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 //10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 printf("%d\n", ~a); return 0;}
The results are as follows:
Here is an application:
How to restore a number
#includeint main(){ int a = 10; a |= (1 struct Stu* ps = &s; printf("%s %d %.1lf\n", (*ps).name, (*ps).age, (*ps).score); printf("%s %d %.1lf\n", ps->name, ps->age, ps->score); return 0;}
These three cases output the same results, interested students can try it themselves!
7. implicit type conversion
For example:
#includeint main(){ short s = 20; int a = 5; printf("%d\n", sizeof(s = a + 4)); printf("%d\n", s); return 0;}
The output of sizeof is 2, where int a is converted to short.
8. Properties of operators
Three factors influence the evaluation of complex expressions:
1. precedence of operators
2. associativity of operators
3. Control evaluation order
Which of two adjacent operators is executed first depends on their priority. If both have the same priority, it depends on their associativity.
//The evaluation of an expression is partially determined by the precedence of the operator.
//Expression 1
a * b + c * d + e * f
When calculating, since * has higher priority than +, it can only be guaranteed that * is calculated earlier than +, but the priority is not
Can decide that the third * is executed earlier than the first +.
//Expression 2
c + -- c ;
As above, the precedence of the operator can only determine whether the operation of minus-comes before the operation of +, but we have no way of knowing whether the acquisition of the left operand of + comes before or after the evaluation of the right operand, so the result is unpredictable and ambiguous.
//code 3-illegal expression
int main ()
{
int i = 10 ;
i = i -- - -- i * ( i = - 3 ) * i ++ + ++ i ;
printf ( "i = %d\n" , i );
return 0 ;
}
About "C language what operators" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.