I'm reviewing the strings, they say that every string will always end in '\0' , that is, that '\0' occupies a space but in this code I define dimension 4: name[4] , it should accept only 3 characters + '\0', but it accepts 4 characters??
#include<stdio.h>
int main() {
char nombre[4]="Hola";
printf("El saludo es: %s",nombre);
return 0;
}
El saludo es: Hola
--------------------------------
Process exited after 1.077 seconds with return value 0
Presione una tecla para continuar . . .
I think that it should throw ERROR from the beginning or, failing that, it should only print "Hello" but it prints "Hello" why? I have tried dev-c++ and gcc on ubuntu 18
What happens is that the one who has to respect the length is the programmer :)
When you create an array with
char nombre[4]
these allocating 4 bytes from a certain memory location (for example0xabcd00
). By assigning "Hello", you are filling all four memory locations, one with each character.At print time, the function
printf
will print the bytes found from the variable addressnombre
until it encounters a null character (0x00 or '\0'). In your case, the memory might have something like:If the characters from position
0xabcd04
were ascii, they would be printed.If you try this program (run online) you will see the error caused by this:
We could think that in memory the variables were assigned like this:
If I use
char hola[5] = "Hola";
, the program printsHola mundo
, as expected. But if I usechar hola[5] = "Hola!";
the program it will printHola!mundo mundo
, since the character!
overwrites the last position where the null character\0
of the variable should behola
, so the functionprintf
assumes that the text of the variablehola
ends with the null character of the variablemundo
The first thing is to understand how the memory of a computer works.
Computer memory is typically divided into blocks. This is done because managing memory at the byte level would consume too many computer resources.
Well, when you declare a variable in your program, the program has to request space from the Operating System in order to locate that variable somewhere in memory. What happens then is that the Operating System allocates a block of memory to your program. From that moment your program will have exclusive access to that memory region and no other application will be able to access it.
In short, if you need 4 bytes, the Operating System will give you a memory block of... say 1KB. From that moment on, your program is responsible for managing that memory correctly.
Well, now your program arrives and what it does is reserve 4 bytes but modify 5:
Apparently nothing happens... but it is very rare that a program has only one variable. Normally, it has several variables, for example:
Since the compiler is free to organize the stack as it likes, let's assume that the chosen layout is as follows:
If we now write 5 characters in
nombre
:It turns out that we have modified the value of
b
!!!! From this point the program can become erratic. If for example it turns out tob
be a pointer, you will have lost the reference to the memory address it was pointing to!!!But it is also that the effects can then be multiplied, if for example now we modify the variable
b
:Our memory map would now look like this:
So when printing
nombre
, since we have lost the string terminator (which was onb
), what will happen is that we will get a nice bunch of totally unreadable and meaningless characters.The program will only stop spitting out characters when one of the following conditions occurs:
\0
in the memory... who knows when that happensSo you see, nothing prevents you from writing more values than those that legally fit in an array... but if you do, be aware of the consequences.
The C language is a "high-level assembler".
The first part of the dec instruction. from the var name, this is:
char nombre[4]
Reserve 4 bytes on the stack.
The second part of the instruction, this is:
= "Hola" ;
Copy to those 4 bytes reserved in the stack the first 4 bytes of the code literal:
"hola"
(which occupies 5 bytes). And since there is no space to copy anything else, it does not copy the one\0
at the end.Ending "C" Strings with
\0
is a "Programming Convention" but NOT ALWAYS REQUIRED in C. Hence, the byte was not copied\0
.Yes, it is mandatory in most of the C functions of the standard libs to end the strings with
\0
to know what length it has (rather where it ends in memory).For example, the function
printf
wants that when passing a string in some of its arguments, this string ends in\0
.Therefore the program described, although not "casque" is "WRONG".
An example of a program considered CORRECT, and without ending the name in would be
\0
pe :The C language is a "high-level assembler".