C++ Basic Syntax and Data Types - A Professional Guide

1. Basic Syntax and Data Types

Q: What is the basic syntax of a C++ program?

A C++ program consists of:

Q: What are the primitive data types in C++?

C++ provides built-in (primitive) data types, similar to C but with enhancements:

Q: What are the sizes and ranges of primitive data types in C++?

(Sizes are typical for 32-bit systems; may vary by system/compiler.)

Type Size (bytes) Range (Signed) Range (Unsigned) Output Format (cout)
char 1 -128 to 127 0 to 255 << (as char or int)
int 4 -2,147,483,648 to 2,147,483,647 0 to 4,294,967,295 <<
short 2 -32,768 to 32,767 0 to 65,535 <<
long 4 or 8 -2^31 to 2^31-1 or -2^63 to 2^63-1 0 to 2^32-1 or 0 to 2^64-1 <<
long long 8 -2^63 to 2^63-1 0 to 2^64-1 <<
float 4 ~1.2E-38 to 3.4E+38 (6 digits) N/A <<
double 8 ~2.3E-308 to 1.7E+308 (15 digits) N/A <<
long double 10 or 16 Varies (higher precision) N/A <<
bool 1 true (1) or false (0) N/A << boolalpha

Q: How do you declare and initialize variables in C++?

Q: Can you give an example of a C++ program demonstrating basic syntax and data types?

#include 
using namespace std;

int main() {
    // Declaration and initialization
    int age = 25;
    double salary = 50000.75;
    char grade = 'A';
    bool isActive = true;
    auto temperature = 36.6; // Type inferred as double
    
    // Uniform initialization (C++11)
    float pi{3.14159f};
    
    // Output using cout
    cout << "Age: " << age << endl;
    cout << "Salary: " << salary << endl;
    cout << "Grade: " << grade << endl;
    cout << boolalpha << "Active: " << isActive << endl;
    cout << "Temperature: " << temperature << endl;
    cout << "Pi: " << pi << endl;
    
    return 0;
}

Compilation and Execution:

g++ basic_syntax_data_types.cpp -o basic_syntax
./basic_syntax

Output:

Age: 25
Salary: 50000.75
Grade: A
Active: true
Temperature: 36.6
Pi: 3.14159

Q: What is the role of using namespace std;?

It allows direct use of standard library names (e.g., cout, cin, endl) without the std:: prefix. For example, cout instead of std::cout.

Note: Overusing using namespace std; in large programs can cause name conflicts; prefer std:: or specific using declarations (e.g., using std::cout;).

Q: What are the advantages of C++ data types over C?

Q: What are common mistakes with C++ syntax and data types?

Q: How do you use auto in C++?

auto (C++11) infers the type of a variable based on its initializer. It simplifies code, especially for complex types (e.g., STL iterators).

Example:

auto x = 42; // int
auto y = 3.14; // double
auto z = "Hello"; // const char*

Q: What are modifiers for data types in C++?

Q: Can you give an example of input/output in C++?

#include 
using namespace std;

int main() {
    int age;
    double height;
    
    // Input using cin
    cout << "Enter age: ";
    cin >> age;
    cout << "Enter height (in meters): ";
    cin >> height;
    
    // Output using cout
    cout << "Age: " << age << ", Height: " << height << " meters" << endl;
    
    return 0;
}

Sample Input/Output:

Enter age: 30
Enter height (in meters): 1.75
Age: 30, Height: 1.75 meters

Q: What are the applications of data types in DSA?