473,796 Members | 2,737 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 4483
Chris Croughton wrote:
On Mon, 30 May 2005 20:51:41 GMT, August Karlstrom
<fu********@com hem.se> wrote:

Chris Croughton wrote:
If I want a strongly typed language I know where to find Ada <g>...


Or why not Oberon-2.

Not powerful enough. For instance, one of the features I like in Ada is
declaring variables with specific ranges (-3..7 etc.), so that they
can't go out of bound without throwing an exception. Oberon-2 was, I
gather, created as a language for teaching, like Pascal (but later so it
learnt from some of the pitfalls of early Pascal), Ada was designed for
Real World(tm) tasks.

(That's not a criticism of Oberon-2, which is fine for its purpose, it's
just not as good for my purposes...)


No, this is a quite common misconception that is probably one of the
reasons the language isn't more widespread than it is. Oberon/Oberon-2
is not merely a teaching language. It was used to build an entire
operating system, namely Oberon. In Oberon-2 you isolate the potentially
dangerous low level code in so called SYSTEM modules. If you import the
SYSTEM pseudo module you can do *anything*. I recommend reading Stefan
Metzeler's article at
http://www.amadeus-3.com/main_files/oberon2vsCPP.php (although he is
biased in favour of the product he sells, he has some good points).

-- August
Nov 14 '05 #31

"Chris Croughton" <ch***@keristor .net> wrote
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.


I would have no problem with you designing such a language, since that
language would not be C and so I wouldn't be using it <g>.

Here's what happens in Java.

