Prev:
Test Your Basic Knowledge!
Next:
The int Data Type
In C, there are four DATA TYPES: char, int, float, and double. Each one has its own properties. For instance, they all have different sizes. The size of a variable can be pictured as the number of containers / memory slots that are required to store it. Later on, we will use the sizeof operator to determine the size of various data types.
We must give each variable a data type to allow and restrict the type of data we can assign to it.
In this lesson we'll be looking at the char data type and how to declare and initialize char variables.
Variables of the char data type can store a single character from a set of 256 characters.
In fact, all of the characters on your keyboard have a unique numerical code associated with it, so in reality, you're storing numerical codes into char variables. The set of codes are known as ASCII codes, where ASCII stands for "American Standard Code for Information Interchange" and is usually pronounced "ask-ee".
The following program prints out the 256 ASCII characters - don't worry if you can't understand the code just yet.
#include <stdio.h>
int main() {
int i,j;
for(i=0 ; i<26 ; i++) {
for(j=0 ; j<10 ; j++) {
printf("%d=%c ", 10*i+j, 10*i+j);
}
printf("\n");
}
return 0;
}
Here's what my computer produced (ignore the watermarks!):
![]() |
I assume the "blocks" occur when there is no character with that numerical value. The "newline" character has the value of 10. I'm assuming that 8 is the backspace character (why else would the equals sign be missing?) and 13 returns you to the start of the line, hence erasing numbers 11 - 13. The maximum numerical value of a character is 255 - after that, the pattern repeats itself. You can clearly see that A is represented by 65, where as a is represented by 97.
Using the chart, try and guess the result of this example...
#include <stdio.h>
int main() {
printf("%c%c%c", 105, 111, 116);
printf("%c%c%c", 97, 45, 115);
printf("%c%c%c", 105, 120, 46);
printf("%c%c%c", 99, 111, 46);
printf("%c%c%c", 117, 107, 10);
return 0;
}
To DECLARE a variable, means to reserve memory space for it.
Declaring a variable involves inserting a variable name after a data type like this:
char variable_name;
You can also declare many variables of the same data type all on one line by separating each one with a comma, like this:
char a, b, c;
INITIALIZING a variable involves assigning (putting in) a value for the first time. This is done using the ASSIGNMENT OPERATOR, denoted by the equals sign, =.
Declaration and initializing can be done on separate lines, or on one line.
To store a character into a char variable, you must enclose it with SINGLE quote marks. Double quote marks are reserved for STRINGS (a sequence of characters). Strings are covered in the later sections. The characters you assign are CHARACTER CONSTANTS, for example, 'H' is a character constant.
You can also assign a char variable an integer, that is, the ASCII code.
The next example demonstrates the declaration and initialization of char variables. Once again, ignore the printf function for now.
#include <stdio.h>
int main() {/* this program prints Hello */
char a,b,c,d; /* declare char variables */
char e = 'o'; /* declaration and
initialization */
a = 'H'; /* initialize the rest... */
b = 'e'; /* b = e is incorrect */
c = 'l'; /* so is c = "l" - you MUST
enclose the character with
single quote marks*/
d = 108; /* the ASCII code for l */
printf("%c%c%c%c%c\n", a, b, c, d, e);
return 0;
}
Prev:
Test Your Basic Knowledge!
Next:
The int Data Type
www.iota-six.co.uk
Copyright © 2001-2003
Unauthorized copying not permitted
Designed and Developed Using Macromedia Studio MX