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..

Recommended Answers

All 14 Replies

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

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);

;)

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

commented: =') +3

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.

>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.

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.

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

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.

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.

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.

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:)

>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:

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 <stdio.h>
#include <string.h>

main()
{
char *msg;

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


2. using arrays and strcpy and puts..

#include <stdio.h>
#include <string.h>

main()
{
char msg[30];

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


i would suggest you use the first one,


good luck!!

>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;
}
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.