Making a simple program in C.

Author: Morris Wright
Date Of Creation: 23 April 2021
Update Date: 1 July 2024
Anonim
c program to make a simple structure program | learn coding
Video: c program to make a simple structure program | learn coding

Content

Ever wanted to program in C ++? The best way to learn this is to look at examples. Take a look at the following simple C ++ program to learn more about the structure of a C ++ program, and then create your own simple program.

To step

  1. Make sure you have a compiler and / or an IDE. Three good choices are GCC, or if you're running Windows, Visual Studio Express Edition or Dev-C ++.
  2. Try some sample programs. Copy and paste the following code into a text / code editor:
      A simple program created by Bjarne Stroustrup (C ++ developer) to check your compiler:

      #include iostream> [[Image: Create a Simple Program in C ++ Step 1 Version 3.webp | center]] #include string> using namespace std; int main () {string s; cout "Your name n"; cin s; cout "Hello," s " n"; return 0; }

    • A program for determining the sum of two numbers:

      [[Image: Create a Simple Program in C ++ Step 2 Version 3.webp | center]] #include iostream> using namespace std; int main () {int no1, no2, sum; cout " nThe first number ="; cin no1; cout " nThe second number ="; cin no2; sum = no1 + no2; cout " nThe sum of" no1 "and" no2 "=" sum n "; return 0; }

    • A program for determining the product of numbers:

      [[Image: Create a Simple Program in C ++ Step 3 Version 3.webp | center]] #include iostream> int main () {int v1, v2, range; std :: cout "Enter two numbers:" std :: endl; std :: cin v1 v2; if (v1 = v2) {range = v2 - v1; } else {range = v1 - v2; } std :: cout "range =" range std :: endl; return 0; }

    • A program for determining the value of exponents:

      [[Image: Create a Simple Program in C ++ Step 4 Version 3.webp | center]] #include iostream> using namespace std; int main () {int value, pow, result = 1; cout "Enter an operand:" endl; cin value; #cout "Enter an exponent:" endl; cin pow; for (int cnt = 0; cnt! = pow; cnt ++) result * = value; cout value "to the power" pow "is:" result endl; return 0; }

  3. Save this as a .cpp file with an appropriate name for the program's function. Note that there are many other extensions for C ++ files - choose one of them (such as *. Cc, *. Cxx, *. C ++, *. Cp).
    • HINT ": it should say Save as type: {choose "All files"}
  4. Compile the program. In Linux and the gcc compiler, you use Command: g ++ sum.cpp. In Windows you use a C ++ compiler, such as MS Visual C ++,Dev-C ++ or another program of your preference.
  5. Run the program. In Linux and the gcc compiler Command: ./a.out (a.out is an exe file produced by the compiler after compiling the program).

Tips

  • cin.ignore () prevents the program from quitting prematurely and closing the window (before you have time to see it)! To end the program, press any key. cin.get () functions the same way.
  • Add // before any comments.
  • You are free to experiment!
  • Learn to program in C ++ with ISO standards
  • For more information about programming in C ++, please visit cplusplus.com

Warnings

  • Your program will crash if you try to assign characters to one of the "int" variables. Because there is no proper error handling, your program cannot convert the values. You better read strings or handle errors.
  • Stay away from Dev-C ++ as far as possible, as it has multiple bugs and it contains an outdated compiler, and has not been updated since 2005.
  • Never use redundant code.

Necessities

  • A text / code editor (e.g. vim, Notepad, etc.).
  • A compiler.
  • Possibly an IDE with an editor and a compiler.
  • Turbo C
  • Codepad online
  • Notepad ++