D
D
Daniel2018-09-11 23:17:31
C++ / C#
Daniel, 2018-09-11 23:17:31

C++ why is the scanf function executed before the printf function?

Why (I work in netbeans) in this code, when executing, you need to enter 1 line that is considered, then some other line that does not participate anywhere, and only then output "Enter your name.\n Your name is **** "And I need to enter the name immediately after the question. It turns out that scanf works faster, it's like that in general.

int main(int argc, char** argv) {
    setlocale(LC_ALL, "ru_RU.UTF-8"); 
    char *name; //=new char[10];
    printf("Введите ваше имя  ");
    scanf("%s",&name);
    printf("\nВаше имя - %s\n",&name);
    
    //system("pause");
    return 0;
}

And this example from the Internet is generally a game, you need to enter 100 lines with a random number of characters until the process ends with a value of 1 million a bunch of digits
int main(int argc, char** argv) {
    setlocale(LC_ALL, "ru_RU.UTF-8"); 
    char buffer[100];       // массив (буфер) для вводимой строки
    gets(buffer);            // вводим строку и нажимаем enter
    printf("%s",buffer);
    
    //system("pause");
    return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2018-09-11
@daniil14056

It turns out that scanf works faster, it's like that in general.

stdout is buffered line by line by default, i.e. output occurs only when the internal buffer overflows or a '\n' character is encountered. To force output of everything that has accumulated in the buffer, you can use fflush(stdout)after printf:
printf("Введите ваше имя  ");
fflush(stdout);
scanf("%s",name);

char *name; //=new char[10];
...
scanf("%s",&name);

This code doesn't work. sscanf scans into an array. Must be
char name[10];
...
scanf("%s",name);

or
char *name = new char[10];
...
scanf("%s",name);

but not what is written there.

I
IceJOKER, 2015-03-22
@IceJOKER

1. think for yourself, look, if this path is easy, then why not? and what for you to think about - "Does it make sense to get into such "constructors" at all?" - it’s the same as you wrote above, only for the thesis, I don’t see the point in going especially deep into the rabbit hole
2. make a website and simply open this website in your application using the webiew component on other sites too). Maximum 2-4 lines in java, and everything else - html/css/js
3. startandroid.ru

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question