Mastering C/C++ Basics: A Guide to Variables, Operators, and Input/Output

Are you starting your journey into the world of programming? Whether you are aiming to write low-level system kernels or high-performance applications, understanding the foundational differences and similarities between C and C++ is crucial.

This guide breaks down the core building blocks of these languages: variables, constants, operators, and the essential input/output (I/O) mechanisms.

1. The Legacy: C vs. C++

Before diving into code, it is helpful to understand the lineage of these tools.

  • C (1972): Created by Dennis Ritchie and Brian Kernighan, C is known for being simple, minimal, and providing fast, low-level memory access. It was the language used to write the UNIX operating system.
  • C++ (1983): Developed by Bjarne Stroustrup, C++ builds upon C by adding object-oriented programming features while remaining mostly compatible with C.

2. Basic Types

Signed Integer

TypeSizeRange
char1 byte-128 .. 127
short2 bytes–32 768 .. +32 767
int2 bytes*
4 bytes*
–32 768 .. +32 767
–2 147 483 648 .. +2 147 483 647
long4 bytes–2 147 483 648 .. +2 147 483 647
long long8 bytes–9 223 372 036 854 775 808 .. 9 223 372 036 854 775 807

Unsigned Integer

TypeSizeRange
unsigned char1 byte0 .. 255
unsigned short2 bytes0 .. 65535
unsigned int2 bytes*
4 bytes*
0 .. 65535
0 .. 4 294 967 295
unsigned long4 bytes0 .. 4 294 967 295
unsigned long long8 bytes0 .. 18 446 744 073 709 551 615

*Dependent on the computer architecture.

Floating Point

TypeSizeRange
float4 bytes3.4E +- 1038 (7 digits)
double8 bytes1.7E +- 10308 (15 digits)

Boolean

TypeSizeValue
bool1 bytetrue/false

Standard Integer

Library: <stdint.h> (C) or <cstdint> (C++)

It is not system/platform dependent.

TypeSize
int8_t, uint8_t1 byte
int16_t, uint16_t2 bytes
int32_t, uint32_t4 bytes
int64_t, uint64_t8 bytes

Character

CharacterCode
‘ ‘ (Space)32
‘0’ .. ‘9’48 .. 57
‘A’ .. ‘Z’65 .. 90
‘a’ .. ‘z’98 .. 122

Wide Character

Store UTF-16 or UTF-32

TypeSize
wchar_t2 bytes (Windows)
4 bytes (Linux)
char8_t1 byte
char16_t2 bytes
char32_t4 bytes

3. Variables and Constants: Storing Data

Variables

A variable is a name given by the programmer to a specific memory region (RAM) used to store a value. Every variable must have a specific data type, which dictates the size of memory allocated and the range of values it can hold.

Syntax for Declaration:

// Explicit type declaration
int age = 12;
float final_score = 9.5f;

// Type inference (C++11 and later)
auto length = 18.6;

Naming Rules: When naming variables, remember to use characters a-z, A-Z, 0-9, or underscores (_). Names cannot begin with a digit and must avoid reserved keywords like int or float.

Constants

Constants are similar to variables but store values that cannot be changed during the program’s execution.

How to Define Constants:

  • Macro (Legacy C): #define PI 3.14159
  • Const Keyword: const int MAX_SIZE = 100;
  • Constexpr (Modern C++): constexpr uint16_t BUFF_SIZE = 1024 * 4;

Tip: By convention, constant names are written in ALL CAPS (e.g., MAX_SIZE) to distinguish them from variables.

4. Operators: Performing Calculations

C/C++ provides a rich set of operators to manipulate data.

Unary Operators: Act on a single operand.

  • a++ (Post-increment): Uses the current value, then increases it.
  • ++a (Pre-increment): Increases the value, then uses it.

Binary Operators: Act on two operands.

  • Standard math: +, -, *, /
  • Modulus (Remainder): % (e.g., 5 % 2 results in 1)
  • Comparison: >, <, ==, != (Returns true or false)
  • Logical: && (AND), || (OR)

Ternary Operator: A shorthand for if-else.

  • Syntax: <condition> ? <true_value> : <false_value>
  • Example: max = (a > b) ? a : b;

Additionally, there are many other operators that you can explore by visiting the following website.

5. Input & Output: Talking to the User

This is where C and C++ diverge significantly in syntax, though the logic remains the same: Input → Processing → Output.

The C Way: stdio.h

C uses the <stdio.h> library with specific format specifiers to manage data types.

printf("Hello %s.\n", name);
// %d for integers, %f for floats, %s for strings

scanf("%f", &math_score);
// Note: You must typically use the address-of operator (&) for variables.

Formatting: You can control precision easily, e.g., %7.3f prints a float with a width of 7 and 3 decimal places.

The C++ Way: iostream

C++ introduces streams via <iostream>, which are generally easier for beginners as they handle data types automatically.

std::cout << "Name = " << name;

std::cin >> math >> lit;

Formatting: Requires <iomanip>. You can use manipulators like setw(width) to format output tables nicely.

Leave a Reply

Your email address will not be published. Required fields are marked *