Monday, January 6, 2014

Basic Input Output

Basic Input and Output in C is easy to learn. You just have to memorize the function for displaying messages [ printf() ]  and getting the input [ scanf() ].  

printf("Some message");
scanf("%+data type",&+variable name);

But first, you need to declare the variable to be used. A variable is where you store the value for the input data. For numbers that range from 1-4 digits, we use the integer or int data type [ d or i ].
For more than 4 digit numbers, we use the long int
 
For int data type:                                                             For long data type:

int num;                              //declare the variable              long int num;
printf("Enter Number: ");    //prompt the  user                   printf("Enter Serial Number: "); 
scanf("%d",&num);           //input data                              scanf("%ld",&num);     // we use %ld for long int   

When getting letters or characters, we use the char data type [ c ].

char letter;
printf("Enter Your Letter: ");
scanf("%c",&letter);

 However the char data type only holds a single character. For getting characters more than 1, we use an array to declare how many characters the user can enter. 

char name[6];                //declare the variable and how many characters can be entered
printf("Enter Name: ");
scanf("%c",&name);



Now that we know the basic data types and how to input data, we can now proceed with the printing of the entered data. In printing data, the same format is used as entering data, the only difference is we use the printf() function in printing data. By the way, unlike other programming languages, C doesn't print or display another message on the following line. So to make the program more neat, we use [ \n ] to make another message appear on the next or new line.

Try the following code:

#include<stdio.h>
main()
{
int name[6];
clrscr();   

printf("Enter Name: ");           //prompt the user
scanf("%c",&name);             //input data

printf("/nHello %c!",name);   //display the data using the printf() function and \n for it to appear on the next line

getch();
}

Output:

Enter Name: Syntax
Hello Syntax!





Hello World - First C Program

In learning programming Languages, every beginner needs to start at the basics. The hello world program contains the basic parts of a program and it's functions. Now, let's proceed with the code.

First, we need to include the libraries to be used. Libraries contain the standard formats or collection of implementations that will be used. 
For example, the library <stdio.h> contains the printf, scanf and other basic functions needed for the program to run.

Declaring libraries always start with a hashtag or sharp ( # )

#include<stdio.h>

Then, we write the main() function.  The main() function contains all the blocks of codes of the program. For convenience of having the screen cleared every time a new program is run, we have the clrscr(); function. It can be written inside the main() function. 

#include<stdio.h>
main()
{


}



Always remember to have the brackets to identify the scope of the main() function. When printing messages, characters, numbers and symbols, we use the printf(); function It is also used to print variables, functions and pointers. Always remember to put a semicolon ( ; ) after every function.

For printing or displaying the "Hello World!" message on the screen we write:


printf("Hello World!");


Note that the Message or string is placed inside two double quotes ( " " ).  And to end the program, we shall use the getch(); function so that the user screen won't after the program execution.

Complete code is:                                                    Output:

#include<stdio.h>
main()
{
clrscr();

printf("Hello World!");

getch();

}