User Tools

Site Tools


cpp:basic_data_types

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
cpp:basic_data_types [2017/03/15 16:10] gthanoscpp:basic_data_types [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 18: Line 18:
 |:::   |**signed long int** |Not   smaller   than   int.   At   least   32   bits.| |:::   |**signed long int** |Not   smaller   than   int.   At   least   32   bits.|
 |:::   |**signed long long int** | Not   smaller   than   long.   At   least   64   bits.  | |:::   |**signed long long int** | Not   smaller   than   long.   At   least   64   bits.  |
-|Integer types (unsigned) | **unsigned   char**  | (same   size   as   their   signed   counterparts) |+|**Integer types (unsigned)** | **unsigned   char**  | (same   size   as   their   signed   counterparts) |
 |:::   |**unsigned short   int**  |:::   | |:::   |**unsigned short   int**  |:::   |
 |:::   |**unsigned int**  |  ::: | |:::   |**unsigned int**  |  ::: |
 |:::   |**unsigned   long   int**  |:::   | |:::   |**unsigned   long   int**  |:::   |
 |:::   |**unsigned   long   long   int**  |:::   | |:::   |**unsigned   long   long   int**  |:::   |
-|Floating-point   types | **float**  |  |+|**Floating-point types** | **float**  |  |
 |:::   | **double**   |Precision   not   less   than float | |:::   | **double**   |Precision   not   less   than float |
 |:::   |**long double** | Precision   not   less   than   double  | |:::   |**long double** | Precision   not   less   than   double  |
-|Boolean type | **bool**  |   | +|**Boolean type** | **bool**  |   | 
-|Void type  | **void**  |no storage  |  +|**Void type**  | **void**  |no storage  |  
-|Null pointer | **decltype(nullptr)**  | |+|**Null pointer** | **decltype(nullptr)**  | | 
 + 
 +Σημειώστε με βάση τον παραπάνω πίνακα ότι με εξαίρεση των τύπο **char** όλοι οι άλλοι βασικοί τύποι δεδομένων δεν καταλαμβάνουν συγκεκριμένο μέγεθος bytes στη μνήμη, αλλά προσδιορίζονται με βάση ένα ελάχιστο συνήθως μέγεθος. Αυτό δεν σημαίνει ότι οι συγκεκριμένοι τύποι δεδομένων δεν έχουν συγκεκριμένο μέγεθος αλλά ότι το μέγεθος αυτό μπορεί να διαφέρει από  μηχανή σε μηχανή. 
 + 
 +===== Δήλωση και αρχικοποίηση μεταβλητών ===== 
 + 
 +Οι μεταβλητές είναι δυνατόν να αρχικοποιηθούν με τους παρακάτω τρεις τρόπους: 
 +<code> 
 +int x=100; 
 +int x(100); 
 +int x{100}; 
 +</code> 
 + 
 +<code c++ main.cpp> 
 +#include <iostream> 
 +using namespace std; 
 + 
 +int main () 
 +
 +  int a=5;               // initial value: 5 
 +  int b(3);              // initial value: 3 
 +  int c{2};              // initial value: 2 
 + 
 + 
 +  a = a + b; 
 +  cout << "a: " << a << endl;  // endl stands for newline 
 +  b = a - c;               
 +  cout << "b: " << b << endl; 
 + 
 +  return 0; 
 +
 +</code> 
 + 
 +===== Αυτόματη εξαγωγή τύπου δεδομένων ===== 
 + 
 +Όταν μία μεταβλητή δηλώνεται ο μεταγλωττιστής μπορεί να προσδιορίσει τον τύπο της από τον τύπο της τιμής που ανατίθεται σε αυτή. Σε αυτή τη περίπτωση αρκεί ο τύπος της μεταβλητής να δηλωθεί ως **auto**, όπως στο παρακάτω παράδειγμα. 
 + 
 +<code cpp using-auto.cpp> 
 +#include <iostream> 
 +using namespace std; 
 + 
 +int main () 
 +
 +  int a{5};               // initial value: 5, sizeof int 
 +  long b(3);              // initial value: 3  sizeof long 
 + 
 +  auto result = a + b;    // compiler automatically extracts the variable type. 
 +   
 +  cout << "sizeof(result): " << sizeof(result) << endl; 
 +  cout << "sizeof(int): " << sizeof(int) << endl; 
 +  cout << "sizeof(long): " << sizeof(long) << endl; 
 + 
 +  return 0; 
 +
 +</code> 
 + 
 +Επίσης, εάν μία μεταβλητή δεν αρχικοποιείται μπορείτε να δηλώσετε ότι είναι ιδίου τύπου με μία άλλη μεταβλητή ως εξής; 
 + 
 +<code cpp using-decltype.cpp> 
 +#include <iostream> 
 +using namespace std; 
 + 
 +int main () 
 +
 +  int a{5};               // initial value: 5 
 +  long b(3);              // initial value: 3 
 +  decltype(b) result; 
 + 
 +  cout << "sizeof(result): " << sizeof(result) << endl; 
 +  cout << "sizeof(int): " << sizeof(int) << endl; 
 +  cout << "sizeof(long): " << sizeof(long) << endl; 
 +   
 +  return 0; 
 +
 +</code> 
  
cpp/basic_data_types.1489594225.txt.gz · Last modified: 2017/03/15 16:10 (external edit)