Eclipse CDT: Problem in running simple C program

This is a discussion on Eclipse CDT: Problem in running simple C program within the Tech Board forums, part of the Community Boards category; Hi all, I just installed CDT tool for C programming under Eclipse, but when compiling the first simple program: Code: ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    maybe need to fflush(stdout) before scanf?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 02:48 PM
  3. Keep running program and 'int' problem
    By willrs2 in forum C++ Programming
    Replies: 21
    Last Post: 02-10-2008, 10: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, 12:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21