Sunday, August 2, 2009

Null terminator is there in char data type not in int data type why?

i am taking about c....


if i do


char name[7];


int age[7];





then i can store 6 char in name and 1 will be null terminator but in age i can store 7 number......


why?


can you pls let me know....

Null terminator is there in char data type not in int data type why?
who told u can store only 6 chars in name?? name can be of size 7 chars..frm [0] to [6]
Reply:I did not think that C had any form of "bounds checking" on any primitive type arrays. You need to put in your own end-of-line or null reference. This is part of the power and problem of C in that you can access memory beyond what you should because there is no explicit bounds checking.
Reply:Dude


you have a misconception


in C language if you initiate with char[7] then you will get from 7 entries from 0 to 7 and 8th will be null and this is required for ending the strings( which obviously is series of characters).





string ending is not required in in data arrays as you dont have to perform string functions on that.
Reply:the null terminator is '\0', but i think for an array on the stack it would not need that, but i could be totally wrong.





if you declare a buffer (on the heap) of length 7, you can only have 6 chars due to the null-terminator.





but i do c# these days, c's not my gig anymore.
Reply:because characters are any button on they keyboard and integers are whole numbers. it is the way the functions in the compiler allow interaction with the header files and the os


No comments:

Post a Comment