2 simple question

This is a discussion on 2 simple question within the C++ Programming forums, part of the General Programming Boards category; hi all, i was browsing archieves ....i see forum memebers are using the follwing syntax 1. unsigned x // is ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    92

    Question 2 simple question

    hi all, i was browsing archieves ....i see forum memebers are using the follwing syntax

    1. unsigned x // is it legal ?

    i have seen only unsigned int x or unsigned char c etc. but some memebers are writing simply unsigned x. no data type !!!!. still the program works. question is how the program know what data type it is ? is it a standard ?


    2. sizeof x // is it legal ?


    i know only sizeof(x) // paranthesis . sizeof is a operator. how the above syntax works ?


    3. returning inside a function.


    func(param1,param2)
    {

    if(condition) // code

    return ; // is it legal ? look ,what to be returned is not specified

    else

    return;


    }


    generally we write return some_variable or return some_number.

    but in the above syntax what will be returned ? nothing is specified at all !!


    thanks


    all the above syntax works. can anybody give some comment on this.
    blue_gene

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    1. unsigned means "unsigned int".

    2. I am unfamiliar with sizeof x without parentheses. hmmm.

    3. You can only return nothing if the function is supposed to return nothing. ie:
    Code:
    int func()
    {
    return;
    }
    Is invalid.
    Code:
    void func()
    {
    return;
    }
    Is valid.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    You can only return nothing if the function is supposed to return nothing. ie:

    ......but if you are writnig a func it is bound to return something. this is a function rule.
    blue_gene

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    You're thinking Visual Basic. No, in C++, you don't have to return a value.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >1. unsigned x // is it legal ?
    Yes. An unadorned "unsigned" is taken to mean unsigned int. Though if you need an unsigned short or long then you must say so explicitly.

    >2. sizeof x // is it legal ?
    Yes, provided that x is not a type name. If the operand to sizeof is a "thing" then you can omit the parentheses, but if the operand is a type you must use them:
    Code:
    int x;
    
    sizeof ( x ); // Legal
    sizeof x; // Also legal
    sizeof ( int ); // Legal
    sizeof int; // Not legal
    >return ; // is it legal ?
    Yes, provided that the function was declared to return void. It is equivalent to falling off the end of the function. The two following functions perform the same way:
    Code:
    void foo()
    {
      return;
    }
    
    void bar()
    {
    }
    >but if you are writnig a func it is bound to return something. this is a function rule.
    C++ makes no distinction between functions and subroutines. A C++ function performs both jobs:
    Code:
    void subroutine ( int x )
    {
      cout<< x <<endl;
    }
    
    int function ( int x )
    {
      cout<< x <<endl;
      return x;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 09:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2005, 12:41 AM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM

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