473,503 Members | 6,587 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

char's and int's

I've seen a lot of functions in the standard library that deal with
characters, but a lot them return/take parameters of type int (which
is usually the integer that represents the character code in the
character set). I know that C automatically converts between int and
char in those cases without problems (or could the singed/unsigned
issue cause problems?), and that character constants have actually
type int, but, what is the reason for some functions in the standard
library to return type int (or take parameters of type int) when one
is supposed to be dealing with characters instead of numbers?

Thanks,
Sebastian

Jul 4 '08 #1
12 1752
On Jul 4, 12:49 pm, s0s...@gmail.com wrote:
I've seen a lot of functions in the standard library that deal with
characters, but a lot them return/take parameters of type int (which
is usually the integer that represents the character code in the
character set). I know that C automatically converts between int and
char in those cases without problems (or could the singed/unsigned
issue cause problems?), and that character constants have actually
type int, but, what is the reason for some functions in the standard
library to return type int (or take parameters of type int) when one
is supposed to be dealing with characters instead of numbers?

Thanks,
Sebastian
Cosider fgetc(). It actually returns an unsigned integer cast to an
int. Casting to the int is required because EOF is defined as negative
int(generally, but not necessarily, -1). So, other than the EOF, we
get an unsigned int.
Jul 4 '08 #2
rahul said:

<snip>
Cosider fgetc(). It actually returns an unsigned integer cast to an
int.
Since chars are integers, you are correct.
Casting to the int is required because EOF is defined as negative
int(generally, but not necessarily, -1). So, other than the EOF, we
get an unsigned int.
No, other than the EOF we get an int that is the result of a conversion of
an unsigned char to an int.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 4 '08 #3
On 4 Jul, 09:59, Richard Heathfield <r...@see.sig.invalidwrote:
rahul said:

<snip>
Cosider fgetc(). It actually returns an unsigned integer cast to an
int.

Since chars are integers, you are correct.
Casting to the int is required because EOF is defined as negative
int(generally, but not necessarily, -1). So, other than the EOF, we
get an unsigned int.

No, other than the EOF we get an int that is the result of a conversion of
an unsigned char to an int.
Don't you mean "a conversion of a char to an int" ?
Jul 4 '08 #4
gw****@aol.com said:
On 4 Jul, 09:59, Richard Heathfield <r...@see.sig.invalidwrote:
>rahul said:

<snip>
Cosider fgetc(). It actually returns an unsigned integer cast to an
int.

Since chars are integers, you are correct.
Casting to the int is required because EOF is defined as negative
int(generally, but not necessarily, -1). So, other than the EOF, we
get an unsigned int.

No, other than the EOF we get an int that is the result of a conversion
of an unsigned char to an int.

Don't you mean "a conversion of a char to an int" ?
No, I don't mean that.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 4 '08 #5
On 4 Jul, 10:09, Richard Heathfield <r...@see.sig.invalidwrote:
gw7...@aol.com said:
On 4 Jul, 09:59, Richard Heathfield <r...@see.sig.invalidwrote:
rahul said:
Casting to the int is required because EOF is defined as negative
int(generally, but not necessarily, -1). So, other than the EOF, we
get an unsigned int.
No, other than the EOF we get an int that is the result of a conversion
of an unsigned char to an int.
Don't you mean "a conversion of a char to an int" ?

No, I don't mean that.
Then could you explain, please? My understanding is that a type char
is provided, for storing, well, characters, in the system's favourite
format. Functions such as fgetc() are used for reading in characters,
but the output they give has to embrace both all real characters and
have something different to indicate EOF. Hence they return an int. So
I would have assumed that the values they can return, other than EOF,
are the values of characters which in turn are the values a char can
have. I don't see where you have got "unsigned" from. AFAIAA you
should use unsigned char rather than char when reading memory
locations storing a different type but I don't see how that is
relevant here.

