In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to solve the pit used by sizeof function in C language". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how to solve the pit used by sizeof function in C language" can help you solve the problem.
First, let's take a look at this piece of code.
Int main () {int item2witprintf ("% d\ n", sizeof (iTunes +)); printf ("% d\ n", I); return 0;}
A very simple piece of code
At that time, I thought the answer should be
four
three
But the result was beyond my expectation.
Why is that?
Let's talk about it in detail.
Through debugging, it is observed that although there is a sentence of iTunes + code, it is not implemented. At this point, many people may have made the same mistake as I did, thinking that sizeof is a function.
In fact, this is not the case, sizeof is actually a kind of unary operator, you can find the space occupied by the variable type, in bytes.
At this point, it should be distinguished from the strlen function. The strlen function calculates the number of characters of the string, excluding'\ 0', while sizeof calculates'\ 0 'because it is the number of bytes.
Let's talk about a few more similar situations like sizeof to avoid stepping on pits.
Logical operators & & and | | can also produce such pits.
Let's look at such a problem.
Int main () {int I = 0, a = 0, b = 2, c = 3, d = 4; I = aura + & & + + b & & canals; / / I = aura + | + b | | cymbals; printf ("a =% d\ nb =% d\ nc =% d\ n", a, b, c); return 0;}
Answer:
Here you may have questions, why is this b not equal to 2, this c is not equal to 4, this is not the post + +?
In fact, logic operators do not count every object in the process of operation. Take this example. Because it is post + +, an is also equal to 0 at this time. For & & logic and, the answer is already obvious. 0 and any number logic sum are equal to 0. So the latter breadwinners and C++ are not counted.
I think logic may be the same, there is only one "truth", logic or get is "truth".
This means that C++ did not come true.
This is the end of the content about "how to solve the pit used by sizeof function in C language". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.