std C++ code example

Example 1: strcmp c++

#include<stdio.h> 
#include<string.h> 


int main() 
{  
      
    char char1[] = "coucou"; 
    char char2[] = "coucou"; 
      
  	if( strcmp(char1, char2) == 0 )
       printf("Strings are the same");
  
  	else
      prinf("Strings are differentes");
  
  
    return 0; 
}

Example 2: using std c++

using namespace std;

Example 3: why to use std:: in c++

When you are using "std::" it means that you will grap the following command
from the "std" namespace.
The std namespace is an abbreviation for standard

If you use "std::" you hvae to include "#include <iostream>"

Example 4: stoi c++

// stoi example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stoi

int main ()
{
  std::string str_dec = "2001, A Space Odyssey";
  std::string str_hex = "40c3";
  std::string str_bin = "-10010110001";
  std::string str_auto = "0x7f";

  std::string::size_type sz;   // alias of size_t

  int i_dec = std::stoi (str_dec,&sz);
  int i_hex = std::stoi (str_hex,nullptr,16);
  int i_bin = std::stoi (str_bin,nullptr,2);
  int i_auto = std::stoi (str_auto,nullptr,0);

  std::cout << str_dec << ": " << i_dec << " and [" << str_dec.substr(sz) << "]\n";
  std::cout << str_hex << ": " << i_hex << '\n';
  std::cout << str_bin << ": " << i_bin << '\n';
  std::cout << str_auto << ": " << i_auto << '\n';

  return 0;
}

Example 5: strcmp c++

int strcmp ( const char * str1, const char * str2 );

// returning value | indicates
// <0	the first character that does not match has a lower value in ptr1 than in ptr2
// 0	the contents of both strings are equal
// >0	the first character that does not match has a greater value in ptr1 than in ptr2

Tags:

C Example