Questions tagged [unary-operator]
278 questions
208
votes
7 answers
++someVariable vs. someVariable++ in JavaScript
In JavaScript you can use ++ operator before (pre-increment) or after the variable name (post-increment). What, if any, are the differences between these ways of incrementing a variable?

Derek Adair
- 21,846
- 31
- 97
- 134
131
votes
4 answers
What is the purpose of a unary "+" before a call to std::numeric_limits members?
I saw this example in cppreference's documentation for std::numeric_limits
#include
#include
int main()
{
std::cout << "type\tlowest()\tmin()\t\tmax()\n\n";
std::cout << "uchar\t"
<<…

Zhang
- 3,030
- 2
- 14
- 31
95
votes
16 answers
What does the unary plus operator do?
What does the unary plus operator do? There are several definitions that I have found (here and here) but I still have no idea what it would be used for. It seems like it doesn't do anything but there has be a reason for it, right?

vrish88
- 20,047
- 8
- 38
- 56
90
votes
8 answers
What is the purpose of the unary plus (+) operator in C?
In C, it's legal to write something like:
int foo = +4;
However, as far as I can tell, the unary plus (+) in +4 is a no-op. Is it?

zneak
- 134,922
- 42
- 253
- 328
67
votes
2 answers
How to printf a memory address in C
My code is:
#include
#include
void main()
{
char string[10];
int A = -73;
unsigned int B = 31337;
strcpy(string, "sample");
// printing with different formats
printf("[A] Dec: %d, Hex: %x,…

varlotbarnacle
- 821
- 1
- 6
- 7
63
votes
5 answers
What does "int* p=+s;" do?
I saw a weird type of program here.
int main()
{
int s[]={3,6,9,12,18};
int* p=+s;
}
Above program tested on GCC and Clang compilers and working fine on both compilers.
I curious to know, What does int* p=+s; do?
Is array s decayed to…

msc
- 33,420
- 29
- 119
- 214
43
votes
4 answers
C: unary minus operator behavior with unsigned operands
I can't seem to find the relevant parts in the C standard fully defining the behavior of the unary minus operator with unsigned operands.
The 2003 C++ standard (yes, C++, bear with me for a few lines) says in 5.3.1c7: The negative of an unsigned…

Alexey Frunze
- 61,140
- 12
- 83
- 180
43
votes
7 answers
What is the purpose of Java's unary plus operator?
Java's unary plus operator appears to have come over from C, via C++.
int result = +1;
It appears to have the following effects:
Unboxes its operand, if it's a wrapper object
Promotes its operand to int, if it's not already an int or…

Syntactic
- 10,721
- 3
- 25
- 25
38
votes
3 answers
Does C correctly handle sizeof(...) and sizeof ... in this case?
In the following code, are the functions test and test2 equivalent?
typedef int rofl;
void test(void) {
rofl * rofl = malloc(sizeof(rofl)); // Is the final rofl here the TYPE?
}
void test2(void) {
rofl * rofl = malloc(sizeof *rofl); // Is…

ccoder987
- 421
- 3
- 6
31
votes
9 answers
Does the unary + operator have any practical use?
Was the unary + operator only included for symmetry with the unary - operator, or does it find some practical use in C++ code?
Searching here, I came across What is the purpose of the unary '+' operator in C?, but the only useful scenarios there…

Masked Man
- 1
- 7
- 40
- 80
27
votes
2 answers
How does different spacing affect the unary operator?
Can anyone explain me how different spacing affects the unary operator?
int i = 1;
int j = i+ + +i; // this will print j=2
int k = i++ +i; // this will print k=3
int l = i+++i; // this will print l=3
int m = i++++i; // compile time error
.

Ravi Godara
- 497
- 6
- 20
24
votes
2 answers
Why does GCC define unary operator '&&' instead of just using '&'?
As discussed in this question, GCC defines nonstandard unary operator && to take the address of a label.
Why does it define a new operator, instead of using the existing semantics of the & operator, and/or the semantics of functions (where foo and…

Jeremy
- 5,055
- 1
- 28
- 44
24
votes
5 answers
Unary operators "++" and "--" weird situation
Here's a test situation for using the unary operator "++":
var j = 0 ;
console.log(j);
j = j++;
console.log(j);
For this, the output is:
0
0
Since the ++ operator's position is at the back of the operand, so its precedence is lower than the…

Andrei Oniga
- 8,219
- 15
- 52
- 89
21
votes
5 answers
What is '-1[p]' when p points to an array (of int) index?
Today I stumbled over a C riddle that got a new surprise for me.
I didn't think that -1[p] in the example below would compile, but it did. In fact, x ends up to be -3.
int x;
int array[] = {1, 2, 3};
int *p = &array[1];
x = -1[p];
I…

Erik Stroeken
- 519
- 4
- 16
21
votes
7 answers
Explain +var and -var unary operator in javascript
I'm trying to understand unary operators in javascript, I found this guide here http://wiki.answers.com/Q/What_are_unary_operators_in_javascript most of it makes sense but what I don't understand is how the following examples would be used in an…

fakeguybrushthreepwood
- 2,983
- 7
- 37
- 53