I have the following program:
int main(){
char condominio[20][20];
return 0;
}
What values would each condo element contain? Any special character? since when I print I don't see anything.
I have the following program:
int main(){
char condominio[20][20];
return 0;
}
What values would each condo element contain? Any special character? since when I print I don't see anything.
The content will be random or indeterminate, it
condominio
will occupy 20 x 20 bytes (assuming itchar
occupies one byte) which will not be initialized.That is to say: 400 bytes of memory will be occupied, whatever the memory had before it was requested by
condominio
, and that memory could have any value depending on how your operating system handles memory.For example, there are operating systems that when freeing memory do not erase its content but rather mark it as available, for example it could happen 1 that...
... the variable
otra_frase
pointed to a memory that it contained"Mi mama me mima"
despite the fact that said information would have been deleted with the instructionfree(frase)
.Knowing this, the variable
condominio
without initializing its values could contain any memory residue from previous or parallel executions.I bet you have printed like this:
And you haven't seen anything at all... try changing print a character (
"%c "
) to print a number ("%d "
):With that change, in my case the execution has shown 2 :
The reason why nothing is printed is all those zeros that tell the standard output that the characters to print have run out (the zero (
0
) indicates the end of the string). If it happenedcondiminio[0][0]
to have a printable character it would have been displayed along with all subsequent printable characters until another zero was found.If you change the code to print strings instead of characters:
Each
condominio[x]
beginning with a printable character will be printed; In my case I have seen the following nonsense:Note that using the value of uninitialized variables is undefined behavior, so anything unexpected could happen... like nothing being displayed.
1 It might also not happen, it depends on how the operating system handles memory.
2 Content may vary with each run.
That line is creating a variable that is capable of storing 20 character strings, each of which will be able to store up to 20 characters (counting the possible null character at the end of the string).
What value does each string contain? Its initial value is the one that the memory positions in which the variable is located had. In C there are no default constructors or initializers. If you want a memory location to have an initial default value you will have to program it explicitly:
Of course, these types of initializations only work for values known at compile time. For variable values you can iterate over the array with a loop, for example.
As for what you say about printing... you do not indicate how you are outputting the variables to the screen, so that part remains pending for you to edit the question. Of course, keep in mind that not all values in the range
char
correspond to printable characters. Printing one of these characters can have the most varied results (line breaks, carriage returns, strange symbols, ...)Your question is based on a two-dimensional array of type char, you can initialize it like this:
To print its content you can:
obtaining as output something similar to:
When an array is declared, if no value is specified when initializing it, all fields are equal to 0.
Therefore all 400 elements (20x20) of your array will have the value 0.
That is exactly the reason why you don't see anything on the screen when you print it. 0 (zero) in ascii is equivalent to NULL. The printable characters range from 32 to 126 (in the reduced table)