Why "Declaration is not allowed here"?

This is a discussion on Why "Declaration is not allowed here"? within the C Programming forums, part of the General Programming Boards category; I begin leaning C language. I run program then errror: "Declaration is not allowed here Code: #include<stdio.h> #include<conio.h> main() { ...

  1. #1
    itc
    itc is offline
    Registered User
    Join Date
    Jan 2012
    Posts
    2

    Why "Declaration is not allowed here"?

    I begin leaning C language.
    I run program then errror: "Declaration is not allowed here
    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
        {
            clrscr();
            int n;
            printf("Input n: ");
            scanf("%d",&n);
            getch();
        }
    Thanks!

  2. #2
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    107
    Quote Originally Posted by itc View Post
    I begin leaning C language.
    I run program then errror: "Declaration is not allowed here
    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
        {
            clrscr();
            int n;
            printf("Input n: ");
            scanf("%d",&n);
            getch();
        }
    Thanks!
    it is working fine whem i am compiling it.

    you just have to change somethings.
    1. don't use <conio.h>,clrscr(),getch. these are non standard.
    2. use getchar(); inplace of getch();

    give the whole error message as given by the compiler

  3. #3
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Local variables (such as 'int n' in your example) should always occur at the beginning of the block. Move your declaration of 'n' to before your call to clrscr().

    This may only be the case in older versions of the C standard which is why joybanerjee39 is not receiving an error because of it, don't remember though.

    Additionally, main should be declared as 'int main( void )' and should return 0, until you know better.

  4. #4
    itc
    itc is offline
    Registered User
    Join Date
    Jan 2012
    Posts
    2
    Thanks........................

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Yes variables must be at the start of a block except in C99
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Expected declaration or statement at end of input"
    By levitylek in forum C Programming
    Replies: 1
    Last Post: 11-16-2010, 09:02 AM
  2. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM
  3. Class Name "monster" not allowed??
    By napkin111 in forum C++ Programming
    Replies: 9
    Last Post: 01-23-2003, 08:39 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

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