Looping through string - exit on null not zero - c

I've written a small program to highlight the problem I'm having in C. I expected that in the following code, the display for loop would print 0, but exit on null; however, the loop exits on 0. What on earth am I missing / don't I understand? Your help is much appreciated.
#include <stdio.h>
int main (void)
{
int i = 0;
int iArray[] = {1, 2, 0, 4, 5, '\0'};
int cArray[] = {1, 2, 0, 4, 5, '\0'};
for(i=0; iArray[i] != '\0'; i++)
{
printf("\nElement: %d", iArray[i]);
}
for(i=0; cArray[i] != '\0'; i++)
{
printf("\nElement: %d", iArray[i]);
}
return 0;
}
Thanks,
Dan

The character literal '\0' is actually the same as the integer literal 0. So it stops when it hits the zero in your arrays.
To be more precise, '\0' is the octal value of zero. All character (and of course string) escape sequences with a leading zero are octal values, and will be inserted by the compiler "as is".
For arrays there is another way of finding the end, namely to get the the number of items in the array, like in
for(i=0; i < sizeof(iArray) / sizeof(iArray[0]); i++)
However, the trick of using e.g. sizeof(iArray) / sizeof(iArray[0]) to get the number of items in the array only works on real array. As soon as you pass an array around (e.g. as an argument to a function) it decays to a pointer and the "trick" can no longer be used.

0 == '\0'
So the iArray[i] != '\0' is the same of iArray[i] != 0
And your arrays contain 0 at the third element so the for loop will print only the 2 first elements of each array.

You're missing quotes, you seem to want to create a string of digits. The digit '1' is not the same as the character with the value 1.
Also, your arrays are mysteriously int, when they should be char.
Try:
const char iArray[] = { '1', '2', '0', '4', '5', '\0'};
which of course can be simpler written as:
const char iArray[] = "12045";

Because 0 and '\0 are equivalent. Actually the first is a number and the second is a char, which is casted (transformed) to int when inserted in the array. Probably you wanted to create a string of characters, so create an array of type char, wrap each number in '0' quotes and replace %d with %c.
Note: '0' represent the character zero, which is different from literal 0 and equivalent '\0', as you can see on the ASCII table. Same happens for 1 vs. '1'
If you intended an array of integers, then you cannot use '\0' as a terminator for the reason stated in the first sentence (unless you are sure 0 will not appear inside the array), keep the length of the array somewhere and use it for iterating.

Related

Why don’t we need for loop to print strings in C?

I don't understand Why don’t we have to print strings in for loop ? In normal cases we need to print arrays in for loop. For example, if we want to print the array of integers. It will be like this:
int a[n];
for (i = 0; i < n; i++){
printf("%d", a[i]);
}
But for strings like:
char s[100] = " Hello ";
printf("%s\n", s);
it is enough to write the name of array.
EDIT: It seems like I didnt ask my question properly as some of you wrote answers which is not related to my question.I edit my question.
Strings terminate with the empty character '\0', that's how it is possible to know when a string ends even without explicitly passing its length.
The difference is that C-style strings (which are char arrays) are zero-terminated, whereas int arrays are normally not zero terminated.
Theoretically, you could also create an int array which is zero-terminated and print that in a loop:
int a[] = {5,7,3,0};
for (i=0;a[i]!=0;i++)
{
printf("%d",a[i])
}
However, the problem with zero-terminated int arrays is that the number 0 could be a meaningful value, so you cannot be sure that it really is the end of the array when you encounter that value. With strings, however, the ASCII-Code 0 does not represent a meaningful value, so you can be reasonably sure that you have reached the end of the string.
Your example is far from analogous. %d refers to a single integer, while %s refers to an entire (but still single) string.
You are not passing the size of the array n[] to printf either - rather you are calling printf n times. You are printing one int just as you are printing one string.
The actual string length is not known a priori, rather printf iterates the string until it encounters the \0 terminator. Equivalent to:
for( int i = 0; s[i] != '\0'; i++)
{
printf( "%c", s[i] ) ;
}
because
char s[100] = " Hello ";
is equivalent to:
char s[100] = { ' ', 'H', 'e', 'l', 'l', 'o', ' ', '\0' } ;
Strings are multiple characters all at once. You cannot ask for a specific character from a string. If you want to do so, you must refer to it as an array of chars like this
for (i = 0; i < n; i++){
printf("%c", s[i]);
}
It is because you are already providing the parameter which is "%s" to the printf function which already has a definition telling it how to print a string.
Hope it helps.
One of the reason is that char only takes 1 byte of memeory and when you just press a character, the first index of array is filled up completely and it moves on to the next one till it encounters NULL character. This is not the case with integer array where the size is more than 1 byte and is machine dependent. So you cannot escape the first index by just pressing the number less than the maximum range. If you try to do this, it will store your numbers in first index only and hence a for loop is required there.

