Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use printf() and scanf() in C without going to the next line?

I want to get date of birth in one line:

#include <stdio.h>

int main()
{
    int BirthYear,BirthMonth,BirthDay;
    printf("Please enter your birth date: ");
    scanf("%d",&BirthYear);
    printf("/");
    scanf("%d",&BirthMonth);
    printf("/");
    scanf("%d",&BirthDay);
    return 0;
}

This is my output:

Please enter your birth date: YYYY
/MM
/DD

But I want to get something like this:

Please enter your birth date: YYYY/MM/DD

In output, it goes to next line after each scanf() without using \n. I use VS Code for IDM.

like image 246
Parsa Saberi Avatar asked Oct 27 '20 13:10

Parsa Saberi


People also ask

How do I stop scanf from going to the next line?

For a simple solution, you could add a space before the format specifier when you use scanf(), for example: scanf(" %c", &ch); The leading space tells scanf() to skip any whitespace characters (including newline) before reading the next character, resulting in the same behavior as with the other format specifiers.

How do I skip enter in scanf?

%*c skips a character from the input.

Can I use scanf without printf?

Use of & in scanf() but not in printf() As a and b above are two variable and each has their own address assigned but instead of a and b, we send the address of a and b respectively. The reason is, scanf() needs to modify values of a and b and but they are local to scanf().

Does printf automatically print a newline?

The printf statement does not automatically append a newline to its output. It outputs only what the format string specifies. So if a newline is needed, you must include one in the format string.

How to use printf and scanf in C language?

C – printf and scanf 1 1. printf () function in C language: In C programming language, printf () function is used to print the (“character, string, float, integer, octal and hexadecimal values”) onto the output ... 2 Example program for C printf () function: String is fresh2refresh.com 3 2. ...

Why all characters in printf () and scanf () functions must be in lower case?

All characters in printf () and scanf () functions must be in lower case. got replaced by a newline. In C programming language, scanf () function is used to read character, string, numeric data from keyboard Consider below example program where user enters a character. This value is assigned to the variable “ch” and then displayed.

How to print on new line on the screen in C?

To print on a new line on the screen, we use “ ” in printf () statement. C language is case sensitive programming language. For example, printf () and scanf () in lowercase letters treated are different from Printf () and Scanf ().

How to generate a newline using printf () statement in C?

Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for double and %x for hexadecimal variable. To generate a newline,we use “ ” in C printf () statement. C language is case sensitive. For example, printf () and scanf () are different from Printf () and Scanf ().


2 Answers

Here is a workaround using ansi control characters. I would not do like this, but just to show that it is possible:

#define PREVLINE "\033[F"
#define MSG "Please enter your birth date: "

int main(void) {
    int BirthYear,BirthMonth,BirthDay;
    
    printf(MSG);
    scanf("%d",&BirthYear);
    printf(PREVLINE MSG "%d/", BirthYear);
    scanf("%d",&BirthMonth);
    printf(PREVLINE MSG "%d/%d/", BirthYear, BirthMonth);
    scanf("%d",&BirthDay);
    printf("You entered: %d/%d/%d\n", BirthYear, BirthMonth, BirthDay);
}

Please note that this is not portable. The terminal needs to support this in order to work. AFAIK there's no 100% portable way to achieve this.

If you want to do this stuff for real, then I recommend taking a look at the ncurses library

Note:

Always check the return value for scanf to detect errors.

Note2:

It may be a good idea to add fflush(stdout); after each printf statement.

I actually wrote another answer today about ascii control characters. It might be interesting: https://stackoverflow.com/a/64549313/6699433

like image 60
klutt Avatar answered Nov 07 '22 02:11

klutt


You can explicitly specify that the three input numbers should be separated by a '/' character by adding that character in the format specifier for the scanf function.

Then, you can ensure that the user gave valid input by checking the value returned by scanf (which will be the number of items successfully scanned and assigned); if that value is not 3, then you will (probably) need to clear any 'leftover' characters in the input buffer, using a getchar() loop until a newline (or end-of-file) is found:

#include <stdio.h>

int main()
{
    int BirthYear, BirthMonth, BirthDay;
    int nIns = 0, ch;
    while (nIns != 3) {
        printf("Enter D.O.B. (as YYYY/MM/DD): ");
        nIns = scanf("%d/%d/%d", &BirthYear, &BirthMonth, &BirthDay);
        while ((ch = getchar() != '\n') && (ch != EOF))
            ; // Clear remaining in-buffer on error
    }
    printf("Entered data were: %d %d %d!\n", BirthYear, BirthMonth, BirthDay);
    return 0;
}
like image 37
Adrian Mole Avatar answered Nov 07 '22 01:11

Adrian Mole