473,785 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference between '\0' and 0

Hello all,

If c is a char then is there any difference between

c = '\0'

and

c = 0

?
Regards,
August
Nov 14 '05 #1
39 4474
August Karlstrom wrote:

Hello all,

If c is a char then is there any difference between

c = '\0'

and

c = 0

?


It doesn't matter what c is.
'\0' and 0 are both constant primary expressions
of type int and value zero.

--
pete
Nov 14 '05 #2


August Karlstrom wrote:
Hello all,

If c is a char then is there any difference between

c = '\0'

and

c = 0

?
Regards,
August

Er,in fact,in C, a char is an 8-bit integer(not all).The ASCII value of
'\0' is 0.So c='\0' equals to c=0 .In many programs,we use char instead
of int when the integer is small.

Nov 14 '05 #3
pete wrote:
August Karlstrom wrote:
Hello all,

If c is a char then is there any difference between

c = '\0'

and

c = 0

?

It doesn't matter what c is.
'\0' and 0 are both constant primary expressions
of type int and value zero.


The Lint program Splint thinks there is a difference:

$ cat test.c
int main(void)
{
char c;

c = '\0';
c = 0;
return 0;
}
$ splint test.c
Splint 3.1.1 --- 15 Jun 2004

test.c: (in function main)
test.c:6:4: Assignment of int to char: c = 0
Types are incompatible. (Use -type to inhibit warning)

Finished checking --- 1 code warning
-- August
Nov 14 '05 #4
yohji wrote:
Er,in fact,in C, a char is an 8-bit integer(not all).The ASCII value of
'\0' is 0.So c='\0' equals to c=0 .In many programs,we use char instead
of int when the integer is small.


1) In fact, '\0' is an int, not a char, so it the question of chars
doesn't enter into the question.

2) In fact, a char is *not* an 8-bit integer. It is an integral type
capable of representing one of the two ranges CHAR_MIN to CHAR_MAX (if
char is signed) or 0 to UCHAR_MAX (if char is unsigned). The minimum
size on a binary machine to be able to meet that for the minimal
requirements of the standard is 8 bits.

3) In fact, the ASCII value of anything is irrelevant in comp.lang.c,
where we expect programs to be appropriate for any environment that
meets the requirements for the source and execution character sets in
the corresponding contexts. There is nothing special about ASCII, which
is only one of many encodings.
Nov 14 '05 #5

"August Karlstrom" <fu********@com hem.se> wrote
It doesn't matter what c is.
'\0' and 0 are both constant primary expressions
of type int and value zero.


The Lint program Splint thinks there is a difference:

test.c:6:4: Assignment of int to char: c = 0
Types are incompatible. (Use -type to inhibit warning)

Lint is of limited value. '\0' is the character constant NUL that represents
a terminated string, whilst 0 is the integer value of nothing. However '\0'
== 0 is guaranteed to be true.
In fact use of a four character sequence doesn't add much to program
readbility, and may detract from it. Most code uses plain 0. It is also
quite common to see the construct

while(*str)
{
/* some code */
str++;
}

If you want strict type control, use a different language to C. C written to
make lint happy tends to be C that is hard to read.
Nov 14 '05 #6
On Sat, 28 May 2005 22:36:32 +0000, Martin Ambuhl wrote:
yohji wrote:
Er,in fact,in C, a char is an 8-bit integer(not all).The ASCII value of
'\0' is 0.So c='\0' equals to c=0 .In many programs,we use char instead
of int when the integer is small.


1) In fact, '\0' is an int, not a char, so it the question of chars
doesn't enter into the question.

2) In fact, a char is *not* an 8-bit integer. It is an integral type
capable of representing one of the two ranges CHAR_MIN to CHAR_MAX (if
char is signed)


It is CHAR_MIN to CHAR_MAX whether char is signed or unsigned. This
corresponds to SCHAR_MIN to SCHAR_MAX when char is signed.

Lawrence
Nov 14 '05 #7
Malcolm wrote:
Lint is of limited value. '\0' is the character constant NUL that represents
a terminated string, whilst 0 is the integer value of nothing.
(The literal 0 can also denote the nil pointer)
However '\0' == 0 is guaranteed to be true.
In fact use of a four character sequence doesn't add much to program
readbility, and may detract from it. Most code uses plain 0. It is also
quite common to see the construct

while(*str)
{
/* some code */
str++;
}

If you want strict type control, use a different language to C. C written to
make lint happy tends to be C that is hard to read.


