Pelles C forum

C language => Beginner questions => Topic started by: colepc on January 28, 2015, 03:58:55 PM

Title: I miss printf() and other beginner Window programming frustrations
Post by: colepc on January 28, 2015, 03:58:55 PM
I knew I couldn't use printf() in a Windows program but I thought at least I could use sprintf(). But no, I was told that it could not be used and I would have to use StringCbPrintf() or StringCchPrintf() instead. They are harder to use and I have yet to make ether one of them work correctly. Guess what value for xx is displayed by popup() in the following code.
int xx;
char pop[301];
xx=2;
StringCbPrintf(pop, 300, "xx=%d",  " ", xx);
popup(pop);

You guessed wrong, xx=1073776114. This is a typical example of how frustrating it is for a beginner to work with Window programming.

The point is I need help with Window programming. Are there any good beginner and reference books available?   
Title: Re: I miss printf() and other beginner Window programming frustrations
Post by: frankie on January 28, 2015, 06:45:41 PM
Quote from: colepc on January 28, 2015, 03:58:55 PM
I knew I couldn't use printf() in a Windows program but I thought at least I could use sprintf(). But no, I was told that it could not be used and I would have to use StringCbPrintf() or StringCchPrintf() instead.
????? Why? Who say that?


Quote from: colepc on January 28, 2015, 03:58:55 PM
They are harder to use and I have yet to make ether one of them work correctly. Guess what value for xx is displayed by popup() in the following code.
int xx;
char pop[301];
xx=2;
StringCbPrintf(pop, 300, "xx=%d",  " ", xx);
popup(pop);

You guessed wrong, xx=1073776114. This is a typical example of how frustrating it is for a beginner to work with Window programming.

Try:

StringCbPrintf(pop, 300, "xx=%d", xx);


Quote from: colepc on January 28, 2015, 03:58:55 PM
The point is I need help with Window programming. Are there any good beginner and reference books available?
Sure  :)
Windows usage is quite different, and frustration for those who come from consolle printout is normal  ;)
The starting point remain the old good "Programming Windows" from Charles Petzold...
Title: Re: I miss printf() and other beginner Window programming frustrations
Post by: colepc on January 29, 2015, 02:45:50 AM
Thanks Frankie. Frustration 256 has been solved. I got a little carried away with the example I found and kept a string parameter in StringCBPrintf() that I didn't support.

I use printf() often with command line. Interestingly, it shows no error using Pelles C for Windows however I don't have a clue as to where its output goes (where is stdout for Windows?).

Pelles C for Windows says I can't use sprint(). When I use sprintf() like this:
#include <stdio.h>
int xx;
char pop[301];
xx=2;
sprintf(pop, "xx - %d", xx);

I get this error:   
error #2048: Undeclared identifier 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA'.
*** Error code: 1 ***
Title: Re: I miss printf() and other beginner Window programming frustrations
Post by: czerny on January 29, 2015, 08:02:14 AM
colepc: Please use code tags for your examples.
#include <stdio.h>

int main(int argc, char *argv[])
{
int xx;
char pop[301];
xx = 2;
sprintf(pop, "xx - %d", xx);

return 0;
}

This one works ok for me! Try to explain your problem better.
Title: Re: I miss printf() and other beginner Window programming frustrations
Post by: frankie on January 29, 2015, 12:18:01 PM
He is compiling in GUI.
Try the sample project Colepc.

P.S. I understoood Colepc you have used strsafe.h that remove the usage of 'unsafe' functions.
If you define STRSAFE_NO_DEPRECATE before the include you have again the functions.
I.e.

#define STRSAFE_NO_DEPRECATE 1
#include <strsafe.h>

You can still use sprintf, etc.
Title: Re: I miss printf() and other beginner Window programming frustrations
Post by: colepc on January 29, 2015, 08:29:04 PM
Thanks Frankie. I can now use sprint(). I'm a happy camper :)

Now that I know about strsafe.h and STRSAFE_NO_DEPRECATE it was easy to find that these are Microsoft creations and not Pelles C creations. However, doing a search for sprintf does not seem to help me find strsafe.h and STRSAFE_NO_DEPRECATE. I even tried searching for the error 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA' and my results were confusing with some results discussing strsafe.h and one result discussing STRSAFE_NO_DEPRECATE but I didn't find a discussion of the two together. Thanks again Frankie for connecting the two.