KEMBAR78
3.input Output Formats | PDF | Computing | Computer Programming
0% found this document useful (0 votes)
17 views10 pages

3.input Output Formats

The document explains input-output functions in C programming, detailing formatted and unformatted I/O functions. It provides a comprehensive list of format specifiers for various data types and examples of using printf and scanf for formatted output and input. Additionally, it discusses unformatted I/O functions like getchar, putchar, gets, and puts, emphasizing the use of fgets for safer string input.

Uploaded by

lathasreeponnada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views10 pages

3.input Output Formats

The document explains input-output functions in C programming, detailing formatted and unformatted I/O functions. It provides a comprehensive list of format specifiers for various data types and examples of using printf and scanf for formatted output and input. Additionally, it discusses unformatted I/O functions like getchar, putchar, gets, and puts, emphasizing the use of fgets for safer string input.

Uploaded by

lathasreeponnada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

INPUT-OUTPUT FORMATS

Console simply means screen and keyboard. There are two types of a console I/O functions:
● Formatted input-output function
● Unformatted input-output function

In C programming, input and output operations are often handled using functions from the standard
I/O library (stdio.h). Here is a table that lists the format specifiers for integers, floating-point
numbers, and characters in C:

Format Description Example


Data Type
Specifier
%d Signed decimal integer printf("%d", 10);

%i Signed decimal integer printf("%i", 10);

%u Unsigned decimal integer printf("%u", 10);


Integer
%o Unsigned octal printf("%o", 10);

%x Unsigned hexadecimal (lowercase) printf("%x", 10);

%X Unsigned hexadecimal (uppercase) printf("%X", 10);

%f Decimal floating point (lowercase) printf("%f", 3.14);

%F Decimal floating point (uppercase) printf("%F", 3.14);

%e Scientific notation (lowercase) printf("%e", 3.14);

%E Scientific notation (uppercase) printf("%E", 3.14);

Floating- %g Use %e or %f based on the value printf("%g", 3.14);


point
%G Use %E or %F based on the value printf("%G", 3.14);

%a Hexadecimal floating point printf("%a", 3.14);


(lowercase)
%A Hexadecimal floating point printf("%A", 3.14);
(uppercase)
Character %c Character printf("%c", 'A');
String %s String printf("%s","Hello");

Pointer %p Pointer printf("%p", ptr);

Special %% Percent sign printf("%%");

NOTE: These format specifiers are used with functions like printf for output and scanf for
input to format data appropriately.

Formatted Input and Output statements in C


In C programming, printf and scanf are used for output and input operations, respectively.

1. printf – Output: The printf function is used to print formatted output to the standard
output (usually the terminal).
The syntax is: printf("format string", arguments);
● Format string: Contains text to be printed and format specifiers.
● Arguments: Values to be printed, matching the format specifiers in the format
string.

We can perform following formatting options with the help of printf( ):


● Right and left justification.
● Rounding floating-point values.
● Inserting literal characters.
● Displaying all types of data with appropriate size and precisions.

Formatted Output (printf)

The printf function is used to print formatted output to the standard output (usually the
terminal).

Example 1: Basic printf

#include <stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
}

Output:

Hello, World!

Example 2: Printing Integers and Floats

#include <stdio.h>

int main() {
int a = 10;
float b = 5.25;

printf("Integer: %d\n", a);


printf("Float: %.2f\n", b);

return 0;
}

Output:

Integer: 10
Float: 5.25

Example 3: Printing Characters and Strings

#include <stdio.h>

int main() {
char ch = 'A';
char str[] = "Hello";

printf("Character: %c\n", ch);


printf("String: %s\n", str);

return 0;
}

Output:

Character: A
String: Hello

Example 4: Width and Precision

#include <stdio.h>

int main() {
int num = 12345;
float pi = 3.14159;

printf("Number with width 10: %10d\n", num);


printf("Pi with precision 2: %.2f\n", pi);

return 0;
}

Output:

Number with width 10: 12345


Pi with precision 2: 3.14
Formatted Input (scanf)

scanf – Input: The scanf function is used to read formatted input from the standard input
(usually the keyboard).
The syntax is: scanf("format string", &variables);
● Format string: Contains format specifiers that dictate the type of input to be read.
● Variables: Addresses of the variables where the input data will be stored.

Example 1: Reading an Integer

#include <stdio.h>

int main() {
int num;

printf("Enter an integer: ");


scanf("%d", &num);

printf("You entered: %d\n", num);

return 0;
}

Output:

Enter an integer: 42
You entered: 42

Example 2: Reading a Float

#include <stdio.h>

int main() {
float num;

printf("Enter a float: ");


scanf("%f", &num);

printf("You entered: %.2f\n", num);

return 0;
}

Output:

Enter a float: 3.14


You entered: 3.14
Example 3: Reading Characters and Strings

#include <stdio.h>

int main() {
char ch;
char str[50];

printf("Enter a character: ");


scanf(" %c", &ch); // Notice the space before %c

printf("Enter a string: ");


scanf("%s", str);

printf("You entered character: %c\n", ch);


printf("You entered string: %s\n", str);

return 0;
}
Output:
Enter a character: A
Enter a string: Hello
You entered character: A
You entered string: Hello
Combined Example
This example demonstrates using both printf and scanf in a single program.
#include <stdio.h>
int main() {
int a;
float b;
char c;
char str[50];
printf("Enter an integer: ");
scanf("%d", &a);

printf("Enter a float: ");


scanf("%f", &b);

printf("Enter a character: ");


scanf(" %c", &c);

printf("Enter a string: ");


scanf("%s", str);

printf("You entered:\n");
printf("Integer: %d\n", a);
printf("Float: %.2f\n", b);
printf("Character: %c\n", c);
printf("String: %s\n", str);

return 0;
}
Output:
Enter an integer: 25
Enter a float: 3.14
Enter a character: X
Enter a string: Example
You entered:
Integer: 25
Float: 3.14
Character: X
String: Example
Unformatted Input-Output Functions
In C programming, unformatted input-output functions are used to read and write data
without formatting. The most commonly used unformatted input-output functions are
getchar, putchar, gets, and puts.

Example 1: Using getchar and putchar

The getchar function reads a single character from the standard input, and the putchar
function writes a single character to the standard output.
Example 2: Using gets and puts
The gets function reads a string from the standard input, and the puts function writes a string
to the standard output. Note that gets is considered unsafe because it does not perform bounds
checking. Instead, fgets is recommended. However, for simplicity, we'll use gets here.

Example 3: Using fgets and fputs

To avoid the risks associated with gets, we can use fgets for safer string input. The fgets
function reads a string from the specified input stream, including standard input, and fputs
writes a string to the specified output stream, including standard output.

You might also like