Paul.
Jul 4 '08 #6
s0****@gmail.com writes:
I've seen a lot of functions in the standard library that deal with
characters, but a lot them return/take parameters of type int (which
is usually the integer that represents the character code in the
character set). I know that C automatically converts between int and
char in those cases without problems (or could the singed/unsigned
issue cause problems?), and that character constants have actually
type int, but, what is the reason for some functions in the standard
library to return type int (or take parameters of type int) when one
is supposed to be dealing with characters instead of numbers?
In very old versions of C (pre-1989), function prototypes did not
exist. An expression of type char passed as an argument would always
be promoted to int, and a function with no visible declaration was
assumed to return int.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 4 '08 #7
gw****@aol.com writes:
On 4 Jul, 10:09, Richard Heathfield <r...@see.sig.invalidwrote:
>gw7...@aol.com said:
On 4 Jul, 09:59, Richard Heathfield <r...@see.sig.invalidwrote:
rahul said:
Casting to the int is required because EOF is defined as negative
int(generally, but not necessarily, -1). So, other than the EOF, we
get an unsigned int.
>No, other than the EOF we get an int that is the result of a conversion
of an unsigned char to an int.
Don't you mean "a conversion of a char to an int" ?

No, I don't mean that.

Then could you explain, please? My understanding is that a type char
is provided, for storing, well, characters, in the system's favourite
format. Functions such as fgetc() are used for reading in characters,
but the output they give has to embrace both all real characters and
have something different to indicate EOF. Hence they return an int. So
I would have assumed that the values they can return, other than EOF,
are the values of characters which in turn are the values a char can
have. I don't see where you have got "unsigned" from. AFAIAA you
should use unsigned char rather than char when reading memory
locations storing a different type but I don't see how that is
relevant here.
That's what the standard says.

C99 7.19.7.1:

If the end-of-file indicator for the input _stream_ pointed to by
stream is not set and a next character is present, the _fgetc_
function obtains that character as an _unsigned char_ converted to
an _int_ and advances the associated file position indicator for
the stream (if defined).

Plain char may be either signed or unsigned. Forcing the characters
read from a file to be treated as unsigned char rather than plain char
ensures that no valid character can appear as EOF.

