I have a question related to null operation in C. From my understanding and the thing i read here, a null operation or differently called a null byte, is used to tell the compilers the end of the string. Interestingly enough till here, now my question stays, does this apply only for strings. In my code, which is a fairly simple programm in c to print the number of elements in
#include <stdio.h>
int main(int argc, char* argv[]) {
int numb_array[2];
numb_array[0]=1;
numb_array[1]=2;
for(int i=0;i<sizeof(numb_array);i++) {
printf("Elements of numb_array %d\n",numb_array[i]);
}
return 0;
}
my numb_array, how does the compilers know when to stop, it is because of the loop( which is the most likely part) or is the null byte also attached to my array even though it is an array of numbers. Or the null byte works only with strings.
Sorry if the question is obvious i am new to c :)
A null character that marks the end of a string is merely data. It is not an operation. An operation is some action that is performed.
The null character does not tell the compiler particularly where the end of the string is. It tells the program where the end of the string is. Determining where the end of a string is is generally a run-time thing; it is something done by the program and/or the routines it uses, not by the compiler. (However, compilers may do some operations with strings while they are compiling.)
Character strings are not the only sequences whose ends are marked by null elements. The argv
parameter received by main
points to a sequence of pointers whose end is marked by a null pointer. On the other hand, many arrays do not mark the end with a special value this way. Programs use other methods of knowing where the end of an array is, such as keeping the number of elements in a variable or defining it as a preprocessor symbol (macro).
In the code you show, you told the compiler when the program should stop, by using the expression i<sizeof(numb_array)
. The second part of a for
determines whether the loop continues or stops. It continues when i<sizeof(num_array)
is true and stops when it is false.
sizeof
is an operator. It produces the number of bytes in its operand. The compiler knows this number because it knows about the operand. When you declared int numb_array[2];
, that told the compiler that numb_array
had two elements, and each of those elements was an int
. Most likely, int
has four bytes (it can vary by C implementation, but four is the most common these days). So the compiler knows that numb_array
has two four-byte elements, so sizeof(numb_array)
produces eight.
So for(int i=0;i<sizeof(numb_array);i++)
continues until i
is eight, then it stops. This is wrong code to write, because numb_array
has two elements, not eight. To write a loop that executes once for each element in the array, you can calculate the number of elements in the array by dividing the size of the array by the size of one element:
for (int i = 0; i < sizeof numb_array / sizeof numb_array[0]; ++i)
printf("numb_array[%d] = %d.\n", i, numb_array[i]);