Here is the first article of my tutorial set. I aim to teach you the basics of C. In this first lesson, I am going to teach you how to output a series of strings on to terminal screen of C program.
Virtually every program code has inputs and outputs. Before compiling a source code, programs generally request an input from the user and then output it to the screen after compiling if the program is well designed and there is no error inside it. Usually, as the program becomes more complicated, error possibility increases. A good software expert easily realizes where the code has faults and corrects it. Since our first code will be very small and very simple, we will not face with any error.
Before starting to write code, we initially have a compiler which is going to evaluate our code. I use Dev C for this. There are many other compilers which run on different platforms like Unix, Linux and Windows.
In this project, we will output the names of the subprograms of Triond on to C terminal screen. This is the simplest algorithm since there is no input in this example. Let's start writing our code step by step.
Step 1: Open the File from the menu bar of C software and save as the blank page Project1. This yields a file with an extension of cpp.
Step 2: Describe the name and aim of the programs. To do so, we use comments. Comments are ignored by the compiler. For commenting, we use some special scripts like double slash or slash-asterisk character .
If we use a single line comment, double slash is enough. However, if our comment is placed more than one line, we use double slash for each line or take the commented part between slash-asterisk and asterisk-slash characters .
Step 3: C needs library files which define what the input and output functions are and what they do when they are used in a code. Iostream is the library file of input and output function.
Before the name of library files, we use a special character, preprocessor directive character. Include is used before the name of every library file and such file names are placed mathematical comparison characters.
Step 4: Every C code uses functions. The default function is main. Main function is the first function compiled by the programs. This function usually calls other functions. Before the name of function, we specify which type of output our code returns. In our program, we think that it returns to integer and use int. After the function name, we use parantheses. In this example, there will be nothing between parantheses but in more complicated programs, there may be parameter names and it's types or definitions. We place our statements between left brace and right brace.
Step 5: To print on to the screen,we use cout function. We simply place our string inside double quote characters. Every statement inside the functions ends with a special semicolon character.
Step 6: To check the validity, we use return function.In this simple example, it returns to 0.
Click here for the code