473,789 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printf and string s

Hello all,

I'm trying to print a string on my screen... But the string comes from a
variable string... This is the code

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
string t = "tes";
printf ("%s", t );
system("PAUSE") ;
return EXIT_SUCCESS;
}
But this isn;t working.... the program is crashing when doing printf...
anyone know how to fix this ?
Sep 14 '06 #1
18 3775
"Joah Senegal" <bl****@hva.nlw rites:
>Hello all,
>I'm trying to print a string on my screen... But the string comes from a
variable string... This is the code
>#include <cstdlib>
#include <iostream>
#include <string>
>using namespace std;
>int main(int argc, char *argv[])
{
string t = "tes";
printf ("%s", t );
system("PAUSE") ;
return EXIT_SUCCESS;
}
>But this isn;t working.... the program is crashing when doing printf...
anyone know how to fix this ?
printf's expecting a C-style char-array "string". Give it what it wants
by using
printf ("%s", t.c_string());
or (better) use C++'s
cout << t;

Sep 14 '06 #2
Joah Senegal said the following on 14/09/2006 14:44:
Hello all,

I'm trying to print a string on my screen... But the string comes from a
variable string... This is the code

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
string t = "tes";
printf ("%s", t );
system("PAUSE") ;
return EXIT_SUCCESS;
}
You try tu use std::string with printf.

printf is a function of library C

now, you can do it with t.c_str(), but it's not very c++ compliant.

try this code:

#include <iostream>
#include <string>

int main( int argc, char *argv[] ) {
std::string t = "tes";
std::cout << t;
return EXIT_SUCCESS;
}
Sep 14 '06 #3
Joah Senegal wrote:
>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
string t = "tes";
printf ("%s", t );
cout << t << endl;
system("PAUSE") ;
return EXIT_SUCCESS;
}
If you *must* use printf, then pass t.c_str(), and you must include
<cstdio>. A std::string does not automagically convert to a char*. Why
did you include <iostreamif you weren't going to use it?

Sep 14 '06 #4
Stephane Wirtel wrote:
You try tu use std::string with printf.

printf is a function of library C

now, you can do it with t.c_str(), but it's not very c++ compliant.
Code that uses it is well-formed and portable, so "compliant" here must
mean something else. Care to elaborate?

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Sep 14 '06 #5
Code that uses it is well-formed and portable, so "compliant" here must
mean something else. Care to elaborate?
I don't want to use C and C++ in the same source, it's C (printf, libc)
or C++ (std::cout, std::string, STL, ...) and not both.

Stephane
Sep 14 '06 #6
Stephane Wirtel wrote:
>Code that uses it is well-formed and portable, so "compliant" here must
mean something else. Care to elaborate?

I don't want to use C and C++ in the same source, it's C (printf, libc)
or C++ (std::cout, std::string, STL, ...) and not both.
printf is C, but it's also C++. Does the fact that it's part of C
somehow pollute it? Do you also refuse to use sin, cos, etc. because
they're C?

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Sep 14 '06 #7
printf is C, but it's also C++. Does the fact that it's part of C
somehow pollute it? Do you also refuse to use sin, cos, etc. because
they're C?
You are right, sorry for my previous answer.
Sep 14 '06 #8
Pete Becker wrote:
Stephane Wirtel wrote:
>>Code that uses it is well-formed and portable, so "compliant" here must
mean something else. Care to elaborate?

I don't want to use C and C++ in the same source, it's C (printf, libc)
or C++ (std::cout, std::string, STL, ...) and not both.

printf is C, but it's also C++. Does the fact that it's part of C
somehow pollute it?
To some degree, it does: you are loosing a bit of typesafety by using the
printf() family. Otherwise, the compiler would have complained about the
OPs attempt to pass a std::string. This relates to their C heritage in that
strict typing is not that much of an issue in C (after all C does not use
type information for overload resolution and templates).

