I want my code to take two characters and print them, but this code only takes one character, prints it and terminates, can you please tell me what's wrong with it? Thank you.

#include <stdio.h>
 int main()
 {
         char a, b;
         scanf("%c", &a);
         scanf("%c", &b);

         printf("%c %c\n", a,b);

         return 0;
 }

Try this instead

fgets

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer.

#include <stdio.h>
int main()
{
	char ch[3];
	fputs("enter two characters->", stdout);
	fgets(ch, 3, stdin);
	printf("%c %c\n", ch[0], ch[1]);

	return 0;
}

I want my code to take two characters and print them, but this code only takes one character, prints it and terminates, can you please tell me what's wrong with it? Thank you.

#include <stdio.h>
 int main()
 {
         char a, b;
         scanf("%c", &a);
         scanf("%c", &b);

         printf("%c %c\n", a,b);

         return 0;
 }

When you enter the first character you are actually entering 2 characters. The character itself and ('\n') when you hit the enter key. So both the scanf's get satisfied. The solution is to use fgets as suggested above

scanf is often the source of input problems. as you can see, you have the most trivial example and it's already being problematic. since there are better alternatives readily available, you'll do well to learn to stop using it as soon as possible.

i too am partial to fgets(). it's intuitive to use and safe. the sooner you start using it, the sooner you will learn to love it.

word of caution: never, ever, use the similar-looking "gets()" function, however. "gets()" is an idiot test. anyone who ever suggests that you use it is an idiot. this especially includes programming instructors. here is why.

scanf is often the source of input problems. as you can see, you have the most trivial example and it's already being problematic. since there are better alternatives readily available, you'll do well to learn to stop using it as soon as possible.

While you are bashing scanf() and gets() , might as well quote you with one minor change:

anyone who ever suggests that you use scanf("%s",) is also an idiot. this especially includes programming instructors. here is why.

:icon_wink:

Thank you all for helping, all advices became very useful to me..

Well can you show me how can i make "fgets" hold a string whose length is not obvious?

you have to specify a length for fgets()

you can test that the last character of the string received by fgets() has a newline, indicating that it is complete. if it doesn't have a newline, that means the input exceeded the length and the remaining characters are still in the input buffer. then you will have to figure out a way to get the rest of the characters and either use them or dispose of them.

you can do something like this

#include <stdio.h>
#include <stdlib.h>

#define MAX_STR_LEN 10   // set to whatever size desired

int main(void)
{
    char inputStr[MAX_STR_LEN+1], extraStr[MAX_STR_LEN+1]; // account for NULL
    size_t len, count;

    while(1)
    {
        printf("\ninput up to %d chars: ", MAX_STR_LEN);
        fflush(stdout);   // because a newline was not printed
        fgets(inputStr,MAX_STR_LEN+1,stdin); // account for NULL
        len = strlen(inputStr);
        if (inputStr[len-1] == '\n') // found newline, string fit
        {                   // get rid of newline amd recalc len
            inputStr[len-1] = '\0';
            len = strlen(inputStr);
            printf("\nstring \"%s\" is %d characters\n",inputStr,len);
        }
        else   // string was too long, newline was not discovered in first fgets()
        {      // print the partial string 
            printf("\nstring \"%s\" used maximum %d characters\n",inputStr,len);
            printf("the remainder \"");
            count = len;
            while (len == MAX_STR_LEN)   // then continue to fgets() the remining
            {                            // string from the input buffer
                fgets(extraStr,MAX_STR_LEN+1,stdin);
                len = strlen(extraStr);
                if (extraStr[len-1] == '\n') // once newline is found,
                {                   // get rid of newline amd recalc len
                    extraStr[len-1] = '\0';
                    len = strlen(extraStr);
                }
                printf("%s",extraStr);  // print 'extra' chars 
                count += len;           // and tally total count.
            }
            printf("\" will be flushed.\ntotal string was %d characters\n",count);
        }
    }
    return 0;
}

Thank you very much :)

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.