Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Char assignment using Octal values (http://www.programmingforums.org/showthread.php?t=5431)

aznluvsmc Aug 16th, 2005 8:36 PM

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.

DaWei Aug 16th, 2005 9:26 PM

An ASCII character in octal notation may only have 3 digits.

aznluvsmc Aug 16th, 2005 9:49 PM

Alright, simple and straight to the point explanation. My C Primer Plus book didn't explicity state this. Thanks! :)

Ooble Aug 17th, 2005 9:22 AM

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'?

Monster Aug 17th, 2005 2:15 PM

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

Ooble Aug 17th, 2005 6:03 PM

I got that. What I want to know is how you insert characters represented as octal numbers into a string.

Scorpions4ever Aug 17th, 2005 7:15 PM

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

Navid Aug 17th, 2005 8:15 PM

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?

DaWei Aug 17th, 2005 9:12 PM

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.

aznluvsmc Aug 17th, 2005 10:14 PM

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?


All times are GMT -5. The time now is 2:07 PM.

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