Output statement in C language
Output statement in C++ language
Output statement in C#.NET
Output statement in VB.NET
Print / Output statement in C language
Displaying Information On-Screen - The printf() Function
The printf() statement is a library function that displays information on-screen. The printf() statement can display a simple text message (as in lines 11 and 15) or a message and the value of one or more program variables.
#include <stdio.h>
printf( format-string[,arguments,...]);
STDIO.H contains the prototypes for the standard input/output functions. printf(), puts(), and scanf() are three of these standard functions. Try running a program without the STDIO.H header and see the errors and warnings you get.
printf() is a function that accepts a series of arguments, each applying to a conversion specifier in the given format string. printf() prints the formatted information to the standard output device, usually the display screen. When using printf(), you need to include the standard input/output header file, STDIO.H.
Example
#include
main()
{
printf("This is an example to print content!");
return 0;
}
Output
This is an example to print content!
printf( "Enter your name and press
A printf() format string specifies how the output is formatted. Here are the three possible components of a format string:
Literal text is displayed exactly as entered in the format string. In the preceding example, the characters starting with the T (in The) and up to, but not including, the % comprise a literal string.
An escape sequence provides special formatting control.
An escape sequence consists of a backslash (\) followed by a single character. In the preceding example, \n is an escape sequence. It is called the newline character, and it means "move to the start of the next line." Escape sequences are also used to print certain characters.
A conversion specifier consists of the percent sign (%) followed by a single character. In the example, the conversion specifier is %d. A conversion specifier tells printf() how to interpret the variable(s) being printed. The %d tells printf() to interpret the variable x as a signed decimal integer.
The most frequently used escape sequences.
Sequence | Meaning |
\a | Bell (alert) |
\b | Backspace |
\n | Newline |
\t | Horizontal tab |
\\ | Backslash |
\? | Question mark |
\' | Single quotation |
The printf() Conversion Specifiers
The format string must contain one conversion specifier for each printed variable. printf() then displays each variable as directed by its corresponding conversion specifier. Exactly what does this mean?The most commonly needed conversion specifiers.
Specifier | Meaning | Types Converted |
%c | Single character | char |
%d | Signed decimal integer | int, short |
%ld | Signed long decimal integer | long |
%f | Decimal floating-point number | float, double |
%s | Character string | char arrays |
%u | Unsigned decimal integer | unsigned int, unsigned short |
%lu | Unsigned long decimal integer | unsigned long |
A single printf() statement can print an unlimited number of variables, but the format string must contain one conversion specifier for each variable. The conversion specifiers are paired with variables in left-to-right order. If you write
printf("Rate = %f, amount = %d", rate, amount);
For example, to print the sum of x and y, you could write
z = x + y;
printf("%d", z);
You also could write
printf("%d", x + y);
/* Demonstration using printf() to display numerical values. */
#include <stdio.h>
int a = 2, b = 10, c = 50;
float f = 1.05, g = 25.5, h = -0.1;
main()
{
printf("\nDecimal values without tabs: %d %d %d", a, b, c);
printf("\nDecimal values with tabs: \t%d \t%d \t%d", a, b, c);
printf("\nThree floats on 1 line: \t%f\t%f\t%f", f, g, h);
printf("\nThree floats on 3 lines: \n\t%f\n\t%f\n\t%f", f, g, h);
printf("\nThe rate is %f%%", f);
printf("\nThe result of %f/%f = %f\n", g, f, g / f);
return 0;
}
Decimal values without tabs: 2 10 50
Decimal values with tabs: 2 10 50
Three floats on 1 line: 1.050000 25.500000 -0.100000
Three floats on 3 lines: 1.050000 25.500000 -0.100000
The rate is 1.050000%
The result of 25.500000/1.050000 = 24.285715
/* Program to calculate the product of two numbers. */
#include <stdio.h>
int a,b,c;
int product(int x, int y);
main()
{
/* Input the first number */
printf("Enter a number between 1 and 100: ");
scanf("%d", &a);
/* Input the second number */
printf("Enter another number between 1 and 100: ");
scanf("%d", &b);
/* Calculate and display the product */
c = product(a, b);
printf ("%d times %d = %d\n", a, b, c);
return 0;
}
/* Function returns the product of its two arguments */
int product(int x, int y)
{
return (x * y);
} Enter a number between 1 and 100: 35 Enter another number between 1 and 100: 23
35 times 23 = 805
Displaying information on screen in C++ language
Displaying Information On-Screen - The cout() Function
#include <iostream.h>
include is a preprocessor instruction that says, "What follows is a filename. Find that file and read it in right here." The angle brackets around the filename tell the preprocessor to look in all the usual places for this file. If your compiler is set up correctly, the angle brackets will cause the preprocessor to look for the file iostream.h in the directory that holds all the H files for your compiler. The file iostream.h (Input-Output-Stream) is used by cout, which assists with writing to the screen.
"cout" and its related object "cin". These two objects, cout and cin, are used in C++ to print strings and values to the screen.
Formatted output operations, cout is used together with the insertion operator (output redirection operator) which is written as << (i.e. two "less than" signs).
cout << "Here is sentence for display "; // prints the sentence on screen
cout << 101; // prints number 101 on screen
cout << name; // prints the value of name on screen
The << operator inserts the data that follows it into the stream that precedes it.
cout << "Hello"; // prints Hello
cout << Hello; // prints the content of variable Hello
Multiple insertion operations (<<) may be chained in a single statement:
cout << "This " << " is a " << "single C++ statement";
Output : This is a single C++ statement.
cout << "I am " << age << " years old ";
Output : I am 60 years old; // if input / value of age variable is 60
cout << "First sentence.\n";
cout << "Second sentence.\nThird sentence.";
Output:
First sentence.
Second sentence.
Third sentence.
The endl manipulator produces a newline character, exactly as the insertion of '\n' does;
cout << "First sentence." << endl;
cout << "Second sentence." << endl;
Output:
First sentence.
Second sentence.
Print / Output statement in C#.NET language
With the Console.WriteLine() or Response.Write() statement, we display the message on the console window and move to the next line.namespace SumGenerator
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Write the first integer:");
int first = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Write the second integer:");
int second = Convert.ToInt32(Console.ReadLine());
int result = first + second;
Console.WriteLine($"The result is {result}");
Console.ReadKey();
}
}
}
Output:
Write the first integer:
10
Write the second integer:
40
The result is 50
Response.Write adds string data to the Response buffer.
Response.Write("Here is statement to display");
Response.Write(Variable_Name);
Plus sign (+) uses to concantenate two strings
Response.Write("Value of variable is : " + Variable_Name);
Response.Write(variable_name1 + "----" + variable_name2);
Response.write() is used to display the normal text and Response.output.write() is used to display the formated text.
How to use Response.Write() in class library?
System.Web.HttpContext.Current.Response.Write(); // as i recall from memory.The difference between Console.Write() and Console.WriteLine() method is based on new line character.
Console.Write() method displays the output but do not provide a new line character.
Console.WriteLine() method displays the output and also provides a new line character it the end of the string, This would set a new line for the next output.
using System;
class Program {
static void Main() {
Console.Write("One");
Console.Write("Two");
// this will set a new line for the next output
Console.WriteLine("Three");
Console.WriteLine("Four");
}
}
Output
OneTwoThree
Four
Output statement in VB.NET
In VB.NET semicolon is not uses in statement.
Console.WriteLine("Here is statement to display")
Response.Write("Here is statement to display")
Response.Write(Variable_Name)
MsgBox("Message")
MsgBox("Message : " & Variable_Name)
sign ampersand(&) uses to concantenate two strings
Response.Write("Value of variable is : " & Variable_Name)
Response.Write(variable_name1 & "----" & variable_name2)
How to use Response.Write() in class library?
System.Web.HttpContext.Current.Response.Write(); // as i recall from memory.
MsgBox("Hi")
Comments
Post a Comment