954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

print sting using printf in main or functions?

Hi,
This is satya,

how we can print a string in C that doesn't use printf in main or functions?
plz help on this

Thank u..

satyanarayanam
Newbie Poster
4 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

check kerningham and ritchie, implement your own print functions using low level apis

ithelp
Nearly a Posting Maven
Banned
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
 
Hi, This is satya, how we can print a string in C that doesn't use printf in main or functions? plz help on this Thank u..
puts(cString);

;)

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

use putc() in a loop to display each character one at a time.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Andor is correct.
the puts() function takes a String (char array) as the parameter.

putc() would output 1 character only, and not really useful or effective in this case.

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

>putc() would output 1 character only, and not really useful or effective in this case.
You must have missed the part where he said "in a loop" and "to display each character one at a time". Of course, the question is a bit ambiguous.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Yes that's understood. What my point was. that. putc() outputs one character so if you wish to output words to the console, you'll need to LOOP for each character you wish to output. thus being very ineffective.
puts() would eradicate the need for looping altogether.

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

>thus being very ineffective.
I don't see a "thus". These are two solutions and lacking some other restriction, both are equally effective.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Well are you going to design a for loop to loop through and output chars each time? or just call one function to do it. I think we all know which is the most appropriate.

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 
Well are you going to design a for loop to loop through and output chars each time? or just call one function to do it. I think we all know which is the most appropriate.



Re-read the original question -- does not say anything about which method is the most effective or requires the least coding. Actually fwrite() will work as well. I think teachers in college ask silly questions like this one just to see if the student has any imaginagion.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Optimization is implied, especially for procedural programming. No-one will write 10 lines of code of they can write one.

And i agree, my lecturers gave silly exercises out, and you werent allowed to use the standard methods that you'd normally do.
Pain in the butt if you ask me.

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 
Optimization is implied, especially for procedural programming. No-one will write 10 lines of code of they can write one. And i agree, my lecturers gave silly exercises out, and you werent allowed to use the standard methods that you'd normally do. Pain in the butt if you ask me.



I agree that nobody would write a program in the real world like that, or they shouldn't anyway. But college is not even close to being the real world. So who knows what the instructor wants:)

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

>I think we all know which is the most appropriate.
Nope, it seems to just be me. Given the extremely vague requirements, it's hard to say what's most appropriate, but it's easy to imagine an extended requirement of processing each character before printing. Discarding whitespace, for example. If I had my way, we would have pressed for more information from the OP rather than made assumptions left and right.

>No-one will write 10 lines of code of they can write one.
Which explains why bubble sort is so popular. :icon_rolleyes:

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
Hi, This is satya, how we can print a string in C that doesn't use printf in main or functions? plz help on this Thank u..

there are 2 ways...

1.you can use a pointer, and puts().

here is the example,

#include
#include

main()
{
char *msg;

msg = "Hello satya!\n";
puts(msg);
}


2. using arrays and strcpy and puts..

#include
#include

main()
{
char msg[30];

strcpy(msg, "Hello Satya!\n");
puts(msg);
}


i would suggest you use the first one,


good luck!!

iTaChi
Newbie Poster
21 posts since Mar 2007
Reputation Points: 29
Solved Threads: 2
 

>there are 2 ways...
Only two?

>main()
int main ( void ), and you need to return an integer.

Both of those solutions are unnecessarily complicated. Can you tell me what's wrong with this?

#include <stdio.h>

int main ( void )
{
  puts ( "Hello satya!" );
  return 0;
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You