Thread: Eclipse CDT: Problem in running simple C program

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    130

    Eclipse CDT: Problem in running simple C program

    Hi all,

    I just installed CDT tool for C programming under Eclipse, but when compiling the first simple program:

    Code:
    #include<stdio.h>
    
    int main(){
    
      char name[30];
      printf("Enter your name\n");  
      scanf("%s", &name);
      printf("Your name is %s", name);
      return 0;
    
    }
    The console did not show the first printf, it waits for input from the user. like this:

    console:

    Mark
    Enter your name
    Your name is Mark

    I compiled this code under cygwin with gcc compiler and it works fine. Can anybody tell me if this is becuase a wrong installation of the CDT tool?

  2. #2
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Remove the '&' operator from the second argument in scanf
    Gentoo Linux - 2.6.22.1
    GCC version 4.2.0

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    maybe need to fflush(stdout) before scanf?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Quote Originally Posted by vart View Post
    maybe need to fflush(stdout) before scanf?
    That solved the issue. But, using gcc ver. 3.4.4 it compiles fine without fflush(). Can you give any reason of why and what is the cause of this?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Can you give any reason of why and what is the cause of this?
    Buffered output is only flushed automatically at a "\n".

    If you want to guarantee that you see output for anything which doesn't end with a newline, say a prompt of some sort, then you must follow it with a flush of the output stream.

    The fact that some systems don't need it is just one of the many subtle system dependencies which keep everyone on their toes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Quote Originally Posted by Salem View Post
    > If you want to guarantee that you see output for anything which doesn't end with a newline, say a prompt of some sort, then you must follow it with a flush of the output stream.
    Why including \n in : printf("Enter your name: \n") did not flush the output?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    That just seems wrong to me - have you checked the Eclipse bug pages?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  2. problem with a simple program
    By alfredd in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2009, 03:48 PM
  3. Keep running program and 'int' problem
    By willrs2 in forum C++ Programming
    Replies: 21
    Last Post: 02-10-2008, 11:22 AM
  4. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  5. Problem with Simple C Program
    By djtomr941 in forum C Programming
    Replies: 22
    Last Post: 12-21-2004, 01:57 PM