lundi 27 janvier 2020

Why first COUT is printing whole input? which is incorrect

Why first "cout" is printing incorrect char array that is if i enter char more than array size it is returning the same as input, whereas second cout is printing fine , that is it is able to detect the error!

#include <bits/stdc++.h>

using namespace std;

int main()
{
    char name[4];

    cin >> name;

    cout << name << endl;
    int  i;
    for(i = 0 ;name[i] != '\0' ; i++)
    {

    }

    cout << name <<endl;

    cout << "Number of chars are: " << i ;

}

if i enter name as: NishantVarshney , output is : NishantVarshney Nish? Number of chars are: 5

Aucun commentaire:

Enregistrer un commentaire