tags:

views:

152

answers:

6

I have tried to implement the puts function.It in actual returns a value but i cant get what should it return.please check my code and guide me further

/* implementation of puts function */
#include<stdio.h>
#include<conio.h>
void puts(string)
{
    int i;
    for(i=0;    ;i++)
    {
        if(string[i]=='\0')
        {
            printf("\n");
            break;
        }
        printf("%c",string[i]);

    }

}
A: 

What's string supposed to be? You should define your function better, try:

void my_puts(const char *string)

instead of

void puts(string)

As noted in the link I included, you need to specify the data type of the argument you're passing (in your example string) and you cannot use the name of a function which has already been defined (i.e. puts ).

Jacob
is there even a default type for function arguments? there must be or else this wouldn't have compiled for the OP.
KevinDTimm
Apart from that, `puts` has already been defined.
Jacob
Paul Nathan
KevinDTimm
A: 

From the manual page:

#include <stdio.h>

int fputc(int c, FILE *stream);
int fputs(const char *s, FILE *stream);
int putc(int c, FILE *stream);
int putchar(int c);
int puts(const char *s);

Return Value

fputc(), putc() and putchar() return the character written as an unsigned char cast to an int or EOF on error.

puts() and fputs() return a non-negative number on success, or EOF on error.

MikeD
A: 

Well, stdio's puts() returns a non-negative number on success, or EOF on error.

mcabral
There's nothing missing; note the break statement within the if.
Marius Gedminas
@Marius, my bad. Edited my answer :)
mcabral
A: 

I don't see any point in implementing puts! anyway, you should read the specs of puts so you can make it.

this may help

int myputs(char* s)
{
   int x = printf("%s\n", s);
   return (x > 0) ? x : EOF;
}

you should include stdio.h so you can use printf and EOF.

Note that this is not EXACT implementation of puts, because on error puts sets an error indicator and do some other stuff.

More details about puts, here.

Yousf
+3  A: 

See comments in code.

int puts(const char *string)
{
    int i = 0;
   while(string[i])  //standard c idiom for looping through a null-terminated string
    {
        if( putchar(string[i]) == EOF)  //if we got the EOF value from writing the char
        { 
            return EOF;
        }
        i++;
    }
   if(putchar('\n') == EOF)  //this will occur right after we quit due to the null terminated character.
   {
       return EOF;
   }
   return 1; //to meet spec.
}

And, as an aside - I've written the equivalent of putc, puts several different times in relation to developing on embedded systems. So it's not always just a learning exercise. :)

Comment on EOF: It is a POSIX constant from stdio.h. In my Linux stdio.h, I have this definition:

/* End of file character.
   Some things throughout the library rely on this being -1.  */
#ifndef EOF
# define EOF (-1)
#endif

That definition code is GPL 2.1.

Paul Nathan
Can you tell me more about EOF? is it a constant?
fahad
@fahad: Yes sir. see: http://www.cplusplus.com/reference/clibrary/cstdio/EOF/ . On a recent Ubuntu box, EOF was #define'd in stdio.h as -1.
Paul Nathan
Your code is invalid because you do not initialize `i`
R Samuel Klatchko
i=0 should be added :)
fahad
I have only written the code off the top of my head, not compiled it. :-)
Paul Nathan
You should also return `EOF` if putting the `'\n'` character results in `EOF`.
caf
@caf: Thank you! Fixed!
Paul Nathan
You mean I can never #define a macro named EOF as -1 without violating the GPL? That kind of sucks.
Dan Olson
@Dan: I wanted to note the license of the code I copied. But, since you take that interpretation, I'd like to license `int i = 0` to myself, with a cost of $100 per use, payable to me in unmarked bills..
Paul Nathan
EOF is also in Standard C. not just posix.
Nyan
A: 

In addition to what has already been said, I am personally opposed to redefining standard library functions. However, if you absolutely must (e.g. for a homework assignment), and your compiler is complaining about conflicting types for 'puts', try putting this at the top:

#define puts _puts
#include <conio.h>
#include <stdio.h>
#undef puts
Joe Suarez