Printing ASCII codes, and some more problem

I have a few questions that I would like to address altogether:

1. Is there a proper way to print ASCII codes? I saw on other forums that they used this syntax:
 
cout<<(char)0;

But it looks messy if I want to print many of these characters. And do we use octal codes only?

2.
a) What is the difference between cout, printf, and cprintf? And which libraries are they defined in?
b) What is the difference between cin, scanf, and cscanf? And which libraries are they defined in?

3. Where can I find a complete list of ASCII codes? (Including rare symbols such as "heart")

Thanks in advance.
Last edited on
If you use the right type of variable, the printed value will match the type:
1
2
3
4
5
6
7
8
9
    char w = 'A';
    int  x = 66;
    char y = 67;
    int  z = 'D';

    cout << w << endl;
    cout << x << endl;
    cout << y << endl;
    cout << z << endl;
A
66
C
68

Usually you will be interested in the decimal or hexadecimal values. Octal is less common.

cout, printf, and cprintf?
cin, scanf, and cscanf?
cin and cout are C++, found in the <iostream> library.

The others are belong to C, in <stdio.h>
cprintf and cscanf are non-standard console-versions which you may find in <conio.h>, but neither the file nor its contents are standardised and may not be available on a given compiler.

As for the list of ASCII codes, Google will help. Be aware that the extended codes for special symbols will depend upon the character set in use, and the displayed symbol may vary.
@Chervil Thanks for your reply. I really appreciate them;most of them answered my questions. But about no. 1, if I print something like a poundsterling symbol, what is the proper syntax? I'm still confused on how to properly print special ASCII symbols using "cout".
Have you tried cout << '£' << endl;?
indojo24 wrote:
If I print something like a poundsterling symbol, what is the proper syntax?

There isn't a single proper syntax. There is simply whichever syntax you prefer. For example any of the following will print the '£' sign:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    cout << " 1 " << '£' << endl;

    cout << " 2 " << (char) 163 << endl;

    char c1 = 163;
    cout << " 3 " << c1 << endl;

    int m = 163;
    cout << " 4 " << (char) m << endl;

    char c2 = '£';
    cout << " 5 " << c2 << endl;

    int n = '£';
    cout << " 6 " << (char) n << endl;
 1 £
 2 £
 3 £
 4 £
 5 £
 6 £

Although that may look confusing, actually, the last four are all variations of 1 and 2.

Here's a short program to display the character set. It skips over certain unprintable characters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <iomanip>

    using namespace std;

int main()
{
    char eof       = 0;
    char bell      = 7;
    char backspace = 8;
    char tab       = 9;
    char creturn   = 13;
    char linefeed  = 10;

    for (int i=0; i<256; i++)
    {
        if (i%16 == 0)
            cout << endl << endl;

        cout << setw(4) << right << i << " ";

        if (i==eof || i==bell || i==backspace || i==tab || i==creturn || i==linefeed)
            cout << ' ';
        else
            cout << (char) i;
    }

    return 0;
}

Note that the actual characters which are displayed will depend on the code page in use, which may depend on the operating system and how the machine is configured. That's a whole additional topic, and can be a cause of issues too.
Last edited on
Topic archived. No new replies allowed.