Assignment after initialization to specific index in an array

After assigning 26th element, when printed, still "Computer" is printed out in spite I assigned a character to 26th index. I expect something like this: "Computer K "
What is the reason?
#include <stdio.h>
int main()
{
char m1[40] = "Computer";
printf("%s\n", m1); /*prints out "Computer"*/
m1[26] = 'K';
printf("%s\n", m1); /*prints out "Computer"*/
printf("%c", m1[26]); /*prints "K"*/
}
At 8th index of that string the \0 character is found and %s prints only till it finds a \0 (the end of string, marked by \0) - at 26th the character k is there but it will not be printed as \0 is found before that.
char s[100] = "Computer";
is basically the same as
char s[100] = { 'C', 'o', 'm', 'p', 'u','t','e','r', '\0'};
Since printf stops when the string is 0-terminated it won't print character 26
Whenever you partially initialize an array, the remaining elements are filled with zeroes. (This is a rule in the C standard, C17 6.7.9 §19.)
Therefore char m1[40] = "Computer"; ends up in memory like this:
[0] = 'C'
[1] = 'o'
...
[7] = 'r'
[8] = '\0' // the null terminator you automatically get by using the " " syntax
[9] = 0 // everything to zero from here on
...
[39] = 0
Now of course \0 and 0 mean the same thing, the value 0. Either will be interpreted as a null terminator.
If you go ahead and overwrite index 26 and then print the array as a string, it will still only print until it encounters the first null terminator at index 8.
If you do like this however:
#include <stdio.h>
int main()
{
char m1[40] = "Computer";
printf("%s\n", m1); // prints out "Computer"
m1[8] = 'K';
printf("%s\n", m1); // prints out "ComputerK"
}
You overwrite the null terminator, and the next zero that happened to be in the array is treated as null terminator instead. This code only works because we partially initialized the array, so we know there are more zeroes trailing.
Had you instead written
int main()
{
char m1[40];
strcpy(m1, "Computer");
This is not initialization but run-time assignment. strcpy would only set index 0 to 8 ("Computer" with null term at index 8). Remaining elements would be left uninitialized to garbage values, and writing m1[8] = 'K' would destroy the string, as it would then no longer be reliably null terminated. You would get undefined behavior when trying to print it: something like garbage output or a program crash.
In C strings are 0-terminated.
Your initialization fills all array elements after the 'r' with 0.
If you place a non-0 character in any random field of the array, this does not change anything in the fields before or after that element.
This means your string is still 0-terminated right after the 'r'.
How should any function know that after that string some other string might follow?
That's because after "Computer" there's a null terminator (\0) in your array. If you add a character after this \0, it won't be printed because printf() stops printing when it encounters a null terminator.
Just as an addition to the other users answers - you should try to answer your question by being more proactive in your learning. It is enough to write a simple program to understand what is happening.
int main()
{
char m1[40] = "Computer";
printf("%s\n", m1); /*prints out "Computer"*/
m1[26] = 'K';
for(size_t index = 0; index < 40; index++)
{
printf("m1[%zu] = 0x%hhx ('%c')\n", index, (unsigned char)m1[index], (m1[index] >=32) ? m1[index] : ' ');
}
}

String termination C/C++ char = 0

#include<stdio.h>
#include<string.h>
void terminateString(char *str){
str[3] = 0;
printf("string after termination is:%s\n",str);
}
int main(){
char str[]="abababcdfef";
terminateString(str);
return 0;
}
Output:
string after termination is:aba
We are only assigning element at index '3' to 0, but why are all characters after that index are ignored? Can someone please explain this behavior?
We are only assigning element at index '3' to 0, but why do all
characters after that index are ignored? Can someone please explain
this behavior?
The convention with a zero-terminated string is that the 0 byte is what indicates the end of the string. So when printf() encounters the zero-byte at position 3, it stops printing.
The ISO C standard defines a string as follows (see, for example, C11 7.1.1 Definition of terms), emphasis is mine:
A string is a contiguous sequence of characters terminated by and including the first null character.
Hence, when you have the character sequence abababcdfef\0, that is indeed a string.
However, when you put a null at offset 3, the string is not aba\0abcdfef\0 but, by virtue of the fact it's only a string up to and including the first null, it is aba\0.
C-string is Null-terminated string. With null-terminated it means "a null character terminates (indicates the end of) the string".
A null character is a character with all its bits set to 0, or \0, presented in memory as 0x00.
When you set str[3] = 0 you're changing str[3] to the terminator token, so when printf reads the terminator, it thinks the string is end and only prints "aba".
What you are demonstrating is the difference in c++ between strings and char arrays. Strings are a sequence of characters that continue up to and including the first null character. A character array is a memory allocation unit. A string might not use the entire character array allocated for it (indeed it is possible that it may even exceed the bounds of the containing array). If you want to diagnostically print an array rather than a string, you would need to iterate over the array in a loop. See below:
#include<stdio.h>
#include<string.h>
void terminateString(char *str){
str[3] = 0;
printf("string after termination is:%s\n",str);
}
int main(){
char str[]="abababcdfef";
terminateString(str);
for (int i = 0; i < sizeof(str)/sizeof(str[0]); i++) {
(str[i] != 0) ? printf("%c ", str[i]) : printf("\\0 ");
}
printf("\n");
return 0;
}
// OUTPUT
// string after termination is:aba
// a b a \0 a b c d f e f \0
c/c++ doesn't really distinguish between 0, '\0', and NULL, they're all just 0 in memory. c style strings are a sequence of characters that end with '\0', so every function that works with them ends after it finds this char. When you assign str[3]=0; it's the same as str[3]='\0'; i.e. stop the string after 3 chars. If you want the letter 0, do str[3]='0';, where the single quotes let the compiler know you want the character 0, ascii 48
Edit:
Note that NULL is a macro that evaluates to 0, not the same as nullptr
apparently starting with C++11 NULL can evaluate to nullptr, in C or C++98, it is 0
http://www.cplusplus.com/reference/cstring/NULL/

Printing the value of a 0-initialized array element prints nothing, why?

I have to initialize a char array to 0's. I did it like
char array[256] = {0};
I wanted to check if it worked so I tried testing it
#include <stdio.h>
int main()
{
char s[256] = {0};
printf("%c\n", s[10]);
return 0;
}
After I compile and run it, the command line output shows nothing.
What am I missing ? Perhaps I initialized the array in a wrong manner ?
TL;DR -- %c is the character representation. Use %d to see the decimal 0 value.
Related , from C11, chapter §7.21.6.1, (emphasis mine)
c If no l length modifier is present, the int argument is converted to an
unsigned char, and the resulting character is written.
FYI, see the list of printable values.
That said, for a hosted environment, int main() should be int main(void), at least to conform to the standard.
You are printing s[10] as a character (%c), and the numeric value of s[10] is 0, which represents the character \0, which means end of string and has no textual representation. For this reason you are not seeing anything.
If you want to see the numeric value instead of the character value, use %d to print it as a decimal (integer) number:
printf("%d\n", s[10]);
Note that end of string isn't the same as end of line, as said in one of your comments. End of string means that any string operation over a character sequence must stop when the first \0 arrives. If the character sequence has anything else after \0, it won't be printed, because the string operation stops on the first \0 character.
An end of line is, however, a normal character, which visual effect is to say the terminal or text editor to print the next character after the end of line in a new line.
If you want to have a vector full of end of line characters (and print them as such), you have to travel the vector and fill it:
char s[256];
int i;
for (i = 0; i < 256; ++i)
s[i] = '\n';
printf("%c\n", s[10]);
The ASCII (decimal/numerical) value of the end of line character (\n) is 12, so, the following snippet will be equivalent:
char s[256];
int i;
for (i = 0; i < 256; ++i)
s[i] = 12;
printf("%c\n", s[10]);
That doesn't work however (it doesn't print a new line):
char s[256] = {'\n'}; // or {12};
printf("%c\n", s[10]);
because the effect of {'\n'} is to assign \n to the first element of the array, and the remainings 255 character are filled with value 0, no matter which type of array are you making (char[], int[] or whatever). If you write an empty pair of brackets {}, all the elements will be 0.
So, these two statements are equivalent:
char s[256] = {}; // Implicit filling to 0.
char s[256] = {0}; // Implicit filling to 0 from the second element.
However, without defining the array:
char s[256];
The array is not filling (not initialized), so, each element of s will have anything, until you fill it with values, for example, with a for.
I hope with all of this examples you get the whole picture.

