Writing Default Code in C: 11 Steps (with Pictures)

Table of contents:

Writing Default Code in C: 11 Steps (with Pictures)
Writing Default Code in C: 11 Steps (with Pictures)
Anonim

There are endless ways to program computers. Ultimately, it is the programmer's choice how to achieve what he needs. However, there are many "good ways" of styles and function usage for better compilation and safer programs. Make sure that future programmers (including yourself) working on your project can read and understand your code.

Steps

Method 1 of 2: Writing default code

Write Standard Code in C++ Step 1

Step 1. Download a C++ IDE (integrated development environment) such as Eclipse, Netbeans and CodeBlocks, or use a text editor such as Notepad++ or VIM

You can also run programs from the command line, in which case a text editor will suffice. It may be helpful to choose an editor that indicates syntax and supports line numbers. Most programmers find that Unix-like systems (Linux, OS X, BSD) are the best environments for program development.

Write Standard Code in C++ Step 2

Step 2. Create a main program file

The main file must contain a function called main(). This is where the execution of the program begins. From this point on, you call functions, instantiating classes, etc. Other files from your application and libraries can be included in this file.

Write Standard Code in C++ Step 3

Step 3. Start writing your program

Enter the code or program you need to create (see below for some examples). Learn the syntax, semantics, object-oriented programming paradigms, data flows, algorithm designs such as linked lists, priority queues, etc. C++ is not an easy language to program in, but it teaches you the basics that are useful for all programming languages.

Write Standard Code in C++ Step 4

Step 4. Add comments to your code

Explain what the functions do and what the variables are for. Choose meaningful names for variables and functions. Start the names of global variables with capital letters. In general, make sure anyone reading your code can understand it.

Write Standard Code in C++ Step 5

Step 5. Use indentation appropriately in your code

Again, see examples below. Write Standard Code in C++ Step 6

Step 6. Compile your code with

g++ main.cpp

Write Standard Code in C++ Step 7

Step 7. Run your program with the following instruction:

./a.out

Method 2 of 2: Examples

Step 1. View example 1:

    /* This is a simple program to understand the basics of the g++ style. This is a program with the g++ compiler.*/ #include /* input and output functions */ using namespace std; /* we use the std (standard) functions */ int main() /* declare the main function; int main(void) is also possible. */ { cout << "\n Hi Daddy"; /* '\n' is a newline (t is a tab) */ cout << "\n Hi mama"; cout << "\n This is my first program"; cout << "\nDate 11/03/2007"; return 0; }

Step 2. View example 2:

    /* This program calculates the sum of two numbers */ #include using namespace std; int main() { float num1, num2, res; /* declare variables; int, double, long also works */ cout << "\n Enter the first number= "; cin >> num1; /* enter user in num1 */ cout << "\n Enter the second number= "; cin >> num2; res = num1 + num2; cout << "\n The sum of "<< num1 <<" and "<< num2 <<" = "<<res '\n'; return 0; }

Step 3. View example 3:

    /* Product of two numbers */ #include using namespace std; int main() { float num1; int num2; double res; cout << "\n Enter the first number = "; cin >> num1; cout << "\n Enter the second number = "; cin >> num2; res = num1 * num2; cout << "\n The product of two numbers = " << res '\n'; return 0; }

Step 4. View Example 4:

    // Use a loop to find a math equation. In this case, the answer is calculated on // Question #1 from Project Euler. #include using namespace std; int main() { // Open main. int sum1=0; intsum2=0; intsum3=0; intsum4=0; // Creates the integers required to determine the answer. for (int a=0; a < 1000; a=a+3) {sum1 = sum1+a;} // Repeats until a is equal to or greater than 1000, adds 3 to a on each loop. Adds a to sum1. for (int b=0; b < 1000; b=b+5) {sum2 = sum2+b;} // Repeats until b is equal to or greater than 1000, adds 5 to b in each loop. Adds b to sum2. for (int c=0; c < 1000; c=c+15) {sum3 = sum3+c;} // Repeats until c is equal to or greater than 1000, adds 15 to c in each loop. Adds c to sum3. sum4 = sum1 + sum2 - sum3; // sum4 is the sum of sum1 and sum2, from which sum3 is subtracted. cout << sum4; // Returns sum4, the answer. cin.get(); // Wait for Enter from user. return 0; // Return statement. } // Main is closed.

Step 5. Check out this example of different styles:

    int main() { int i = 0; if(1+1==2){ i = 2; } } /* This is the Whitesmith style */ int main() { int i; if (1+1==2) { i = 2; } } /* This is GNU style */ int main () { int i; if (condition) { i = 2; function(); } }

Tips

  • Always use an ISO compiler for your programs.
  • The default executable created by the compiler is called 'a.out'.
  • If you're writing something with many different variables or functions, try commenting on it so it's easier to debug and understand later!

Popular by topic