Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Comments and Formatting | Getting Started with C++
C++ Foundations: An Introductory Course

bookComments and Formatting

Understanding how to use comments and proper formatting is essential for writing clear and maintainable C++ code. Comments are notes you add to your code that are ignored by the compiler, helping you and others understand what the code does. In C++, you can use single-line comments with //, which comment out everything from the // to the end of the line. For multi-line comments, you use /* to begin and */ to end the comment, allowing you to span comments across several lines. Good formatting—such as consistent indentation, spacing, and clear organization—makes your code easier to read, debug, and share.

main.cpp

main.cpp

copy
12345678910111213141516171819202122
#include <iostream> int main() { // This is a single-line comment. // It explains the next line of code. std::cout << "Hello, comments and formatting!" << std::endl; // Print a message to the console. /* This is a multi-line comment. You can use it to write longer explanations or temporarily disable blocks of code. */ int number = 42; // Declare and initialize a variable. // Output the value of number std::cout << "The number is: " << number << std::endl; return 0; // Indicate that the program ended successfully. }

Consistent code formatting and clear comments are crucial for code readability. In the previous example, notice how each logical section of the program is separated by blank lines, and indentation is used to show which statements belong inside the main function. Comments are placed above or beside the code they describe, making it easy to understand the purpose of each line. Variable names are descriptive, and spacing around operators and after commas helps the code look clean and uncluttered. Following these practices ensures that your code is easy to read, understand, and maintain, whether you are working alone or with others.

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 2

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

bookComments and Formatting

Deslize para mostrar o menu

Understanding how to use comments and proper formatting is essential for writing clear and maintainable C++ code. Comments are notes you add to your code that are ignored by the compiler, helping you and others understand what the code does. In C++, you can use single-line comments with //, which comment out everything from the // to the end of the line. For multi-line comments, you use /* to begin and */ to end the comment, allowing you to span comments across several lines. Good formatting—such as consistent indentation, spacing, and clear organization—makes your code easier to read, debug, and share.

main.cpp

main.cpp

copy
12345678910111213141516171819202122
#include <iostream> int main() { // This is a single-line comment. // It explains the next line of code. std::cout << "Hello, comments and formatting!" << std::endl; // Print a message to the console. /* This is a multi-line comment. You can use it to write longer explanations or temporarily disable blocks of code. */ int number = 42; // Declare and initialize a variable. // Output the value of number std::cout << "The number is: " << number << std::endl; return 0; // Indicate that the program ended successfully. }

Consistent code formatting and clear comments are crucial for code readability. In the previous example, notice how each logical section of the program is separated by blank lines, and indentation is used to show which statements belong inside the main function. Comments are placed above or beside the code they describe, making it easy to understand the purpose of each line. Variable names are descriptive, and spacing around operators and after commas helps the code look clean and uncluttered. Following these practices ensures that your code is easy to read, understand, and maintain, whether you are working alone or with others.

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 2
some-alt