Input and Output in C programming

 

Input/Output Operation

Ø  Input means to provide the program with some data to be used in it. 

Ø  Output means to display data on the screen or write the data to a printer or a file.

Ø  The C programming provides standard library functions to read any given input and display output on the console.

Ø  While dealing with input-output operations in C, we use the following two streams:

Ø  Standard input or stdin is used for taking input.

Ø  Standard output or stdout is used for giving output.

Ø  The functions used for standard input and output are present in the stdio.h header file.

Ø  Hence, to use those functions, we need to include the stdio.h header file in our program

Formatted I/O

Ø  The formatted functions basically present or accept the available data (input) in a specific format.

Ø  The standard library in C contains various functions for the input-output operations.

Ø  The scanf() and printf() out of these functions help a programmer format the functions in their desired format.

Ø  The program can use these functions for reading any form of data, like a real number, an integer, a character, and many more.

printf()

Ø  The printf() function basically gets defined in the header file stdio.h, and we use it for showing the standard output (the output available on the console).

Ø  The programmers use the printf() function for printing the value of the variable or any simple sentence (text).

Ø  These variables can be of float, char, int, or the other data types.

Examples of printf()

            Printing of a Sentence

            #include <stdio.h>

            int main() {

printf(“Hello World”);

return 0;

            }

            Printing of an Integer

            Here, we use the format specifier %d or %i.

            #include <stdio.h>

            int main() {

            int q = 50;

            printf(“The given value of q is: %d”, q);

            return 0;

            }

            Printing of a Character

            Here, we use the format specifier %c.

            #include <stdio.h>

            int main() {

            char age = ’43’;

            printf(“Today is My Birthday and I am: %c”, age);

            return 0;

            }

            Printing of Double and Float

            Here, we use the format specifier %f for the float value and the format specifier %lf for the double value.

            #include <stdio.h>

            int main() {

            double num_11 = 15556522.0978678;

            float num_22 = 15.50;

            printf(“The value of the num_11 is: %lf \n”, num_11);

            printf(“The value of num_22 is: %f”, num_22);

            return 0;

            }

            Printing of Multiple Outputs

            #include <stdio.h>

            int main() {

            int year = 1997;

            int month = 08;

            int day = 18;

            printf(“My Birth date is: %d-%d-%d”, year, month, day);

            return 0;

            }

scanf()

Ø  We make use of the scanf() function whenever we want the program to receive inputs from us.

Ø  Thus, when the program receives input from the user, it stores those input values into any variable.

Ø  In short, we use the scanf() function to receive inputs of all datatypes from a user.

Ø  The only thing that we must take care of is that those variables in which we store the input values have a similar data type.

Examples of scanf()

Taking Input of Integer Value

Ø  Here, we must perform the definition of the integer variable before using the scanf() function in the program.

            #include <stdio.h>

            int main() {

int number;

printf(“Hello, please enter a digit here: “);

scanf(“%d”, &number);

printf(“The digit that you entered is: %d”, number);

return 0;

 }

Taking Input of Float Value

#include <stdio.h>

 int main() {

float input_from_user;

printf(“Hi, please enter a decimal value: “);

scanf(“%f”, &input_from_user);

printf(“The value that you have entered is: %f”, input_from_user);

return 0;

 }

Taking Input of Character Value

 #include <stdio.h>

 int main() {

char gender;

printf(“Hi, we would like you to enter your gender (F, M or O): “);

scanf(“%c”, &gender);

printf(“Thanks, your gender is: %c”, gender);

return 0;

}

Taking Input of Multiple Values

#include <stdio.h>

 int main() {

int age;

char gender;

 printf(“Please enter your gender and then age(F, M or O): “);

scanf(“%c %d”, &gender, &age);

 printf(“The information that you entered is: %c and %d”, gender, age);

return 0;

 }

Unformatted I/O

 Ø  The unformatted functions are not capable of controlling the format that is involved in writing and reading the available data. Thus, these functions constitute the most basic forms of output and input.

Ø  The supply of input or the display of output isn’t allowed in the user format – thus, we call these functions unformatted functions for input and output.

