Mistakes to Avoid

In this tutorial, we will look at the mistakes that beginners often make when writing C programs that use the scanf/printf functions for input and output. We will also learn how to fix these mistakes.


Mistake #1 – Using ‘&’ in printf

One common mistake made by beginners is mistakenly adding the ‘&’ symbol before variables when passing them as arguments to the printf function. Here’s an example of what it looks like:

printf(“%d %d”, &p, &q);

Although the compiler won’t show any errors for this, you’ll get unexpected results if you include printf functions like this in your program.

Let’s run the following program

#include <stdio.h>

int main()
{
    int p = 10, q = 20;
    printf("%d %d\n", &p, &q);
    return 0;
}

Output:

6422300 6422296

Process returned 0 (0x0)   execution time : 2.708 s
Press any key to continue.

When I ran the program, the output I got was not what I expected. Instead of printing the values of p and q, it displayed some numbers on the console. These numbers are actually memory addresses of the variables p and q.

In C, when you create a variable, you are essentially allocating memory for that variable. So, the names p and q represent specific memory locations with addresses 6422300 (decimal) and 6422296 (decimal) respectively. The values stored in these memory locations are 10 and 20 respectively.

In the printf function, by using the ‘&’ operator, we are trying to access the memory addresses of the variables p and q, not their values. That’s why we are getting this strange output.

To print the values of p and q, we need to remove the ‘&’ symbol in front of these variables in the printf function call. The corrected program should look like this:

#include <stdio.h>

int main()
{
    int p = 10, q = 20;
    printf("%d %d\n", p, q);
    return 0;
}

Output:

10 20

Process returned 0 (0x0)   execution time : 0.028 s
Press any key to continue.

Mistake #2 – Including ordinary characters in scanf

It is allowed to include regular characters in the scanf format string, apart from the format specifiers. However, it can lead to confusion for the user if the expected input format is not clear. In such cases, using tightly packed format specifiers in the format string is more sensible.

For instance, consider the following scanf call that expects the user to enter input in the format "%d, %d":

scanf("%d, %d", &p, &q);

This scanf call requires an integer followed by a comma, optional whitespace characters, and then another integer. If the format is unclear to the user, they may enter the input like this:

10 20

Here, 10 and 20 are separated by a space, which doesn’t match the format string of the scanf function. As a result, scanf will only read the first integer. When it encounters that the whitespace character in the input doesn’t match the comma in the format string, it puts the whitespace back into the buffer and stops. Therefore, only the variable p will receive the value 10, while q will be assigned a garbage value (random value).

To avoid this issue, if you only want to accept values from the user, it is best to either tightly pack the format specifiers or, for visual clarity, separate them with a single space. Here are two examples:

scanf("%d%d", &p, &q);

OR

scanf("%d %d", &p, &q);

Mistake #3 – Including a newline character at the end in scanf

What happens when you include a newline character (\n) at the end of the format string in the scanf function? The simple answer is that “your program will never terminate until you enter a non-whitespace character.”

Let’s understand why this happens with the help of the following program:

#include <stdio.h>

int main()
{
    int p, q;
    scanf("%d%d\n", &p, &q);
    printf("Values of p and q are %d %d\n", p, q);
    return 0;
}

In this program, the variables p and q are declared without initial values. These values will be provided through a scanf function call, but the format string intentionally includes a newline character (\n) at the end.

When you run the program and enter the integers, followed by hitting enter, you will notice that nothing is displayed. We expected to see the values of p and q. If you continue to press enter, the program will never terminate. This happens because of the typical behavior of the scanf function when it encounters a whitespace character in the format string.

As we learned in the previous tutorial, scanf reads whitespace characters in the input until it encounters a non-whitespace character. By pressing enter, you are providing a sequence of whitespace characters (including the newline character) to scanf. However, scanf will only terminate when it encounters a non-whitespace character. Therefore, to terminate the program, you need to enter a non-whitespace character.
Here’s what happens when I provide a non-whitespace character (3) in the input after entering the values 10 and 40:

10 40
3
values of p and q are 10 40

Process returned 0 (0x0)   execution time : 5.477 s
Press any key to continue.

Since there is no format specifier after \n in the format string, scanf will put back the value 3 into the buffer, which can be read by the next scanf call (if any).

My advice is to never use “\n” at the end of the format string in the scanf function call.



Leave a comment

Leave a comment

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

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.