| | |
Null Char
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 40
Reputation:
Solved Threads: 0
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?
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
Ms. Dipti
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
•
•
•
•
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
You will get the same effect (other than type of elements) with
C Syntax (Toggle Plain Text)
int a[3] = {1, 2};
•
•
•
•
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?
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.
•
•
Join Date: Sep 2008
Posts: 40
Reputation:
Solved Threads: 0
•
•
•
•
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
ie a[2] will get the value of zero.C Syntax (Toggle Plain Text)
int a[3] = {1, 2};
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
Ms. Dipti
When you are using the printf function:
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.
C Syntax (Toggle Plain Text)
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.
Founder of :
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
By
>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
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
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
•
•
•
•
So, my query is, why this is happening only in case of strings and not for the others( like int, float, single character)?
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).
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
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.
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
![]() |
Similar Threads
- Copy a string to a 2d array of type char (C++)
- input file to char array (C)
- converting a char[] to an int (C)
- Null pointer on building a binary tree (Java)
- Function syntax to take in a char array and return keyboard input (C)
- Problem With char (C++)
- removing the null terminator at the end of string (C)
- Passing char * to function and populating inside (C++)
Other Threads in the C Forum
- Previous Thread: Need a way forward
- Next Thread: Binary (base 2) to Decimal (base 10) HELP!
Views: 2163 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable visualstudio voidmain() wab win32 windows.h






