Wednesday, November 12, 2014

C Programming Input Output (I/O)

C Programming Input Output (I/O)

ANSI standard has defined many library functions for input and output in C language. Functions printf() and scanf() are the most commonly used to display out and take input respectively. Let us consider an example:

#include <stdio.h>      //This is needed to run printf() function.
int main()
{
    printf("C Programming");  //displays the content inside quotation
    return 0;
}
Output
C Programming
Explanation of How this program works
  1. Every program starts from main() function.
  2. printf() is a library function to display output which only works if #include<stdio.h>is included at the beginning.
  3. Here, stdio.h is a header file (standard input output header file) and #include is command to paste the code from the header file when necessary. When compiler encounters printf() function and doesn't find stdio.h header file, compiler shows error.
  4. Code return 0; indicates the end of program. You can ignore this statement but, it is good programming practice to use return 0;.

I/O of integers in C


#include<stdio.h>
int main()
{
    int c=5;
    printf("Number=%d",c);
    return 0;
}
Output
Number=5
Inside quotation of printf() there, is a conversion format string "%d" (for integer). If this conversion format string matches with remaining argument,i.e, c in this case, value of c is displayed.

#include<stdio.h>
int main()
{
    int c;
    printf("Enter a number\n");
    scanf("%d",&c);  
    printf("Number=%d",c);
    return 0;
}
Output
Enter a number
4
Number=4
The scanf() function is used to take input from user. In this program, the user is asked a input and value is stored in variable c. Note the '&' sign before c. &c denotes the address of c and value is stored in that address.

I/O of floats in C


#include <stdio.h>
int main(){
    float a;
    printf("Enter value: ");
    scanf("%f",&a);
    printf("Value=%f",a);    //%f is used for floats instead of %d
    return 0;
}

Output
Enter value: 23.45
Value=23.450000
Conversion format string "%f" is used for floats to take input and to display floating value of a variable.

I/O of characters and ASCII code

#include <stdio.h>
int main(){
    char var1;
    printf("Enter character: ");
    scanf("%c",&var1);     
    printf("You entered %c.",var1);  
    return 0;
}
Output
Enter character: g
You entered g.
Conversion format string "%c" is used in case of characters.

ASCII code

When character is typed in the above program, the character itself is not recorded a numeric value(ASCII value) is stored. And when we displayed that value by using "%c", that character is displayed.

#include <stdio.h>
int main(){
    char var1;
    printf("Enter character: ");
    scanf("%c",&var1);     
    printf("You entered %c.\n",var1);  
/* \n prints the next line(performs work of enter). */
    printf("ASCII value of %d",var1);  
    return 0;
}
Output
Enter character:
g
103
When, 'g' is entered, ASCII value 103 is stored instead of g.
You can display character if you know ASCII code only. This is shown by following example.

#include <stdio.h>
int main(){
    int var1=69;
    printf("Character of ASCII value 69: %c",var1);
    return 0;
}
Output
Character of ASCII value 69: E
The ASCII value of 'A' is 65, 'B' is 66 and so on to 'Z' is 90. Similarly ASCII value of 'a' is 97, 'b' is 98 and so on to 'z' is 122.
Click here to learn about complete reference of ASCII code.

More about Input/Output of floats and Integer

Variations in Output for integer an floats

Integer and floating-points can be displayed in different formats in C programming as:
#include<stdio.h>
int main(){
    printf("Case 1:%6d\n",9876);      
/*  Prints the number right justified within 6 columns  */
    printf("Case 2:%3d\n",9876);      
/* Prints the number to be right justified to 3 columns but, there are 4 digits so number is not right justified  */
    printf("Case 3:%.2f\n",987.6543);
/* Prints the number rounded to two decimal places */
    printf("Case 4:%.f\n",987.6543);
/* Prints the number rounded to 0 decimal place, i.e, rounded to integer */
    printf("Case 5:%e\n",987.6543);
/* Prints the number in exponential notation(scientific notation) */
    return 0;
}
Output
Case 1:  9876
Case 2:9876
Case 3:987.65
Case 4:988
Case 5:9.876543e+002

Variations in Input for integer and floats

#include <stdio.h>
int main(){
    int a,b;
    float c,d;
    printf("Enter two intgers: ");
/*Two integers can be taken from user at once as below*/
    scanf("%d%d",&a,&b);
    printf("Enter intger and floating point numbers: ");
/*Integer and floating point number can be taken at once from user as below*/
    scanf("%d%f",&a,&c);
    return 0;
}
Similarly, any number of input can be taken at once from user.

No comments:

Post a Comment