Dev C Commands

Dev c++ commands

Activity for Dev-C. And using a getchar command at the end of the code, but nothing worked. Please someone help!! Obtaining source code library for dev-c.

  • Nov 10, 2016  Dev-C is an integrated development environment (IDE) for the C programming language. It presents a feature-rich environment, tools for writing and debugging, as well as a compiler to provide you with all the tools necessary to program software in C. The program is a fork of the Bloodshed Dev-C environment, designed for advanced programmers looking to create applications.
  • C: Functions We've talked a little bit about functions in the past - they're pieces of code that can be executed on command. In fact we've been using functions right since the very start of this process, we just haven't really talked about them in depth - this is what this tutorial is all about.

C++ (pronounced 'see-plus-plus') is an object oriented, general purpose programming language that was created in 1983 by Bjarne Stroustrup. It's used mainly for desktop software and game development, and is an extremely useful programming language to know.

So to start C++ programming, you'll need some way to write your C++ code, and then also a compiler which turns that code into something that your computer can directly read (often called 'machine language'). If you're using the Mac OS X operating system or a Linux distribution, I'd recommend using any text editor of your choice to write your code (I particularly like Sublime Text 2), and then using the 'g++' compiler to compile your code via the Terminal. For (Mac) OS X, g++ can be very easily installed as it comes bundled along with XCode.

