9 Replies - 455 Views - Last Post: 08 July 2013 - 11:53 AM Rate Topic: -----

#1 amit100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-July 13

String

Posted 07 July 2013 - 01:43 PM

How to print a string in C without using printf() and puts() functions.
Is This A Good Question/Topic? 0
  • +

Replies To: String

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,009
  • Joined: 05-May 12

Re: String

Posted 07 July 2013 - 01:49 PM

Simple, use fprintf() and fputs(). :)

I think you need to be more specific with your question and tell us what you are trying to achieve. Is this as school assignment or a test question?
Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 769
  • View blog
  • Posts: 2,247
  • Joined: 20-March 10

Re: String

Posted 07 July 2013 - 02:25 PM

Also,

there is putchar
putc
fwrite

But as Skydiver has said you need to be more specfic.

Best Wishes

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: String

Posted 07 July 2013 - 06:14 PM

How far down are you trying to go here? printf and puts implementation vary between platforms, but printf usually involves three steps

1) parse arguments (do this using vsprintf and va args - thats what the linux implementation does).
2) fill the printf buffer (sometimes malloc is called if the memory size isn't sufficient)
3) iterate over the buffer while calling putchar

i.e. puts could be something as simple as this

int puts(char *str) {
   for(; *str; ++str) {
      if(putchar(*str) == EOF)
         return EOF;
   }
   return 0; /* non negative => success */
}


This post has been edited by jjl: 07 July 2013 - 06:20 PM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,009
  • Joined: 05-May 12

Re: String

Posted 07 July 2013 - 08:09 PM

And there is also the question of where are you trying to print to? Are you trying to print to the console, an old fashioned line printer, an 80's dot matrix printer, a modern laser printer, a virtual publishing file like a .PDF, an OpenGL texture, etc.?

This post has been edited by Skydiver: 07 July 2013 - 08:10 PM

Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: String

Posted 07 July 2013 - 09:58 PM

Well if he is implementing printf and puts, then output should be going to standard output stream.
Was This Post Helpful? 0
  • +
  • -

#7 amit100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-July 13

Re: String

Posted 08 July 2013 - 01:32 AM

Actually i want to print any msg for eg: "I love India" in the console without using any library function like putchar,puts,printf etc.Can i do this in any way but using the Output Stream.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,009
  • Joined: 05-May 12

Re: String

Posted 08 July 2013 - 05:31 AM

Do you count operating system functions as library functions? If not, look at closely at your OS's documentation for how to write to the console directly.
Was This Post Helpful? 0
  • +
  • -

#9 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 769
  • View blog
  • Posts: 2,247
  • Joined: 20-March 10

Re: String

Posted 08 July 2013 - 09:59 AM

You could do...

#include <stdio.h>
size_t length(char* sentence)
{
    size_t counter =0;
    do{
        counter++;

      }while(sentence[counter]!='\0');

      return (counter);
}

void print(char* sentence)
{
    fwrite(sentence, sizeof(char),length(sentence),stdout);

}

int main()
{
	
	print("Hello World !\n");
	
    return 0;
}




But I am guessing you will say I dont want to use stdio.h but I want to use the standard output stream.

You could always ECHO it as I think Skydiver is alluding to but its just too horrible to do that so I am not saying....

Regards

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: String

Posted 08 July 2013 - 11:53 AM

If you are not going to use the standard library, then your implementation will be platform depenedent (unless you write cases for each platform).

In example, here is a linux implementation, which builds off snoopy's example
#include <unistd.h> /* for write */

#define STDOUT_FD 2

size_t length(char* sentence) {
    size_t counter =0;

    do{
        counter++;
      }while(sentence[counter]!='\0');
      return (counter);
}

void print(char* sentence) {
    write(STDOUT_FD, sentence, length(sentence));
}

int main() {
    print("Hello World !\n");
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1