Null Char

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2008
Posts: 40
Reputation: cutedipti is an unknown quantity at this point 
Solved Threads: 0
cutedipti cutedipti is offline Offline
Light Poster

Null Char

 
0
  #1
Oct 1st, 2008
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?
Thanks & Regards
Ms. Dipti
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Null Char

 
0
  #2
Oct 1st, 2008
Originally Posted by cutedipti View Post
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
  1. int a[3] = {1, 2};
ie a[2] will get the value of zero.



Originally Posted by cutedipti View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 40
Reputation: cutedipti is an unknown quantity at this point 
Solved Threads: 0
cutedipti cutedipti is offline Offline
Light Poster

Re: Null Char

 
0
  #3
Oct 3rd, 2008
Originally Posted by grumpier View Post
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
  1. 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)?
Thanks & Regards
Ms. Dipti
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 429
Reputation: Denniz is on a distinguished road 
Solved Threads: 15
Denniz's Avatar
Denniz Denniz is offline Offline
Posting Pro in Training

Re: Null Char

 
1
  #4
Oct 3rd, 2008
When you are using the printf function:
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,042
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 178
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Null Char

 
1
  #5
Oct 3rd, 2008
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 variable a were an array of floats or ints.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Null Char

 
1
  #6
Oct 3rd, 2008
Originally Posted by cutedipti View Post
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 char and 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).
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Null Char

 
0
  #7
Oct 4th, 2008
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,042
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 178
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Null Char

 
0
  #8
Oct 4th, 2008
Originally Posted by WaltP View Post
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."
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Null Char

 
0
  #9
Oct 4th, 2008
A c-style string (eg a string literal) is an array of char that, by convention, is terminated with '\0'.
Last edited by grumpier; Oct 4th, 2008 at 5:34 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC