Why did this not work????

This is a discussion on Why did this not work???? within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<conio.h> #define a 90; void main() { char x=a; int b=10; float c=30.0f; clrscr(); printf("%c %f \n",x,c); getch(); ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    17

    Why did this not work????

    Code:
    #include<stdio.h>
    #include<conio.h>
    #define a 90;
    void main()
    {
    char x=a;
    int b=10;
    float c=30.0f;
    clrscr();
    printf("%c  %f \n",x,c);
    getch();
    }
    .....it shows declaration not allowed in line7

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    You have a loose semicolon on line 3. define is not a statement but a preprocessor directive, thus it does not need to be terminated by a semicolon.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    sorry for that....

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    Code:
    #include<stdio.h>
    #include<conio.h>
    static int p=10;
    int q=10;
    void main()
    {
    printf("%d %d",p,q);
    getch();
    }
    ......where and how will the values of p and q differ....is there any difference between the meaning of declaration and usage of p and q

  5. #5
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Is this a quiz?

    Value will not differ, but usage is p is restricted to file scope by static declaration, whereas q is available for use in other files, unless shadowed there.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    no no this is a doubt i had......

    then how is the declaration of q different if had declared it as

    extern int q;

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Read up on declaration vs definition in C.
    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.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mohnish_khiani View Post
    Code:
    #include<stdio.h>
    #include<conio.h>
    #define a 90;
    void main()
    {
    char x=a;
    int b=10;
    float c=30.0f;
    clrscr();
    printf("%c  %f \n",x,c);
    getch();
    }
    .....it shows declaration not allowed in line7
    It didn't work because #define is character substitution not a variable definition.
    Your code with this define in it would appear to the compiler as:
    Code:
    void m90in()
    {
    ch90r x=90;
    int b=10;
    flo90t c=30.0f;
    clrscr();
    printf("%c  %f \n",x,c);
    getch();
    }
    Every occurance of the letter a would be substituded with the number 90.

  9. #9
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by CommonTater View Post
    It didn't work because #define is character substitution not a variable definition.
    Your code with this define in it would appear to the compiler as:
    Code:
    void m90in()
    {
    ch90r x=90;
    int b=10;
    flo90t c=30.0f;
    clrscr();
    printf("%c  %f \n",x,c);
    getch();
    }
    Every occurance of the letter a would be substituded with the number 90.
    No. Only complete words are substituted by the preprocessor. You can see it yourself by viewing the output of the preprocessor with E flag for GCC.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's the WRONG WAY CORRIGAN rule of #defines (macro's):

    Make them clear and use them sparingly. Macro hell is not yet half-full, and it's VERY hot!

    I hate macro hell - not a tourist attraction!

    A macro for a? You're outta your RAMind.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The real reason it didn't work is because his compiler doesn't support C99 (mixed declarations). The ; at the end of his macro use counted as an actual non-declaration statement. Basically, before C99, unless your compiler had an extension that allowed it, then this was invalid:
    Code:
    int x; /* declaration */
    x++; /* non-declaration statement */
    int y; /* you can't do this now */
    And thus:
    Code:
    char x = 90; /* declaration */
    ; /* oops */
    float c; /* can't do this now */

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    2,550
    Quote Originally Posted by mohnish_khiani View Post
    Code:
    #include<stdio.h>
    #include<conio.h>
    static int p=10;
    int q=10;
    void main()
    {
    printf("%d %d",p,q);
    getch();
    }
    ......where and how will the values of p and q differ....is there any difference between the meaning of declaration and usage of p and q
    IIRC, this depends on if it is C or C++. Opps that was for const int vars.
    Tim S.
    Last edited by stahta01; 02-25-2011 at 05:00 PM.

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    sorry for that #define blunder...it's solved now....
    but i still didn't get the difference b/w the two declarations...regarding there scope,lifetime and usage
    Code:
     #include<stdio.h>
    #include<conio.h>
    static int p=10;
    int q=10;
    void main()
    {
    printf("%d %d",p,q);
    getch();
    }
    .....my doubt is related to C not C++

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So grab your C book, and look up the keyword 'static'. It should explain it. Sounds like you just want another answer without putting out any thought or effort.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    what i understood is that when a variable is declared as static it doesn't loose it's value even if the scope of the function within which it is declared is finished and a global variable is the one whose value is visible to all the functions......and also what i understand is that if i declare the variables in the way above...their meaning becomes the same......am I right???

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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