Can i assign a integer to a character pointer array?

int k=5;
char* result = (char *)malloc(100 * sizeof(char));
result[count] = k;
Considering the above code, if I print the contents of result[count] it prints smiley symbols. I tried like below,but of no use.
result[count]=(char)k;
Can anyone help me?
I am percepting that malloc(100*sizeof(char)) will create 100 blocks each of size of character contiguously. Please correct me if I am wrong?
I'll disregard all the fluff about arrays and malloc as it is irrelevant to the problem you describe. You seem to essentially be asking "What will this code print:"
char c = 5;
printf("%c", c);
It will print the symbol 5 in your symbol table, most likely a non-printable character. When encountering non-printable characters, some implementations choose to print non-standard symbols, such as smileys.
If you want to print the number 5, you have to use a character literal:
char c = '5';
printf("%c", c);
or alternatively use poor style with "magic numbers":
char c = 53; // don't write code like this
printf("%c", c);
It is a problem of character representation. Let's begin by the opposite first. A char can be promoted to an int, but if you do you get the representation of the character (its code). Assuming you use ASCII:
char c = '5';
int i = c; // i is now 0x35 or 53 ASCII code for '5'
Your example is the opposite, 5 can be represented by a char, so result[count] = k is defined (even if you should have got a warning for possible truncation), but ASCII char for code 5 is control code ENQ and will be printed as ♣ on a windows system using code page 850 or 437.
A half portable (not specified, but is known to work on all common architectures) way to do the conversion (int) 5 -> (char) '5' is to remember that code '0' to '9' are consecutive (as are 'A' to 'Z' and 'a to 'z'), so I think that what you want is:
result[count] = '0' + k;
First, let's clean up your code a bit:
int k=5;
char* result = malloc(100);
result[count] = k;
sizeof(char) is always equal to 1 under any platform and you don't need to cast the return of malloc() in C.
This said, as long as your count variable contains a value between 0 and 99, you're not doing anything wrong. If you want to print the actual character '5', then you have to assign the ASCII value of that character (53), not just the number 5. Also, remember that if that array of chars has to be interpreted as a string, you need to terminate it with '\0':
int k=5, i;
char* result = malloc(100);
for (i=0; i<98; i++) {
result[i] = ' ';
}
result[98] = 53;
result[99] = '\0';
printf("%s\n", result);
The elements in the array you're trying to allocate are all the size of a char, 1 byte. An int is 4 bytes thus your issues. If you want to store ints in an array you could do:
int result[100];
Followed by storing the value, but honestly from the way your questioned is posed I don't know if that's actually what you're trying to do. Try rephrasing the post with what you're trying to accomplish.
Are you trying to store 5 as a character?

Resources