Input/output statements, Assignment statements

        There are some library functions which are available for transferring the information between the computer and the standard input and output devices. These functions are related to the symbolic constants and are available in the header file. In C Language input and output functions are available as C compiler functions or C library provided with each C compiler implementation. These all functions are collectively known as Standard I/O Library functions.

stdin : This file is used to receive the input (usually is keyboard file, but can also take input from the disk file).

stdout : This file is used to send or direct the output (usually is a monitor file, but can also send the output to a disk file or any other device).

stderr : This file is used to display or store error messages.

Types of input/output function:

Input output functions in C programming fall into two categories, namely, formatted input

output (I/O) functions and unformatted input output (I/O) functions .

i) FORMATTED INPUT/OUTPUT FUNCTION:

Formatted Input (scanf):

The function scanf() is used for formatted input from standard input and provides many of the conversion facilities of the function printf().

Syntax

scanf (“format specifier”, num1, num2,……);

The function scanf() reads and converts characters from the standards input depending on the format

specification string and stores the input in memory locations represented by the other arguments (num1,

num2,….).

For Example:

scanf(“ %c %d”,&Name, &Roll No) ;

Note: the data names are listed as &Name and &Roll No instead of Name and Roll No respectively. This is how data names are specified in a scanf() function. In case of string type data names, the data name is not preceded by the character &.

Format specifiers in console formatted I/O functions

Some of the most commonly used format specifiers used in console formatted input/output functions are displayed in the table below -

Format Specifier Description

%d          Format specifier used to read or display a signed int integer

               value.

%u          Format specifier used to read or display an unsigned int

                integer value.

%ld         Format specifier used to read or display a long integer value

               or signed long integer value.

Formatted Output (printf)

We will look at how to use format specifiers to print formatted output onto the screen.The printf function must be supplied with a format string, followed by any values that are to be inserted into the string during printing:

printf(string, expr1, expr2, …);

Example:

int i, j;

float x, y;

i = 10;

j = 20;

x = 43.2892f;

y = 5527 0f;

y = 5527.0f;

printf("i = %d, j = %d, x = %f, y = %f\n", i, j, x, y);

Output: i = 10, j = 20, x = 43.289200, y = 5527.000000

%c               Format specifier used to read or display a char , character

                    value.

%f               Format specifier used to read or display a float , floating-point

                    value.

%lf               Format specifier used to read or display a double ,

                    floating-point value.

%s               Format specifier used to read or display a string value, to be

                    stored in a char[] array.

C program using formatted input and output function:

#include <stdio.h>

void main ()

{

int a , b , c ;

printf ( "Please enter any two numbers: \n" );

scanf ( "%d %d" , & a , & b );

c = a + b ;

printf ( "The addition of two number is: %d" , c );

}

Output:

Please enter any two numbers:

12

3

The addition of two number is:15

The above program scanf() is used to take input from the user, and respectively printf() is used to

display output result on the screen.

ii) UNFORMATTED INPUT/OUTPUT FUNCTION

Unformatted console input/output functions are used to read a single input from the user at console and it also allows us to display the value in the output to the user at the console.

Some of the most important formatted console input/output functions are -

getchar() Function

The getchar() function reads character type data form the input. The getchar() function reads

one character at a time till the user presses the enter key.

getchar() C Program

#include <stdio.h> //header file section

#include <conio.h>

int main()

{

char c;

printf("Enter a character : ");

c = getchar();

printf("\nEntered character : %c ", c);

return 0;

}

Functions Description

getch()          Reads a single character from the user at the console, without

                     echoing it .

getche()        Reads a single character from the user at the console, and

                     echoing it .

getchar()       Reads a single character from the user at the console, and

                     echoing it , but needs an Enter key to be pressed at the end.

gets()            Reads a single string entered by the user at the console.

puts()            Displays a single string's value at the console.

putch()          Displays a single character value at the console.

putchar()       Displays a single character value at the console.



getch() Function - The getch() function reads the alphanumeric character input from the user. But, that the entered character will not be displayed.

getch() C Program

getch.c

#include <stdio.h> //header file section

#include <conio.h>

int main()

{

printf("\nHello, press any alphanumeric character to exit ");

getch();

return 0;

}

Hello, press any alphanumeric character to exit

Note: The above program will run until you press one of many alphanumeric characters. The key pressed by you will not be displayed.

getche() Function - getche() function reads the alphanumeric character from the user input. Here, character you entered will be echoed to the user until he/she presses any key.

getche() C Program

getche.c

#include <stdio.h> //header file section

#include <conio.h>

int main()

{

printf("\nHello, press any alphanumeric character or symbol to exit \n ");

getche();

return 0;

}

output

Hello, press any alphanumeric character or symbol to exit

k

Note: The above program will run until you press one of many alphanumeric characters. The key pressed by you will be echoed.

putchar() Function - putchar() function prints only one character at a time.

putchar() C Program

putchar.c

#include <stdio.h> //header file section

#include <conio.h>

int main()

{

char c = 'K';

putchar(c);

return 0;

}

Output

K

Note: Here, variable c is assigned to a character 'K'. The variable c is displayed by the putchar(). Use Single quotation mark ' ' for a character.

putch() Function - The putch() function prints any alphanumeric character.

putch() C Program

putch.c

#include <stdio.h> //header file section

#include <conio.h>

int main()

{

char c;

printf("Press any key to continue\n ");

c = getch();

printf("input : ");

putch(c);

return 0;

}

Output

Press any key to continue

input : d

Note: The getch() function will not echo a character. The putch() function displays the input you pressed.

String IO Functions

gets() Function - The gets() function can read a full string even blank spaces presented in a string. But, the scanf() function leaves a string after blank space is detected. The gets() function is used to get any string from the user.

gets() C Program

gets.c

#include <stdio.h> //header file section

#include <conio.h>

int main()

{

char c[25];

printf("Enter a string : ");

gets(c);

printf("\n%s is awesome ",c);

return 0;

}

Output:

Enter a string : Randy Orton

Randy Orton is awesome

Note: The gets() function reads a string from through keyboard and stores it in character array c[25]. The printf() function displays a string on the console.

puts() Function - The puts() function prints the charater array or string on the console. The puts() function is similar to printf() function, but we cannot print other than characters using puts() function.

puts() C Program

puts.c

#include <stdio.h> //header file section

#include <conio.h>

int main()

{

char c[25];

printf("Enter your Name : ");

gets(c);

puts(c);

return 0;

}

output

Enter your Name: john

john

ASSIGNMENT STATEMENT

An a ssignment statement is a statement containing the assignment operator "=" that assigns a literal value, the value of a variable or the value of an expression to a variable or object.

The syntax of an assignment statement is VariableName = Expression where VariableName is the name of a variable that has been declared, and Expression is either a literal, a variable, or an expression which can be evaluated to a value.The same syntax applies in the case of an object rather than a variable.

Examples of assignment statements (assuming that the variables have been declared):

● NumberMoles = 0.327 setting the number of moles to 0.327

● x = x + 1 incrementing the value of x by one

● x = (y + 2) / (z - 3) assign x a value based on the values of y and z

● FileOpen = True setting a boolean variable to True

● Range("BirthYear").Value = 1988 assigning a property value of an object

Comments

Popular posts from this blog

Storage Class

Data types in C