C Basic Input and Output

C Tutorials from Roots - Part 16

Chrys Forcha
Introduction
This is part 16 of my series, C Tutorials from Roots. Since the beginning of this series, we have been writing programs but we have not input data using the keyboard. We have been outputting data using the monochrome (black and white) screen or its simulation like the Command Prompt window. Using the keyboard for input and a monochrome screen for output, where input and output is text, is said to be using the console. In this part of the series, I show a simple way of inputting data and a simple way of outputting data with C.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The main Function
This is what we have seen about the main function:

int main()
{
//statements

return 0;
}

The return value of the main function is a value of type, int. That is why the main function is preceded by the word, int, at its definition. The last statement of the main function is "return 0;" which returns zero. Always end your main function with this return statement. It is possible to return some integer other than zero, but I will not go into that.

Any code outside the main function that has to be executed, must be called directly or indirectly from the block of the main function. When the program starts, statements of the main function are executed from top to bottom ending at "return 0;". Before it reaches this end (returning zero), calls must have been made to code segments (functions) outside the block of the main function.

Executing a Program
To execute a program, you normally type something like the following at the DOS prompt:

filename.exe

and then you press Enter. This is a place where you can type input for the program. The program name is filename.exe, where filename is the name you give. After typing the program name, you can type a space and then type strings that will be input to the program. The strings are separated by spaces (not by commas). So each of the strings should not have a space, since spaces are used to separate strings. For this to work, you need to put the following parameters in the parentheses of the main function:

int argc, char *argv

You have two parameters here: an int and an array of strings. The identifier (argc) for the int parameter is the number of strings in the array; that is why it is in the square brackets of the array parameter. For simple input, you do not have to know the value of this number, so you do not have to know the value in the object identified by argc.

Consider the following:

filename.exe one two three

This is what you will type at the DOS prompt to try the code below. filename.exe represents the name you will give your executable file. Give any name you want, but let it end with .exe. After this, there is a space and then three strings. The strings above are separated by spaces. Quotation makes are not used. Each string should not have a space. Read and try the following code using the above line at the DOS prompt.

#include

int main(int argc, char *argv
{
printf(argv[0]); printf("\n");
printf(argv[1]); printf("\n");
printf(argv[2]); printf("\n");
printf(argv[3]); printf("\n");

return 0;
}

You output should be,

filename.exe
one
two
three

The array element, argv[0] from the second parameter would give the filename of the program. The rest of the elements would give strings you typed, in the order you typed them.

Outputting Data
You can output data using the printf function. We have seen some examples of the use of this function in this and the previous parts of the series. I will give you a more formal explanation of how this function is used later in the series.

I did not explain the details of the parameters for the main function. However, for these basic tutorials just accept things the way I have given.

Well, let us stop here and continue in the next part of the series.

Chrys

Published by Chrys Forcha

I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.