C language content

In C programming, formatted input and output functions are crucial for interacting with users and processing data. The printf and scanf functions are primarily used for these purposes.

printf Function

The ‘printf‘ function is used to print output to the standard output stream (typically the screen). It uses a format string that specifies how subsequent arguments are converted for output.

Syntax:

				
					int printf(const char *format, ...);

				
			

Parameters:

  • format‘: A C string that contains the text to be written to stdout. It can optionally contain format specifiers that are replaced by the values of subsequent arguments.

Format Specifiers: Format specifiers in the ‘format‘ string start with a ‘%‘ and are followed by a character that indicates the type of data to be printed.

SpecifierTypeExample Output
%dInteger42
%iInteger42
%uUnsigned int42
%oOctal52
%xHexadecimal2a
%XHexadecimal2A
%fFloating-point42.000000
%eScientific4.200000e+01
%EScientific4.200000E+01
%gShorter of %e or %f42
%GShorter of %E or %f42
%cCharacterA
%sStringHello, World!
%%Literal %%

Example:

				
					#include <stdio.h>

int main() {
    int age = 25;
    float height = 5.9;
    char name[] = "John";

    printf("Name: %s\n", name);
    printf("Age: %d\n", age);
    printf("Height: %.1f\n", height);

    return 0;
}

				
			

scanf Function

The ‘scanf‘ function is used to read formatted input from the standard input stream (typically the keyboard). It reads data according to the format string provided and stores the values in the provided variable locations.

Syntax:

				
					int scanf(const char *format, ...);

				
			

Parameters:

  • format‘: A C string that contains a format string to determine the types of input to be read and the format in which to read them.

Format Specifiers: Format specifiers in the ‘format‘ string start with a ‘%‘ and are followed by a character that indicates the type of data to be read.

SpecifierTypeExample Input
%dInteger42
%iInteger42
%uUnsigned int42
%oOctal52
%xHexadecimal2a
%fFloating-point42.0
%eScientific4.2e+01
%cCharacterA
%sStringHello, World!

Example:

				
					#include <stdio.h>

int main() {
    int age;
    float height;
    char name[50];

    printf("Enter your name: ");
    scanf("%s", name);

    printf("Enter your age: ");
    scanf("%d", &age);

    printf("Enter your height: ");
    scanf("%f", &height);

    printf("Name: %s\n", name);
    printf("Age: %d\n", age);
    printf("Height: %.1f\n", height);

    return 0;
}

				
			

Detailed Breakdown of Format Specifiers

Flags:

  • - : Left-justify within the given field width; Right-justify is the default.
  • + : Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers.
  • 0 : Pad with zeros instead of spaces.
  • # : Used with o, x, or X specifiers to precede the output with 0, 0x, or 0X respectively for values different from zero.

Width and Precision:

  • Width: An integer that specifies the minimum field width.
  • Precision: A dot (.) followed by an integer that specifies the number of digits after the decimal point for floating-point numbers, or the maximum number of characters to be printed for strings.

Example with Flags, Width, and Precision:

				
					#include <stdio.h>

int main() {
    int num = 123;
    float pi = 3.14159;
    char str[] = "Hello";

    printf("Integer with width 5: %5d\n", num);
    printf("Integer with width 5 and left-justify: %-5d\n", num);
    printf("Floating point with precision 2: %.2f\n", pi);
    printf("String with width 10: %10s\n", str);

    return 0;
}

				
			

By understanding these functions and format specifiers, you can effectively handle formatted input and output in C.

Scroll to Top