Hi,
I would like to know the logic of finding whther the given number is signed or unsigned.
program on signed and unsigned numbers
Started by Nethra, Dec 01 2007 06:24 AM
33 replies to this topic
#1
Posted 01 December 2007 - 06:24 AM
|
|
|
#2
Posted 01 December 2007 - 06:40 AM
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.
Thread moved to the right forum.
#3
Posted 01 December 2007 - 10:21 AM
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.
Without such a declaration or a convention it is impossible to tell between the two.
#4
Posted 02 December 2007 - 06:41 PM
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.
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.
#5
Posted 02 December 2007 - 10:56 PM
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'.
#6
Posted 03 December 2007 - 01:04 AM
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.
You can use a simple macro, because macros don't care which types it gets.
#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.
#7
Posted 03 December 2007 - 01:49 AM
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.
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.
#8
Posted 03 December 2007 - 02:22 AM
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.
#9
Posted 03 December 2007 - 04:54 AM
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.
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.
#10
Posted 13 December 2007 - 06:19 PM
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..
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..
#11
Posted 13 December 2007 - 10:40 PM
If you don't free up the memory you allocated, you'll get memory leaks. Most new, modern operating systems does free up a normal application's allocations automatically at termination, though.
#12
Posted 13 December 2007 - 11:17 PM
They don't until the program ends.


Sign In
Create Account


Back to top









