C language content

In the C programming language, printf and scanf are standard library functions used for input and output operations. They are part of the standard I/O library, stdio.h. Here is a detailed description of these functions:

printf Function

printf‘ is used to print formatted output to the standard output (usually the screen). The function is declared in the stdio.h header file.

Syntax:

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

				
			

Parameters:

  • format‘: A constant character string (C string) that contains the text to be written to the standard output. It can include format specifiers that are replaced by the values of variables provided as additional arguments.
  • ...‘: Ellipsis indicates that the function accepts a variable number of arguments.

Format Specifiers:

Format specifiers are placeholders in the format string that are replaced by the values of corresponding arguments. Some common format specifiers are:

  • %d‘ or ‘%i‘: Integer (decimal)
  • %f‘: Floating-point number
  • %c‘: Character
  • %s‘: String
  • %u‘: Unsigned integer
  • %x‘ or ‘%X‘: Unsigned hexadecimal integer
  • %o‘: Unsigned octal integer
  • %p‘: Pointer
  • %%‘: Percent sign

Return Value:

The ‘printf‘ function returns the number of characters printed (excluding the null byte used to end output to strings).

Example:

				
					#include <stdio.h>

int main() {
    int num = 10;
    float pi = 3.14159;
    char ch = 'A';
    char str[] = "Hello, World!";
    
    printf("Integer: %d\n", num);
    printf("Float: %f\n", pi);
    printf("Character: %c\n", ch);
    printf("String: %s\n", str);
    
    return 0;
}

				
			

scanf Function

scanf‘ is used to read formatted input from the standard input (usually the keyboard). The function is declared in the stdio.h header file.

Syntax:

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

				
			

Parameters:

  • format: A constant character string that contains the format specifiers which define the expected input.
  • ...‘: Pointers to variables where the read values will be stored.

Format Specifiers:

The format specifiers in ‘scanf‘ are similar to those in ‘printf‘, but they must correspond to pointers to variables where the input values will be stored.

  • %d‘: Integer (decimal)
  • %f‘: Floating-point number
  • %c‘: Character
  • %s‘: String
  • %u‘: Unsigned integer
  • %x‘ or ‘%X‘: Unsigned hexadecimal integer
  • %o‘: Unsigned octal integer

Return Value:

The ‘scanf‘ function returns the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.

Example:

				
					#include <stdio.h>

int main() {
    int num;
    float pi;
    char ch;
    char str[100];
    
    printf("Enter an integer: ");
    scanf("%d", &num);
    
    printf("Enter a float: ");
    scanf("%f", &pi);
    
    printf("Enter a character: ");
    scanf(" %c", &ch); // Note the space before %c to consume any whitespace
    
    printf("Enter a string: ");
    scanf("%s", str); // Reads a word (until whitespace)
    
    printf("You entered:\n");
    printf("Integer: %d\n", num);
    printf("Float: %f\n", pi);
    printf("Character: %c\n", ch);
    printf("String: %s\n", str);
    
    return 0;
}

				
			

Key Points:

  • printf is used for output, and scanf is used for input.
  • Both functions use format specifiers to define the type of variable being printed or read.
  • printf returns the number of characters printed, while scanf returns the number of successfully read items.
  • Always use & before variable names in scanf to pass the address of the variable (except for strings where the name of the array itself acts as a pointer).

Understanding and using printf and scanf effectively are fundamental skills in C programming, allowing for basic input and output operations essential in many applications.

Scroll to Top