Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

A C++ Program Example

c example

Using a fast and reliable C or C++ compiler is very important for beginners and professionals learning to write console apps. The right C++ IDE helps teach beginners the various elements and syntax of a C++ program to a beginner and reminds more experienced developers of parameters to functions and methods. For both types of developer a good IDE is a tool which enhances their workflow and promotes greater developer efficiency.

What are the origins of C++?

The C++ programming language is one of the most popular and versatile programming languages. The C Programming Language was developed in the 1970s and since 1970, C has inspired the invention of a vast array of other programming languages, such as C++. The C++ programming language includes a substantial number of elements of the C language and builds on those solid foundations with object-oriented programming features like Classes, Objects, and Methods. Still, this oldest genie language is popular, mostly in the top 3 but if we consider its other variations and its usage on microchips and IoTs it is the most used programming language.

C and C++ compilers are great tools to develop when they are paired with a professional C++ IDE such as RAD Studio and C++ Builder.

What do I need to use the C++ program example?

To edit and run a simple C++ program you need a compiler, a linker, and an editor into which you type your C++ program code. The best and the simplest way is to use a C++ IDE which combines all these things into one package along with other useful tools.

The C++ Builder CE version is free C and C++ IDE for building simple or professional applications on the Windows, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. C++ Builder comes with Rapid Application Development Studio, also known as RAD Studio, and C++ Builder is one of the most professional IDEs that work under RAD Studio. C++ Builder comes from a venerable history which began with the incredibly popular Borland TurboC in 1990. This was later renamed Borland C++ Builder. Under the Embarcadero brand, it comes with new versions, features, updates, and support. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey FMX framework for cross-platform UIs. There is a free C++ Builder Community Edition for students, beginners, and startups.

You can download the free C++ Builder Community Edition here: https://www.embarcadero.com/products/cbuilder/starter.
Professional developers can use the Professional, Architect or Enterprise versions of C++ Builder. Please visit https://www.embarcadero.com/products/cbuilder.

Is there a C++ program example of a GUI application using C++ Builder?

If you download C++ Builder Community Edition (or RAD Studio CE version) or a free trial of the full version of C++ Builder. Install it on your windows computer and run RAD Studio or C++ Builder. Beginners and students normally start to learn C++ with simple code. Let’s create a new Multi-Device (Cross-Platform) C++ application by using FireMonkey (FMX) framework. It’s a simple program, but when we’ve finished it’s capable of running on other devices, not just a Windows PC!

Choose File->New-> “Multi-Device Application – C++ Builder” menu.

This will create a New C++ Project for Windows and can be easily compiled for other operating systems like iOS , Android. This will allow you develop C++ apps with FMX UI elements. If you don’t need UI Elements, this means you don’t need VCL or FMX frameworks, you create a console application too. Modern applications have a GUI’s and skinned Styles. Note that VCL projects are Windows only and FireMonkey projects are Multi Device (multi-platform) applications that you can compile and run on Windows, MacOS, iOS and Android .

Save all Unit files and Project file to a folder.

How to design your first screen or form when you start a C++ program?

Simply drag and drop components from the Palette window on the right side; Memo (TMemo) and Button (TButton) to your form design. Arrange their width, height and position. You can edit each of their properties from the Object Inspector on the left side.

Note that you can switch between the GUI Design mode to Code Mode by pressing F12, or vice versa. If you want, you can switch to your header file (.h) of your cpp file (.cpp) from the button tabs. You can change your Build Configuration from the left Project window by setting it to Debug or Release mode.

How to add your C++ code to your first C++ program example?

Double click to Button1 to create OnClick() event for this button. Add these lines into Button1Click() event,

[crayon-6769b031aaae0160464673/]

This example above is a modern “Hello World” example for Windows which runs with C++ Builder.

How to compile your C++ program example?

Now you can compile this C++ code; just press the F9 key or just click the Run button in the center of top bar. This will let the IDE check your code and compile both the Unit and project files and link them together. You can also use the Run menu from the top to run your application.You should see this below as a form application

If you get an error, check your code – it’s easy to mistype or have the braces (“}” characters) in the wrong place. Note that C++ is case sensitive, so upper and lower characters should be also same. Then press the F9 key or just click the Run button again.

From this point, you can add more codes or you can switch back to design mode by pressing F12. And you can add more components, then more codes. This is how modern C++ coding works with an UI Toolkit. You can try this simple “Hello World” example below too,

Is there a C++ program example as a console application?

You can develop console apps in C++ Builder too. This full example below is a “Hello World” C++ example that can be run with C++ Builder.

[crayon-6769b031aaaef139472408/]

Some key parts of our C++ program example

Let’s explain this small C++ example above. When you start coding in C or C++, first of all, you should include libraries that you use. Generally for beginners iostream library is enough to print outputs and get inputs in simple beginner apps. We can include this library as below,

[crayon-6769b031aaaf4845067918/]

Next, you should add the main procedure (the main program, the main function) as below,

[crayon-6769b031aaaf6482315845/]

After that you should write your programming code lines into this procedure, between { and } brackets. If you are a beginner “Hello World” examples are good to understand feature of that programming language. It is good to understand how to edit text, how to write in its format, how to compile and link, how to debug and execute, how to deploy or release.

In C++, after every command you use you should put ; to the end of your commands. In this example above we print a text with cout method of std library. We use :: operator as a library dependency, for example std::cout, and we use << for the output. Here is a example to print a text as an output,

[crayon-6769b031aaaf9714895660/]

In this example main() function is a integer (int) function, that means we should return an integer value. Thus, we should return 0 by using return 0; which means our main app successfully run and exit.

[crayon-6769b031aaafb115829741/]

Here is a C++ program example compiled with Embarcadero C++ compilers

If you don’t want to use project settings and you just want to compile a *.cpp file you can use compilers listed in bin directory of C++ Builder or RAD Studio. For example for an 32bit Windows application you can use C++ Builder Compiler for Windows 32bit bcc32.exe as below,

[crayon-6769b031aaafc620225264/]

or you can use bcc32c C++ Builder CLANG Compiler for Windows 32bit as below,

[crayon-6769b031aaafe616668495/]

Note that when you compile a C file in this way, the compiler will generate all exe and other output files to the same folder.

Don’t forget, you can also use Borland C++ Compiler and Dev-C++ to compile C programs. We highly recommend you use C++ Builder CE version that may allow you switch to the professional version easily.

How to compile a C++ program example in Linux?

You can use same C++ example code above in Linux. Write that example code in an editor (i.e Sublime Text Editor) and save as “myapp.cpp”. After these steps, in Terminal, go to folder of your C++ file and type g++ and your file name. Be sure that you are supervisor or allowed to save files to that folder. For example, you can compile your app as shown below.

In Linux, GNU C/C++ comes as default language installed. Generally gcc compiler is used as a C compiler and g++ compiler is used as C++ compiler. If your c++ program file is myapp.cpp you can compile this with g++ command as below,

[crayon-6769b031aab00918875420/]

Be sure that you have permission to write to folder there. Now you can easily run your myapp.out as given below,

[crayon-6769b031aab04639524528/]

Here is the full input and output process in Linux terminal

[crayon-6769b031aab06645185509/]

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

For students, beginners, and startups you can download a free C++ Builder Community Edition. If you are a professional developer, you can download a free trial copy of Professional, Architect, or Enterprise versions of C++ Builder.

Exit mobile version