For the complete list of the tutorial on this blog, you can check these following links:
1. Introduction
2. First C-Program
3. Types and Variables
4. Conditional Statements
5. Function and Math Operations
6. Arrays
7. Pointers
8. I/O in C
Allright, so far we’ve just used all static values. Every values are staticly predefined and a program would be useless without some way to talk to the users of the program. If you consider yourself a user, and your colleague as a program, then you’ll supply him with informations about stuffs. And your colleague has to be able to catch those supplies and do something as predetermined before else.
This way of communication is called input output (I/O). One way to communicate that we’ve learned and implemented is printf()
, which tells the program to deliver some informations to users.
Let’s start by knowing what printf
can do.
printf("Hello World");
That is pretty much regular usage of printf, printing Hello World string into the display. If you have a variable, then you can let printf to put it with help of format specifier %
.
int a = 5;
int b = 10;
int c = a + b;
printf("a is %d, b is %d, the sum would be %d", a, b, c);
And the output will exactly the same as this more efficient, less variable usage printf:
printf("a is %d, b is %d, the sum would be %d", a, b, a+b);
Casting also can be used, to let compiler treat some values to a different type, for example you want to print integer values from float (albeit the value will be rounded) like this:
float phi = 3.14; printf("the value of phi is %d", (int)phi);
Always make sure that you watch your variables value, as rounded values could lead into different meaning from several constant, e.g, phi value. Always use float or double to represent these kind of constants.
Now, if printf display some informations to the user, C is capable to record users input using several methods, e.g. reading files, command-line argument and using run time function, scanf()
. However, we’ll gonna use only scanf for now to design some kind of computer-to-user interview program. Here, the program will ask user and user will have to prepare answers. Each of the answer is recorded using scanf and saved to a variable.
Basically, scanf requires you to include #include <stdio.h>
, similar with printf.
/* create a variable named userInput */
int userInput;
/* ask user about something */
printf("What is your age?");
When you compile above code and let it runs, the output will be a question What is your age? to any users who are using the program. When they want to answer with age numbers, program catch the entered value with scanf. Expected input should be stored in a memory address of correlated variable (with suitable data type), which in this case an integer.
/* create a variable named userInput */
int userInput;
/* ask user about something */
printf("What is your age?");
/* retrieve the user input */
scanf("%d", &userInput);
The scanf function ignores blanks and tabs in its format string, and will skip over white space as it looks for the proper input values. Whenever the question appears on the screen, then the program will prompt user to provide an answer. Any valid numbers will then stored on the userInput
variable.
Now, let’s make a simple program that will ask user’s informations, e.g. age, lastname and firstname. Then, at the end, we give the user the table-like summary (of course using command line). First of all, we will learn how to retrieve user’s name as string arrays. It’s little bit different than how we record integer. Take a look at the complete program first:
First, declare two arrays variables, first is char userLastName[256]
and the second is char userFirstName[256]
. The number here means the length of the reserved memory for each variable is 256 char
or equals to 256 bytes due to 1 char equals to 1 byte of memory space (1 char is 8 bits, 8 bits is 1 byte).
Then, after asking the user about his/her age, next question is for the name. We will use %s
to capture strings or characters. We will store the user’s name into declared variable arrays. As now we want to treat those input as sequence of some characters, we want to reserve memory space through the variable array. Now, we will point scanf to write the captured input to the address of the first index of the array. Thus we have to pass &arrayName[0]
instead of just &arrayName
as you can see on line:19.
Now, we have user’s age, last name and first name. line:26 to 29 are used to format the summary. Keep in mind that you can represent the summary as you wish with your own preferences.
Here’s the output from my terminal:
=====USER INFORMATION=====
Full Name : Janu Aditya
Age : 27
==========================
Next, we will try to exploit another way to pass user’s inputs into our program by reading files and passing arguments.