Closed Thread
Page 1 of 4 123 ... LastLast
Results 1 to 10 of 34

Thread: program on signed and unsigned numbers

  1. #1
    Nethra is offline Newbie
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    program on signed and unsigned numbers

    Hi,

    I would like to know the logic of finding whther the given number is signed or unsigned.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    Signed numbers are numbers which either - or + in front of them. The minus-sign is used for negative numbers, while the plus-sign is used for positive numbers. An integer variable defined signed will be able to hold both negative and positive numbers, while an integer variable defined unsigned only will be able to hold positive integers.

    Thread moved to the right forum.

  4. #3
    Join Date
    Oct 2007
    Posts
    538
    Rep Power
    21
    You don't discover if a number is signed or unsigned. You declare it to be one or the other.

    Without such a declaration or a convention it is impossible to tell between the two.

  5. #4
    Nethra is offline Newbie
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0
    I need the program which finds whether the values stored in variables is signed or unsigned.


    ie. for example

    int a = 2;
    unsigned int b = 7;

    here for 'a' it should print as signed and for 'b' it should print as unsigned.

  6. #5
    Join Date
    Oct 2007
    Posts
    538
    Rep Power
    21
    AFAIK there is no way to determine the type of variable at run time. When you compile the code there is no special marker that says 'This is unsigned'.

  7. #6
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    Like G_Morgan said, it's not possible to find out whether a variable is defined unsigned or signed, but it's possible to find out whether the values stored in the variables are unsigned or signed.
    You can use a simple macro, because macros don't care which types it gets.
    Code:
    #define IsSigned(Value) ((Value) < 0) ? (1) : (0)
    It's quite simple, it checks if the value is less than zero, and if it's, then we can say it's signed, and if it's equal or greater than zero, then it's not signed. Remember, if your signed variables value is positive, and you pass it to the macro, it will say it isn't signed. And that's because it isn't possible to check the variable itself, only the value.

  8. #7
    Nethra is offline Newbie
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    program to convert multiline comment type to single line comment type.

    Hi,

    Thanks for the quick reply and I got the point.

    I am trying a program which convert the multiline comment type in a program to single line comment type
    ie. /*have a
    nice day */

    to

    //have a
    //nice day

    but i am not able to get the end of line in the multiline comment type and so i am not able to put comment symbol in next line for single line comment type.

  9. #8
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    When you find the start of a multiline-comment (/*) you shall start out by removing it, and then put a singleline-comment (//) instead. Then you check if there's an end of a multiline-comment (*/) on all the following lines. If there's none on a line, you just put a singleline-comment in the start, and so on, until you find it. When you then find it, you put a singleline-comment in, remove the end of multine-comment, and then stop searching.

  10. #9
    Join Date
    Oct 2007
    Posts
    538
    Rep Power
    21
    To change a multi line comment into a series of single line comments I'd use a scanner. This is pretty much a higher level problem than it seems (it's quite easy but needs some understanding of fundamental CS concepts to do it right). It is the very first topic on compiler theory.

    Essentially you read a character stream one character at a time simply outputting i until you find a /. If the next character after that is a * you output // and switch state (essentially call a different function). In this new state you simply output every character again with two exceptions:
    1. a newline character is followed by //.
    2. if you find * and the following character is / then it represents an accepting state and you switch state back to the first state (i.e. return to the first function).

    You then loop again in that function output all characters until you either meet the condition to enter state 2 again or reach EOF and exit.

    something like this

    [HIGHLIGHT="C"]
    void state1() {
    while(look != EOF) {
    if(look == '/') {
    Emit('/'); //output the first slash
    getChar(); //next char in look
    if(look == '*') {
    Emit('/');
    getChar();
    state2();
    }
    }
    }
    }

    void state2() {
    int check = 1;
    while(check) {
    switch(look) {
    case('\n'):
    Emit('\n');
    getChar();
    Emit('/');
    getChar();
    Emit('/');
    getChar();
    break;
    case('*'):
    getChar();
    if(look == '/') {
    Emit('\n');
    check = 0;
    } else {
    Emit('*');
    Emit(look);
    }
    getChar();
    break;
    case(EOF):
    check = 0;
    break;
    default:
    Emit(look);
    getChar();
    }
    }
    }
    [/HIGHLIGHT]

    All you need do is implement getChar() and Emit(), initialise the stream and then call state1.
    Last edited by G_Morgan; 12-03-2007 at 04:57 AM.

  11. #10
    Nethra is offline Newbie
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    dynamic memory deallocation

    Hi,

    Thanks for the reply...

    Now i would like to know how the dynamic memory allocated by malloc/calloc/realloc dynamically get deallocated if not released explicitly using free function.


    thanks in advance..

Closed Thread
Page 1 of 4 123 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Similar Threads

  1. Replies: 10
    Last Post: 04-18-2011, 04:35 PM
  2. C program to detect prime numbers
    By sobersaber in forum C and C++
    Replies: 8
    Last Post: 11-27-2010, 12:25 PM
  3. Replies: 16
    Last Post: 08-07-2010, 07:24 PM
  4. Simple program to add odd numbers
    By nahrain in forum Visual Basic Programming
    Replies: 4
    Last Post: 04-06-2009, 01:55 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts