Can I put an int into a char?

Status
Not open for further replies.

Jef Patat

Junior Member level 1
Joined
Feb 4, 2005
Messages
15
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,281
Activity points
149
Hello,


I found this in some else's code:
...
void write_E2(unsigned int address, unsigned char data)
{
EEADR = address;
EEADRH = (address >> 8);
//The EEADRH:EEADR register pair is used to address
//the data EEPROM for read and write operations.
//EEADRH holds the two MSbits of the address; the
//upper 6 bits are ignored. The 10-bit range of the pair
//can address a memory range of 1024 bytes (00h to
//3FFh).
...
Is this a correct way of doing if you know:
...
extern volatile near unsigned char EEADR;
...


This means putting an int into a char.
will it choose the right byte, where can i find info about this?


Kind regards, Jef
 

Re: char=int

Should be OK but tools like PC-Lint will warn you about this.

The "lint proof" way would be:
EEADR = (unsigned char) address;
EEADRH = (unsigned char) (address >> 8);

this is called a cast.

Search for casting in a good C book ...

best regards
 

Re: char=int


Hi,

If you assign a int to a char variable the lower order 8 bits of the int variable will be assigned to the char variable.

So I think it is valid.

if you have a C compiler with you can check this by writing a simple C program.

Regards
Gopi.
 

    Jef Patat

    Points: 2
    Helpful Answer Positive Rating
Re: char=int

Thank you,

I think the lint-proof way is a better and clearer way of programming. But now at least i know it is correct.

Kind regards, Jef
 

Re: char=int

Hi

PC-Lint will give more warnings as it is more strict.
So you can ignore some of the warnings which may be not applicable for your Coding. I suggest, using PC-Lint is a way to find the loop holes in our Coding.

Have fun
Regards
Gopi
 

char=int

As C Man said - look to the K & R book where casting is described - so you will get answer to your question from standard point of view: what compiler must do if it complies to standard.
 

    Jef Patat

    Points: 2
    Helpful Answer Positive Rating
Re: char=int

artem said:
As C Man said - look to the K & R book where casting is described - so you will get answer to your question from standard point of view: what compiler must do if it complies to standard.
C Man said a good book, not K & R book
but... I'm still looking for a good book and apperently you think K & R is a good book, where can I find it? is it downloadable?

Kind regards, Jef
 

Re: char=int

Jef Patat please use the search function on edaboard:


The book from Kernighan & Ritchie is for example here:


best regards
 

char=int

Dennis Ritchie created C (from B) back in the early 1970's. Ritchie is the "R" in "K&R". That was a long time ago, and the ANSI C Standard has been revised since then, so the K&R book isn't quite up-to-date, but it's still a great book if you already know structured programming concepts, and simply want a crash course on C. If you want a book full of how-to-program tutorials, look elsewhere.

Here are answers to many common C questions - the comp.lang.c FAQ:
http://www.eskimo.com/~scs/C-faq/top.html

The Development of the C Language:
**broken link removed**
 

Re: char=int

I did use the search function, but it's always interesting to have someone advise a book.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…