January 13th, 2002 09:28 AM
C Programming Tutorial-Chapter 2
C Programming Tutorial-Chapter 2
Things Covered In Chapter 2
Types of Data in C
How the Computer Stores Data
Formatting text using printf()
I assume that you've read the first chapter of this tutorial.
Ohterwise you can get it at:
Types of Data in C
This information may seem a bit superfluous at this stage, but read it anyway because you won't undestand the next section (or the ramaining chapters in the tutorial) without it.
In C, there are two basic types of data: numerical and string. Numerical data is any number like say, 20 or 54.76 while string data is alphanumeric data like say, "cgkanchi" or "hello world!". String data, in C is always enclosed in double quotes so "20" is a string while 20 isn't (don't worry, it'll get clearer as you go along). Numerical data in C is of three basic types:
1. char: As you might guess, char data consists of single characters (which are just numbers to the computer). But char data can also store numbers in the range of -128 to +127 (I'll get to that in the next section). char data is always enclosed in single quotes, like so 'c'.
2. int: Integer data stores whole numbers from -32,768 to +32,767.
3. float: Float or floating point data is used to store decimal numbers between 3.4 x 10 ^ -38 to 3.4 x 10 ^ 38 (10 ^ 38 is read as 10 raised to 38. So 2 ^ 2 would be 2 raised to 2 or two squared which is 4).
What about bigger numbers? Well there's a long data type to store integer data between -2,147,483,648 to 2,147,483,647 and the double data type to store data between 1.7 x 10 ^ -308 to 1.7 x 10 ^ 308. What about char? There is no such thing as a long char but if you want to store a number greater than 127 then just use an int instead. You can also qualify any of these types as unsigned which means all data they store is positive or zero. So the range of char changes from -128 to 127 to 0 to 255, that of int changes from -32,768 to 32,767 to 0 to 65,535 and so on. For some background information on how exactly the computer stores data read the next section. Otherwise, just skip to the section on screen output.
How the Computer Stores Data
The computer is a digital creature. So it stores everything and I mean EVERYTHING in the form of numbers. Actually it stores all the data that you put into it as binary code. So, to the computer, any data is stored in the form of 1s and 0s. The computer has small compartments of data called bits. Every bit can store either a 1 or a 0. In other words, a bit can have only one value at any given time. Now, eight bits make a byte. A data which is qualified as char occupies one byte of memory. Similarly, int data occupies 2 bytes, float and long data 4 bytes and double data 8 bytes. Given seven places, each of which has two possible values (in this case 0 and 1), there are 128 possible combinations. That's why a char has a range of -128 to 127. What about the eighth bit you ask? Well, the eighth bit is called the sign bit and is used to store the sign of the number. When you qualify data as unsigned, the sign bit isn't used. Given eight places, each with two possible values, there are 256 possible combinations. That makes the range of an unsigned char into 0 to 255.
Formatting Text Using printf()
We ended our last chapter with an analysis of the hello.c program. Remember the statement that does most of the work in the program? Yes I'm talking about the printf("Hello World!"); thing. "printf" stands for "print formatted" and is one of the most popular ways to output text to the screen.
Type in this program to see the different types of data:
A program to illustrate the different types of data*/
printf("This is char data: %c\n",'A');
printf("This is char data in numerical form: %c\n",65);
printf("This is int data %d:\n",65);
printf("This is float data: %f\n",65.00);
printf("This is float data with only two decimals data: %.2f\n",65.00);
printf("This is double data: %2f\n",65.00);
printf("This is a string: %s","Hello");
Save this program as data.c . Compile. Fix errors. Run.
This program is very similar to the hello.c program in chapter 1 except that it has many printf statements. The major difference is the %something things. The % sign followed by a letter is known as a placeholder. What a placeholder does is that it tells the printf statement to look for a particular type of data after the final double quote("). You can have multiple placeholders in a single statement. In this case, the printf statement looks for data in the order in which the placeholders appear.
Here is a list of placeholders and what they do:
Placeholder What it does
%c Displays char data
%i, %d Display int data
%f Displays float data
%2f Displays double data
%.2f Displays float with the requisite
number of decimals as
specified after the period(.)
%s Displays string data
%o Displays octal data (more on octal
and hexadecimal in another chapter)
%x Displays hexadecimal data
As the % sign has a special meaning for printf, to display it, you must double it up. For example, printf("Something%%"); will display Something%
Remember the "\n" thing that makes the printf statement go to the next line? That is called an escape sequence. Escape sequences consist of a backslash(\) and a letter following them. Escape sequences can be used to do a lot of interesting things in C. They are used simply by tagging them on at any point in a string. For example printf("Hel\nlo"); makes the program display "Hel" on one line and "lo" on the next.
Here is a list of some of the escape sequences:
Escape Sequence What it does
\n Goes down to the beginning of the next line
\r Goes back to beginning of line
\t Prints a tab
\ Goes down one line without going
back to the beginning of the line
\x Recognizes the next characters(upto
the next space) as hexadecimal
\o Recognizes the next characters(upto
the next space) as octal numbers
\a The computer speaker lets out a
\b Goes back one character
In addition to these certain characters have a special meaning for the printf function. So to display them you must precede them by a backslash. The two most notable ones are the backslash itself and the double quotes("). So, to display a backslash you'd type "\\" and to display double quotes you'd type "\""(the first and the last are the double quotes for the string and the middle one is the oune you want to display).
So, that's all for chapter 2 of my C tutorial. Until next time, I'll give you a few assignments to do.
1. Write a program to display the sentence "I Love C", said the programmer. (With the quotes).
2. Write a program to display
Using only a single printf statement. (Hint: remember \n).
3. Write a program to display "C:\ABC\ABC\" (without the quotes).
Don't hesitate to ask any doubts or questions related to the tutorial. And experienced programmers who happen to read this, please fill in any placeholders or escape sequences I might have missed.
January 13th, 2002 09:56 AM
C is for cookie, thats good enough for me...
\"Computer games don\'t affect kids; I mean if Pac-Man affected us as kids, we\'d all be running around in darkened rooms munching magic pills and listening to repetitive electronic music.\" Kristian Wilson, Nintendo, Inc. 1989
February 22nd, 2002 06:00 AM
that was very informative...............just one question
is there gonna be a chapter III??????????
Genius of the mind is not necessarily from the mind of a genius
February 22nd, 2002 06:11 AM
I'm very busy right now, but look for chapter 3 in about two or three weeks' time.
February 25th, 2002 11:59 AM
hey kanchi that was great....good post...
A laptop, internet connection and beer.
March 18th, 2002 09:22 PM
Nice post cgkanchi!! Can't wait for #3.
Can I get out of this prison?
Can I stay this prison forever?
March 19th, 2002 11:37 AM
March 25th, 2002 07:51 PM
wow this site is like addicting and hard to leave hope someother new people
share or/bump up stuff they find too (hint)(hint)