C++ Lesson 3: Printing and Definitions

Author’s Whisper

Finally able to post on the public account, let me write an article.

# Printing## IntroductionPrinting… I think I mentioned it before, but I haven’t taught it yet.## What is PrintingOutputting messages

Note

Right, just how to be perfunctory…

## Syntax of Printing

Note

This is much easier to teach than the previous two lessons, when did I become so perfunctory?

Still using Python as an example

print("Hello World")

JavaScript is here too

console.log("Hello, World!")

Main content: C++

#include <iostream>using namespace std;int main() {  cout << "Hello, World!" << endl;}

Note

Since the previous two lessons have already used this many times, I won’t elaborate (lazy).

# Definitions## What is a DefinitionDefining variables, still using Python

a = 0

But C++ has many more data types:byte;short;int;long;long long;long int;unsigned long;unsigned intlong doublestring;bool;charvoidbyte: Byte type (-128 to 127)short: Short integer (-32768 to 32767)int: Integer (-2147483648 to 2147483647)long: Long integerlong int: Long integer (yes, no difference from long)long long: Long long integerlong double: Long double precision floating pointunsigned int: Unsigned integer (no negative numbers)unsigned long: Unsigned long integer (no negative numbers)string: String (Note: must be in double quotes)bool: Boolean value (true | false)void: Void typechar: Character type (Note: must be in single quotes and a single character)## Syntax of Definitions

#include <iostream>#include <string>using namespace std;int main() {  byte a;  short b;  int c;  long d;  long long e;  string f = "asasa";  bool g = true;  bool h = false;  char i = 'a';  long int j;  unsigned long k;  unsigned int l;  long double m;}

Note

Getting lazy, so tired.

### Void Type (No Type)#### IntroductionNo type pointer

PS

The author has never used void.

Generally used for functions##### Defining a Function with No Return Value

#include <iostream>using namespace std;int main() {  return 0;}void add(int a,int b) {  int c = a + b;  // Since a void function has no return value, to add, a new variable must be defined}

##### Defining a Function with No Parameters

#include <iostream>using namespace std;int main() {  return 0;}int add(void) {  return 2+3;  // Since there are no parameters, the addition can only use fixed values}

##### Void Type Conversion

#include <iostream>using namespace std;int main() {  void *a;  int *b;  a = b; // No need for casting, direct conversion}

Data types that require casting

#include <iostream>using namespace std;int main() {  float a;  int b;  b = a; // Requires casting, will report an error}

Author’s Inner Voice

Whether I explain it early or late, it’s still an explanation, let’s first talk about casting.

How to cast?Automatic conversion hasn’t been discussed yet.Automatic conversionThis won’t be discussed here, but it should be noted that when converting char to int, the int value obtained is the ASCII code of the char character.Forced conversion

#include <iostream>using namespace std;int main() {  float a;  int b;  b = (int)a; // float to int  a = (float)b; // int to float}

Note: Unsigned integers have no negative numbers!Note: When a value overflows, it will start from the opposite number (positive to negative | negative to positive).##### Precautions When Using VoidIn the ANSI standard, void cannot be used for operations.But in the GNU standard, it can, because in the GNU standard, the operation of void * is the same as that of char *.

Ahhhhhhhhhhh

So tired.

Leave a Comment