The difference between scanf() and getline()

Posted on 2005-04-22
Last Modified: 2008-02-26
I have been playing around with scanf() and getline() and I notice that there is a difference in strings. Take a look at the following code:

#include <stdio.h>

int main()
  int bytes_read;
  int nbytes = 100;
  char *my_string;
  char *other;

  other = (char*) malloc(100);

  puts ("Please enter a line of text.");

  my_string = (char *) malloc (nbytes + 1);
  bytes_read = getline (&my_string, &nbytes, stdin);

  if (bytes_read == -1)
      puts ("ERROR!");
      puts ("Please enter the same text again: ");
      scanf("%s", other);

      puts("\nLength of first string:");
      printf("%d", strlen(my_string));
      puts("\nLength of Second string:");
      printf("%d", strlen(other));
      if(my_string == other)
        } else {
                puts("\nNO MATCH");

  return 0;

Now, lets say I input 'string' for both prompts and here is my output:
Please enter a line of text.
Please enter the same text again:

Length of first string:
Length of Second string:

Now, my question is, first, why are the two strings not matching eventhough I input the same thing for both. And two, how do I make them match and get rid of the extra space or garbage character produced by the getline() function.

Question by:igor92128
    LVL 23

    Accepted Solution

    There are many differences:

    scanf("%s") will read chars upto, but not including whitespace.
    if your line is:
    hello world
    then scanf("%s") will return "hello"

    getline returns an entire line, including the newline at the end.
    (newline is considered whitespace by scanf).

    By your example output. I suspect you entered a single word
    (no spaces) at each prompt.  The difference in length is because
    the getline return includes the newline character and the scanf
    return does not.  These differences are clearly stated in the man
    pages for the calls.

    LVL 4

    Expert Comment


    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now