Typically EOF is -1. On a system with 8-bit bytes, where plain char
is signed (two's-complement), a byte with all bits set will be read as
255. If it were interpreted as a plain char, it would be read as -1.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 4 '08 #8
gw****@aol.com said:
On 4 Jul, 10:09, Richard Heathfield <r...@see.sig.invalidwrote:
>gw7...@aol.com said:
On 4 Jul, 09:59, Richard Heathfield <r...@see.sig.invalidwrote:
rahul said:
Casting to the int is required because EOF is defined as negative
int(generally, but not necessarily, -1). So, other than the EOF, we
get an unsigned int.
>No, other than the EOF we get an int that is the result of a
conversion of an unsigned char to an int.
Don't you mean "a conversion of a char to an int" ?

No, I don't mean that.

Then could you explain, please? My understanding is that a type char
is provided, for storing, well, characters, in the system's favourite
format.
Right. Unfortunately, things aren't quite that neat.
Functions such as fgetc() are used for reading in characters,
but the output they give has to embrace both all real characters and
have something different to indicate EOF. Hence they return an int.
Right so far.
So
I would have assumed that the values they can return, other than EOF,
are the values of characters which in turn are the values a char can
have. I don't see where you have got "unsigned" from.
4.9.7.1 The fgetc function

Synopsis

#include <stdio.h>
int fgetc(FILE *stream);

Description

The fgetc function obtains the next character (if present) as an
unsigned char converted to an int , from the input stream pointed to
by stream , and advances the associated file position indicator for
the stream (if defined).
So - assuming for the moment that we're not at the end of the file... the
fgetc function reads one byte from a stream, and interprets that byte as
if its bit pattern represents an unsigned char. (Whether or not that
interpretation is appropriate is neither here nor there as far as fgetc is
concerned.) It then converts that value into an int, and returns the int.

You should pick the value up using an int. If that int is != EOF (or, on
some of the more esoteric platforms, if feof(fp) and ferror(fp) both yield
0), you can safely store that value in an unsigned char. If you would
rather store it in a char, that's entirely up to you, but from now on that
value might no longer be representable as an unsigned char and so might be
unsuitable for passing to functions such as is* and to* without a cast.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 4 '08 #9
On Jul 4, 12:53*pm, rahul <rahulsin...@gmail.comwrote:
>
Cosider fgetc(). It actually returns an unsigned integer cast to an
int. Casting to the int is required because EOF is defined as negative
int(generally, but not necessarily, -1). So, other than the EOF, we
get an unsigned int.
My mistake; I meant an unsigned char converted to an int so that it
can accommodate EOF.
Jul 4 '08 #10
On 4 Jul, 10:48, Richard Heathfield <r...@see.sig.invalidwrote:

[Snip detailed explanation including...]
* * * * *int fgetc(FILE *stream);

Description

* *The fgetc function obtains the next character (if present) as an
unsigned char converted to an int , from the input stream pointed to
by stream , and advances the associated file position indicator for
the stream (if defined).
Ah. You live and learn. Thanks very much to you and to Keith for
filling me in on this point.

Paul.
Jul 4 '08 #11
s0****@gmail.com wrote:
I've seen a lot of functions in the standard library that deal with
characters, but a lot them return/take parameters of type int (which
is usually the integer that represents the character code in the
character set). I know that C automatically converts between int and
char in those cases without problems (or could the singed/unsigned
issue cause problems?), and that character constants have actually
type int, but, what is the reason for some functions in the standard
library to return type int (or take parameters of type int) when one
is supposed to be dealing with characters instead of numbers?
The type of ('a') is type int.
strchr(s, 'a');

makes more sense with

char *strchr(const char *s, int c);

than it does with

char *strchr(const char *s, char c);
--
pete
Jul 4 '08 #12
On Jul 4, 9:39 am, pete <pfil...@mindspring.comwrote:
s0s...@gmail.com wrote:
I've seen a lot of functions in the standard library that deal with
characters, but a lot them return/take parameters of type int (which
is usually the integer that represents the character code in the
character set). I know that C automatically converts between int and
char in those cases without problems (or could the singed/unsigned
issue cause problems?), and that character constants have actually
type int, but, what is the reason for some functions in the standard
library to return type int (or take parameters of type int) when one
is supposed to be dealing with characters instead of numbers?

The type of ('a') is type int.

strchr(s, 'a');

makes more sense with

char *strchr(const char *s, int c);

than it does with

char *strchr(const char *s, char c);
That's true at some extent. But one doesn't always pass a character
constant to a character-handling function. In fact, I'd say one rarely
does so. But the points rahul, Keith Thompson and Richard Heathfield
have expressed seem more reasonable. Thanks to everybody.

Sebastian

Jul 5 '08 #13

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

Similar topics

7
4803
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
6
9841
by: Charlie | last post by:
Dear all, I am stuck on this problem, couldn't find an answer on any C++ book in the bookshop. Following is a cut of my program, and its error message:...
5
3939
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
7
5561
by: techno | last post by:
Dear all, Our bitmap has some x00 values ( '\0' ) and i am storing it in char* array. the problem is that the '\0' is treated as eos character in c and it is truncating it so the characters...
6
2285
by: dddddddd2444444 | last post by:
Hi,please help... It works fine when I define a 2-D array like char code. But it won't work when I try to define the array dynamically using a function. It just crashes. Does anyone know why?...
33
3614
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of...
5
7765
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array...
2
22570
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
18
4024
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
29
9923
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
7193
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,...
0
7264
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,...
0
7449
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...
0
5562
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,...
1
4992
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...
0
4666
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...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
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 ...
0
371
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...

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.