Basic syntax and structure of a C++

A C++ program begins with preprocessor directives, an example of which is including header files. Preprocessor directives provide instructions to the compiler and tell it what additional files to include in the compilation process.

Following the preprocessor directives are declarations, which include variables, constants, and user-defined functions.

The main function is the entry point of any C++ program, and contains all of the program’s executable code. Within the main function are more definitions, which are additional declarations of data types, variables, constants, and user-defined functions. Finally, the program is concluded with the return 0 statement, indicating success.

Developing a C++ program requires careful attention to the order in which the preprocessor directives, declarations, main function, and definitions are written. Only by understanding the basic structure of a C++ program can a programmer write effective, efficient, and bug-free code.

Here is an example of a basic C++ program that blinks LED on a 13-th pin of an Arduino Uno controller and can be written in Arguing IDE:

#include <avr/io.h>
#include <util/delay.h>
int time_ms = 1000;     // Variable declaration
void setup();           // Function declaration
void loop();

int main() 
{
    setup();            // Function call
    while (true){       // Main LOOP
        loop();
    }
    return 0;
}
void setup() {          // Function definition
    PDDB |= (1<<PINB5);
}
void loop(){
    PORTB |= (1<<PINB5);
    _delay_ms(time_ms);
    PORTB &= !(1<<PINB5);
    _delay_ms(time_ms);
}

: Native C++ program for ATmega328. {#lst:native_cpp}

Programming an Arduino Uno board in native C++ is much more difficult than in Arduino IDE. Arduino IDE makes it easier for users to write and debug code without having to know the details of the underlying hardware. In addition, the IDE provides many additional functions which simplify the usage of additional peripherals and actuators such as serial communication, LCDs, servo motors, step motors… This is especially true and important for beginners.

Task 1: Writing a Basic C++ Program for Arduino (Without Arduino IDE Functions)

To better understand the fundamentals of C++ before using Arduino-specific functions, write a simple C++ program that runs on an Arduino board but does not rely on setup() and loop(). The program should:

  • Blink an LED connected to pin 13 of an Arduino Uno.
  • Use direct register manipulation instead of digitalWrite().
  • Implement a main function as in standard C++ programs.

Modify and complete the following template:

#include <avr/io.h>
#include <util/delay.h>

int main() {
    DDRB |= (1 << PINB5); // Set pin 13 as output

    while (true) {
        PORTB |= (1 << PINB5);  // Turn LED on
        _delay_ms(500);
        PORTB &= ~(1 << PINB5); // Turn LED off
        _delay_ms(500);
    }

    return 0;
}

Questions to consider:

  • Why does this program use DDRB, PORTB, and PINB5 instead of pinMode() and digitalWrite()?
  • How does the while(true) loop function compared to loop() in Arduino IDE?
  • What happens if the return 0; line is removed?

Task 2: Analyzing the Structure of a Native C++ Arduino Program

Use the provided native C++ code for Arduino (from the learning material) and identify the following elements by line number:

  • Preprocessor Directive (#include statement)
  • Variable Declaration
  • Function Declaration
  • Function Definition
  • Function Call
  • Main Function
  • Comment

By completing these tasks, you will gain a deeper understanding of how C++ works independently of Arduino’s simplified environment, giving you a solid foundation for more advanced programming.

Summary:

<++>

Issues:

Not including a semicolon at the end of each statement:

Every statement in C++ must end with a semicolon. If a semicolon is omitted, the code will not compile correctly.

Not properly formatting the code:

Properly indenting and spacing code is important in C++ to make the code easier to read. Not formatting the code correctly can lead to syntactical errors.

Not using correct capitalization:

C++ is a case sensitive language and therefore proper capitalization is important. If the wrong capitalization is used, it can lead to syntax errors.