Can you find the output of this coding?

seoways

Elite Member
Jr. VIP
Joined
Dec 19, 2009
Messages
7,647
Reaction score
3,085
This code shows an important difference b/w C and JAVA:

Just find out the output of below code in C and JAVA?

Code:
int i=5;
int j=i+++++i;
 
Last edited:
i am not quite sure if the

i+++++i is correct.. generally it is either used i++ or i+i
 
Disregard, i thought it was c# lol

Post before I re-read the question.
Invalid syntax...

You may either do i++ or ++i, i've provided a sample for you to give you an idea of what the difference is.
Code:
int firstNumber = 5;
int secondNumber = 0;

Console.WriteLine("Initial values");
Console.WriteLine(">>> First number: {0}", firstNumber);
Console.WriteLine(">>> Second number: {0}", secondNumber);

secondNumber = firstNumber++;

Console.WriteLine("\nA reference of the first numbers value is passed to the second number before first number is incremented");
Console.WriteLine(">>> Compute: secondNumber = firstNumber++;");
Console.WriteLine(">>> First number: {0}", firstNumber);
Console.WriteLine(">>> Second number: {0}", secondNumber);

Console.WriteLine("\n##########\nReset values\n##########\n");

firstNumber = 5;
secondNumber = 0;
Console.WriteLine("Initial values");
Console.WriteLine(">>> First number: {0}", firstNumber);
Console.WriteLine(">>> Second number: {0}", secondNumber);

secondNumber=++firstNumber;
Console.WriteLine("\nFirst number is incremented, and then a reference of the first number is assigned to the second number");
Console.WriteLine(">>> Compute: secondNumber = ++firstNumber;");
Console.WriteLine(">>> First number: {0}", firstNumber);
Console.WriteLine(">>> Referenced number: {0}", secondNumber);
 
Last edited:
90% sure it would be a error in c .
because a variable(i) is being accessed more than once in a single assignment operation
 
90% sure it would be a error in c .
because a variable(i) is being accessed more than once in a single assignment operation
It is definitely not an error code....
We can access the value of a variable(i) at any number of time in a single line of coding.
Don't you know this simple basics!!!:thumpdown
 
It is definitely not an error code....
We can access the value of a variable(i) at any number of time in a single line of coding.
Don't you know this simple basics!!!:thumpdown

my bad ..it has been a year since i last coded in c .

the behavior is undefined , and by access more than once i meant a value can be changed (accessed) only once b/w sequence points , acc to c standards .


it will still show error , lvalue .
there should be spaces between increment op and plus sign .
 
Last edited:
The output wouldn't be anything since all you have done is assign values to variables. In C, j would be 12, however clang and gcc both refuse to compile it as written. You need to add a space between 'i++ + ++i'.
 
Well I think with these kind of questions, you don't have to bother if it is compiler proof code. It's all about understanding what the code does, in this case understanding prefix and postfix operators.

So why all the fuzz about compiler errors?
 
As Java Language Specification says, all operators are resolved in term of largest length, so in Java "+++++" is not "++ + ++", so, this code would not compile.
 
as in C++ scope i think this code will return with compile time error, because you are using variable two times in single assignment.
but i am not sure about it.

Regards,
Waseem
 
j = 12 is the answer as said by sOap

I think j=12 is the correct answer.

First step, i++ value will be assigned(5) and incremented by one (i=5,j=5)
next step ++i value will be increased(++6) and added so, (i=5+7)
 
JAVA
1) With
int j=i+++++i; DIDN'T COMPILE
12hcEBZ.png
2) With int j=i++ + ++i; COMPILED - Result = 12
Tw6EOwK.png
C++
1) With
int j=i+++++i; DIDN'T COMPILE
vHDHk2S.png
2) With int j=i++ + ++i; COMPILED - Result = 12
4JmkSO8.png
 
Yupp as s0ap pointed out, you need to add two spaces so that it reads i++ + ++i;

EDIT: PHP would output 12 as well...

Code:
<?php

$i=5;
$j = $i++ + ++$i;

echo $j ;

//outputs 12
 
Last edited:
Back
Top