C++

13.03.2008 13:57

 Poznámky k syntaxi jazyka C++ vypsané z https://www.cplusplus.com/doc/tutorial

Proměnné

 Jméno

  Popis Velikost  Rozsah*
char Character or small integer. 1byte signed: -128 to 127
unsigned: 0 to 255
short int (short) Short Integer. 2bytes signed: -32768 to 32767
unsigned: 0 to 65535
int Integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
long int (long) Long integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
bool Boolean value. It can take one of two values: true or false. 1byte true or false
float Floating point number. 4bytes 3.4e +/- 38 (7 digits)
double Double precision floating point number. 8bytes 1.7e +/- 308 (15 digits)
long double Long double precision floating point number. 8bytes 1.7e +/- 308 (15 digits)
wchar_t Wide character. 2 or 4 bytes 1 wide character

Konstanty

Numerické

75         // decimal
0113       // octal
0x4b       // hexadecimal

75         // int
75u        // unsigned int
75l        // long
75ul       // unsigned long3.14159    // 3.14159
6.02e23    // 6.02 x 10^23
1.6e-19    // 1.6 x 10^-19
3.0        // 3.03.14159L   // long double
6.02e23f   // float

Character a string

'z'
'p'
"Hello world"
"How do you do?"

 

speciální znaky

\n newline
\r carriage return
\t tab
\v vertical tab
\b backspace
\f form feed (page feed)
\a alert (beep)
\' single quote (')
\" double quote (")
\? question mark (?)
\\ backslash (\)

 

Příklad:

'\n'
'\t'
"Left \t Right"
"one\ntwo\nthree"

"string expressed in \
two lines"

"this forms" "a single" "string" "of characters"

 

Boolean

true a false

Definované konstanty (#define)

 

#define identifier value

Např.:
#define PI 3.14159265
#define NEWLINE '\n'

Deklarované constanty (const)

const int pathwidth = 100;
const char tabulator = '\t';

Operátory

Znak přiřazení

 =

a=5; a=b;

Aritmetické operátory ( +, -, *, /, % )

+ součet, - rozdíl, * součin / podíl % modulo

a = 11 % 3;

Složené přiřazení (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)

výraz                            znamená
value += increase;         value = value + increase;
a -= 5;                          a = a - 5;
a /= b;                          a = a / b;
price *= units + 1;          price = price * (units + 1);

Example 1                                             Example 2
B=3;                                                      B=3;
A=++B;                                                  A=B++;
// A contains 4, B contains 4                  // A contains 3, B contains 4

Rovnosti a nerovnosti ( ==, !=, >, <, >=, <= )

== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Logické operátory ( !, &&, || )

a b a && b
true true true
true false false
false true false
false false false

|| OPERATOR

a b a || b
true true true
true false true
false true true
false false false

Příklad:

( (5 == 5) && (3 > 6) )  // evaluates to false ( true && false ).
( (5 == 5) || (3 > 6) )  // evaluates to true ( true || false ).

 Conditional operator ( ? )

condition ? result1 : result2

7==5 ? 4 : 3     // returns 3, since 7 is not equal to 5.
7==5+2 ? 4 : 3   // returns 4, since 7 is equal to 5+2.
5>3 ? a : b      // returns the value of a, since 5 is greater than 3.
a>b ? a : b      // returns whichever is greater, a or b.

Comma operator ( , )

 

a = (b=3, b+2);

Napřed přiřadí 3 do proměnné b a pak b+2 do proměnné a. Takže nakonec bude v proměnné a hodnota 5 a hodnota b bude 3.

Bitwise operátory ( &, |, ^, ~, <<, >> )

operátor asm equivalent popis
& AND Bitwise AND
| OR Bitwise Inclusive OR
^ XOR Bitwise Exclusive OR
~ NOT Unary complement (bit inversion)
<< SHL Shift Left
>> SHR Shift Right

 

Přetypování

Příklad:

int i;
float f = 3.14;
i = (int) f;

nebo

i = int ( f );

sizeof()

a = sizeof()

Priorita operátorů

Level Operator Description Grouping
1 :: scope Left-to-right
2 () [] . -> ++ -- dynamic_cast static_cast reinterpret_cast const_cast typeid postfix Left-to-right
3 ++ -- ~ ! sizeof new delete unary (prefix) Right-to-left
* & indirection and reference (pointers)
+ - unary sign operator
4 (type) type casting Right-to-left
5 .* ->* pointer-to-member Left-to-right
6 * / % multiplicative Left-to-right
7 + - additive Left-to-right
8 << >> shift Left-to-right
9 < > <= >= relational Left-to-right
10 == != equality Left-to-right
11 & bitwise AND Left-to-right
12 ^ bitwise XOR Left-to-right
13 | bitwise OR Left-to-right
14 && logical AND Left-to-right
15 || logical OR Left-to-right
16 ?: conditional Right-to-left
17 = *= /= %= += -= >>= <<= &= ^= |= assignment Right-to-left
18 , comma Left-to-right

Kontakt

Elektro

pfory@seznam.cz

Vyhledávání

Všechny elektronické součástky pracují na DÝM, jakmile z nich jednou unikne, přestávají pracovat.

© 2008 Všechna práva vyhrazena.

Vytvořte si webové stránky zdarma!Webnode