I am trying to check if an element inside a int array is empty for C language
There is no value that means "empty" - every possible value of an int
represents an integer number.
You know statically that your elements are all uninitialized ... because you didn't initialize them.
(More precisely, they are initialized to indeterminate values, which are permitted to include trap representations, although I'd be very surprised if you encounter one of these for int
.)
I would add some number to the element of numbers if I know the element is empty
If in your program you would like to use some int
value to mean "empty or currently unused" - you're perfectly welcome to define those semantics.
Just initialize all your elements to INT_MIN
or whatever other value you're willing to sacrifice, and then you can compare them to that later.
C doesn't automatically do this for you, because some people want to be able to use the full range of int
for representing numbers, are willing to keep track of whether they've initialized things or not, and wouldn't thank the compiler for doing extra work they don't need.
Oh, and for completeness:
What is inside an non-initialized int array?
Nobody knows. Could be anything in there. If you want something specific, set it yourself.
If some value was guaranteed to be there, that could only be because the system promised to always put it there. That's extra work that would be almost always wasted in a well-formed program, so C doesn't do it. Some other languages do.