Thread: inline declarations in Visual Studio 2010

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    8

    Smile EDITED: problem when testing my program in Eclipse Console

    Whenever I run my code under the Eclipse IDE it first requires me to enter the input, and only then shows the output (Although when I run it through cmd it works fine).
    For example: for this code:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	printf("Hello World!\nEnter a number: ");
    	int x = 0;
    	scanf("%d",&x);
    	printf("You've entered %d", x);
    	return 0;
    }
    I see an empty console every time i run it, and only after I enter a number (let's say 99), I see:
    99 //that's actually part of the standard input, and not part of the standard output
    Hello World!
    Enter a number: You've entered 99
    whether the expected result should be
    Hello World!
    Enter a number:
    and only after I enter 99, i would see:
    99 //again - this is not the issue here, but just so you won't get confused: this is supposed to be my input
    You've entered 99
    Old post was:
    Title: inline declarations in Visual Studio 2010
    Content:
    hi,
    I usually don't use visual studio for programming in c, but I've decided to try it.
    I've noticed that for some reason i can't use inline declaration in Visual studio (although as far as I know it is perfectly legal in c99). For example, when writing this simple program:
    Code:
    #include <stdio.h>
    
    int main()
    {	
    	printf("Enter an integer: ");
    	int x = 0;
    	scanf("%d", &x);
    	printf("you've entered: %d", x);
    	return 0;
    }
    I get
    error C2143: syntax error : missing ';' before type
    and
    error C2065: 'x' : undeclared identifier
    Of course, switching the first two lines in the main function solves this problem.

    Since I find Eclipse inconvenient (please excuse me dear Eclipse fans), and I'm used to using VS for programming .Net applications, I'd rather keep using it.

    How can I enable inline declaration in Visual Studio 2010?
    Thanks in advance!
    Last edited by dvirf; 03-25-2011 at 09:52 AM. Reason: I've decided to use Eclipse, since Visual Studio isn't fully compatible with c99

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The microsoft compiler is not entirely C-99 compatible... a compromise because both C and C++ code is run through the same compiler.

    That however should not be a problem for any reasonable programmer... just adapt and carry on.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    I am not allowed to adapt to it - the university demands us to work by the c99 standard
    I switched to Eclipse and I have a question about it. Whenever I run the code under the Eclipse IDE it first requires me to enter the input, and only then shows the output (Although when I run it through cmd it works fine).
    For example: for this code:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	printf("Hello World!\nEnter a number: ");
    	int x = 0;
    	scanf("%d",&x);
    	printf("You've entered %d", x);
    	return 0;
    }
    I see an empty console every time i run it, and only after I enter a number (let's say 99), I see:
    99 //that's actually part of the standard input, and not part of the standard output
    Hello World!
    Enter a number: You've entered 99
    whether the expected result should be
    Hello World!
    Enter a number:
    and only after I enter 99, i would see:
    99 //again - this is not the issue here, but just so you won't get confused: this is supposed to be my input
    You've entered 99
    Last edited by dvirf; 03-25-2011 at 09:53 AM.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    fflush(stdout). by yourself then or read the link... =)
    Not sure why eclipse is not flushing stdout.(usually line buffered).
    https://bugs.eclipse.org/bugs/show_bug.cgi?id=173732
    Last edited by Bayint Naung; 03-25-2011 at 10:11 AM.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    wow thanks!
    the
    setvbuf(stdout, NULL, _IONBF, 0);
    as the first line of the main is a great workaround for this.

    two more questions:
    1. is there a way to set focus on the console when i press F11 (or run)? because now i have to manually go the the console -> display selected console -> and choose "HelloWorld.exe"
    2. can eclipse run the program straight through cmd?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dvirf View Post
    I am not allowed to adapt to it - the university demands us to work by the c99 standard
    Well if you were on VS... then I assume windows?

    The closest to C-99 standards you're going to get is Pelles C. This is a free, standards based compiler and IDE for C-99 coding in CLI and GUI modes on 32 and 64 bit windows. It includes all resource editors, headers and libraries for windows platform development. Be sure to look through the help file, before you start coding... it's the most complete C99 reference I've ever seen. Also there are a number of additional libraries supplied, clearly marked for what is and is not C99...

    And yes your code would work on Pelles.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dvirf View Post
    wow thanks!
    the as the first line of the main is a great workaround for this.
    Turning off buffering is a completely ridiculous workaround. The solution is to flush the buffer yourself by calling fflush() when necessary.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    How to get printf to print

    I don't disagree with anything that's been said so far, but I think it's worth mentioning that when printf() results in no text being actually printed to screen but rather stored in a buffer, it's usually because there is no newline character at the end of your string. Try this:

    Code:
    printf("You've entered %d\n", x);
    Richard

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    Quote Originally Posted by CommonTater View Post
    Well if you were on VS... then I assume windows?

    The closest to C-99 standards you're going to get is Pelles C. This is a free, standards based compiler and IDE for C-99 coding in CLI and GUI modes on 32 and 64 bit windows. It includes all resource editors, headers and libraries for windows platform development. Be sure to look through the help file, before you start coding... it's the most complete C99 reference I've ever seen. Also there are a number of additional libraries supplied, clearly marked for what is and is not C99...

    And yes your code would work on Pelles.
    Thanks for the reply. I wanted to use VS on my laptop, but the university stuff compile our code in unix environment. Apparently they are using Eclipse and want our source code to compile with the following flags for the gcc:
    Code:
    -std=c99 -pedantic-errors -Wall -Werror


    Quote Originally Posted by brewbuck View Post
    Turning off buffering is a completely ridiculous workaround. The solution is to flush the buffer yourself by calling fflush() when necessary.
    hmmm.. I can't understand the implications of turning off buffering. Why is it bad?



    Quote Originally Posted by Richardcavell View Post
    I don't disagree with anything that's been said so far, but I think it's worth mentioning that when printf() results in no text being actually printed to screen but rather stored in a buffer, it's usually because there is no newline character at the end of your string. Try this:

    Code:
    printf("You've entered %d\n", x);
    Richard
    Unfortunately, it doesn't work.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by dvirf View Post
    hmmm.. I can't understand the implications of turning off buffering. Why is it bad?
    Because sending text to the screen is a slow process. So what the OS does is that it takes all text that is meant to be sent to the screen and saves it in a so-called buffer. And when it has accumulated enough text or when it decides it is a good time, it flushes the buffer. This basically means it sends the big chunk of text in the buffer to the screen all at once, thereby reducing the latency overhead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    Elysia, but if i'm using the printf function whenever i want to print, why should the text be buffered?
    sorry if i'm a bit naive
    i guess that in most of my programs (so far..) i need to send the text to the screen immediately, so i'm having a hard time "accepting" with the above solution being a bad idea.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What you use doesn't matter. The buffering is there to prevent characters being sent to the screen one-by-one.
    It's much faster to take all your characters and send them to the screen at once than to send them individually. Therefore, there is a buffer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio 2010 coming for Mac OS X
    By DavidP in forum General Discussions
    Replies: 5
    Last Post: 05-26-2011, 09:49 AM
  2. Visual C++ and Visual Studio 2010
    By 03jh01 in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2010, 04:03 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM