Printf and Scanf – input and output functions in C?

Printf and Scanf in C: Enter, course of and output processed knowledge is the primary purpose of each laptop program. A program without knowledge is an ineffective program.

On this put up we are going to study some primary C features to enter and output worth of a variable.

C supplies a header file stdio.h (Commonplace Enter Output) that comprises numerous Enter/Output features. So as to carry out any I/O operation you could add an embody assertion on the high of your C code.

#embody <stdio.h>

To learn — Basic C program — Click Here

Character enter and output

C language supplies two perform for character enter/output getchar() and putchar().

Enter a personality utilizing getchar()

In C we use getchar() perform to learn a single character from the keyboard. The getchar() perform returns an integer i.e. ASCII value of enter character.

getchar() perform waits for enter until a person presses any character. It accepts any character as an enter. Therefore be cautious whereas utilizing getchar(). As non-printable characters like clean area, new traces, tab, and so forth. are additionally thought-about enter to getchar().

Output a personality utilizing putchar()

We use putchar() perform in C to show or output a personality on the console (monitor). The putchar() perform takes an integer argument (ASCII worth of character) and shows its character illustration on console.

Instance program to display character enter/output in C

/**
 * C program to display character enter output 
 * utilizing getchar() and putchar() perform.
 */

#embody <stdio.h>

int fundamental()

    char grade;
    printf("Enter scholar grade: ");

    /* Enter character utilizing getchar() and retailer in grade */
    grade = getchar();

    /* Output grade utilizing putchar() */
    putchar(grade);

    return 0;

Enter utilizing scanf() perform

scanf() is a flexible perform to learn enter from commonplace enter machine (keyboard). It could actually enter any primitive or derived sort.

Learn extra – Primitive and Derived types in C

Syntax of scanf() perform

scanf("format-specifiers", var1-memory-address, ... , varN-memory-address);

scanf() accepts a string containing the listing of format specifiers. A format specifier is a particular string that begins with % image, adopted by a personality. It directs scanf() or printf() perform to enter or output a variable worth of some sort.

For instance – %d is a format specifier that tells scanf() to learn an integer worth%f is used for float. Likewise, there are a lot of format specifiers for various varieties.

Learn extra – Constants in C

Subsequent parameter, var1-memory-address directs scanf() to retailer the enter worth to some reminiscence location (variable). Right here we offer a listing of variables to retailers enter knowledge. The variable sort must be suitable with talked about format specifiers. As well as, scanf() accepts variable reminiscence deal with not variable identity. Therefore, we should prefix variable names with & images.

We use & (deal with operator) to get precise reminiscence deal with of any variable.

Output utilizing printf() perform

We use printf() perform to print/show output on console (monitor).

Syntax of printf() perform

printf("format string containing format-specifiers", variable_list);

printf() perform accepts a format string as the first parameter. Format string comprises string or message we wish to print on the display screen. Optionally, it might comprise format specifiers in case we have to print variable worth.

printf() replaces all incidence of format specifiers with the variable worth. Optionally it typecasts a variable worth to specifier sort if wanted.

Instance program to enter/output variable utilizing scanf() and printf() perform

/**
 * C program to display enter output utilizing scanf() and print()
 */

#embody <stdio.h>

int fundamental()

    int num1, num2, sum;
    
    /* Easy message to person */
    printf("Enter two numbers: ");

    /* Enter worth in two variables */
    scanf("%dpercentd", &num1, &num2);

    /* Discover sum */
    sum = num1 + num2;

    /* Print sum */
    printf("Sum = %d", sum);

    return 0;

Allow us to observe the above program.

  • Assertion int num1, num2, sum; declares three integer variables.
  • Subsequentprintf("Enter two numbers: "); show easy message to person to enter two numbers. It’s thought-about as programming observes to show a message earlier than each enter to person. Doing this can notify the person to offer some enter.
  • In assertion scanf("%dpercentd", &num1, &num2);%d tells scanf() perform to learn an integer variable and retailer in num1. Equally, second %d additionally tells scanf() to learn integer worth and retailer in num2.
  • The assertion sum = num1 + num2 calculates sum of num1 and num2 and shops in sum.
  • Lastly, within the assertion printf("Sum = %d", sum); Sum = is printed as it’s an easy message.
    Subsequent, after printf() encounters format specifier %d. It replaces %d with the worth of sum, which lastly prints the sum of each number.

I hope, I did not puzzle you with this. Be happy to drop your queries in the feedback part if any.

Learn more at Teche Programmer — Click Here

Leave a Reply

Your email address will not be published. Required fields are marked *