473,796 Members | 2,599 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
39 4482
August Karlstrom wrote:
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)


A nil pointer? Perhaps you mean NULL pointer constant, but this would
not apply to type char which is being discussed here...

Rob Gamble

Nov 14 '05 #11
Robert Gamble wrote:
August Karlstrom wrote:
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)

A nil pointer? Perhaps you mean NULL pointer constant, but this would
not apply to type char which is being discussed here...


I used "nil pointer" to emphasize the distinction between the "points to
nothing" concept and the C macro constant NULL. In Oberon for instance,
NIL is a keyword (typeless constant) used for pointers and variables of
procedure type.

-- August
Nov 14 '05 #12
August Karlstrom wrote on 28/05/05 :
If c is a char then is there any difference between

c = '\0'

and

c = 0


No semantic difference. It's only a style issue.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair

Nov 14 '05 #13
August Karlstrom wrote on 29/05/05 :
If c is a char then is there any difference between
c = '\0'
and
c = 0

The Lint program Splint thinks there is a difference:
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


It's true that and int is not a char. Why in the world are you using a
char ? The cases where a single char is necessary are extremely rare
(only scanf("%c", &c) comes to my mind, and scanf() is not a
recommended function...)

That said, Lint seems to consider that '\0' is a char that is wrong in
C. (Or maybe, Lint is checking in C++ mode, in that case 0 is an int
and '\0' is a char). Your extension seems to be .c, that is correct for
a C-program. Check the Lint configuration.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.

Nov 14 '05 #14
Robert Gamble wrote on 29/05/05 :
(The literal 0 can also denote the nil pointer)


A nil pointer? Perhaps you mean NULL pointer constant, but this would
not apply to type char which is being discussed here...


'nil pointer' is a generic term used in Computer Science. Its
C-implementation is null pointer constant or NULL.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair

Nov 14 '05 #15
Dik T. Winter wrote on 29/05/05 :
<...> The conclusion is that Splint does not handle character
constants correctly.


Arf! Who has checked the checker ?

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.

Nov 14 '05 #16
August Karlstrom wrote:
Robert Gamble wrote:
August Karlstrom wrote:
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)

A nil pointer? Perhaps you mean NULL pointer constant, but this would
not apply to type char which is being discussed here...


I used "nil pointer" to emphasize the distinction between the "points to
nothing" concept and the C macro constant NULL.


You said that literal 0 can denote the "nil pointer". There is no nil
pointer in C, but there is a NULL pointer constant which can be
expressed as a literal 0 in a pointer context. I don't see any
distinction in your statement, on the contrary it looks like you are
confused. In C, the NULL pointer "points to nothing", there is no
seperate concept of nil in C.
In Oberon for instance,
NIL is a keyword (typeless constant) used for pointers and variables of
procedure type.
This is C and there is no such thing.
-- August


Rob Gamble

Nov 14 '05 #17
"Emmanuel Delahaye" <em***@YOURBRAn oos.fr> writes:
[...]
That said, Lint seems to consider that '\0' is a char that is wrong in
C. (Or maybe, Lint is checking in C++ mode, in that case 0 is an int
and '\0' is a char). Your extension seems to be .c, that is correct
for a C-program. Check the Lint configuration.
I wouldn't say that Splint (the particular lint implementation that
generates the message) is wrong to issue a warning. It's not required
to limit itself to complaining about violations of the standard. It's
diagnosting (what its authors see as) a style issue -- which is part
of what lint is supposed to do. I happen to agree with it in this
case; initializing a char object with '\0' is clearer than using 0,
even though it's semantically identical.

On the other hand, the error message itself is probably incorrect.
The message is:
test.c:6:4: Assignment of int to char: c = 0
Types are incompatible. (Use -type to inhibit warning)


In fact, types int and char are compatible for assignment (but not for
some other purposes). A naive programmer might infer from the message
that implicit conversions are a bad thing; this is a dangerous
assumption, since the obvious way to avoid them is to use casts, which
can be far more dangerous. And using the "-type" option would
probably inhibit some useful warnings.

A digression: In my opinion, C depends too heavily on implicit
conversions. If I were designing the language from scratch, most
conversions that are implicit in C would probably be explicit -- but
most of them would be expressed by some mechanism less heavyweight
than a cast. The resulting code would probably be more verbose than
typical C. If you like extreme terseness, be glad that I don't
actually design programming languages. But given the actual design of
C, it almost always makes sense to use implicit conversions rather
than explicit casts, which tend to swat flies with sledgehammers.
Implicit conversions are bad from a language design point of view, but
good (or at least far better than the alternative) from a C
programming point of view.

--
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 #18
"August Karlstrom" <fu********@com hem.se> wrote

I used "nil pointer" to emphasize the distinction between the "points to
nothing" concept and the C macro constant NULL. In Oberon for instance,
NIL is a keyword (typeless constant) used for pointers and variables of
procedure type.

Conventionally C uses all bits zero to represent an invalid pointer. On a
few machines this isn't useful (maybe because of hardware traps or something
like that) so a different bit pattern will be used. The constant zero and
the definition NULL always represent this pointer, however.

There is no reason you cannot use other addresses to represent invalid
pointers internally, however. I cannot think of a good reason for so doing,
but it would be perfectly possible to set up a global somewhere and then
test against it before reading or writing. These would be nil pointers but
not null pointers.

(The situation isn't quite as simple as I have painted it because it is
illegal to do any sort of calculation with an invalid pointer, except a null
pointer. So strategies along the lines of
#define MYNULL 12345678
will work on some compliers but not all. The act of loading this address
into a rgister may trigger an error.).

Nov 14 '05 #19
"Malcolm" <re*******@btin ternet.com> writes:
"August Karlstrom" <fu********@com hem.se> wrote

I used "nil pointer" to emphasize the distinction between the "points to
nothing" concept and the C macro constant NULL. In Oberon for instance,
NIL is a keyword (typeless constant) used for pointers and variables of
procedure type.

Conventionally C uses all bits zero to represent an invalid pointer. On a
few machines this isn't useful (maybe because of hardware traps or something
like that) so a different bit pattern will be used. The constant zero and
the definition NULL always represent this pointer, however.


I would state this differently, to de-emphasize the idea that
all-bits-zero is "convention al".

C uses some specific pointer representation to represent a null
pointer. This is represented in source by a null pointer constant,
such as a literal 0 or the macro NULL. On many (most?)
implementations the internal representation happens to be
all-bits-zero, but well written C code does not know or care what the
actual representation is; it can even be different for different
pointer types.

(If you had written "typically" rather than "conventionally ", I
wouldn't have had any quarrel.)

--
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 #20

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

Similar topics

34
7114
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
3023
by: b83503104 | last post by:
Hi, Can someone tell me the difference between single quote and double quote? Thanks
26
4422
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
2853
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
15759
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
4163
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
2717
by: Petronius | last post by:
Hallo, does anyone have an idea how to implement difference lists in Javascript? Thanks allot in advance
5
3490
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
2682
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
12127
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
9685
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
9533
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,...
0
10461
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10019
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9057
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
7555
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
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.