Exploring Standard Input and Output in C

Now that we have written our first program, I am sure like Oliver Twist, you want more. Well, fear not, for today we unlock the magic of user interaction!

The standard input /output header <stdio.h> contains some functions we can call depending on whether we want to feed input into our program or display or print something on the screen.

In our earlier lesson, we ran our first program and called the printf() function. Without telling the linker to include (link) the library that contains the standard input (stdin) and the standard output (stdout) header, we would not have been able to use printf().

Let's try that and see what happens.

int main (void) 
{
    printf("Hey Agnes! Let's learn together");
    return (0);
}

What happens when you run this code?

By including the <stdio.h> header in our code, we open the gates to accepting input from the user via the keyboard and displaying captivating output on the screen. Get ready to embark on an exciting journey as we explore the functions that bring our programs to life!

As we stated earlier, we can call a function from <stdio.h> depending on whether we want to accept user input or print a message.

To accept input, we can use:

getc(stdin), getchar(), scanf(), gets()

To display output, we can use:

puts(), putchar(), printf()

The 'f' in printf() and scanf() represents 'formatted'.
While printf() and scan(f) are formatted input and output, getc(stdin), gets(), getchar(), puts() are unformatted inputs and output respectively.

Printing To The Screen

As we said earlier, printf(), putchar() and puts() are output functions. Whereas printf() is formatted, putchar() and puts() are unformatted. When we use putchar() we directly print a single character; puts() is used to print a string of characters. The printf() function however, can print anything (character / string) we want so long as we format it correctly. We will explain further in subsequent lessons.

Let's take a closer look at the output functions we have mentioned:

printf()The Enchanter:

Cast a spell with printf()! This powerful function lets you conjure formatted output. Mix in some placeholders and format specifiers to create captivating messages with data that will leave your users spellbound! Type and run the example below:

#include <stdio.h>

int main(void) 
{
    printf("Hey Agnes! Let's learn together\n");
    return (0);
}

puts() The Showman:

Step right up and witness the magic of puts()! This function dazzles your audience with unformatted strings. It adds a touch of elegance by automatically including a newline character, leaving your users in awe! See an example below.

#include <stdio.h>
int main(void) 
{ 
    puts("Hey Agnes! Let's learn together");
    return (0);
 }

Do you notice the difference between our printf() statement and our puts() statement?

In printf(), we added \n; but we did not do that in puts(). When we run both codes, we still get the same results. Let's remember that printf() is formatted. That is why we have to tell it to add a new line in this case with a backslash n \n.

Engaging with User Input

Prepare to engage in interactive adventures by accepting input from your users using these captivating functions:

scanf()The Mind Reader

Enter the world of mind reading with scanf()! This function allows you to decipher input based on specified format specifiers. Prepare to unravel the mysteries hidden within the user's input!

#include <stdio.h>

int main(void) 
{
    char let;
    printf("What's the first letter of your name?\n");
    scanf("%c", &let);
    printf("You entered: %c\n", let);
    return (0);
}

Now, I know this is a lot to take in. We started with two lines in our main function and now we are already writing five lines! For now, please concentrate on the scanf() function in the statement. You already know about printf(), though the second printf() in our code is not as straightforward as the one we already know. I will explain %c as seen in our code above and the statement char let; in our next lesson where we take a look at variables and format specifiers.

getc(stdin) and getchar():The Character Whisperers

Become a character whisperer with getc(stdin) and getchar()! These functions let you extract single characters from the user's input via the standard input (stdin). Immerse yourself in the enchantment of unformatted input!

#include <stdio.h>

int main(void) 
{
    char let;
    printf("What's the first letter of your name?\n");
    let = getc(stdin);
    printf("You entered: %c\n", let);
    return (0);
}

Note: In these code examples, we guide users to provide input and display their entered values.

On your own, try your hands on printing to the screen with the putchar() function (which can only print a single character); then try accepting input with the gets() function. That wasn't so hard, was it? Did you find it challenging? Share your experience and feedback with me.

Congratulations on harnessing the power of standard input and output functions in C programming! By mastering the art of printf(), putchar(), puts(), scanf(), getc(stdin), gets() and getchar(), you can create programs that truly engage and captivate your audience. So go forth, weave your code spells, and create interactive experiences that leave users eagerly awaiting their next encounter with your enchanting programs!

Thank you for spending time with me today. Don't forget to say hi to me on Twitter. Your feedback will be very much appreciated too. Until our next lesson, happy coding!