Another popular choice for C++ coding and compilation is using an Integrated Development Environment (IDE) in which you can write and compile your code. For OS X you might want to use XCode if you're feeling worried about the g++ route (g++ can be a little complicated), and if you're using Windows, popular IDEs include Code::Blocks, which I personally recommend however does require some setup at the start (you may need to do some research), and Visual C++ Express, which is much easier to set up, however will require some sort of application pausing before the end of the 'main' function in most cases as it generates a window which will disappear after execution. If you decide to go with Visual C++ Express, just keep this in mind -- you may wish to put the system('PAUSE'); or _getch(); lines before the end of your 'main' function (the latter of which requires you to #include <conio.h>) -- if you don't understand what this means, you will soon enough.

So by this point, you should have your development environment properly set up (even if you don't entirely know how to use it yet). If you're using an IDE, create a new C++ project, often called a 'console project', and remove any code/text which is generated for you, and if you're using a separate text editor and compiler, just create a new file with the '.cpp' extension (the file extension for C++ source files) - I'm going to name mine 'project.cpp'.

To start off with, we're just going to add what's called a 'comment' to the top of our file which says that we've written the program. Comments are simply notes that you write (to either yourself, your team, or whoever else might see the code) that the C++ compiler completely ignores, and so these don't affect how your application actually runs. We can do single-line comments in C++ by using a double slash (//) and multi-line comments using /* to start the comment, and */ to end the comment. Take, for example, the following:

After a comment on your top line (which isn't entirely necessary), we need to 'include' a few things before we begin writing our main application logic. When we 'include' things in C++, we're simply taking pieces of pre-written code from a file - as such, includes are usually done at the start of a section of code so that the functionality can be used in the rest of the code. In this case we want to include the 'iostream' file (which stands for input output stream) so we can output and take in basic text data. These includes always start with a hash symbol (#), and then use the include keyword followed by the name of the file we wish to include (which we specify, in this case, in angle brackets):

As alluded to earlier, the stuff inside 'iostream' is for inputting and outputting text. The two most well-known things in here are called cout and cin (pronounced 'see-out' and 'see-in'), which are for outputting data and getting data from the user. These both, however, require the 'std' namespace - I'm not going to go into great detail about namespaces right now, but essentially just think about different namespaces as different drawers. A lot of the core C++ standard stuff is in the 'std' drawer and we can specify this by either writing std:: before every time we write cout, cin, and other things which require it, or for our simple purposes here, we can just use the namespace throughout out whole document. This is done by writing the using keyword, followed by the namespace keyword, followed by the namespace name, in this case, std:

Notice that I also finished the line by using a semicolon (;) - lines are often finished with a semicolon in C++ to show that the instruction or the line has finished. In this case, we're telling the compiler that we're done specifying the namespace by writing the semicolon.

With the top part of our document setup, we can actually begin writing our program's point of entry. All basic C++ programs start at what is called the 'main' function. A function is just a piece of code with a name, and that name, in this case, is 'main'. Functions also have a type, generally speaking the 'main' function should have the integer (whole number) type, and this is shown by writing the int keyword before the function name. This function type means that inside the function we must have a line which returns an integer value - don't worry too much about what this means right now, but if we return '0' from main, it generally means that we did everything without any errors. After we've written the function's name, we have to specify some brackets (which some people populate with some things, but we're going to leave them empty), and then some curly brackets which will actually contain our main application code. For now, I'm just going to put a comment in there. Our code so far should look as follows:

Notice that I put a tab-space before the comment - this is because the comment is inside the 'main' function and I wanted to visually show this. Indented code isn't necessary for your code to compile and run correctly, however it makes finding errors a hell of a lot easier, and if you don't do it, nobody will like you (or want to work with you!).

While we're talking about the 'main' function itself, let's put in that 'return' line we talked about earlier so that our main function abides to its type. This is done by simply writing the return keyword followed by the value which you want to return, followed by a semicolon - so in this case, return 0;. This will actually stop our 'main' function's execution though, so we want it to be the very last line of the function:

From here, we're going to complete our basic program by simply getting it to output 'Hello World!' (or text of your choice) to the screen. As alluded to earlier, this is done by using cout (which, remember, is in the std namespace). We can output a combination of different letters, numbers, and symbols, known as a string, by writing the cout keyword, followed by insertion operators, <<, followed by our string which is represented by wrapping in double quotes. We also need a semicolon to end the line.

The way cout works, is essentially that you can just put insertion operators after each piece of data you want to output to output some more. Numbers don't have to be surrounded in double quotes to be processed in the correct way be the compiler, so if we wanted to output a number after our 'Hello World!' piece of text, we could either put it inside our string, which would encapsulate the data, or we could put it after another set of insertion operators (and move the semicolon to the 'new' end of the line):

You should try to get used to this functionality. Without looking at the snippet below which reveals the answer, try to create a basic cout which would output a sentence with a number in the middle.

We can add newlines to this output by either writing a backslash followed by the letter 'n' in a string (remember that strings are encapsulated by double quotes), or by using the endl keyword after some insertion operators. So our final piece of code which we're going to compile and run (which I've commented a bit to make it easier to understand), is as follows:

To compile and run the code in an IDE, you can usually just hit a conveniently placed 'Run' or 'Debug' button (Key shortcuts of F8 in Code::Blocks, F5 in Visual C++ Express, and +R in XCode), however the process of compiling and running is slightly more confusing when you have a separate text editor and compiler. If this is the case, save the file with the code we've written (and with the '.cpp' file extension, so call it something like 'project.cpp'), and then open up a Terminal window (if you don't know how to do this in your OS, Google it). You can then navigate to wherever your project file (i.e. 'project.cpp') is stored using the 'cd' command (again, you'll have to Google if you don't know how to do this), and then run some compilation and running commands which are compiler and OS-specific. If you're using g++, you can run g++ -o project project.cpp to compile the code in 'project.cpp' to a file named 'project', and then on OS X and Linux you can run ./project to run the generated 'project' file (and hence the program) itself.

In future tutorials I will expect you to know how to compile and run code in your own development environment, so it'd probably be a good idea for you to create a few C++ projects and compile/run them to get used to your development environment. As a little challenge to tie off the information in this tutorial (which will, of course, be built upon in future tutorials), try writing another cout line after the one we've already written that outputs something else.

We've talked a little bit about functions in the past - they're pieces of code that can be executed on command. In fact we've been using functions right since the very start of this process, we just haven't really talked about them in depth - this is what this tutorial is all about.

As outlined earlier - a function is simply a piece of code (which may or may not need a value to work properly) that you can execute by 'calling' it. There are a bunch of functions built into the core language, but functions can also be created manually (we'll be learning about this a bit later on). The classic function notation looks like this: functionName();. In the previous snippet we would be calling a function called functionName which doesn't take any values to work properly (if it took any values - we'd put them in the brackets). A classic example of a function is the sqrt function which is defined inside math.h. We used this back in the basic mathematics tutorial, it simply square roots any value you pass to it. If we ever need to pass multiple values to a function, we separate them with a comma - so something like this: functionName(valueone, valuetwo, valuethree);.With this under our belts - let's start learning how to create our very own functions that can execute any preset code for us. For this tutorial we are going to work on creating a function which can add two numbers together.

Believe it or not, you've already got one function in your basic program structure. The main function! Let's dissect it a little so we can use something similar to create custom functions. The first thing it consists of is the function's type - it's an int in this case because the main function needs to be an int to be the int main that the compiler recognises as the program's entry point. The next thing (seperated from the type by a space) is the function's name - in this case it's main, and as I just alluded to, this is so the compiler recognises it as a point of entry. Next we have some empty brackets - these are here to illustrate that the function doesn't take any values, if we wanted a function to take values in (in the case of the function we want to create - we'll need to take two values so we can add them together) then we would put them inside the brackets. From here we simply have some curly brackets, inside which is the actual code we want the function to execute when called.

So firstly - why do functions need a type? Well, it's all to do with return. If a function is of type int, it must return an integer (e.g. return 0 which in the case of the main function illustrates that the program exited without any errors). Values that functions return essentially go in the place where the function was originally called - so the sqrt function returns the result of the square root operation (hence when we put it in a cout or something, the square root value get's inserted at the point we called it). Function can have the type of any version of our function, let's just create a function which can output some basic text - let's stick with the classics and make it output 'Hello World'. So firstly let's decide on a type and name - void is probably a good choice here as we can just use cout in the function rather than making it return anything. For the name, I'm going to go with 'say_hello'. So to make the function, we simply write a definition (the structure of which we've already talked about) before the main function in the code. It's important that it's before, otherwise the compiler won't know what you're talking about when you reference the function in main. The following would work fine for our simply say_hello function (I've just used the basic structure we've already discussed and put a cout inside):

Alternatively, you could put the function definition below the main function and simply leave what is called a prototype before the main function (to tell the compiler not to worry when trying to call the function in main as we define it later). A prototype is essentially just the first line of our function definition (no curly brackets) with a semi-colon afterwards. So if we wanted to use the prototype approach instead of putting the function before (some people prefer doing things this way - a lot of it is down to personal preference), we could do something like this:

Now you've got the function setup (either before the main function, or after with a function prototype before) - you can go ahead and call it from the main function using the process we talked about earlier (while we're talking about this - it's worth noting that functions can call each other if you want this nested-like functionality with multiple functions).

So our code at the moment should look something like this (I'm not going to use prototypes in this example just to avoid confusing anyone who doesn't quite understand them):

On running the above program, you should see that our basic function works! Brilliant, now let's modify it a bit so that it can add two numbers. Firstly let's change its name from 'say_hello' to something more appropriate like 'add_two_integers'. For now we can leave it as void and then make it simply do the addition in the cout - but we'll change this later and make it instead use return. Now let's make it take in two integer values, as it requires these to do its job properly (how else would it add two numbers?) - we write these just like variable definitions apart from they are inside the function definition's brackets (and inside the prototype brackets too if you're using a function prototype). If you want the function to take more than one value (like we do), then you can separate the values with commas. I'm going to call the parameters that my function takes (the proper terminology for the values it takes), 'value_one', and 'value_two'. Our function definition at the moment should look something like this:

From here, we can reference 'value_one' and 'value_two' (or whatever you called the 'variables' in the function definition) in our function and it will refer to whatever values the function was called with. It's important to note that if you try to call the function with two values that do not match the type of those that you put in the function definition (or you specify a different number of values) - the compiler should spit out an error telling you that it can't find the function you're talking about. So with the knowledge that we can reference variables that the function was passed, let's make our function output the addition of the two variables it was passed:

It's that simple, as long as we update our function call so that it uses the correct function name and passes the correct values, everything should work!

Excellent! The only problem is that our function for adding two integers isn't usable if we want to put the number in a more complex cout, do more mathematics to it, or pass it to another function. Our function's name is also a little bit misleading as the function doesn't actually add two integers, it outputs the addition of two integers. To fix this, let's just change the function type and use return. Since the addition of two integers should always result in an integer, we can use the int function type. Now we've done this we must make the function return a value or the compiler will (should) complain. Let's go ahead and remove the cout line currently in there as it's getting in the way, and replace it instead with a line that returns the addition.

Dev Commands

All we need to do now is update the function call in main so we actually use the value it returns somehow - I'm just going to multiply the addition of the two values by 2. The full source code should look something like the following:

Commands

And tada, we created our very own fully functional function! This is a very important technique when creating applications/programs using C++ and should be a process you get very used to in the creation process. If you fancy the challenge, try creating a basic function that takes three parameters - it adds the first two, and then multiplies the result by the third parameter if the third parameter is less than 100.

Dev C++ Code

There are a few more things we should probably talk about before this tutorial is over, namely what we can do when some parameters are not specified. The first thing we can do is something called overloading functions. This is a key concept in C++ which allows you to write two or more functions with the same name that take different parameters!

At current, our 'add_two_integers' function will only be called when we pass it two integer values, however we could, if we wanted, write a function with the same name that takes different parameters or parameters of different types, and the compiler would call the correct function for the job depending on the parameters we passed in. Let's say, for example, that we also wanted a version of our function that added floats -- if we renamed the function to 'add_two_numbers' (so that the name makes more sense), we could write both versions of our function (one for integers and one for floats) like so:

In the example above, we could then call 'add_two_numbers' with either two integers or two floats without a problem, and the appropriate function would be called. Similarly, we could, if we wanted, make a function with the same name that just takes completely different parameters, and the compiler would choose the right one, spitting out an error if it can't find one which takes the parameters we're giving it. This concept is very important when creating bigger applications and is used all over the place in the world of C-programming.

The other way we could address the 'issue' of the wrong number of parameters being passed (although not the wrong type), is by using default parameters. This is essentially where we can just give a value to use for a parameter if none are specified, so in our 'add_two_numbers' function we might want to default the values to 0 and 0 if no parameters are passed to it -- this can be accomplished via an equals sign in the function parameter declaration brackets:

Dev Command Linux

In the above we could just call add_two_numbers (); with no problem. It's important to remember that after the first default parameter in a function declaration (e.g. int value_one = 0), all preceding parameters must also have default values assigned to them (and if you think about it, this makes sense).

It heavily depends on usage situations, but function overloads are often preferable to default parameters in situations in which overloads don't cause a whole bunch of code repeating -- this is because mixing default parameters and function overloads can mess things up a little bit. Consider the following in which the compiler doesn't know which function to use:

Your compiler should spit out an error in cases like this - something along the lines of 'ambiguous call to overloaded function', but be careful as this can really mess up your code and cause a lot of confusion!

Dev c++ pdf tutorial

Dev C All Commands

To tie off this tutorial, let's just touch on a concept called recursion. Recursion, in the context of functions, is where a function calls itself. This concept can seem a little bit confusing at first, however it shouldn't be totally alien to you. Usually it's best when people figure out recursion for themselves, so all I'm going to do to help you along is show you the following example of recursion:

Dev C++ Commands

Recursion can be a little mind-bending, and in the above example isn't entirely necessary, but it's a very important topic when programming and is the best solution in a number of situations. If you don't understand the above code snippet, try running through the program line-by-line, starting from the 'countdown' call in 'main', and just do exactly what your program would do upon execution in your mind. We've covered a lot in this tutorial so you should be doing a lot of experimentation to test out the different things demonstrated -- none of the concepts covered should be hard as nails, but you need to make sure you fully understand them before moving on to the next tutorial (as is the case with most of the tutorials in this course - each tutorial builds on things learnt from the last).