INPUT / OUTPUT STATEMENT
Scanf() Function
The scanf() function is used to read information from the standard input device(keyboard),scanf() function starts with a string argument and may contain additional arguments.
Syntax :
Scanf( “Control string”,“Addresslist “);
Scanf( “Control string”,“Addresslist “);
Where Control String: The below table illustrates code formats(control strings) in Input/Output statements.
Format
|
Code Meaning
|
% c
|
To read a single character
|
%d
|
To read a decimal number
|
%e
|
To read a floating point value
|
% f %g
|
To read a floating point value To read a floating point value % o To read an octal value
|
%x
|
To read hexadecimal value
|
%s
|
To read a string
|
%u
|
To read an unsigned integer
|
Each variable name(argument) must be proceeded by an ampersand(&).The ‘&’ symbol “address of” the variable.
Rules for writing scanf() function
1.The control string must be preceded with(%) sign and must be within quotations.
2.If there is a number of input data items, items must be separated by commas and must be preceded with(&) sign.
3.The control string and the variables going to input should match with each other.
4.It must have termination with semicolon.
5.The scanf() reads the data values until the blank space n numeric input or maximum number of character have been read, or error is detected.
printf ( ) ;
• This function provides for formatted output to the screen.
The syntax is:
printf ( “Control string ”, var1, var2, … ) ;
printf ( “Control string ”, var1, var2, … ) ;
• The “format” includes a listing of the data types of the variables to be output and, optionally, some text and control character(s).
• Example: float a=5.6 ; int b=4 ;
printf ( “You entered %f and %d \n”, a, b ) ;
Rules for writing printf() function
1. Place appropriate headings in the output.
2. The variable must be separated by commas, and need not be preceded with ‘&’ sign.
3. The control string ad the variables must match in their order.
4. The control string must be in quotations and there we can also use any other text to print with data.
5. Print special messages wherever required in output.
getchar ( ) ;
This function provides for getting exactly one character from the keyboard.
Example: char ch; ch = getchar ( ) ;
#include<stdio.h>
void main() {
char c;
printf(“enter a character”);
c=getchar(); printf(“c = %c ”,c); }
putchar (char) ;
This function provides for printing exactly one character to the screen.
Example:
char ch;
ch = getchar ( ) ; /* input a character from kbd*/
putchar (ch) ; /* display it on the screen */
getch()
These functions read any alphanumeric character from the standard input device
• The character entered is not displayed by the getch() function until enter is pressed
• The getch() accepts but does not display the character
Syntax getch( );
putch()
This function prints any alphanumeric character taken by the standard input device Example: #include void main() {
char ch;
printf(“Press any key to continue”);
ch = getch(); printf(“ you pressed:”);
putch(ch); }
gets(): This function is used for accepting any string until enter key is pressed (string will be covered later)
syntax: char str[length of string in number];
gets(str);
Example #include<stdio.h>
void main() {
char ch[30];
printf(“Enter the string:”);
gets(ch);
printf(“Entered string: %s”, ch); }
puts() : This function prints the string or character array. It is opposite to gets()
Syntax: char str[length of string in number];
gets(str);
puts(str);
Program to Print an Integer
#include <stdio.h>int main()
{ int number;// printf() dislpays the formatted output printf("Enter an integer: ");
// scanf() reads the //formatted input and storesthem
scanf("%d", &number); // printf() displays the //formatted output
printf("You entered: %d", number);
return 0;}
{ int number;// printf() dislpays the formatted output printf("Enter an integer: ");
// scanf() reads the //formatted input and storesthem
scanf("%d", &number); // printf() displays the //formatted output
printf("You entered: %d", number);
return 0;}
Program to Add Two Integers
#include <stdio.h>
int main()
{
int fNumber, SNumber, sum;
printf("Enter two integers: "); // Two integers entered by user is stored
//using scanf() function
//using scanf() function
scanf("%d %d", &fNumber, &SNumber);
// sum of two numbers in stored in //variable //sumOfTwoNumbers
// sum of two numbers in stored in //variable //sumOfTwoNumbers
sum = fNumber + SNumber;
// Displays sum
printf("%d + %d = %d", firstNumber, secondNumber, sumOfTwoNumbers);
return 0;}
Program to Print ASCII Value
#include <stdio.h>
int main()
{ char c;
printf("Enter a character: ");
// Reads character input from the user
// Reads character input from the user
scanf("%c", &c);
// %d displays the integer value of a character
// %c displays the actual character
// %d displays the integer value of a character
// %c displays the actual character
printf("ASCII value of %c = %d", c, c);
return 0;}
No comments:
Post a Comment