Thread: C in Visual Studio 2010, simple code fails

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    19

    C in Visual Studio 2010, simple code fails

    So, first of all, could someone give me a very short tutorial how to program C in Visual Studio 2010?

    I have this code, a direct copy from a book:


    Code:
    #include <stdio.h>
    int main(void)
    {
    	printf("\n**************"); /* Draw the top of the box */
    	for(int count = 1; count <= 8; ++count)
    	{
    		printf("\n* *"); /* Draw the sides of the box */
    	}
    	printf("\n**************\n"); /* Draw the bottom of the box */
    	return 0;
    }

    I try to build it:

    Error 1 error C2143: syntax error : missing ';' before 'type'
    Error 2 error C2143: syntax error : missing ';' before 'type'
    Error 3 error C2143: syntax error : missing ')' before 'type'
    Error 4 error C2143: syntax error : missing ';' before 'type'
    Error 5 error C2065: 'count' : undeclared identifier
    Warning 6 warning C4552: '<=' : operator has no effect; expected operator with side-effect
    Error 7 error C2059: syntax error : ')'
    Error 8 error C2065: 'count' : undeclared identifier
    Error 9 error C2146: syntax error : missing ';' before identifier 'printf'


    What do I do wrong?

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Try replacing this
    Code:
    for(int count = 1; count <= 8; ++count)
    with this
    Code:
    int count;
    for(count = 1; count <= 8; ++count)
    since the second is available only with C99 standard which might not be the case

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    Quote Originally Posted by C_ntua View Post
    Try replacing this
    Code:
    for(int count = 1; count <= 8; ++count)
    with this
    Code:
    int count;
    for(count = 1; count <= 8; ++count)
    since the second is available only with C99 standard which might not be the case
    Better, but not good enough - Now I get only four errors, which of three points to that for-loop initialization line.

    Error 1 error C2143: syntax error : missing ';' before 'type'
    Error 2 error C2065: 'count' : undeclared identifier
    Error 3 error C2065: 'count' : undeclared identifier
    Error 4 error C2065: 'count' : undeclared identifier

    So the code right now is:

    Code:
    #include <stdio.h>
    int main(void)
    {
    	printf("\n**************"); /* Draw the top of the box */
    	int count;
    	for(count = 1; count <= 8; ++count)
    	{
    		printf("\n* *"); /* Draw the sides of the box */
    	}
    	printf("\n**************\n"); /* Draw the bottom of the box */
    	return 0;
    }
    Thanks for the answer anyway!

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Instead of:
    Code:
    int main(void)
    {
    	printf("\n**************"); /* Draw the top of the box */
    	int count;
    	for(count = 1; count <= 8; ++count)
    Try...
    Code:
    int main(void)
    {
    	int count;
    	printf("\n**************"); /* Draw the top of the box */
    	for(count = 1; count <= 8; ++count)
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    Quote Originally Posted by hk_mp5kpdw View Post
    Instead of:
    Code:
    int main(void)
    {
    	printf("\n**************"); /* Draw the top of the box */
    	int count;
    	for(count = 1; count <= 8; ++count)
    Try...
    Code:
    int main(void)
    {
    	int count;
    	printf("\n**************"); /* Draw the top of the box */
    	for(count = 1; count <= 8; ++count)


    Now it works but I don't understand why. Is it about declaring all the variables in the beginning of main?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Standard C only allows variables to be declared at the beginning of a block.
    That is, after every opening brace in your functions.

    C++ and C99 allow more flexibility, including the for ( int count form.
    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.

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    Quote Originally Posted by Salem View Post
    Standard C only allows variables to be declared at the beginning of a block.
    That is, after every opening brace in your functions.

    C++ and C99 allow more flexibility, including the for ( int count form.
    Does VS2010 support C99? If so, how do I enable it?

    Also, what's the smartest way in VS2010 to break the program before exiting (I mean in a simple program usually the program is finished before I see anything)? system("pause") is said to be bad habit, getchar(); didn't work and getch(); is undefined (although it works).

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No - Microsoft has said in the past that it won't support C99.

    getchar() would work, but you might need more than one, if you've been using scanf().

    The first would consume the newline left by scanf; more accurately, you need a loop to scan for the \n.

    Then a single getchar() would wait.
    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.

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    VS2010 is a poor choice IDE for C99 because like Salem said, MS doesn't support the standard. I usually only use VS2010 for my Windows side development under .NET but I use Codeblocks for C programming.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    Quote Originally Posted by claudiu View Post
    VS2010 is a poor choice IDE for C99 because like Salem said, MS doesn't support the standard. I usually only use VS2010 for my Windows side development under .NET but I use Codeblocks for C programming.
    Ok, I thought so too.

    Thanks to all of you, I got a flying start into C programming with VS2010 now!

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The easiest way to stop the console from closing is to run without debug.
    Otherwise, you can set a breakpoint at the end of main and run with debug. Works equally well.
    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.

  12. #12
    Registered User
    Join Date
    Jul 2010
    Posts
    1

    Wink file extension

    did you use the file extension .c ofr your code? if so try using .cpp this
    should let you code how you want.

  13. #13
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I think he wants to program using C not C++. That should be pretty clear, given the forum we are on.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  14. #14
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    Quote Originally Posted by claudiu View Post
    I think he wants to program using C not C++. That should be pretty clear, given the forum we are on.
    Yeah, absolutely, just C, not C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. Templates and Macros plus more...
    By Monkeymagic in forum C++ Programming
    Replies: 8
    Last Post: 01-20-2007, 05:53 PM
  4. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM