C + + syntax 2 - data types

Basic built-in type

  • Boolean bool
  • Character char
  • Integer int
  • (single precision) float ing point
  • Double precision floating point double
  • No type void
  • Wide character wchar_t

type specifier

  • signed
  • unsigned
  • short
  • long

Some basic types can be decorated with one or more type modifiers.

typedef

wchar_t is this:

typedef short int wchar_t;

So wchar_t is actually the same space as short int.

#include<iostream>  
#include <limits>
 
using namespace std;  
  
int main()  
{  
    cout << "type: \t\t" << "************size**************"<< endl;  
    cout << "bool: \t\t" << "Bytes occupied:" << sizeof(bool);  
    cout << "\t Maximum:" << (numeric_limits<bool>::max)();  
    cout << "\t\t Minimum:" << (numeric_limits<bool>::min)() << endl;  
}

variable

Local variable & global variable

  • Variables declared in the definition of function parameters are called formal parameters.

  • A local variable is a variable declared inside a function or a code block.

  • Global variables: variables declared outside all functions.

    • Values are valid throughout the life cycle of the program
    • Can be accessed by any function
  • The names of local variables and global variables can be the same. However, within a function, the value of a local variable overrides the value of a global variable.

  • When a local variable is defined, the system will not initialize it. You must initialize it yourself.

  • When defining global variables, the system will initialize automatically. int: 0,char '\0', float 0,double 0,pointer NULL.

#include <iostream>
using namespace std;
 
// Global variable declaration
int g;
 
int main ()
{
  // Local variable declaration
  int a, b;
 
  // Actual initialization
  a = 10;
  b = 20;
  g = a + b;
 
  cout << g;
 
  return 0;
}

constant

Also called literal quantity

Boolean Literals

There are two Boolean constants, which are standard C + + Keywords:

  • The value true represents true.
  • The false value represents false.

character constants

  • A character constant can be an ordinary character (such as' x '), an escape sequence (such as' \ t'), or a general character (such as' \ u02C0 ').
  • If the constant starts with L (only in uppercase), it means that it is a wide character constant (e.g. L'x '), which must be stored in wchar_ In a variable of type T.
    Otherwise, it is a narrow character constant (such as' x '), which can be stored in a simple variable of type char.
  • Escape sequence code
    • \: \ characters
    • \':' character
    • \":" character
    • \? : ? character
    • \a: alarm ring
    • \b: backspace key
    • \f: page feed
    • \n: line break
    • \r: enter
    • \t: horizontal tab
    • \v: vertical tab
    • \ooo: one to three digit octal number
    • \xhh . . . : A hexadecimal number of one or more digits

string constant

  • Enclosed in double quotation marks ""
  • You can use \ as a separator to branch a long string constant.
string greeting = "hello, runoob";
cout << greeting;

Define constants

Like C, constants can be defined using #define and const

String class

C + + provides the following two types of string representations:

#include <iostream>
#include <string>
 
using namespace std;
 
int main ()
{
   string str1 = "runoob";
   string str2 = "google";
   string str3;
   
   int  len ;
 
   // Copy str1 to str3
   str3 = str1;
   cout << "str3 : " << str3 << endl; // runoob
 
   // Connect str1 and str2
   str3 = str1 + str2;
   cout << "str1 + str2 : " << str3 << endl;  // runoobgoogle
 
   // Total length of str3 after connection
   len = str3.size();
   cout << "str3.size() :  " << len << endl;  // 12
 
   return 0;
}

Pointer

Similar to the pointer in C language, see https://blog.csdn.net/lovechris00/article/details/123116088#_529

quote

  • You can think of a reference as the second label attached to a variable in memory
  • You can access the contents of a variable through the original variable name or reference

Affirms

int i = 17;
double d;

// Declare reference variable r for i 
int&  r = i;   // Here, & is read as a reference
double& s = d;
 
cout << "Value of i reference : " << r  << endl; // 17

d = 11.7; 
cout << "Value of d reference : " << s  << endl;  // 11.7

Reference as parameter

#include <iostream>
using namespace std;
 
// Function declaration
void swap(int& x, int& y);
 
int main ()
{
   // Local variable declaration
   int a = 100, b = 200; 
   swap(a, b); 
   return 0;
}
 
// Function definition
void swap(int& x, int& y)
{
   int temp;
   temp = x; /* Save the value of address x */
   x = y;    /* Assign y to x */
   y = temp; /* Assign x to y  */
  
   return;
}

Reference as return value

Replacing pointers with references makes C + + programs easier to read and maintain.

double vals[] = {10.1, 12.6, 33.1, 24.1, 50.0};
 
double& getEle(int i) {  
   double& ref = vals[i];    
   return ref;   // Returns the reference of the ith element. Ref is a reference variable and ref refers to vals[i] 
}
 
// To call the main function of the function defined above
int main ()
{  
   getEle(1) = 20.23; // Change the 2nd element
   getEle(3) = 70.8;  // Change the 4th element 
   return 0;
}

Referenced scope

  • When returning a reference, it should be noted that the referenced object cannot exceed the scope.
    Therefore, it is illegal to return a reference to a local variable, but you can return a reference to a static variable.
int& func() {
   int q;
   //!  return q; //  An error occurred while compiling
   static int x;
   return x;     // Safe, x is still valid outside the scope of the function
}

enumeration

format

enum Enumeration name{ 
     identifier [=Integer constant], 
     identifier [=Integer constant], 
... 
    identifier [=Integer constant]
} Enumerating variables;

enum color { red, green, blue } c;
c = blue;

enum color { red, green=5, blue }; // The default value of the first name is 0

Date time

  • The C + + standard library does not provide the so-called date type.
  • C + + inherits the structures and functions of C language for date and time operations.
  • In order to use date and time related functions and structures, you need to reference the < CTime > header file in the C + + program.
  • There are four time related types: clock_t,time_t,size_t and tm.
  • Type clock_t,size_t and time_t can express the system time and date as some integer.
  • The structure type tm saves the date and time in the form of C structure. The definition of tm structure is as follows:
struct tm {
  int tm_sec;   // Seconds, normal range from 0 to 59, but allowed to 61
  int tm_min;   // Minutes, ranging from 0 to 59
  int tm_hour;  // Hours, ranging from 0 to 23
  int tm_mday;  // The day of the month, ranging from 1 to 31
  int tm_mon;   // Month, ranging from 0 to 11
  int tm_year;  // Years since 1900
  int tm_wday;  // The day of the week, ranging from 0 to 6, from Sunday
  int tm_yday;  // The day of the year, ranging from 0 to 365, from January 1
  int tm_isdst; // Daylight saving time
};

Important functions about date and time in C/C + +

  1. time_t time(time_t *time);
    This function returns the current calendar time of the system and the number of seconds elapsed since January 1, 1970. If the system does not have time, - 1 is returned.
  2. char *ctime(const time_t *time);
    The returns a string pointer representing the local time in the form of day month year hours:minutes:seconds year\n day month year hours: Minutes: seconds year \ n \ 0..
  3. struct tm *localtime(const time_t *time);
    This function returns a pointer to the tm structure representing the local time.
  4. clock_t clock(void);
    This function returns the time used by the processor clock since the execution of the program (usually the beginning of the program). - 1 if time is not available.
  5. char * asctime ( const struct tm * time );
    This function returns a pointer to a string, which contains the information stored in the structure pointed to by time. The return form is: day month date hours:minutes:seconds year\n Day Month Date hours: Minutes: seconds year \ n \ 0..
  6. struct tm *gmtime(const time_t *time);
    This function returns a pointer to time. Time is a tm structure, which is represented by coordinated universal time (UTC), also known as Greenwich mean time (GMT).
  7. time_t mktime(struct tm *time);
    This function returns the calendar time, which is equivalent to the time stored in the structure pointed to by time.
  8. double difftime ( time_t time2, time_t time1 );
    This function returns the number of seconds between time1 and time2.
  9. size_t strftime();
    This function can be used to format the date and time in the specified format.

example

#include <iostream>
#include <ctime>
 
using namespace std;
 
int main( )
{
   // Based on the current date / time of the current system
   time_t now = time(0);
   cout << "1970 Seconds elapsed so far:" << now << endl;  // 1503564157
   
   // Convert now to string form
   char* dt = ctime(&now);
 
   cout << "Local date and time:" << dt << endl;  // Sat Jan  8 20:07:41 2011
 
   // Convert now to tm structure
   tm *gmtm = gmtime(&now);
   dt = asctime(gmtm);
   cout << "UTC Date and time:"<< dt << endl;  // Sun Jan  9 03:07:41 2011
   
   tm *ltm = localtime(&now);
   cout << "year: "<< 1900 + ltm->tm_year << endl; 
   cout << "month: "<< 1 + ltm->tm_mon<< endl;
   cout << "day: "<<  ltm->tm_mday << endl;
   cout << "time: "<< ltm->tm_hour << ":";
   cout << ltm->tm_min << ":";
   cout << ltm->tm_sec << endl;

}

22-02-26 (VI)

Keywords: C++

Added by spiyun on Sat, 26 Feb 2022 08:07:19 +0200