Thread: Omitted the variable type

  1. #1
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459

    Omitted the variable type

    Hello,

    I've seen many people omit the types when declaring variables, for example

    Code:
    unsigned x;
    and in extreme cases,

    Code:
    x;
    I've looked around in the C89 draft (briefly), but I couldn't answer my question which is, What type will x be?

    Thanks in advance
    Last edited by zacs7; 08-16-2007 at 05:31 AM. Reason: Spellingz

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I don't know the standard's definition, but in practice...

    Code:
    unsigned x;
    is equivalent to
    Code:
    unsigned int x;
    Code:
    x;
    does nothing. It is a valid expression, but one with no side effects. It's like
    Code:
    2 + 5;
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I believe, however that you could use:

    Code:
    foo(x) {
       return x + 9;
    }
    And whilst a modern compiler with warnings enabled would throw out half a dozen warnings, it would compile and work exactly like:
    Code:
    int foo(int x) {
       return x + 9;
    }
    or
    Code:
    int foo(x) 
    int x;
    {
       return x + 9;
    }
    --
    Mats

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by zacs7 View Post
    I've looked around in the C89 draft (briefly), but I couldn't answer my question which is, What type will x be?
    int.

    But this is not going to be allowable in C99.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Thanks all

  6. #6
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by zacs7 View Post
    Hello,

    I've seen many people omit the types when declaring variables, for example

    Code:
    unsigned x;
    and in extreme cases,

    Code:
    x;
    I've looked around in the C89 draft (briefly), but I couldn't answer my question which is, What type will x be?

    Thanks in advance
    unsigned (int).

  7. #7
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Code:
    x;
    Syntax error. x undeclared. "Extreme," indeed.
    Last edited by MacNilly; 08-16-2007 at 11:20 PM.

  8. #8
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466

    wrong

    Quote Originally Posted by brewbuck View Post
    int.

    But this is not going to be allowable in C99.
    Wrong man. Compiles just fine using C99 standards.

    Code:
    int main() {
      unsigned x;
    }
    Code:
    gcc -std=c99 test.c

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MacNilly View Post
    Wrong man. Compiles just fine using C99 standards.
    I meant no type specifier at all, will not be allowed. As in:

    Code:
    x;
    You crack me up.

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Is there any reason people leave the type off? I mean it just causes obstrufication... For me anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. Replies: 6
    Last Post: 07-29-2008, 04:37 AM
  4. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 09:51 AM
  5. "Deciding" in runtime the type of a variable
    By mikahell in forum C++ Programming
    Replies: 28
    Last Post: 07-22-2006, 09:51 AM