C language getchar() and putchar()

3.3k views Asked by At

I'm a new C language learner and I have a problem below, I tried to print name out but it did not print. This is what I tried:

#include <stdio.h>
#include <ctype.h>

int main()
{
    char name;
    int len = 0;

    printf("Enter the user name: ");
    name = getchar();

    while (name != '\n')
    {
        len++;
        name = getchar();
    }

    printf("char = %d\n", len);
    printf("name = ");
    putchar(name);

    return (0); 
}

output:

Enter the user name: abcd
len = 4
name = 

it should print out name = abcd.

I appreciate that and thank you!

5

There are 5 answers

4
Dipesh Dulal On

Learn about the concept of array in C first.

Use scanf() and printf() like below:

#include <stdio.h>

int main(){
    char name[20];
    printf("Enter the name: ");
    scanf("%s", name);

    printf("Name: %s", name);
    return 0;
}
0
J...S On

getchar() and putchar() can only handle a single character at a time. Since you need to store a string which is basically a sequence of characters, you need to declare name as a character array.

You can't use putchar() to print a string. Try puts() or printf().

And add a \0 character to denote the end of string when you encounter the '\n' at which point you stop reading.

char name[20];
int len = 0;

printf("Enter the user name: ");
name[len] = getchar();

while (name[len] != '\n')
{
    name[++len] = getchar();
}
name[++len]='\0';

If putchar() must be used to print the string, you have the length of the string in len. Make a loop with a variable i=0 and keep iterating as long as i<len while incrementing i by 1

for(i=0; i<len; ++i)
{
    putchar(name[i]);
}


You might want to do some error checking to ensure that getchar() worked properly. It will return EOF on error.
And name should be big enough to hold the input string.

0
Kashif Faraz Shamsi On

This is how you can take input and print using getchar() and putchar().

#include <stdio.h>
int main(void)
{   
int name;
printf("Enter the user name: ");
    while ((name = getchar()) != EOF)
         putchar(name);
    return 0;
    }

I hope this helps. :)

0
Basile Starynkevitch On

You may need to learn more about programming and about data structures. I would recommend SICP, an excellent and freely available introduction to programming (which is not about C).

You need to learn more of C. Notably about arrays (but they have limitations, I leave you to find out which ones), perhaps struct, pointers, C dynamic memory allocation. That should take you a few weeks, and you need to read several books (and also some reference material like here, perhaps glance also into the specification of C11, i.e. n1570).

(ask yourself what would or should happen if the user of your program entered a name of 50 letters, or even a million ones; and what about "wrong" input, e.g. digits or punctuation? think about what a name really is inside a computer)

Since you are required to only use getchar and putchar, you need to read carefully their specification. Be aware that stdio is often buffering. Perhaps you'll need to use fflush.

Then you could read a name into some data structure (I leave you to find out which ones), and you could read one character at a time using getchar to fill that data structure. Likewise you could output that data structure one character at a time using putchar.

Of course you'll use some control flow primitives (probably some kind of looping).

We won't do your homework.

Don't forget to enable all warnings and debug info in your compiler (e.g. compile with gcc -Wall -Wextra -g if using GCC....). Read documentation about your compiler (e.g. look into the Invoking GCC chapter). Of course, learn how to use the debugger (gdb, see Debugging with GDB)

You are the one starting to learn C programming. Be persevering, it takes some time (weeks or months of work). Read a few books, look into the source code of existing software in C (e.g. free software on github).

PS. For experts on C, doing your homework is trivial; however you should learn by yourself - the purpose of the homework is to teach you something - and if you copy a solution from elsewhere you won't learn anything

1
Sushant Verma On

getchar() and putchar() handles only single character.

getchar()-

is an input function. It is used to read one character at a time from console input (generally keyboard).
Ex:

char c=getchar();

putchar()-

is an output function. It is used to display one character at a time onto console output (generally monitor). It accepts one argument of character type.
Ex:

char c;
putchar(c);

you should use gets() and puts() to work with strings or you can use printf() and scanf()