2 - Variables, User Input, Math Functions
2 - Variables, User Input, Math Functions
Escape Sequences
float y;
printf(“Please enter a float value => “); // prompt
scanf(“%f”, &y); // read the value entered, note again the use of “&”
// NOTE: when READING a value do not use %5.2f, just %f
NOTE: Although you can enter multiple values in one scanf statement (by puttng a
space between numbers to be entered) it is confusing for the user. Therefore, use one
prompt and one scanf for each value to be entered.
To enter numbers in exponential notation use %f for input and enter the number as:
float z = 12.3e6;
printf(“%.2e”, z); // this will print 1.23e7
or printf(“%.4E”, z); // this will print 1.2300E7
Initializing characters and strings
Input buffer
Keyboard
Memory
Inputting characters
3. char x;
printf(“Please enter a character: “);
fflush(stdin); /* needed to prevent errors if another character entered */
x = getch(); /* user hits a key to enter value for x, no need for Enter */
printf("%c", x); /* user only sees the character that's printed */
4. char x;
printf(“Please enter a character: “);
fflush(stdin);
x = getche(); /* user hits a key to enter value for x, no need for Enter */
printf("%c", x) /* user sees two values, one that he entered and the
printed one - NOTE the e at the end of getche() means echo (show) the
character entered*/
5. char x;
printf(“Please enter a character: “);
fflush(stdin); // Visual C++ requires the use of fflush(stdin) before getchar()
x = getchar(); /* user hits a key to enter value for x, AND Enter */
printf("%c", x); /* user sees two values, one that he entered
and the printed one */
Note:
Try each of these ways of entering characters and note the differences.
Inputting strings
Note: When entering strings you MUST NOT use &. The Ampersand
#include<stdio.h>
main()
{
char letter; /* declare letter to be of type char. Can hold ONE character */
char name[20]; /* the square bracket indicates that up to 20 (in this case)
characters can be entered for variable “name”. */
printf("Please enter a letter and hit Enter ");
fflush(stdin); /* this command is used to clear the buffer and avoid input
errors. */
scanf(" %c", &letter);
getch(); /* Keeps output window open until user hits a key to continue OR */
getchar(); /* but with getchar() you must use the Enter key after a key is hit or
just hit Enter. Use fflush(stdin); before getchar(); */
Storing Characters
How does the computer store characters and strings, when all data is stored in binary
form using 0s and 1s?
Characters are stored as numbers, where a character is converted to its ASCII code
equivalent. Check the Appendix in your book for ASCII codes. Do NOT memorize
them.
The standard character set is converted to numbers ranging from 0 to 127. For
example, an A is converted to 65, B to 66, C to 67 and so on. Here is an example of how
a character is saved and how it is printed.
If we now print the value of x, the value printed will depend on the format that is
specified in the print statement.
More on Strings
char val[ ] = "Hello"; /* sets aside 6 bytes to val and (five for Hello and one
for the null character at the end of the string */
NOTE: You do not need to memorize ASCII values and the information is presented
only to help you understand how characters are stored.
Sample Program 1.
#include<stdio.h>
main()
{
int x, y, total;
printf("Please enter an integer: ");
scanf("%i", &x);
printf("\nPlease enter another integer: ");
scanf("%i", &y);
total = x + y;
printf("The sum of %i and %i = %i", x, y, total);
getch();
}
Sample Program 2
#include<stdio.h>
main()
{
char first_name[20], last_name[10], initial;
printf("Please enter your first name: ");
fflush(stdin);
scanf("%s", first_name); // allows input of one word
printf("\nPlease enter your last name: ");
scanf("%s", last_name);
printf("\nPlease enter your middle initial: ");
fflush(stdin); // use before charcter input
scanf(" %c", &initial); // allows input of one character
}
Doing Math in C.
The following are called OPERATORS. They’re used to carry out math operations.
Addition: +
Subtraction: -
Multiplication: *
Division: /
Remainder: % Modulus operator
Use brackets () where possible to avoid ambiguity or you may get errors.
If you need to enter very large numbers you need to use exponential notation.
#include<stdio.h>
main()
{
float x;
printf("Please enter a floating point number: ");
scanf("%f", &x);
printf("\nYou entered %.2e", x);
getch();
}
The user can enter a number as 12345.678 or as 12.345678e3
The output in either case will be 1.23e4 which in mathematical form is 1.23x104
More math functions
#include<math.h> /* use the math.h header file for the following functions
v = abs ( y );