473,569 Members | 2,764 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 1764
On Jul 4, 12:49 pm, s0s...@gmail.co m 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.i nvalidwrote:
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.i nvalidwrote:
>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.i nvalidwrote:
gw7...@aol.com said:
On 4 Jul, 09:59, Richard Heathfield <r...@see.sig.i nvalidwrote:
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.co m 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_Keit h) 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.i nvalidwrote:
>gw7...@aol.c om said:
On 4 Jul, 09:59, Richard Heathfield <r...@see.sig.i nvalidwrote:
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_Keit h) 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.i nvalidwrote:
>gw7...@aol.c om said:
On 4 Jul, 09:59, Richard Heathfield <r...@see.sig.i nvalidwrote:
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...@gm ail.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

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

Similar topics

7
4811
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 ordered by my instructor and she does have her reasons so I have to use char*. So there is alot of c in the code as well Anyways, I have a linked...
6
9852
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: ============================================ 280 map<char,vector<int> > _dict; 281 char* op_addr = op.get_address(); 282 int op_time = op.get_time_stamp();
5
3947
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 variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with...
7
5570
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 after it are not pass to the function. here is the code snippet /* DATA8583.data = ( char ) ( unsigned int ) 0xF0;
6
2292
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? The compiler i'm using is Dev c++. #include <stdio.h> #include <stdlib.h>
33
3632
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 serializing the object array). I can think of three ways to do this:
5
7776
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 of bytes. For example, the .lib contains this function: int create(int id, int scale, unsigned char *image); In the wrapper DLL I have this...
2
22582
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 read (the file will be written by only this program); file can be either in text or binary (preferably binary as the files may be read repeatedly); the...
18
4029
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
9929
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
7693
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...
0
7605
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...
0
7917
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. ...
0
8118
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...
0
7962
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...
0
6277
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...
0
3651
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...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
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

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.