6 Replies - 239 Views - Last Post: 21 April 2013 - 01:52 AM Rate Topic: -----

#1 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 10-October 12

is it the same ? (character arrays)

Posted 20 April 2013 - 09:58 AM

Lets assume we have a char my_array[100] array that has a random content for example "fksdkfsf"
if i want to check if this string is over i use either
for (i=0;i<10;i++){

if(b[i]==0){

printf("end of string");

break;}
}


or


for (i=0;i<10;i++){

if(b[i]=='\0'){

printf("end of string");

break;}
}


when i run them they seem to do the same work, but do they ? please enlighten me :)

Is This A Good Question/Topic? 0
  • +

Replies To: is it the same ? (character arrays)

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,599
  • Joined: 12-December 12

Re: is it the same ? (character arrays)

Posted 20 April 2013 - 10:15 AM

0 is an integer constant literal, '\0' is defined to be a null character - that is, a character with all bits set to zero. So, no, there is no difference, although IMO using '\0' makes your intention clearer - that you are looking for the null (terminating) character.
Was This Post Helpful? 2
  • +
  • -

#3 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 10-October 12

Re: is it the same ? (character arrays)

Posted 20 April 2013 - 10:17 AM

View Postandrewsw, on 20 April 2013 - 10:15 AM, said:

0 is an integer constant literal, '\0' is defined to be a null character - that is, a character with all bits set to zero. So, no, there is no difference, although IMO using '\0' makes your intention clearer - that you are looking for the null (terminating) character.

so in every cases it will be working fine with both 0 and '\0' but its better to use '\0' to make my code more understandable ?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,599
  • Joined: 12-December 12

Re: is it the same ? (character arrays)

Posted 20 April 2013 - 10:20 AM

View Postfledgeling, on 20 April 2013 - 05:17 PM, said:

View Postandrewsw, on 20 April 2013 - 10:15 AM, said:

0 is an integer constant literal, '\0' is defined to be a null character - that is, a character with all bits set to zero. So, no, there is no difference, although IMO using '\0' makes your intention clearer - that you are looking for the null (terminating) character.

so in every cases it will be working fine with both 0 and '\0' but its better to use '\0' to make my code more understandable ?

Yes. Although, I suspect that most programmers would be equally happy to use 0. It is a minor point. Andy.
Was This Post Helpful? 2
  • +
  • -

#5 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 10-October 12

Re: is it the same ? (character arrays)

Posted 20 April 2013 - 10:21 AM

View Postandrewsw, on 20 April 2013 - 10:20 AM, said:

View Postfledgeling, on 20 April 2013 - 05:17 PM, said:

View Postandrewsw, on 20 April 2013 - 10:15 AM, said:

0 is an integer constant literal, '\0' is defined to be a null character - that is, a character with all bits set to zero. So, no, there is no difference, although IMO using '\0' makes your intention clearer - that you are looking for the null (terminating) character.

so in every cases it will be working fine with both 0 and '\0' but its better to use '\0' to make my code more understandable ?

Yes. Although, I suspect that most programmers would be equally happy to use 0. It is a minor point. Andy.

ok then, thanks a lot Andy :D
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1051
  • View blog
  • Posts: 4,458
  • Joined: 09-June 09

Re: is it the same ? (character arrays)

Posted 20 April 2013 - 01:38 PM

bookmark this: http://www.asciitable.com/
Was This Post Helpful? 1
  • +
  • -

#7 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 10-October 12

Re: is it the same ? (character arrays)

Posted 21 April 2013 - 01:52 AM

just did, thanks :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1