OldSchoolCode

.com



This tutorial has to do with user input
Ok, to get user input - you can use a method similar to printf. It's called scanf, which basically means scan the keyboard for input. First you have to declare a variable to hold user input. we'll only deal with integers (int) right now.
int myNumber;
Now you call scanf, telling it to place the number it gets from the user into the memory address referenced by myNumber. Remember, variables are really just reference addresses in RAM which hold the actual value. scanf uses the same %d, %f, %c style as printf.
scanf( "%d", &myNumber );
This tells it to take the first thing the user enters and place it in myNumber. Notice the '&' sign before myNumber. That sign is called the "address of" operator. Since variables really just reference memory addresses, &myNumber tells it to put the input at the address of myNumber (the memory address that myNumber references). That can be a tough concept to understand, so feel free to ask any questions. We'll get into more memory addressing later. And I want to be sure that you always understand it.

One last thing, when using printfs you can tell it to print on the next line by using "\n". For instance
printf ( "\nI am printing on a new line" );
would print the text "I am printing on a new line" on the next line down. This can be helpful when formatting your output.

__________________________________________________________
//Whoa man, we haven't seen this before!
//Microsoft's latest compiler (2005) warns
//you if you are using functions which have the
//potential to be insecure.  Since we're just
//learning C, we don't care about this right now
//and we can put this line in to tell the compiler
//to keep quiet with the insecure warnings
#define _CRT_SECURE_NO_DEPRECATE   
   
#include <stdio.h>
#include <conio.h>

void main( void )
{
   //The keyword int specifies a 32-bit signed integer
   //the variable is what ever you want to call it "myNumber"
   int myNumber;

   //call printf to print the screen asking for the user to type
   //in a number.The required header for printf is <stdio.h>
   printf( "\n Type in a number:  " );

   //call scanf to except the number typed from the user
   //and assign it to &myNumber
   //Required Header for scanf is <stdio.h>
   scanf( "%d", &myNumber );

   //call printf to print out "Here is your number :" and
   //it will add the number to the end where "%d,myNumber is.
   printf( "\n Here is your number :  %d",myNumber );

   //getch() tells the program to wait for the user to hit a key
   //This way the screen doesn't dissapear before you read it.
   //Required Header for getch() is <conio.h>
   getch();
}