Getting input, Arrays, Character Strings and Preprocessors
In previous lesson you learned about variables and
printing output results on computer console. This lesson discusses more about
variables and adds important concepts on array of variables, strings of
characters and preprocessor commands. We will also learn more on getting input
values from console and printing output results after performing required
calculations and processes. After this lesson you should be able to develop
programs which get input data from user, do simple calculations and print the
results on the output screen.
Receiving input values from keyboard
Let's have an example that
receives data values from keyboard.
Example 2-1: example2-1.c
printf("Enter value for a :");
printf("Enter value for b :");
Enter value for a : 10
Enter value for b : 20
As scanf itself enters a new line character after
receiving input, we will not need to insert another new line before asking for
next value in our second and third printf functions.
General form of scanf
function is :
Format string contains placeholders for variables that
we intend to receive from keyboard. A '&' sign comes before each variable
name that comes in variable listing. Character strings are exceptions from this
rule. They will not come with this sign before them. We will study about
character strings in this lesson.
are not allowed to insert any additional characters in format string other than
placeholders and some special characters. Entering even a space or other undesired
character will cause your program to work incorrectly and the results will be
unexpected. So make sure you just insert placeholder characters in scanf format
The following example
receives multiple variables from keyboard.
Pay attention that scanf function has no error
checking capabilities built in it. Programmer is responsible for validating
input data (type, range etc.) and preventing errors.
Arrays are structures that
hold multiple variables of the same data type. An array from integer type holds
The array "scores" contains an array of 10
integer values. We can use each member of array by specifying its index value. Members
of above array are scores,...,scores and we can work with these variables
like other variables:
Example 2-2: example2-2.c
Receive 3 scores of a student
in an array and finally calculate his average.
printf("Enter Score 1 : ");
printf("Enter Score 2 : ");
printf("Enter Score 3 : ");
printf("Sum is = %d\nAverage = %f\n",sum,avg);
Enter Score 1 : 12
Enter Score 2 : 14
Enter Score 3 : 15
Sum is = 41
Average = 13.000000
In C language we hold names,
phrases etc in character strings. Character strings are arrays of characters.
Each member of array contains one of characters in the string. Look at this
Example 2-3: example2-3.c
printf("Enter your name : ");
printf("Hello, %s , how are you ?\n",name);
Enter your name : Brian
Hello, Brian, how are you ?
If user enters
"Brian" then the first member of array will contain 'B' , second cell
will contain 'r' and so on. C determines end of a string by a zero value
character. We call this character as "NULL" character and show it
with '\0' character. (It's only one character and its value is 0, however we
show it with two characters to remember it is a character type, not an integer)
Equally we can make that
string by assigning character values to each member.
name=0; //or name='\0';
As we saw in above example placeholder for string variables is %s. Also we
will not use a '&' sign for receiving string values. For now be sure to
remember this fact and we will understand the reason in future lessons.
Preprocessor statements are
those lines starting with '#' sign. An example is #include<stdio.h> statement that we used to include stdio.h header file into our programs.
Preprocessor statements are
processed by a program called preprocessor before compilation step takes place.
After preprocessor has finished its job, compiler starts its work.
#define preprocessor command
#define is used to define
constants and aliases. Look at this example:
Example 2-4: example2-4.c
#define PI 3.14
#define ERROR_1 "File not
#define QUOTE "Hello World!"
printf("Area of circle = %f *
diameter", PI );
printf("\nQuote : %s\n",QUOTE);
Area of circle = 3.140000 * diameter
Error : File not found.
Quote : Hello World!
Preprocessor step is
performed before compilation and it will change the actual source code to below
code. Compiler will see the program as below one:
printf("Area of circle = %f *
diameter", 3.14 );
%s","File not found.");
In brief #define allows us to
define symbolic constants. We usually use uppercase names for #define
variables. Pay attention that we do not use ';' after preprocessor statements.
Variable limit for holding
values is related to the amount of memory space it uses in system memory. In
different operating systems and compilers different amount of memory is
allocated for specific variable types. For example int type uses 2 bytes in DOS
but 4 bytes in windows environment. Limitations of variable types are mentioned
in your compiler documentation. If oue program is sensitive to the size of a
variable (we will see examples in next lessons), we should not assume a fixed
size for them. We should instead use sizeof() function to determine size of a
variable or variable type (and we should do t).
Example 2-5: example2-5.c
printf("Integer type uses %d bytes
of memory.\n", sizeof(i));
printf("float type uses %d bytes of
You see we can use both a
variable and a variable type as a parameter to sizeof() function. Below table shows
variable limitations of Turbo C and Microsoft C in DOS operating system as an
Bytes used Range
char 1 256
int 2 65536
short 2 65536
long 2 4 billion
float 4 6 digits *
double 8 10 digits *
We have two kinds of variables from each of the above
types in C programming language: signed and unsigned. Signed variables can have
negative values too while unsigned values only support positive numbers.
If a signed variable is used, high boundary will be
divided by two. This is because C will divide the available range to negative
and positive numbers. For example signed int range is (-32768,+32767).
You can declare a variable as “signed” or “unsigned”
by adding "signed" or "unsigned" keywords before type name.
signed int a;
unsigned int b;
We are allowed to assign values larger than 32767 to
variable "b" but not to variable "a". C programming
language may not complain if we do so but program will not work as expected. Alternatively
we can assign negative numbers to "a" but not to "b".
Default kind for all types is signed so we can omit
signed keyword if we want a variable to be signed.
1. Write a program that asks for work hours, wage per hour and tax rate and then prints payable money for a person.
2. Using arrays write a program that receives tax rate, work hours and wage per hour for two persons, saves work hours of those persons in an array and then calculates and prints payable money for each of them.
3. Write a program that asks for your name and outputs 3 first characters of it, each in a separate line. Use %c as placeholder of a single character in printf format string.