beginner C++ question (int increment)

Doesn't compile under VS 2012 RC. Theres's also a typo, i think : one 'Y' instead of 'y'

LE: Nvm, compiles with the C compiler, fails with the C++ one.
 
forgive the noob question but in the first post
int y = 5,x;

I understand int y = 5
but not int y = 5,x;
wheres the x come from and what does the comma mean ?
 
forgive the noob question but in the first post
int y = 5,x;

I understand int y = 5
but not int y = 5,x;
wheres the x come from and what does the comma mean ?

that's the same as:
Code:
int y=5;
int x;
 
"<type> ordering" means that the order of evaluation is dictated by the language specification, "strong ordering" means that even if something could be re-arranged behind the curtain, it's not re-arranged ever. "weak ordering" means that it's only necessary that the visible effects need to appear as-if ordered respective some context (=> a specific observer situation, not all and any), but operations can and will be re-arranged, and other contexts may observe no apparent ordering at all.
But that distinction only becomes important for you if you look at memory-models and multi-processing.

If you compile a debug-build the compiler often makes everything strong ordered, because it's easier and otherwise the compiler would have to re-order your source-code as well, according to what's going on on the CPU. If you try to debug a release-build, you observe that the debugger is mostly unable to provide you with a correct state-view (what's in what variables etc.) because it's all re-arranged behind the curtain.

But back to your example. I'm sure VC does apply a coherent interpretation of the meaning of the used operators (nothing to do with precedence). That is, the prefix operator has the highest priority and post-evaluation visibility, it will execute all prefix-operations in the line first. The postfix operator has the lowest priority and pre-evaluation visibility, and it will execute all postfix-operations in the line last. For the compiler the line(s) translate(s) to:

y = y + 1, y = y + 1, y = y + 1, x = y + y + y; /* prefix */ or
x = y + y + y, y = y + 1, y = y + 1, y = y + 1; /* postfix */

That is, prefix/postfix-ops are taken out of the statement and put before and after the original statement. Which has something to do with the definition regarding visibility, prefix-op changes become visible before the variable is queried for it's value, postfix-op changes become visible after the variable is queried. In your case the compiler translates this to: prefix-op changes become visible before the statement is executed, and the variable is queried for it's value, and: postfix-op changes become visible after the statement is executed, and the variable is queried for it's value.

As others said, if you can't understand how a statement operates, don't use it. If you need an hour thinking of how it may operate, and you have doubts it may work always the same, don't use it. C/C++ is not a challenge for spagetti-code or for testing your cognitive abilities to understand spagetti-code.

For that, I suggest you this: :devilish:
Code:
snip


thank you for detailed response, makes perfect sense. on the spaghetti code i very much subscribe to the Math teacher methodology :LOL:. i was just playing around, you always test your knowledge and understanding at the extremes.

by when it comes to actually writing something i dont even like doing things like:

z = ( x >y ) ? x : y ;

i would rather

Code:
if ( x > y)
{
 z = x;
}
else
{
 z = y ; 
}
it will likely change over time but right now i know exactly what the IF statement does without having to think but the Arithmetic if on the other hand i have to think about it.



Is that code snippet decompiled code or something?
 
Last edited by a moderator:
Nope, it is code written to be just about as un-obvious as possible. See if you can work out what it does :p

lol.... i'll give it a shot. but i just got to work so it will have to wait :cry: . would rather be learning and playing then designing internet gateways that have to conform to this pain in the arse http_://www.dsd.gov.au/infosec/ism/index.htm . thats the non classified edition the higher ones are burtal.
 
by when it comes to actually writing something i dont even like doing things like:

z = ( x >y ) ? x : y ;

i would rather

Code:
if ( x > y)
{
 z = x;
}
else
{
 z = y ; 
}

I think you just need to apply a rule of thumb - if you can see what a statement does at a glance (or at least without having to think about for more than a couple of seconds) then it's fine. Using that metric, I think the "z = ( x >y ) ? x : y ;" is perfectly acceptable, and it's certainly less verbose!.

Better, though, might be
Code:
#define MAX(X,Y)    ( (X)>(Y) ? (X)  : (Y)) 
....

    z = MAX(x,y);

Statements with multiple side effects, on the other hand, should be avoided. It will come back to bite you (or some other poor soul who has to maintain the code).
 
its more the operators in that instance I'm not used to, thus the need to think about it.

im about to try the ethatron C code juggernaut :oops:


edit: cant get it to compile

Code:
1>c:\projects\ethatron\ethatron\ethatron.c(14): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>c:\projects\ethatron\ethatron\ethatron.c(19): warning C4244: '+=' : conversion from 'P' to 'N', possible loss of data
1>c:\projects\ethatron\ethatron\ethatron.c(23): error C2065: 'Y' : undeclared identifier
1>c:\projects\ethatron\ethatron\ethatron.c(23): warning C4244: '=' : conversion from 'P' to 'N', possible loss of data
1>c:\projects\ethatron\ethatron\ethatron.c(23): warning C4244: '=' : conversion from 'P' to 'N', possible loss of data
1>c:\projects\ethatron\ethatron\ethatron.c(23): warning C4244: '=' : conversion from 'P' to 'N', possible loss of data
1>c:\projects\ethatron\ethatron\ethatron.c(25): warning C4244: '=' : conversion from 'P' to 'N', possible loss of data
1>c:\projects\ethatron\ethatron\ethatron.c(27): error C2065: 'Y' : undeclared identifier
1>c:\projects\ethatron\ethatron\ethatron.c(27): warning C4020: 'm' : too many actual parameters
1>c:\projects\ethatron\ethatron\ethatron.c(27): warning C4244: '=' : conversion from 'P' to 'N', possible loss of data
1>c:\projects\ethatron\ethatron\ethatron.c(28): warning C4244: 'initializing' : conversion from 'P' to 'N', possible loss of data
1>c:\projects\ethatron\ethatron\ethatron.c(28): warning C4244: '=' : conversion from 'P' to 'N', possible loss of data
1>c:\projects\ethatron\ethatron\ethatron.c(29): error C2065: 'Y' : undeclared identifier
1>c:\projects\ethatron\ethatron\ethatron.c(29): warning C4020: 'm' : too many actual parameters
 
Last edited by a moderator:
From my previous post: you're invoking the C++ compiler instead of the C one. You will also have to change that 'Y' @ ln27 to something else as that is plain wrong.
 
From my previous post: you're invoking the C++ compiler instead of the C one. You will also have to change that 'Y' @ ln27 to something else as that is plain wrong.

my quick google told me if i name the source file *.c it would then use the c compiler, i did that then got an error about incorrect headers ( c++ and not c) google and found the needed change for that. then i get those listed errors.


edit:

just tried this as well:
http://msdn.microsoft.com/en-us/library/bb384838.aspx
same issue :(


when it comes to programming im much like the old homer Simpson, ignorant not stupid :)
 
Last edited by a moderator:
Better, though, might be
Code:
#define MAX(X,Y)    ( (X)>(Y) ? (X)  : (Y)) 
....

    z = MAX(x,y);

Statements with multiple side effects, on the other hand, should be avoided. It will come back to bite you (or some other poor soul who has to maintain the code).

The problem with a macro is that if x or y are, say, function calls, they are evaluated more than once, so a templated function is a better option. (You can force inline it if you need to.)
 
I initialized Y but now get

Code:
1>c:\projects\ethatron\ethatron\ethatron.c(36): error C2857: '#include' statement specified with the /YcStdAfx.h command-line option was not found in the source file
1>

edit: fixed with
#include"stdafx.h"
now compiles
 
Better, though, might be
Code:
#define MAX(X,Y)    ( (X)>(Y) ? (X)  : (Y)) 
....
 
    z = MAX(x,y);

IMHO a macro is almost never better than anything if you have some other generic programming tool - in this case templates. Macros are pretty dubious in general, outside of uses like include guards or switching through debug/release builds, for example. But that's just me.
 
I initialized Y but now get

Code:
1>c:\projects\ethatron\ethatron\ethatron.c(36): error C2857: '#include' statement specified with the /YcStdAfx.h command-line option was not found in the source file
1>

Disable precompiled header usage, StdAfx.h is VS's "nickname" for that.
 
well it now looks like this

Code:
// ethatron.cpp : Defines the entrAy point for the console application.
//
#include"stdafx.h"
#include<stdlib.h>
#include<stdio.h>


#define F for(
#define D return
#define E typedef
#define V malloc(sizeof
#define Z(x)(t(0,(x-C)/y)-l+1e-6*(x-Q))
#define i m(p(r,o,v),e,d 
E double P;E long N;E char*w;
N G,l,I,C,B,A,W,L,S,R,O,c,k,s=80,U=13,T=169;
P sqrt(P);
N H()
{
	F;
	O=scanf(" %1[#]%*[^\n]",&O);
	);
	scanf("%ld%*c",&O);
	D O;
}
P M(N R)
{
	s=s>=k?printf("%ld%c"+3*(C<1),s-k,R&7?32:10):s;
	D R>0?B/2?H():getchar():R?.9+.7*M(R+1):0;
}

P t(N x,P K)
{
	D x?(x>U?0:t(x+2,K)*(x-1)/x/K)+1/sqrt(K):t(2,K*K/U+1)*K;
}
N _(N J,N x)
{
	F*(x?&W:&A)=W+J+x;
	(O=x=W/(J=k*k))||A<J||((O=2*W/J)&&2*A/J<3);
	A+=A-J+1)
	{
		F J*=x+O;
		S--*B&&x==O;
		O=x=!J)
		{
			s+=x+s;
			M(0);
		}
		W+=W-J;
		S+=2;
		L+=L-J*k+M(S%8==B);
	}
	D 1;
}
P*p(P*J,P*x,P*O)
{
	F R=T;R--;)O[R]=J[R]+.8*x[R];D O;
}

P

m(P*W,P*x,P s)
{
	F R=U;
	--R;
	)
	{
		P*A=W+R*U;
		W[R]+=s/12;
		*W+=s;
		*A-=*x++;
		F;
		A>W;
		)
		{
			*A/=*W;
			F O=R;
			O;
			O--)A[O]-=W[O]**A;
			A-=U;
		}
	W+=14;
	}
	D*W;
}

int main(int z,w*y)
{
int Y;
N K=s,g;Y;
M(z);
s=B=C=15-H();
s=G=M(8);
s=l=M(C);
G*=g=C%3?1:3;

if(I=s=M(8))
	{
		N b=G+9,x=b*340+U,J=b*(l+5),*n=V(N)*J),*j=n+5*b;P*h=V(P)*x),*q=h+b,*e=q+b;
		P d=K;
		F;
		J--;
		)n[J]=J<b?0:I/2;
		F;
		x;
		)h[--x]=0;
		s=M(x);
		k=256;
		C=4-C;
		B=C/4;
		s=_(_(T,0),0);
		F;--z;
		)
		
		{
			R=atoi(*++y);
			c=R>0?K=R,c:-2*R;
		}
	
		z=G/K+1;
		z+=g>z;
		I++;
		c++;
		F;
		++J<l;
		)
		{
			P u=0,C,y;
			P*H=e+U,*o=H+b*T,*r=o-2*T,*v=r-T;
			F;
			x<G;
			x++)
			{
				p(H,o-T,o);
				o+=T;
				H+=T;
				q[x]=.7*(q[x-1]+h[x]);
			}
		
			p(H,H,r);
			F;
			x;
			)
			{
				P*S=e;
				w l=" !{   ,;lf6D@";j+=1-g;F;
				*++l;
				)
				{
					*S++=*j;
					j+=*l%3*g-g+*l%5*b-3*b;
				}
				y=M(x-G);
				y=M(-J)*M(-x)+M(-J-1)*y+.01;
				y=sqrt((u+q[--x]+.1)/y);
				o-=T;C=i)+.5;K=M(-B);
				{
					N f=I,Q=0;F;
					f-Q>c;
					)
					{
						P l=0;
						N H=C+c/2+1;
						H%=c;
						H+=(f+Q+c-2*H)/2/c*c;
						l=Z(Q);
						O=(A-W)*Z(H)/Z(f);
						_(O,R=B?K>=H:L/k>O+W);
						*(R?&Q:&f)=H;
					}
				
					Q+=f;
					*S=*j=Q/=2;
					s=B?s:Q+k;
					f=Q+n[x/z];
					n[x/z]=x%z+J%(z*2/g)?f:!putc(x?l[4*g*f/z/z/I-8]:10,stderr);
					H-=U;F O=156;O--;
					)
						{
						H--;
						*H=e[O%U]*e[O/U]/y+.8**H;
						}

						C-=Q+.5;y=i/.9)-Q-C;p(p(r+T,r,r),H,r);
						h[x]=.7*h[x]+C*C;
						u+=h[x];
						u*=.7;
						d+=C>0?-y:y;

					}
				}
			j+=9;
			}
		_(_(x,x),x);
		}
	D 0;
}

still zero idea what is going on ......lol
 
The problem with a macro is that if x or y are, say, function calls, they are evaluated more than once, so a templated function is a better option. (You can force inline it if you need to.)
/me looks in his "ANSI C. A Lexical Guide".... no mention of template in the index. :p
 
As for the OP - undefined behavior means exactly that: undefined behavior. Both x and y can end up with any value compiler thinks is correct (or randomizes by looking at the weather in Kuala Lumpur). On top of that compiler can do whatever it wants extra, e.g. execute printf("This code sucks");. Since this is undefined behavior it would have been perfectly valid to do it and compilers have done this in the past - GCC 1.17 would launch NetHack or Rogue upon discovering undefined behavior in the code. Trying to reason "this should be X" or "why do I get Y" is absolutely pointless.
 
had a go at the start of grey-scale image compressor ( did you write this code Ethatron?). Pretty hard with kids about and the logic of some of the macro's is beyond me

Code:
// replace F with for(
#define F for(
// replace D with return
#define D return
//replace E with typedef
#define E typedef

// replace V with malloc(sizeof
// malloc is used to request/allocate a block of memory sized in bytes, block isn't initalized
#define V malloc(sizeof
// a macro that i cant figure out how to read it
#define Z(x)(t(0,(x-C)/y)-l+1e-6*(x-Q))
// another marco of stuff..lol
#define i m(p(r,o,v),e,d
//use typedef to create aliases fp double = P, long in = N , 1byte char = *w
E double P;
E long N;
E char*w;
//all of these are long ints
N G,l,I,C,B,A,W,L,S,R,O,c,k,s=80,U=13,T=169;
// returns square root, cant see the point in it yet still builds when i comment it out :)
P sqrt(P);

//fuction called H
N H()
{
	// FOR (
	F;
	// takes input data dont understand the paramaters 
	// & is bitwise AND , seems to care about the data last 
	// time fuction was ran, output is O and O is used within the function
	O=scanf(" %1[#]%*[^\n]",&O);
	
	);

	scanf("%ld%*c",&O);
	//returns O
	D O;
 
had a go at the start of grey-scale image compressor ( did you write this code Ethatron?). Pretty hard with kids about and the logic of some of the macro's is beyond me
If you run it through the preprocessor it will replace all the #defines for you.
 
Back
Top