본문 바로가기

old_Coding/C/C++

[C++] '\0' & '\n'


With testing length of characters, I discovered very harm misunderstanding.
I usually wrote '\n' to mean 'NULL', but it's terribly wrong.


Here is my source code.


* Code *
#include <iostream>
#include <string.h>
#include <locale.h>

int main()
{
char* Str1 = "Count." ;
char* Str2 = "Count.\0" ;
char* Str3 = "Count.\n" ;
wchar_t* wStr1 = L"Count." ;
wchar_t* wStr2 = L"Count.\0" ;
wchar_t* wStr3 = L"Count.\n" ;

std::cout << "Length of " << Str1 << " =" << strlen(Str1) << std::endl ;
std::cout << "Length of " << Str2 << " =" << strlen(Str2) << std::endl ;
std::cout << "Length of " << Str3 << " =" << strlen(Str3) << std::endl ;

wprintf(L"Length of '%s' : %d\n", wStr1, wcslen(wStr1) );
wprintf(L"Length of '%s' : %d\n", wStr2, wcslen(wStr2) );
wprintf(L"Length of '%s' : %d\n", wStr3, wcslen(wStr3) );

// to stop to destroy
int i ;
std::cin >>  i ;
return 0 ;
}






If you are a student who studies C++, let you guess the result.
Here is the result.

* Result


I have known '\n' means 'NULL', but it didn't.
'\n' means 'ENTER'.!!
'\0' means 'NULL'.