954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Null Char

Hi,
When we define any integer array like
int a[3]={1,2,3};
then it will assign the values as a[0]=1, a[1]=2, and a[2]=3
But, when we define any character array as
char a[3]={'a','b'};
then compiler here automatically consider '\0' null character to represent the end of the string

So, my question here is:
As compiler do not needs to use any such terminator to indicate that the int array or float array is terminated. Then, why compiler needs to assign '\0' null terminator in char array & why not in others?

cutedipti
Light Poster
45 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 
Hi, But, when we define any character array as char a[3]={'a','b'}; then compiler here automatically consider '\0' null character to represent the end of the string


That's not true. You have explicitly initialised two elements of three-element array. The standards go about it in a round-about way (there's a logic train to follow to get to the conclusion, and the C and C++ standards have different logic trains) but the end result is that a[2] in your example will get a value of zero.

You will get the same effect (other than type of elements) with

int a[3] = {1, 2};

ie a[2] will get the value of zero. So, my question here is:
As compiler do not needs to use any such terminator to indicate that the int array or float array is terminated. Then, why compiler needs to assign '\0' null terminator in char array & why not in others?
It doesn't. If the number of explicitly initialised elements of an array is less that the size of the array, the standards specify how the other elements are initialised.

There is an anomaly in C (and C++) that allows char arrays to be initialised using strings (and arrays of wchar_t to be initialised with wide strings). Strings, by convention, are zero terminated. There is no equivalent to this style of initialisation for other types. But this anomaly is a different thing from what you're asking about.

grumpier
Posting Whiz in Training
211 posts since Aug 2008
Reputation Points: 193
Solved Threads: 32
 

That's not true. You have explicitly initialised two elements of three-element array. The standards go about it in a round-about way (there's a logic train to follow to get to the conclusion, and the C and C++ standards have different logic trains) but the end result is that a[2] in your example will get a value of zero.

You will get the same effect (other than type of elements) with

int a[3] = {1, 2};

ie a[2] will get the value of zero.

It doesn't. If the number of explicitly initialised elements of an array is less that the size of the array, the standards specify how the other elements are initialised.

There is an anomaly in C (and C++) that allows char arrays to be initialised using strings (and arrays of wchar_t to be initialised with wide strings). Strings, by convention, are zero terminated. There is no equivalent to this style of initialisation for other types. But this anomaly is a different thing from what you're asking about.

Hi,
Ya it's absolutely right that when we initialize array as:
a[3]={1,2} then it will assign last element to zero automatically.
But while considering as a string it must be terminated by '\0' null character.
when we write a code like:

int i;
char a[3]={'a','b','c'};
for(i=0;i<=2;i++)
printf("\t%c",a[i]);

then output for this code is as
a b c

but if it is like
int i;
char a[3]={'a','b','c'};
printf("%s",a);

then the output will be
abc with some garbage value means it's not 100% correct one

and if it is like
int i;
char a[3]={'a','b'};
printf("%s",a);

then the ouput will be 100% correct one as
ab

So, my query is, why this is happening only in case of strings and not for the others( like int, float, single character)?

cutedipti
Light Poster
45 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

When you are using the printf function:

printf("%s", a);


The function is treating variable "a" as a pointer to the beginning of a stream of characters. However, it doesn't have the information about how long the stream of characters is, it has no knowledge that "a" is defined as an array. Thus, the function will simply read from the beginning of the address that "a" points to, byte by byte, until it encounters the null character which indicates the end of a character string.

Integer, float and other types are not used to represent string.

Denniz
Posting Pro in Training
429 posts since Sep 2008
Reputation Points: 118
Solved Threads: 15
 

By char a[3]={'a','b'}; you are implicitly assigning a zero to a[2] which is what makes the array a string.

>So, my query is, why this is happening only in case of strings and not for the others( like int, float, single character)?
Because you can't do this printf("%s",a); if variablea were an array of floats or ints.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 
So, my query is, why this is happening only in case of strings and not for the others( like int, float, single character)?


Because the %s format specifier tells printf() and related function that the corresponding argument is a pointer to charand to keep printing chars until it finds a zero.

When an array is passed to a function (be it array of char, double, or structs) only a pointer to the first element is passed. No information about the number of elements in the array is passed. So the function has to use some convention (as printf() does with the %s specifier) or make an assumption (eg it assumes the array is of length 5).

grumpier
Posting Whiz in Training
211 posts since Aug 2008
Reputation Points: 193
Solved Threads: 32
 

There is a difference between a character array and a string. A character array
char a[3] = {'a', 'b', 'c'};
is not a string. Just because it happens to end in a '\0' when you don't define all the elements char a[3] = {'a', 'b'};
does not make it a string. Don't use it as such.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
There is a difference between a character array and a string. A character array char a[3] = {'a', 'b', 'c'}; is not a string. Just because it happens to end in a '\0' when you don't define all the elements char a[3] = {'a', 'b'}; does not make it a string. Don't use it as such.

I admit that I don't know if you are talking semantic definitions over here, but I disagree. What it makes an string a string is that '\0', and as long as the automatic assignment is honored by the compiler, that is for all intend and purposes a string. Whether the '\0' falls in the last subscript or before that.

Is it a lousy way of making a string? Sure. But not more than assigning the elements individually as a[0] = 'a'; a[1] = 'b'; a[2] = '\0';

So, please, explain "There is a difference between a character array and a string."

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

A c-style string (eg a string literal) is an array of char that, by convention, is terminated with '\0'.

grumpier
Posting Whiz in Training
211 posts since Aug 2008
Reputation Points: 193
Solved Threads: 32
 

hi im vikram my question is
WEATHER A NULL CHARACTER OCCUPY ANY SPACE IN MEMORY IF YES THAN HOW MUCH SPACE IT NEEDED?

VIKRAM KARKI
Newbie Poster
1 post since Mar 2011
Reputation Points: 10
Solved Threads: 0
 
hi im vikram my question is WEATHER A NULL CHARACTER OCCUPY ANY SPACE IN MEMORY IF YES THAN HOW MUCH SPACE IT NEEDED?

Why are you resurrecting an old thread(Oct 4th, 2008) and why the CAPS?

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You