Ok, I tend to agree.
-- August
Nov 14 '05 #8
In article <Pg************ *******@newsb.t elia.net> August Karlstrom <fu********@com hem.se> writes:
....
$ cat test.c
int main(void)
{
char c;

c = '\0';
c = 0;
return 0;
}
$ splint test.c
Splint 3.1.1 --- 15 Jun 2004

test.c: (in function main)
test.c:6:4: Assignment of int to char: c = 0
Types are incompatible. (Use -type to inhibit warning)


It is a bug that it does not complain about c = '\0'. In that case the
types are also "incompatib le". But the mindset of the writers can be
deduced from the following in the known bugs file:
Pre-processing ISO8859-1 characters
Splint does not process ISO8859-1 characters correctly since it is
building on a pre-ISO8859-1 version of gcc's pre-processor, and
character \377 is indistinguishab le from EOF.

'\377' is an intr with value 255, so quite distinguishable from EOF, which
is also an int. The conclusion is that Splint does not handle character
constants correctly.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #9
August Karlstrom <fu********@com hem.se> writes:
pete wrote:
August Karlstrom wrote:
Hello all,
If c is a char then is there any difference between
c = '\0'
and
c = 0
?

It doesn't matter what c is.
'\0' and 0 are both constant primary expressions
of type int and value zero.


The Lint program Splint thinks there is a difference:

$ cat test.c
int main(void)
{
char c;

c = '\0';
c = 0;
return 0;
}
$ splint test.c
Splint 3.1.1 --- 15 Jun 2004

test.c: (in function main)
test.c:6:4: Assignment of int to char: c = 0
Types are incompatible. (Use -type to inhibit warning)

Finished checking --- 1 code warning


The difference is only stylistic; there is absolutely no difference as
far as the language is concerned. I consider
c = '\0';
to be clearer than
c = 0;
because it implies to the reader (though not to the compiler) that a
character value is being assigned. Apparently the authors of splint
felt the same way.

Since a character constant, unlike an integer constant, normally can't
have a value outside the range of type char, it's not unreasonable for
splint to treat it as being of type char. But since even a reference
to a char object:
char c = '\0';
char x = c;
is promoted to int before being converted back to char, it's difficult
to know just what's going to keep splint from complaining.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
7112
by: yensao | last post by:
Hi, I have a hard time to understand difference and similarities between Relational database model and the Object-Oriented model. Can somebody help me with this? Thank you in advance. Yensao
21
3018
by: b83503104 | last post by:
Hi, Can someone tell me the difference between single quote and double quote? Thanks
26
4420
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the total number of days. This is the beginning: starttime = Date.parse("Aug 10,2003, 07:07") sdt = new Date(starttime)
21
2852
by: Rich | last post by:
I was considering C# for developing a scientific application, but I have noticed a ~30% difference between VC++ .NET and C# on the same machine, under identical conditions: double a = 0,b = 0, c = 0, d = 0, e = 0; for(int n = 0; n != 6000000; n++) { a = n % 5 *2 / 3 - 4 + 6 / 3 - n + n * 2; b = n * 2.3 - 1 *2 / 3 - 4 + 6 / 3 - n + n * 2; c = n * 3 / 3.5 *2 / 3 - 4 + 6 / 3 - n + n * 2;
4
15758
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow which calculated the difference in years and days between two dates, and takes leap years into account? I'm calculating the difference in the usual way, i.e....
3
4162
by: bbawa1 | last post by:
Hi, I have a table which has a field ItemsReceived of type datetime. I have a grid view which has two columns. In first column i have to show the data from field ItemsReceived and in second column I have to show difference between Currenttime and date from ItemReceived. How can I do that.
12
2716
by: Petronius | last post by:
Hallo, does anyone have an idea how to implement difference lists in Javascript? Thanks allot in advance
5
3489
by: Julius | last post by:
Hej dudes, I need to calc the difference between two timestamps / dates ... For example what i need to calculate: Date 1: 2007.11.06 - 20:13:04 Date 2: 2007.11.07 - 21:13:04 Difference: 1 day, 1hour
9
2680
by: viki1967 | last post by:
Hi all! This new forum its great! :) Congratulations !!! My answer: why this my code not working? Nothing error but not work the difference.... : <html>
11
12124
by: cmb3587 | last post by:
I have two arrays and I'm trying to create a 3rd array that is the difference between the two arrays Ex: arrayA: 3 5 8 9 arrayB: 3 4 6 9 difference of A-B: 5 8 however, my code is just returning me an array of 0's
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9485
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8986
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.