Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 16th, 2005, 8:36 PM   #1
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 3 aznluvsmc is on a distinguished road
Char assignment using Octal values

Hi,

I'd like to know why assigning a value to char using an octal value sometimes gives a warning message causing incorrect output.

Please look at the following code:

int main(void)
   {
   char c = '\074', d = '\0101', e = '\101';

   printf("c is %c d is %c e is %c", c, d, e);

   return 0;
   }

Here c is assigned the character "<" but d gives a warning on compile and will be displayed as the character "1" where as e displays "A". I don't understand why I can have a leading 0 inconjunction with the "\" character sometimes and not other times.

I could just assign the values with leading zeroes and take away the "\" and single quotes but I'd just like to understand my problem to better understand C.
aznluvsmc is offline   Reply With Quote
Old Aug 16th, 2005, 9:26 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
An ASCII character in octal notation may only have 3 digits.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 16th, 2005, 9:49 PM   #3
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 3 aznluvsmc is on a distinguished road
Alright, simple and straight to the point explanation. My C Primer Plus book didn't explicity state this. Thanks!
aznluvsmc is offline   Reply With Quote
Old Aug 17th, 2005, 9:22 AM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Out of curiosity, how does the compiler differ between "\074", meaning octal character 74, and "\074", meaning a null byte followed by the characters '7' and '4'?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Aug 17th, 2005, 2:15 PM   #5
Monster
Newbie
 
Join Date: Jan 2005
Posts: 20
Rep Power: 0 Monster is on a distinguished road
Quote:
Originally Posted by Ooble
Out of curiosity, how does the compiler differ between "\074", meaning octal character 74, and "\074", meaning a null byte followed by the characters '7' and '4'?
Single quotes = octal number
Double quotes = string
Monster is offline   Reply With Quote
Old Aug 17th, 2005, 6:03 PM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I got that. What I want to know is how you insert characters represented as octal numbers into a string.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Aug 17th, 2005, 7:15 PM   #7
Scorpions4ever
Programmer
 
Join Date: Jun 2005
Posts: 86
Rep Power: 4 Scorpions4ever is on a distinguished road
Quote:
Originally Posted by Ooble
I got that. What I want to know is how you insert characters represented as octal numbers into a string.
printf("This is the first letter of the alphabet: \101\n");
Works for me on gcc 3.3.3
Scorpions4ever is offline   Reply With Quote
Old Aug 17th, 2005, 8:15 PM   #8
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
This is the first time i've ever seen ooble asking a question...

I got a question too, why is their an octal format and hex format when you can use the decimal format?
Navid is offline   Reply With Quote
Old Aug 17th, 2005, 9:12 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Hex format gives you a view of the bit representations once you get the visualization down, because it's a power of 2 and decimal isn't. Octal is the same way, but is primarily a throwback to when busses weren't as wide. Hexadecimal requires that one invent 6 new numeric symbols (if you can call the traditional A-F an invention), whereas, with octal, you just toss a couple of the leftover ones, no imagination necessary.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 17th, 2005, 10:14 PM   #10
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 3 aznluvsmc is on a distinguished road
Wow, a simple question has spurned an interesting conversation. Speaking of which is \377 the highest octal value you can represent since that is all eight bits set to 1?
aznluvsmc is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:38 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC