Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

Variables 2 the search for char...

This time I oughta get it rignt. However, it's 2 A.M. right now so I'm not so sure if I'll botch this up again. Anyways in this section we are going to discuss the char variable. char stands for character. As the name suggests this "variable type" is used to store characters. Whether these be single letters, words, or other stuff. Type this fine piece of C code into your compilers.

#include < stdio.h >
main ( )
{
char name[5];
printf ("Hey dude what's your name? \n");
scanf ("%s", &name);
printf ("Only dorks are named %s", name);
return (0);
}

Compile it and run it.
HAHAHAHAHAHAAHAHAHAHAHAHHAHAHA...A GOOD LAUGH IS HAD BY ALL. Anyways it is basically the same concept as the variables we worked with before only it uses a char variable type. Look intently at the 4th line of code. Here we have "declared" a char variable and named it name...bleh a half hearted attempt at humour this early in the morning. The number enclosed in the brackets is the space in the computer's memory that we have set aside for it. Besides that it isn't too different from all the stuff we've working with in variables.
THE getch ( ) FUNCTION

Now we get a little bit more complicated. The getch funciton basically works like this. It waits for you to press a key and stores that key. Confused? Type this tidbit into your compilers and run it

#include < stdio.h >
#include < conio.h >
main ( )
{
char key;
printf ("Press a key dude\n");
key = getch( );
printf ("Hey you pressed the '%c' key!!!", key);
return (0);
}

Depending on what kind of compiler you use the source code might or might not work. If not try putting a getchar instead of getch. You see a new include directive, conio.h. This is where the getchar function is stored and when you type it in it retrieves it for you. The getch function waited for you to press a key and stored that key into the variable named key.
key = getch ( );
After it is stored in the key variable you displayed using the printf function
printf ("Hey you pressed the '%c' key!!!", key);

Notice the %c placeholder. This is used for displaying a character. This single quotation marks around the %c key is important so don't forget them.

If you haven't done this already try and change getch to getchar and see what happens. It's basically the same thing only it waits for you to hit the return button.

Let's modify this source code to give us ascii values.

#include < stdio.h >
#include < conio.h >
main ( )
{
char key;
printf ("Press a key dude\n");
key = getch( );
printf ("Hey you pressed the '%c' key!!!\n", key)
printf ("Don't you know its ascii value is %i!!!", key)
return (0);
}

Well in this here example, the same concept is applied as the one before. We added a printf line that placed contents of the key variable into the %i place holder. Doing this tells you the ascii value of that key.
Well that is all for now kiddies. There is much more about the char variable that will be covered, ie; putchar and so on. However I will these later. So come back soon and be amazed at all the mighty attributes of the char variable. - digitalscribe

Back to tutorials start page