C++ Comparison of String Literals

前端 未结 8 616
夕颜
夕颜 2020-11-27 06:01

I\'m a c++ newbie (just oldschool c). My son asked for help with this and I\'m unable to explain it. If he had asked me \"how do I compare strings\" I would have told hi

8条回答
  •  鱼传尺愫
    2020-11-27 06:15

    You are comparing memory addresses. The example that follow explains how to compare 2 strings:

    #include "stdafx.h"
    #include 
    #include  //prototype for strcmp()
    
    int _tmain(int argc, _TCHAR* argv[])
    {
     using namespace std;
    
     cout << strcmp("A", "Z"); // will print -1
     cout << strcmp("Z", "A"); // will print 1
    
     return 0;
    }
    

提交回复
热议问题