Programming in C - Learning the Ins and Outs
Its great that this big powerful computer can crunch numbers and all, but even a pocket calculator allows for human input. What good is a calculator that gives the same answer, calculating the same problem every single time.
Seems like it would be an ideal time to find out how to tell a computer to allow us to input stuff. Try out this program:
/*----------------- Keyboard Input Program ---------------------------*/
#include <stdlib.h>
#include <stdio.h>
int ch;
void main()
printf("please enter one character using the keyboard and hit enter:\n");
ch = getc( stdin );
printf("The character you just typed in was: %c\n", ch);
This highly sophistocated program allows you to type something on the keyboard, and mimics on the screen what you typed. You could actually type an entire letter to congress if you chose, but it would only acknowledge the first letter ( ascii character ) that you entered. While this is not all that practical in and of itself, it could be used in conjunction with other programs too.
How to lock up your computer:
If you create a for statement like this:
for ( ; ; )
{
/* statement block goes here */
}
you have created an "infinate loop". Note that in this for statement, there are no expressions at all in the 3 expression fields. Because of this, it will execute again and again without stopping.
It is possible to use this type of a loop to find out "how many" executions you need to create your "timing loop" ( or for whatever other reason. You can simply do it like this:
for ( ; ; )
{
x=x+1
printf(" %d\n", x);
}
but somehow, you have to leave a means of escaping the loop - a break statement of some kind. So the question is, how do you create a "conditional loop"? Here is an example of a conditional loop that will let you keep inputting letters until the "escape" letter is entered:
#include <stdlib.h>
#include <stdio.h>
int x;
void main()
{
printf(" Enter a letter or number:\n ( enter * to exit )");
for ( x=' '; x != '*'; )
{
x = getc(stdin);
putchar(x);
}
printf("\n Exiting Loop Now!\n");
exit(0);
}
On The Following Indicator...
(
GREEN
will indicate your current location)
|
|
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9
|
10
|
[
ElectronicsTheory.Com
]
This Course was written by Ray Dall © All Rights Reserved.
This page and all it's content Copyright, Trademarks, Intellectual Properties
and other legal issues 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Ray Dall.
All Rights Reserved.
And for what it's worth... this page was last updated HexDate 01-11--7D1
VISITORS: