![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
Alright, simple and straight to the point explanation. My C Primer Plus book didn't explicity state this. Thanks!
![]() |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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'?
|
|
|
|
|
|
#5 | |
|
Newbie
Join Date: Jan 2005
Posts: 20
Rep Power: 0
![]() |
Quote:
Double quotes = string |
|
|
|
|
|
|
#6 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Quote:
|
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
377 IS highest for a byte, yes, and the "unfulfilled" nature is, to me, one of the drawbacks of octal notation. Address busses aren't limited to 8 bits, though. Twelve bits were common at one time, which fills up nicely. Hex became more common when the busses went to 16.
__________________
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 |
|
|
|
|
|
#8 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I got that. What I want to know is how you insert characters represented as octal numbers into a string.
|
|
|
|
|
|
#9 | |
|
Programmer
Join Date: Jun 2005
Posts: 86
Rep Power: 4
![]() |
Quote:
printf("This is the first letter of the alphabet: \101\n"); |
|
|
|
|
|
|
#10 |
|
Hobbyist Programmer
|
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? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|