Ø  The unformatted input-output functions further have two categories:

Ø  The character functions

Ø  The string functions

character functions

Ø  We use the character input functions for reading only a single character from the input device (the keyboard).

Ø  On the other hand, we use the character output functions for writing just a single character on the output source (the screen).

Ø  Here, the getchar(), getche(), and the getch() refer to the input functions of unformatted type, while the putchar() and putch() refer to the output functions of unformatted type.

string functions

Ø  In any programming language, the character array or string refers to the collection of various characters.

Ø  Now, various types of input and output functions are present in C programming that can easily read and write these strings.

Ø  The puts() and gets() are the most commonly used ones.

Ø  Here, the gets() refers to the input function used for reading the string characters while the puts() refers to the output function used for writing the string characters (in unformatted forms).

getchar-putchar

getchar

Ø  The getchar() function in a program basically reads the characters available from the terminal.

Ø  It then returns these characters in the form of integers.

Ø  The getchar() function is capable of reading just a single character at any given time.

Ø  A programmer can easily use this method in the case of a loop if they are willing to read more than a single character.

putchar

Ø  The putchar() function in a program displays all the characters that are passed to this function on the screen.

Ø  After that, it returns the very same character. The putchar() function also displays one single character at any given time.

Ø  We can also utilise the putchar() method in any given loop if we are willing to display multiple characters.

getchar-putchar(Example)

            #include <stdio.h>

            void main( )

            {

            int x;

            printf(“Please enter a character here”);

            x = getchar();

            putchar(x);

            }

getch-putch

getch

Ø  getch() is a nonstandard function and is present in conio.h header file which is mostly used by MS-DOS compilers like Turbo C.

Ø  This function is used to Accept one character , from keyboard ,without echo to the screen

Ø  Without echo means when we type on screen it is not visible.

Ø  This function provides functionality of not getting echo to the screen

Ø  This Function takes One Character as an input

Ø  Many programmer use this Function at the end of the program to stop screen until a Character is given as an input.(but its work is not to stop the screen, it Wait's for the character )

putch

Ø  This function is used to print one character ,on the screen

Ø  This Function also print One Character as an input

 

 getch-putch(Example)

#include<stdio.h>

void main(){

char var1;

printf("Enter The Character>");

var1 = getch();

putch(var1);

}

getche

Ø  This function is used to Accept one character, from keyboard, echo to the screen

Ø  This Function also takes One Character as an input

#include<stdio.h>

void main(){

char var1;

printf("Enter The Character:");

var1 = getche();    

printf("Character  was  %c",var1);

}

gets-puts

Ø  The gets and puts functions are used for taking string input and giving string output.

gets

Ø  The gets() function reads a line of text from stdin(standard input) into the buffer pointed to by str pointer, until either a terminating newline or EOF (end of file) occurs.

puts

Ø  The puts() function writes the string str with a newline character ('\n') at the end to stdout. On success, a non-negative value is returned.

gets-puts(Example)

#include <stdio.h>

int main(){

/* character array of length 100 */

char str[100];

printf("Enter a string: ");

  gets(str);

  puts(str);

  getch();

  return 0;

}

Scanf vs gets

 Ø  The main difference between these two functions is that scanf() stops reading characters when it encounters a space, but gets() reads space as a character too.

Ø  If you enter a name as Dennis Ritchie using scanf() it will only read and store Dennis and will leave the part of the string after space.

Ø  But gets() function will read it completely.

#include <stdio.h>

int main() {

 // using scanf()

char n1[50];

printf("Please enter n1: ");

 scanf("%s", n1);

 printf("You entered: %s", n1);

 return 0;

}

#include <stdio.h>

int main() {

   // using gets()

   char n1[50];

   printf("Please enter n1: ");

   gets(n1);

   printf("You entered: %s", n1);

   return 0;

}

 

 

 

 

 

             

             

 

 

 

No comments:

Post a Comment

Research in Social Science

   C  Concept of Research in Social Science -         Understanding the concept of Research Methods, Techniques and Tools: Interview, Fo...