What do you mean by '\o<octal number>'?

  • Thread starter dE_logics
  • Start date
  • Tags
    Mean
In summary, you can specify an arbitrary byte-sized bit pattern using '\ooo' where ooo is 1 to 3 octal digits or "\xhh" where hh is 1 or 2 hexadecimal digits. The 'o' in '\ooo' stands for octal and can be dropped if not needed. Special escape codes, such as "\n" for newline, have defined values in the ascii table. The leading zero in octal codes can also be dropped.
  • #1
dE_logics
742
0
book said:
In addition, an arbitrary byte-sized bit pattern can be specified by

'\ooo'

where ooo is one to three octal digits (0...7) or by "
I'm not sure what they mean here...can someone explain?

When ever I do, for e.g -

"\o7" or "\o42" in printf to try and print ASCII code 7 or 52 (decimal)

Compiler returns \o is not recognized.

However if I do "\07" or "\042" I get the characters 7 and 52 (as in ASCII code).


My question is, what is this 'o'?
 
Technology news on Phys.org
  • #2
You can use "\ooo" where "ooo" is 1 to 3 octal digits. In other words, "ooo" can be "1", "304", "12", "77"...but not "o12."

You can also use "\xhh", where "hh" is 1 or 2 hexadecimal digits...ie, "\x34" or "\x9f" or "\xb."

This is the complete ascii table:
http://www.cs.utk.edu/~pham/ascii_table.jpg

Some of the characters have special escape codes defined, such as "\n" for newline, which has hex value "A" and octal value "012", therefore the following are all equivalent:

\n
\xa
\xA (pretty sure this is case insensitive)
\012
\12 (im pretty sure the leading zero can be dropped)
 
Last edited by a moderator:
  • #3
dE_logics said:
However if I do "\07" or "\042" I get the characters 7 and 52 (as in ASCII code).
Octal \042 is the code for the character with ASCII code 34, not 52. This character is the double-quote, ".
 
  • #4
junglebeast said:
(im pretty sure the leading zero can be dropped)

Yes it can.

Thanks everyone!
 

Related to What do you mean by '\o<octal number>'?

1. What is an octal number?

An octal number is a number system that uses a base of 8, as opposed to the decimal system which uses a base of 10. This means that there are only 8 digits in the octal system (0, 1, 2, 3, 4, 5, 6, 7) compared to the 10 digits in the decimal system (0, 1, 2, 3, 4, 5, 6, 7, 8, 9).

2. How do you convert from decimal to octal?

To convert from decimal to octal, you can use the repeated division method. First, divide the decimal number by 8 and write down the remainder. Then, divide the quotient by 8 and write down the remainder again. Continue this process until the quotient is 0. The octal number is then the remainders written in reverse order. For example, to convert 25 from decimal to octal, the process would be: 25 ÷ 8 = 3, remainder 1; 3 ÷ 8 = 0, remainder 3. Therefore, the octal number is 31.

3. Why do we use octal numbers?

Octal numbers are used in computer programming because they are a convenient way to represent binary numbers. Each octal digit can represent exactly 3 binary digits, making it easier to work with binary numbers in programming. Octal numbers are also useful for representing file permissions in Unix systems.

4. What is the relationship between octal and hexadecimal numbers?

Octal and hexadecimal numbers are both number systems commonly used in computer programming. Octal numbers use a base of 8, while hexadecimal numbers use a base of 16. This means that each hexadecimal digit can represent exactly 4 binary digits, making it more compact than octal. This is why hexadecimal numbers are often used in computer memory addresses and color codes.

5. Can octal numbers be negative?

No, octal numbers cannot be negative. In the octal system, only non-negative numbers are represented. If a negative value needs to be represented, it is usually converted to its corresponding positive value and then a negative sign is added in front. For example, -5 in decimal would be represented as -5 in octal as well, since there is no negative representation in the octal system.

Similar threads

  • Programming and Computer Science
Replies
4
Views
5K
  • Precalculus Mathematics Homework Help
2
Replies
39
Views
4K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
19
Views
2K
  • Programming and Computer Science
Replies
4
Views
15K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
29
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
Back
Top