Status of array with characters [duplicate] - c

This question already has answers here:
When initializing a char array, is the remaining space zero filled or uninitialized?
(2 answers)
Closed 9 years ago.
I was declaring a char array like this-
char str[16]= "The world is";
I know that its 13th character will be '\0'. But I am curious about rest of the character values till 16 in str. Are all of them assigned to '\0'. I searched it but could not find a good explanation. So I thought another fastest way is to ask here.:)

The rest will contain zero. What you are actually doing is initializing your array as follows:
char str[16]= {'T','h','e',' ','w','o','r','l','d',' ','i','s','\0'};
If you initialize an array in C that you have given a length, with something that is shorter, C will fill the rest with 0.
Example: this will give an array filled with zero's
int str[16]= {0};
so what you wrote is equivalent to:
char str[16]= {'T','h','e',' ','w','o','r','l','d',' ','i','s','\0',0,0,0};
Note that 0 == '\0' (both 0000...)

Related

string without null terminator [duplicate]

This question already has answers here:
Undefined, unspecified and implementation-defined behavior
(9 answers)
Using printf with a non-null terminated string
(6 answers)
Closed 1 year ago.
#include <stdio.h>
int main()
{
char as[4];
*as='0';
*(as+1)='1';
*(as+2)='2';
*(as+3)='3';
printf("%s",as);
return 0;
}
The output i got is : 0123.
In the above program i declared an array of size 4 -> char as[4];
and in that i stored 4 chars 0,1,2,3.
Normally if we declare a char array, '\0' will be stored at last (i.e. as[3]='\0');
but i stored '3' in it. how it did not generate error?.
There is absolutely nothing stopping you from using an array of char as an array of char. There could be any number of reasons to want this.
However, C strings are null-terminated by definition. Using the %s specifier in printf tells it to expect a null-terminated string, so your program will (probably) not work correctly unless you give it such a string.

How to convert integer to char array[]? [duplicate]

This question already has answers here:
How can I convert an int to a string in C?
(10 answers)
Closed 3 years ago.
Please read till the end.
I have a function that loops through a character array until it finds the '\0' end character in a character array.
I want to convert the integer
int number = 128;
To a character array
char data[] = ""; // data[] = "128" , contains '\0'
I have found several ways suggested (memcpy, itoa) but I want a conversion that adds the '\0' automatically at the end.
itoa() automatically adds a null terminator.
See: http://www.cplusplus.com/reference/cstdlib/itoa/

Alternative to strlen not breaking on 0 [duplicate]

This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 4 years ago.
Is there any better way of getting the right length of array containing digits?
I have an array of digits: 0, 0, 1 and I try to get length of it. It obviously breaks and returns 0. I am new to C but I tried to make custom strlen function:
int custom_strlen(char *str) {
for(int i = 1; ;i++) {
if (str[i] == 0) {
return i;
}
}
return -47;
}
but it is not that efficient and in some cases returns unexpected values as well. The expected out put would be 3 in this case.
Is there any function to use?
An array of integers is not a string. C arrays do not contain length information inherently. The way strlen works is that C strings are null terminated, meaning the last character is NUL (null character), which is 0. Otherwise, there is just no way to know how long an array is.
I think you may be wanting to do an array of '0','0','1'. Can you post the array you are using?
As mentioned, C strings are null terminated.
The only choices are
Using a string terminated with some special character that you watch for (like a null) or
Keeping track of how long the string is when you create it.
FWIW, if it's not null terminated, it's not actually a string in C, it's just memory that contains chars that you happen to be interpreting as a string.

How do I calculate the number of chars in a string (if not all the space is taken) [duplicate]

This question already has answers here:
Getting wrong string length
(3 answers)
Closed 4 years ago.
I have this piece of code:
char* input = malloc(sizeof(char)*100);
scanf("%s", input); //let's say that the number of chars in "%s" is 5
How do I calculate how many chars I typed in (5)? I tried by playing around with sizeof(), but couldn't find a solution.
Edit (better explanation): the input variable can host up to 100 chars, but let's say I type in the terminal 'abcde': then it hosts only 5 chars, the other 95 are not taken. I want to calculate that '5'.
You have to find the null terminator.
int i = 0;
while(input[i] != 0) {
++i;
}
//i marks the spot
But yeah, strlen() does a better job, since it has some improved/optimized searching, since it uses word(16/32/64? bit) compare and stuff.

comparing strings using equality sign in C programming [duplicate]

This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 4 years ago.
#include<stdio.h>
int main()
{
char str1[] = "ComputerProgram";
char str2[] = "ComputerProgram";
(str1==str2)? (printf("Equal")):(printf("unequal"));
return 0;
}
The answer according to me should be equal but it comes out to be unequal.
However if I use strcmp(str1,str2) == 0 answer comes out to be equal. How is it working in == case.? Also, I tried to print the ASCII values of srt1 and str2, they came out to be different. So I think that might be the reason. Now the problem is how does == work for strings?
Your arrays str1 and str2 will decay to pointers to their first elements when you compare them. That is, you compare two pointers that will never be equal.
In short, your comparison str1 == str2 is equal to &str1[0] == &str2[0].
What strcmp does differently is that is compares each character in the first string against each corresponding character in the other string, in a loop.
str1==str2 is comparing the addresses of the strings, not the strings themselves.strcmp will go to these addresses and compare all characters.

Resources