We want to attach an integer to a generic structure (let's say a tree).

To retrive our integer.

Object *obj = tree.getMyObjec t();
if( obj instanceof Integer)
int x = ((Integer) obj).intValue() ;
in C

void *ptr = tree_getmyobjec t(tree);
if(ptr)
x = *(int*) ptr;

Java can offer somewhat better safety, but this is largely illusory. If
there is no object both C and Java can return null, whilst if the tree has
been corrupted in some way so that it no longer holds integers, the Java
program will treat it the same as a null (unless you add more lines of code)
whilst the C program will be corrupted and most probably crash a few lines
later. Neither is ideal because, once your program has encountered an error,
there is no ideal way of dealing with the situation.
Nov 14 '05 #32
In article <11************ **********@g43g 2000cwa.googleg roups.com> "Old Wolf" <ol*****@inspir e.net.nz> writes:
Dik T. Winter wrote:

'\377' is an int with value 255


Is that correct? I know that '\xFF' is implementation-defined
as to whether it is -1 or 255 ..


Indeed, I see now that "the value is the one that results when an object
with type char whose value is that of the single character or escape
sequence is converted to type int". So there is a good reason SPlint
does not see the difference between '\377' and EOF on some systems.
--
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 #33
Old Wolf wrote:
Dik T. Winter wrote:
'\377' is an int with value 255

Is that correct? I know that '\xFF' is implementation-defined
as to whether it is -1 or 255 ..


Fascinating. 'Splain me this Batman.

C:\work\c\clc>c at ow.c
#include <stdio.h>
int main(void) {
printf("%d %d %d %d\n", '\xff', 0xff, 0377, '\377');
return 0;
}

C:\work\c\clc>o w
-1 255 255 -1

I thought they'd print the same thing. Is this implementation-defined as
well?

--
Joe Wright mailto:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #34
Keith Thompson wrote:
... 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, ...


ISO/IEC 9899 (Committee Draft  January 18, 1999):
6.5.16 Assignment operators

[#3] ... The type of an assignment expression is
the type of the left operand unless the left operand has
qualified type, in which case it is the unqualified version
of the type of the left operand. ...

6.5.16.1 Simple assignment

[#2] In simple assignment (=), the value of the right
operand is converted to the type of the assignment
expression and replaces the value stored in the object
designated by the left operand.

--
Dietmar Schindler
Nov 14 '05 #35
(Note followups to poster...)

On Tue, 31 May 2005 15:04:02 GMT, August Karlstrom
<fu********@com hem.se> wrote:
Chris Croughton wrote:

Not powerful enough. For instance, one of the features I like in Ada is
declaring variables with specific ranges (-3..7 etc.), so that they
can't go out of bound without throwing an exception.
Which Oberon-2 doesn't support (and C, C++ and most other languages
don't support either, of course).

Other things:

Strings can't contain quote marks (no 'escape' like backslash). It
isn't clear from the specification whether they can contain
non-printing characters like tab and newline.

No unsigned types (implicit 2's-complement arithmetic?). No bit
operators except (sort of) in SYSTEM.

Even less specification of sizes of integer types than in C90 (as far
as I can see everything from LONGREAL to SHORTINT could be a single
bit!).

Files? I/O in general?
Oberon-2 was, I
gather, created as a language for teaching, like Pascal (but later so it
learnt from some of the pitfalls of early Pascal), Ada was designed for
Real World(tm) tasks.
No, this is a quite common misconception that is probably one of the
reasons the language isn't more widespread than it is. Oberon/Oberon-2
is not merely a teaching language. It was used to build an entire
operating system, namely Oberon.


I wonder how, I see nothing in the specification:

http://www.zel.org/aos/o2report.htm

which even mentions input or output in any standard way. SYSTEM defines
procedures for accessing 'memory' directly, but nothing to access I/O
(not all I/O is memory-mapped!). Presumably all of the "nasty bits" are
written in C or assembler and then linked in (in some unspecified way).

(All other copies of the specification seem to be variants of the one I
quoted.)
In Oberon-2 you isolate the potentially
dangerous low level code in so called SYSTEM modules. If you import the
SYSTEM pseudo module you can do *anything*. I recommend reading Stefan
Metzeler's article at
http://www.amadeus-3.com/main_files/oberon2vsCPP.php (although he is
biased in favour of the product he sells, he has some good points).


He's very biased against C and C++, certainly. His Deutsche Bank
example is just stupid, why they bothered to use C++ at all I don't
understand and that sort of draconian limitation is certainly not
necessary to writing safe and maintainable C or C++ programs. Similarly
with his Microsoft example, the fact that MS write unsafe code is
nothing to do with using C++, it's because (a) they have to try to keep
backwards compatibility with systems which were never designed for
security (right back to MSDOS!) and (b) because they have a design goal
of "more features" (and especially flashy ones).

His rant against C++ boils down to "I don't like C++" and "I like
Oberon-2". Some of his statements are just plain wrong (his section on
"local procedures", with the idea of putting a for loop into a local
procedure to declare the loop variable -- huh? Either he means a local
block, which C and C++ have, or he's really confused).

And what on earth is this term 'accolades' used for braces (squiggly
brackets, {})? The dictionary definition of 'accolade' is "An
expression of approval; praise. A ceremonial embrace, as of greeting or
salutation. Ceremonial bestowal of knighthood." (American Heritage
Dictionary, via http://dictionary.reference.com/). He uses the (spit!)
K&R style as an example of how difficult it is to line them up, ignoring
styles which make it easy to do so.

Chris C
Nov 14 '05 #36
Joe Wright wrote:
Fascinating. 'Splain me this Batman.

C:\work\c\clc>c at ow.c
#include <stdio.h>
int main(void) {
printf("%d %d %d %d\n", '\xff', 0xff, 0377, '\377');
return 0;
}

C:\work\c\clc>o w
-1 255 255 -1

I thought they'd print the same thing. Is this implementation-defined as
well?


I'm not Batman, but I'll try regardless.
ISO/IEC 9899 (Committee Draft -- January 18, 1999):
6.4.4.4 Character constants

[#10] ... If an integer character constant
contains a single character or escape sequence, its value is
the one that results when an object with type char whose
value is that of the single character or escape sequence is
converted to type int.

[#13] EXAMPLE 2 Consider implementations that use two's-
complement representation for integers and eight bits for
objects that have type char. In an implementation in which
type char has the same range of values as signed char, the
integer character constant '\xFF' has the value -1; if type
char has the same range of values as unsigned char, the
character constant '\xFF' has the value +255 .

--
Dietmar Schindler
Nov 14 '05 #37
Keith Thompson <ks***@mib.or g> writes:
"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).


Just a minor nit. The word 'compatible' has a specific meaning in the
C standard document (section 6.2.7). In that sense of the word, the
types here are not compatible. So the wording of message seems
technically correct, even if it may be misleading.

I basically agree with everything else you said. It would be nice if
there were standard language for when types are "assignable "; even
nicer would be if the standard language were consistent with common
usage. Using "compatible " for a circumstance that really means
something more like "equivalent " seems an unfortunate choice, at least
in retrospect.

(And this doesn't even address another kind of "compatibility" , like
pointers to different structure types, where types are guaranteed to
have the same representation and alignment requirements, yet are
neither compatible nor assignable.)
Nov 14 '05 #38
Dietmar Schindler <dS***@arcor.de > writes:
Keith Thompson wrote:
... 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, ...


ISO/IEC 9899 (Committee Draft  January 18, 1999):
6.5.16 Assignment operators

[#3] ... The type of an assignment expression is
the type of the left operand unless the left operand has
qualified type, in which case it is the unqualified version
of the type of the left operand. ...

6.5.16.1 Simple assignment

[#2] In simple assignment (=), the value of the right
operand is converted to the type of the assignment
expression and replaces the value stored in the object
designated by the left operand.


C99 6.3.1.1 p2, p3 says:

The following may be used in an expression wherever an int or
unsigned int may be used:

-- An object or expression with an integer type whose integer
conversion rank is less than the rank of int and unsigned int.

-- A bit-field of type _Bool, int, signed int, or unsigned int.

If an int can represent all values of the original type, the value
is converted to an int; otherwise, it is converted to an unsigned
int. These are called the integer promotions.48) All other types
are unchanged by the integer promotions.

It's not clear to me whether the integer promotions are applied to the
RHS of an assignment operator. If they are, then "the value of the
right operand" would refer to the value after conversion. If not, it
refers to a value of type char.

--
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 #39
Keith Thompson <ks***@mib.or g> writes:
Dietmar Schindler <dS***@arcor.de > writes:
Keith Thompson wrote:
... 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, ...


ISO/IEC 9899 (Committee Draft ^T January 18, 1999):
6.5.16 Assignment operators

[#3] ... The type of an assignment expression is
the type of the left operand unless the left operand has
qualified type, in which case it is the unqualified version
of the type of the left operand. ...

6.5.16.1 Simple assignment

[#2] In simple assignment (=), the value of the right
operand is converted to the type of the assignment
expression and replaces the value stored in the object
designated by the left operand.


C99 6.3.1.1 p2, p3 says:

The following may be used in an expression wherever an int or
unsigned int may be used:

-- An object or expression with an integer type whose integer
conversion rank is less than the rank of int and unsigned int.

-- A bit-field of type _Bool, int, signed int, or unsigned int.

If an int can represent all values of the original type, the value
is converted to an int; otherwise, it is converted to an unsigned
int. These are called the integer promotions.48) All other types
are unchanged by the integer promotions.

It's not clear to me whether the integer promotions are applied to the
RHS of an assignment operator. If they are, then "the value of the
right operand" would refer to the value after conversion. If not, it
refers to a value of type char.


It seems as though the integer promotions are not applied to the RHS
of an assignment operator. Footnote 48 says

"The integer promotions are applied only: as part of the usual
arithmetic conversions, to certain argument expressions, to the
operands of the unary +, -, and ~ operators, and to both operands of
the shift operators, as specified by their respective subclauses."

The descriptions of other operators explicitly cite the usual
arithmetic conversions (eg, 6.5.6 p4). Furthermore, looking up "usual
arithmetic conversions" in the index, it lists: 6.3.1.8, 6.5.5,
6.5.6, 6.5.8, 6.5.9, 6.5.10, 6.5.11, 6.5.12, 6.5.15. No mention of
assignment (6.5.16).
Nov 14 '05 #40

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
10465
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
10242
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9061
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
7558
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
6800
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
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4127
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
3
2931
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.