Topic : C++ for Dumies
Author : Randy Nixon
Page : << Previous 2  Next >>
Go to page :


will first write a program that squares a number. Let's code shall we? Type this program into your C compiler;

#include < stdio.h >
main ( )
{
int x,y;
printf ("Enter a number and I'll square it!!!\n");
scanf ("%d", &y);
x = y*y;
printf ("%d is your number squared!!! HAHAHAHA!", x);
return (0);
}


Compile it and run it. Remember to NOT click and paste!!!!

Let's breakdown lines 4,5,6, and 7 of the above source code.

Line 4
int x,y;
In this line we are declaring our variables. We are basically making the little sections of memory in this line of code. So here we have 2 'boxes' called x and y. They are integer variables ready to put their talents to work for you.

Line 5
printf ("Enter a number and I'll square it!!!\n");
Just a printf statement that asks a question.

Line 6
scanf ("%d", &y");
This is where scanf is used. The %d is what is called a place holder. We'll get into it's signifcance later. The scanf function here was patiently waiting for you to enter a number. When you pressed enter it took that number and "threw" it into the y variable. That is why you had to type in the &y. Soooooo now we have a number sitting in the little y box.

Line 7
x=y*y;
This tells your computer to do some number crunching. It takes what is in the y variable and multiplies it by itself. Afterwards, it takes that number and throws it into the x variable. So now we have the answer sitting in the x variable.

Line 8
printf ("%d is your number squared!!! HAHAHAHA!", x);
Okay it's a printf statment with some subtle differences. The %d is a placeholder as said before. %d is specifically used for int variables. Used in printf it will display the variable that you stored in x. At the end of the quotations and comma you see x. This basically 'tells' your compiler the x variable will be displayed in the %d place holder. Put y in there instead and see what happens.

Play around with the different math functions. REMEMBER THAT C DOES MATH BACKWARDS. Ie; 4=3+1.

You know what though? If your just using int, it will not work with everything. Case in point;

#include < stdio.h >
main ( )
{
int x,y;
printf ("Enter a number and I'll reciprocate it.\n");
scanf ("%d", &y);
x = 1/x;
printf ("%d is your answer.",x);
return (0);
}


Compile it and run it. When it asks you for a number enter 2. For those don't know the reciprocoal (a number divided into 1) of 2 is 0.5. Press enter and you should get something cooky. It will not work. The int variable will not work well with anything other then whole numbers. What to do? What to do? Real C programmers don't panic and move to Montana (unlike pascal people), they find another way!!! That last sentence wasn't necessary, I'm always searching for new ways to annoy people with my unique brand of dry humor. Try using a different variable type. In this case use float. Retype this into your compiler;

#include < stdio.h >
main ( )
{
float x,y;
printf ("Enter a number and I'll reciprocate it.\n");
scanf ("%f", &y);
x = 1/x;
printf (%f is your answer.",x);
return (0);
}


It should work now. I'll tell you how to fix those annoying decimal place 0's later. I'm at school now and I'm using valuable class time to work on my hobby. If your an experienced programmer and you detect mistakes just email me and let me know. I'll talk to all later you wondrful internet people later. - Digitalscribe.


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




EVEN MORE C TO BRIGHTEN YOUR DAY!!!


Welcome to the next little diddy on char. Sorry it takes soooo long between tutorials now. I've been real busy lately and it takes me a while to complete these. Anyways eventhough I promised it before but I will do my best to have a new tutorial up every week. Anyways come back every satuday and check out new updates.

If you recall last time we went into a little tidbit on char. There is much more we can learn from char variables and we will start this marvelous learning process by typing code. Enter this into your compilers and run it!

#include < stdio.h >
#include < conio.h >
main ( )
{
char key;
printf ("Enter a key!\n");
key = getche( );
printf ("\tYou pressed the '%c' key!!!", key);
return (0);
}


This is basically some of the same stuff we've done before and only it containes the getche function. It produces the same kind of effect as getch, only it displays immeadiately what you type. To help you distinguish between the two imagine that e at the end of getche stands for echo. Wait a minute! I wonder if it really does stand for echo? Hmmmmm........

I know in the last tutorial I said I would cover putchar ( ), but I think I'll hold off on that until we get into loops. I do that because the programs I use to discuss loops uses putchar a lot...that and the fact that I'm extremely lazy. Anyways we let us now discuss constants. Don't worry if the discussion on variable seem a bit scanty, as I go into further tutorials variables will also be used a lot and discussed plentifly.


BEHOLD CONSTANTS IN ALL IT'S MIGHTY GLORY!!!
Constants are extremely easy to comprehend. It almost works like variables in that the computer sets aside memory for values, but it is different in that these values are defined and set in stone. These values will not change. They refuse to compromise. They are not human. They cannot feel pain. They cannot be reasoned with............sorry. Type this into thy compiler and run it

#include < stdio.h >
#define TAX 10000000
main ( )
{
float x,t;
printf ("How old are you\n");
scanf ("%f",&x);
t = x*TAX;
printf ("You have insulted the mighty IRS you owe %.0f dollars!",


Page : << Previous 2  Next >>