c++ novice scanf_s C6066, C6273, C6328 error question

Asked 2 years ago, Updated 2 years ago, 136 views

Sorry for the incorrect title. I don't know what to say about the topic.

#include <iostream>
using namespace std;
int main() {
    cout <<" Copy the word 'two' as it is entered. Just use two words. \n"<< endl;
    char a1[1000] = { 0 };
    char a2[1000] = { 0 };
    cin >> a1 >> a2;
    cout << a1 << " " << a2 << endl;
}

If you type it in visual studio like this, it runs well without any errors or warnings.

By the way,

#include <stdio.h>

int main() {

    printf (copy the 'two' words exactly as you entered them. Just use two words.\n");
    char a1[1000] = { 0 };
    char a2[1000] = { 0 };
    scanf_s("%s %s",a1,a2,sizeof(a1),sizeof(a2));
    printf("\n%s %s", a1, a2);

}

If you type it like this,

C6066 : An item other than a pointer has been passed to _Param_(4).
C6273: Non-integer item passed to _Param_(3).

C6328: Size mismatch: 'unsigned__int64' passed to _Param_(5).

There are a lot of warnings, but the execution goes well without errors.

I'm asking you because I'm curious about the reason why one is normal and the other is warned a lot

c++ scanf

2022-09-20 13:12

1 Answers

In the question, you said that the second code displays a warning but runs well without errors. I think you thought there was no error because the error statement did not appear, but the second program does not work properly and the program ends with an error.

There is an error in the sentence below.

scanf_s("%s %s", a1, a2, sizeof(a1), sizeof(a2));

You have to change the sentence above as below.

scanf_s("%s %s", a1, sizeof(a1), a2, sizeof(a2));


2022-09-20 13:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.