Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-01-2007, 09:24 AM
Nethra Nethra is offline
Newbie
 
Join Date: Dec 2007
Posts: 13
Rep Power: 0
Nethra is on a distinguished road
Default program on signed and unsigned numbers

Hi,

I would like to know the logic of finding whther the given number is signed or unsigned.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 12-01-2007, 09:40 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

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.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-01-2007, 01:21 PM
G_Morgan G_Morgan is offline
Guru
 
Join Date: Oct 2007
Age: 24
Posts: 507
Last Blog:
Just over the next hil...
Rep Power: 10
G_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura about
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-02-2007, 09:41 PM
Nethra Nethra is offline
Newbie
 
Join Date: Dec 2007
Posts: 13
Rep Power: 0
Nethra is on a distinguished road
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 12-03-2007, 01:56 AM
G_Morgan G_Morgan is offline
Guru
 
Join Date: Oct 2007
Age: 24
Posts: 507
Last Blog:
Just over the next hil...
Rep Power: 10
G_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura about
Default

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'.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 12-03-2007, 04:04 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

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.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 12-03-2007, 04:49 AM
Nethra Nethra is offline
Newbie
 
Join Date: Dec 2007
Posts: 13
Rep Power: 0
Nethra is on a distinguished road
Default 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 12-03-2007, 05:22 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

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.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 12-03-2007, 07:54 AM
G_Morgan G_Morgan is offline
Guru
 
Join Date: Oct 2007
Age: 24
Posts: 507
Last Blog:
Just over the next hil...
Rep Power: 10
G_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura about
Default

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

C Code:
  1. void state1() {
  2.   while(look != EOF) {
  3.     if(look == '/') {
  4.       Emit('/'); //output the first slash
  5.       getChar(); //next char in look
  6.       if(look == '*') {
  7.         Emit('/');
  8.         getChar();
  9.         state2();
  10.       }
  11.     }
  12.   }
  13. }
  14.  
  15. void state2() {
  16.   int check = 1;
  17.   while(check) {
  18.     switch(look) {
  19.       case('\n'):
  20.         Emit('\n');
  21.         getChar();
  22.         Emit('/');
  23.         getChar();
  24.         Emit('/');
  25.         getChar();
  26.         break;
  27.       case('*'):
  28.         getChar();
  29.         if(look == '/') {
  30.           Emit('\n');
  31.           check = 0;
  32.         } else {
  33.           Emit('*');
  34.           Emit(look);
  35.         }
  36.         getChar();
  37.         break;
  38.       case(EOF):
  39.         check = 0;
  40.         break;
  41.       default:
  42.         Emit(look);
  43.         getChar();
  44.     }
  45.   }
  46. }

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 07:57 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 12-13-2007, 09:19 PM
Nethra Nethra is offline
Newbie
 
Join Date: Dec 2007
Posts: 13
Rep Power: 0
Nethra is on a distinguished road
Default 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..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -5. The time now is 11:40 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads