Im doing "Sams Teach Yourself C in 24 Hours".
Which can be found here.
I'm at hour 6 right now.

I most likely will run into several problems while trying to solve his excercises.
I don't want to make a new thread everytime I run into a problem.
So I thought since all the small programs (excersises) comes from his tutorial they could all be puttin into one thread.
If I get another problem later I'd reply to this thread with the new problem.
Anyone else who have problems from his tutorial can post in this thread too.

I have 2 problems right now.

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int x,y;
	x=1;
	y=3;
	printf("1+=3 is: 4 ?: %d.\n1+=-3 is: 0? ?: %d.\n1-=3 is: -2 ?: %d.\n1-=-3 is: -4 ?: %d.\n1*=3 is: 3 ?: %d.\n1*=-3 is: -3? ?: %d.\n",x+=y,x+=-y,x-=-y,x*=y,x*=-y);
	system("PAUSE");
}

The "is: X" is what I guessed the expression would become.
I really doubt this is working correct because if you run this program everything equals '-6' except the last line which equals 41 million.
It just can't be right.
OR if it actually would be correct it would just be nice to have that confirmed because it's weird how all expressions could end up with those results.

second problem is with VC++ express.
When I click on debug I get a message box with the title "No Debugging Information" and the message "Debugging information for 'hour 6.exe' cannot be found or does not match. Binary was not built with debug information.
Do you want to continue debugging?"

I don't know what that means exacly and don't know how to fix that.

Recommended Answers

All 5 Replies

>>I really doubt this is working correct because if you run this program everything equals '-6' except the last line which equals 41 million.

its because the printf() statement does not have enough parameters. Count the number of %d's in the format string then count the number of actual parameters -- they are not the same to printf() is picking up garbage as the last parameter.

And the reason printf() displays the same value for all the parameters is because the program is using undefined behavior, meaning there is no guarentee when the compiler will save the result of each of those computations. It would appear that only the final result of x is getting passed to printf() for all the parameters.

Second problem: you have your compiler set for Release builds and you need to change it to Debug guilds. Select menu item Build --> Configuration Manager. set "Active Ssolution configuration" to "Debug" then rebuild the program.

>>Anyone else who have problems from his tutorial can post in this thread too.

Not a good idea because it will get too confusing. Everyone should start their own threads to ask questions

1. The reason why you're getting some junk for last line is because you have given 6 %d(s) inside the format string and supplied only 5 values.

2. Execute the following code and you should see some difference. May be that should point you in right direction.
Here are the links that explain why this happens.
Order of evaluation.
Precedence and Order of evaluation.

#include <stdio.h>
#include <stdlib.h>

int main()
{
       int x,y;
       //1
    x=1; y=3; printf( "\n\nx=%d, y=%d", x, y ) ;
    printf("\n1+=3 is: 4 ?: %d.",x+=y);

       //2
    x=1; y=3; printf( "\n\nx=%d, y=%d", x, y ) ;
    printf("\n1+=3 is: 4 ?: %d.\n1+=-3 is: 0 ?: %d.",x+=y,/*guess what's the value of x and y at this point?*/x+=-y);

       //3
    x=1; y=3; printf( "\n\nx=%d, y=%d", x, y ) ;
    printf("\n1+=3 is: 4 ?: %d.\n1+=-3 is: 0 ?: %d.\n1-=3 is: -2 ?: %d.",x+=y,x+=-y,x-=y);

       //4
    x=1; y=3; printf( "\n\nx=%d, y=%d", x, y ) ;
    printf("\n1+=3 is: 4 ?: %d.\n1+=-3 is: 0 ?: %d.\n1-=3 is: -2 ?: %d.\n1-=-3 is: -4 ?: %d.",x+=y,x+=-y,x-=y,x-=-y);

    //you can do the rest..

    return 0 ;

}

Those links were for C++ here is one link for C.

PS: Like AD said the behavior is undefined or to be more precise defined differently for different compilers, [which in turn could be undefined :) ]
My compiler produces this output for the code you posted in post #1:
1+=3 is: 4 ?: -6.
1+=-3 is: 0? ?: -9.
1-=3 is: -2 ?: -6.
1-=-3 is: -4 ?: -9.
1*=3 is: 3 ?: -3.
1*=-3 is: -3? ?: 4006960.
Press any key to continue . . .

>>PS: Like AD said the behavior is undefined or to be more precise defined differently for different compilers

ansii standards never ever, not even once, defined something for specific compilers.

>>PS: Like AD said the behavior is undefined or to be more precise defined differently for different compilers

ansii standards never ever, not even once, defined something for specific compilers.

Accepted. Rephrased: to be more precise defined differently, by compilers, for different compilers.
The point I was trying to make was that for some compilers you might have a defined behavior.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.