11 Replies - 728 Views - Last Post: 31 December 2018 - 09:54 AM Rate Topic: -----

#1 RyanMco   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 93
  • Joined: 27-December 18

fundamental knowledge of programming(Specifically in C)

Posted 31 December 2018 - 08:47 AM

Hi guys, I'm struggling a serious problem which couldn't understand it alone, so I appreciate your help in advance !


I'm struggling the concept of how several things aren't the same in "type" but they are the the same at the output. let me clear more, how is it actually to say str[5]=='\0' is the same to say str[5]==0 which they mean we arrived to the final of the string?(assuming that str is a string .. char *) and not specifically that case; I'm talking in general how multiple things with different types leading to the same output? and they are equal in meaning?! how we decide that? actually how could multiple things with different representation be the same in meaning(if there's an analogous for would be nice!!) although they're not the same in appearance(typesetting)?!
is there a theory in math that declares although different in representation, it could lead to the same meaning?
thanks in advance



thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: fundamental knowledge of programming(Specifically in C)

#2 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 934
  • View blog
  • Posts: 3,741
  • Joined: 13-June 14

Re: fundamental knowledge of programming(Specifically in C)

Posted 31 December 2018 - 08:57 AM

View PostRyanMco, on 31 December 2018 - 03:47 PM, said:

I'm struggling the concept of how several things aren't the same in "type" but they are the the same at the output. let me clear more, how is it actually to say str[5]=='\0' is the same to say str[5]==0


In C, a char is an int. The character '\0' is the sentinel character, which has an ASCII code of 0, so that its integer value is 0.

Quote

I'm talking in general how multiple things with different types leading to the same output? and they are equal in meaning?! how we decide that? actually how could multiple things with different representation be the same in meaning(if there's an analogous for would be nice!!) although they're not the same in appearance(typesetting)?!



I'm not really sure what you're on about here. It would help if you gave more examples.
Was This Post Helpful? 0
  • +
  • -

#3 dr3am1nc0d3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 01-December 18

Re: fundamental knowledge of programming(Specifically in C)

Posted 31 December 2018 - 09:17 AM

In C, there are no types of type String, instead you denote a string by an array of subsequent characters so type chat[10] firstName;

This actually allocates space for 9 characters as the final character is known as the null pointer (written as '\0').

This post has been edited by ndc85430: 31 December 2018 - 09:18 AM
Reason for edit:: Removed quote of OP.

Was This Post Helpful? 0
  • +
  • -

#4 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 934
  • View blog
  • Posts: 3,741
  • Joined: 13-June 14

Re: fundamental knowledge of programming(Specifically in C)

Posted 31 December 2018 - 09:19 AM

dr3am1nc0d3, there's no need to quote posts in full - it just wastes space. Just use the "Reply" button, or when you need to quote, just quote the minimal part you want to reply to.
Was This Post Helpful? 0
  • +
  • -

#5 german-one   User is offline

  • New D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 36
  • Joined: 05-August 18

Re: fundamental knowledge of programming(Specifically in C)

Posted 31 December 2018 - 09:22 AM

View Postndc85430, on 31 December 2018 - 08:57 AM, said:

In C, a char is an int.

This is actually not true. char and int are types used to represent an integral value but they are still different types. A char is an integral type with implementation-defined signedness, usually having a width of 8 bits. An int is a signed type that usually has a width of 32 bits (the standard only guarantees to have at least the same width as a char though).
However, it's true that you can assign an int to a variable of type char without getting any warning. The int will be narrowed implicitely. That is, only the least 8 bits of the value are used. In case of '\0' vs. 0 it's pretty simple. The binary representation of int 0 is 32 zero bits. Narrowed down to 8 bits results in 8 zero bits which is char '\0'.
Was This Post Helpful? 1
  • +
  • -

#6 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 934
  • View blog
  • Posts: 3,741
  • Joined: 13-June 14

Re: fundamental knowledge of programming(Specifically in C)

Posted 31 December 2018 - 09:23 AM

Thanks for the correction, german-one!
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2706
  • View blog
  • Posts: 4,315
  • Joined: 21-June 11

Re: fundamental knowledge of programming(Specifically in C)

Posted 31 December 2018 - 09:30 AM

Actually, in C, but not C++, character literals have type int, not char. So both 0 and '\0' would represent a 32-bit1,2 zero that is narrowed to an 8-bit2 integer when assigning to char.

In C++ on the other hand, single-byte character literals have type char and only multi-byte character literals have type int.

1 Assuming sizeof(int)==4
2 Assuming CHAR_BIT == 8

This post has been edited by sepp2k: 31 December 2018 - 09:31 AM

Was This Post Helpful? 2
  • +
  • -

#8 dr3am1nc0d3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 01-December 18

Re: fundamental knowledge of programming(Specifically in C)

Posted 31 December 2018 - 09:34 AM

If Character literals are of type int, then why is sizeof(char[10]) = 10 bytes as opposed to 40 bytes?
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6664
  • View blog
  • Posts: 22,744
  • Joined: 05-May 12

Re: fundamental knowledge of programming(Specifically in C)

Posted 31 December 2018 - 09:36 AM

I was about to follow-up with that. For reference, see C++ character literal vs. C character constant

There is a difference between the getting the size of a character literal, and getting the size of an array meant to store the type char like you are doing above.
Was This Post Helpful? 2
  • +
  • -

#10 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2706
  • View blog
  • Posts: 4,315
  • Joined: 21-June 11

Re: fundamental knowledge of programming(Specifically in C)

Posted 31 December 2018 - 09:40 AM

View Postdr3am1nc0d3, on 31 December 2018 - 05:34 PM, said:

If Character literals are of type int, then why is sizeof(char[10]) = 10 bytes as opposed to 40 bytes?


Because sizeof(char) is 1 and sizeof(char[10]) is ten times that. Character literals have nothing to do with that. The interesting size to check is sizeof('\0') (or any other single-character literal instead of '\0'), which is 41 in C and 1 in C++.

1 Or rather: whatever sizeof(int) is

This post has been edited by sepp2k: 31 December 2018 - 09:42 AM

Was This Post Helpful? 0
  • +
  • -

#11 german-one   User is offline

  • New D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 36
  • Joined: 05-August 18

Re: fundamental knowledge of programming(Specifically in C)

Posted 31 December 2018 - 09:46 AM

In terms of the OP's question we are not talking about char literals. '\0' is part of an array of char. Thus, int 0 will be narrowed if you assign it to an element of the array.
Was This Post Helpful? 0
  • +
  • -

#12 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2706
  • View blog
  • Posts: 4,315
  • Joined: 21-June 11

Re: fundamental knowledge of programming(Specifically in C)

Posted 31 December 2018 - 09:54 AM

View Postgerman-one, on 31 December 2018 - 05:46 PM, said:

In terms of the OP's question we are not talking about char literals.


OP's question was about 0 vs. '\0', the latter of which is a character literal (and thus has type int in C, just like 0 does).

Quote

Thus, int 0 will be narrowed if you assign it to an element of the array.


Yes, 0 will be narrowed, but so will '\0' (which is a character literal). That's all I'm saying.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1