Besides, "pollute" is a loaded term and has a non-technical aspect to its
meaning. In that aesthetical sense, printf() clearly pollutes a program --
at least in my eyes :)
Do you also refuse to use sin, cos, etc. because they're C?
Nope, those are typesafe despite coming from C. (However, in my eyes these
also polute a program but for a different reason: you are calling a
function with a somewhat ill-defined contract: the standard says that
sin(x) computes the sin of x, which is a lie for almost all values of x.
What it actually computes is some unspecified approximation to the sin of
x. Thus, precision guarantess become a portability issue.)
Best

Kai-Uwe Bux
Sep 14 '06 #9
Kai-Uwe Bux wrote:
Pete Becker wrote:
>Stephane Wirtel wrote:
>>>Code that uses it is well-formed and portable, so "compliant" here must
mean something else. Care to elaborate?
I don't want to use C and C++ in the same source, it's C (printf, libc)
or C++ (std::cout, std::string, STL, ...) and not both.
printf is C, but it's also C++. Does the fact that it's part of C
somehow pollute it?

To some degree, it does: you are loosing a bit of typesafety by using the
printf() family.
That's different. There are reasons to choose C++ streams, and reasons
to choose printf. There is no reason for a blanket refusal to use C,
which is what I was replying to.
>
Besides, "pollute" is a loaded term and has a non-technical aspect to its
meaning.
Yes, that's why I used it.
In that aesthetical sense, printf() clearly pollutes a program --
at least in my eyes :)
But, again, this wasn't about the merits of printf, but of C in general.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Sep 14 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9196
by: Tony Murphy | last post by:
I've got an application that sends emails (not spam!). The application reads a template file (html/text) into a string, the string is processed and placeholders filled in as appropiate. I call a 3rd party api for sending the email, the api is written in c, so i need to convert the string into LPSTR (windows c style string?), i'm from unix world and new to windows mutant. I know that c_str() converts a string to a c string and data()...
11
5955
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <= 4 */ };
4
3053
by: ben | last post by:
why is it, in the below code, when there's a printf statement (the one commented with /* ****** */) the final for loop prints out fine, but without the commented with stars printf statement included in the code there's a bus error on the fourth element in the final for loop? final for loop print out when the /* ****** */ printf line is included in the code: ab ba
7
2556
by: sunfiresg | last post by:
During an interview, I am asked to answer a question: Printf is a major formatted output function provided by the standard C library. Printf accepts a formatting string followed by a various number of arguments to replace formatting specifiers in the formatting string. You should implement the subset of the printf function compliant to the following specification.
4
17207
by: sushant | last post by:
hi why do we use '&' operator in scanf like scanf("%d", &x); but why not in printf() like printf("%d" , x); thnx in advance sushant
12
1822
by: drM | last post by:
I have looked at the faq and queried the archives, but cannot seem to be able to get this to work. It's the usual factorial recursive function, but that is not the problem. It hangs after the user enters a number. However, as I indicate, if one adds something else after the number, the function proceeds and finishes successfully. I would appreciate some helpful hints. thanks in advance. >>>>>>>>>
7
96331
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %% should be used. Wouldn't it have been better (from design perspective) if the same escape character had been used in this case too. Forgive me for posting without verfying things with any standard compiler, i don't have the means for now.
36
35553
by: Debaser | last post by:
I've recently read in one of my old C books that puts() is a better function call with regard to performance than printf() in the following situation: puts("Some random text"); vs. printf("Some random text\n");
3
2624
by: google | last post by:
Consider the following code: char str; char str2; strcpy(str, "%alfa% %beta% d%100%d %gamma% %delta%"); printf("printf: "); printf("1%s2", str); printf("\nsprintf: "); sprintf(str2, "1%s2", str); //Interesting stuff happens here printf(str2);
43
366
by: Jrdman | last post by:
someone has an idea on how the printf function is programmed ?
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9502
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9973
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7522
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5407
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5542
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4081
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3681
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2899
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.