Oct 16, 2018 A simple calculator can be made using a C program which is able to add, subtract, multiply and divide, two operands entered by the user. The switch and break statement is used to create a calculator. 1 Open Dev C: Go to Desktop and double clicks on Dev C icon or click on Windows's start menu and then click on All Programs and find Dev C from list and click on it. 2 After Opening: Click on File Menu and then click on New option and then click on Source File or press Ctrl+N. LLVM: Writing a Simple Programming Language - a step by step C tutorial on how to build a compiled language (using LLVM). You should basically use LLVM for the back-end, since that will save you hundreds of man-years of work and is open source. Run Dev/C.If you selected to run the program at the end of installation, you can skip to step 2,. Go to Start Menu All Programs Bloodshed Dev-C Dev-C. This should start the program. Create a New Source File.If you are prompted with a set up window, use all the default settings. Once the program opens, you need to create a new source file so you can start writing your first program.
![]() The left panel above shows the C++ code for this program. The right panel shows the result when the program is executed by a computer. The grey numbers to the left of the panels are line numbers to make discussing programs and researching errors easier. They are not part of the program. Let's examine this program line by line:
You may have noticed that not all the lines of this program perform actions when the code is executed. There is a line containing a comment (beginning with // ). There is a line with a directive for the preprocessor (beginning with # ). There is a line that defines a function (in this case, the main function). And, finally, a line with a statements ending with a semicolon (the insertion into cout ), which was within the block delimited by the braces ( { } ) of the main function.The program has been structured in different lines and properly indented, in order to make it easier to understand for the humans reading it. But C++ does not have strict rules on indentation or on how to split instructions in different lines. For example, instead of We could have written: all in a single line, and this would have had exactly the same meaning as the preceding code. In C++, the separation between statements is specified with an ending semicolon ( ; ), with the separation into different lines not mattering at all for this purpose. Many statements can be written in a single line, or each statement can be in its own line. The division of code in different lines serves only to make it more legible and schematic for the humans that may read it, but has no effect on the actual behavior of the program.Now, let's add an additional statement to our first program: In this case, the program performed two insertions into std::cout in two different statements. Once again, the separation in different lines of code simply gives greater readability to the program, since main could have been perfectly valid defined in this way:The source code could have also been divided into more code lines instead: And the result would again have been exactly the same as in the previous examples. Preprocessor directives (those that begin by # ) are out of this general rule since they are not statements. They are lines read and processed by the preprocessor before proper compilation begins. Preprocessor directives must be specified in their own line and, because they are not statements, do not have to end with a semicolon (; ).Simple Program In Dev C++Using namespace stdIf you have seen C++ code before, you may have seencout being used instead of std::cout . Both name the same object: the first one uses its unqualified name (cout ), while the second qualifies it directly within the namespacestd (as std::cout ).cout is part of the standard library, and all the elements in the standard C++ library are declared within what is called a namespace: the namespace std .In order to refer to the elements in the std namespace a program shall either qualify each and every use of elements of the library (as we have done by prefixing cout with std:: ), or introduce visibility of its components. The most typical way to introduce visibility of these components is by means of using declarations:The above declaration allows all elements in the std namespace to be accessed in an unqualified manner (without the std:: prefix).With this in mind, the last example can be rewritten to make unqualified uses of cout as:Both ways of accessing the elements of the std namespace (explicit qualification and using declarations) are valid in C++ and produce the exact same behavior. For simplicity, and to improve readability, the examples in these tutorials will more often use this latter approach with using declarations, although note that explicit qualification is the only way to guarantee that name collisions never happen.Namespaces are explained in more detail in a later chapter. How To Write A Simple Program In Dev C++
Comments are closed.
|
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2020
Categories |