Noobie C question, quick answer

This is a discussion on Noobie C question, quick answer within the C Programming forums, part of the General Programming Boards category; Hello, I am writing a C compiler in Java and I'm getting the following errors in one of my generated ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    Noobie C question, quick answer

    Hello, I am writing a C compiler in Java and I'm getting the following errors in one of my generated C programs:


    output.c:6: warning: data definition has no type or storage class
    output.c:6: error: conflicting types for M
    output.c:1: note: previous declaration of M was here
    output.c:6: error: invalid initializer
    output.c:7: warning: data definition has no type or storage class
    output.c:7: error: conflicting types for M
    output.c:1: note: previous declaration of M was here
    output.c:7: error: invalid initializer

    and my program looks like:

    Code:
    int M[5000];
    #define base M[0]
    #define top M[1]
    #define jump_reg M[2]
    
    base = 3;
    top = 3;
     #include <stdio.h>
    int max ( int a , int b ) 
    {
    	if ( a > b ) goto AutoGenLabel1;//auto-if
    	goto AutoGenLabel0;//auto-if
    	AutoGenLabel1://auto-if
    	return a;
    	AutoGenLabel0://auto-if
    	;//auto-if
    	return b;
    }
    int main ( ) 
    {
    	int a , b;
    	scanf ( "%d" , & a );
    	scanf ( "%d" , & b );
    	M[base] = max ( a , b );//auto
    	printf ( "%d\n" , M[base] );
    }
    What exactly is going on?

    Thanks for your fast respone

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Code:
    base = 3;
    top = 3;
    You can't put these statements here. Try putting them inside main().
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    You can define constants and variables globally (i.e. outside of any function), but statements, including assignments, can only be executed inside a function. Put your assignments
    Code:
    base = 3;
    top = 3;
    inside the main function.

  4. #4
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    You can't declare variables outside of a function. You can define them, but you can't give them a value.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Of course C will let you put those statments outside of main. But you need to specify their type as int base = 3; or float top = 2; They will then become global variables with a preset first value (i.e. initializers)

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,775
    You cannot have statements outside functions. Declarations and definitions are OK.
    Btw, these gotos, are they generated by your so-called Java C compiler? They look quite nasty.
    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.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yikes... Elysia... did I actually make it into your Sig?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,775
    Yes, I couldn't resist quoting that, since I feel the same way!
    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.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Elysia View Post
    Yes, I couldn't resist quoting that, since I feel the same way!
    ROFL... I don't know... should I be flattered or embarassed????

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by CommonTater View Post
    Of course C will let you put those statments outside of main. But you need to specify their type as int base = 3; or float top = 2; They will then become global variables with a preset first value (i.e. initializers)
    That is exactly what "define a variable" means. However, in this case he can't do that, because he has already #define'd base and top in the preprocessor directives at the beginning of the program, so the assignments MUST be inside a function.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by CommonTater View Post
    ROFL... I don't know... should I be flattered or embarassed????
    A little of both perhaps, which I'm sure about cancel out making it overall neutral.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438

    .

    Quote Originally Posted by R.Stiltskin View Post
    That is exactly what "define a variable" means.
    you declare a variable, not define
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,775
    Quote Originally Posted by CommonTater View Post
    ROFL... I don't know... should I be flattered or embarassed????
    It's a compliment! Think nothing less of it.

    Quote Originally Posted by rogster001 View Post
    you declare a variable, not define
    Partly true. When you allocate space for it, you also creating it, making it into a definition.
    A definition is also a declaration.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By Toonzaka in forum C Programming
    Replies: 3
    Last Post: 02-19-2010, 10:58 AM
  2. Quick question about types...
    By Elysia in forum C++ Programming
    Replies: 32
    Last Post: 12-07-2008, 04:39 AM
  3. quick question
    By surfingbum18 in forum C Programming
    Replies: 5
    Last Post: 12-04-2007, 05:16 AM
  4. stupid question need quick answer
    By mbsupermario in forum C++ Programming
    Replies: 5
    Last Post: 10-14-2002, 06:17 AM
  5. inline fuction please answer this question
    By PLOsTYLe in forum C Programming
    Replies: 0
    Last Post: 09-17-2002, 02:38 AM

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