• Create Account

Need scary sound effects or creepy audio loops for your next horror-themed game? Check out Highscore Vol.3 - The Horror Edition in our marketplace. 50 sounds and 10 loops for only \$9.99!

### #ActualBacterius

Posted 06 February 2013 - 12:27 PM

printf prints (writes) to stdout, the standard output (which is usually a terminal window, and often hidden in Windows GUI programs). sprintf "prints" to a null-terminated char array (a C string) so you use that when you need to format something but don't want it ending up directly on the terminal - say you want to save it in some text file. It has nothing to do with the return value.

char title[255];
sprintf(title, "%s", g_engine->getAppTitle().c_str());


Translation: allocate a 255-character C string (which can then contain 254 characters + the null character), and write "%s" into it, using arguments g_engine->getAppTitle().c_str()). So this will just copy the application title (?) into "title". Not a very educational use of sprintf, but you get the idea, you can make it more complicated:

int a = 4;
int b = 7;
int c = a + b;
char foo[10];
sprintf(foo, "%d + %d = %d", a, b, c);
// foo now contains "4 + 7 = 11"


Yeah, this isn't exactly Python but that's how general-purpose string formatting works in C. Since you're using C++, you might want to prefer C++ alternatives (such as stringstreams and the << operator).

### #3Bacterius

Posted 06 February 2013 - 12:27 PM

printf prints (writes) to stdout, the standard output (which is usually a terminal window, and often hidden in Windows GUI programs). sprintf "prints" to a null-terminated char array (a C string) so you use that when you need to format something but don't want it ending up directly on the terminal - say you want to save it in some text file. It has nothing to do with the return value.

char title[255];
sprintf(title, "%s", g_engine->getAppTitle().c_str());


Traduction: allocate a 255-character C string (which can then contain 254 characters + the null character), and write "%s" into it, using arguments g_engine->getAppTitle().c_str()). So this will just copy the application title (?) into "title". Not a very educational use of sprintf, but you get the idea, you can make it more complicated:

int a = 4;
int b = 7;
int c = a + b;
char foo[10];
sprintf(foo, "%d + %d = %d", a, b, c);
// foo now contains "4 + 7 = 11"


Yeah, this isn't exactly Python but that's how general-purpose string formatting works in C. Since you're using C++, you might want to prefer C++ alternatives (such as stringstreams and the << operator).

### #2Bacterius

Posted 06 February 2013 - 12:23 PM

printf prints (writes) to stdout, the standard output (which is usually a terminal window, and often hidden in Windows GUI programs). sprintf "prints" to a null-terminated char array (a C string) so you use that when you need to format something but don't want it ending up directly on the terminal - say you want to save it in some text file.

### #1Bacterius

Posted 06 February 2013 - 12:22 PM

printf prints (writes) to stdout, the standard output (which is usually a terminal window, and often hidden in Windows GUI programs). sprintf "prints" to a null-terminated char array (a C string) so you use that when you need to format something but don't want it ending up directly on the terminal - say you want to save it in some text file).

PARTNERS