Nov 06, 2008 If your just opening the executable from the GUI then the program is likely running but exits before you get a chance to see it. Putting the getchar method before exitting will hold the program until you press a character. When I compile and run my programs in Dev C, the output window opens and shows the output. Then instanlty the window flashes and disappears. How do I make. If your program is reading input from standard input and you forgot to provide input via stdin. Your program contains infinite loop, which may never break. Your program contains infinite recursive function calls. May be your program is trying to process large data and it takes much time to process. Hello bro I have a problem and that problem is when i am in Dev-c and write my code then compiled and click run the program doesn't running and relying me 'consolePause.exe has stopped working'; how can I solve that problem guy's.
Linux Environment The C language is a whole lot older than GUI programming, Windows, or thePC. Its natural environment is running text-based programs from thecommand line. There is no standard windowing system for C, though manyare available as extra libraries. C's history is tied and Unix, and it runs very happily in that environment. On the PC, C compilers have been available since the DOSdays. The C++ refinement is newer, and has grown up in both places.
Windows Environment I/O Redirection Run-Time Errors Some Unix Commands
C/C for Visual Studio Code (Preview) C/C support for Visual Studio Code is provided by a Microsoft C/C extension to enable cross-platform C and C development on Windows, Linux, and macOS. Getting started C/C compiler and debugger. The C/C extension does not include a C.
Linux EnvironmentThese days,the most common version of the Unix environment is found in Linux(which is actually a Unix clone). It is available for free. Someleading distributors are Fedora andUbuntu. There is also adistributioncalled Knoppixwhich runs directly from a CD. It can also be installed on a hard drivealong side Windows without re-partitioning. Linux distributions come well-equipped with programming tools, includingthe C and C++ compiler. And pretty much anything else you need.
If you don't have your own Linux,you can use theDepartment's Linux server, Sandbox.Your instructor can get you a Sandbox account if you want one and don't haveone. You can connect to Sandbox using the ssh protocol. A Linux install willcome with the needed client; if you're using Windows you will can get Putty. Connect to sandbox.mc.edu and log in to run commands.
C or C++ programs are plain text files.To create one, simply fire up you favoritetext editor. Linux comes with several, both graphicaland text-based. If you're using Sandbox, you'll have to live with text-based. The simplest one of these is called pico.Log on with putty, and use the pico command tostart the text editor.This brings up a screen into which you may enter and modify yourprogram. When pico is running, the characters which you typebecome part of the program you are creating.To give commands to pico, you must enter a control character byholding down the Ctrl key and pressing another character.The list of available commands is shown at the bottom ofpico's screen while it is running; for instance, Control-O isthe command to write your changes to disk.
Other text-based editors are vi and emacs, both morecapable than pico, and with a steeper learning curve.Linux graphical editors include the simple gedit, or many others.
Want to be invisible when you're at Starbucks? How to stop little snitch. Lockdown by locationLittle Snitch has a multitude of customizable features, but one of my favorites is Automatic Profile Switching (APS), which allows you to create filtering profiles based on the network you're connected to. No problem, you can create a profile for that. Another sidebar on the right displays a Connection Inspector which you use to view general and detailed information about data being sent with specific information about the application selected and why it might be sending or receiving information.While viewing the Map or using Little Snitch's rules window you can select different apps and processes and use a small switch to allow or deny network traffic by flipping a small Rule Management switch.
An alternative is to edit your file locally and send it to Sandbox.If you're coming from Windows, a nice program iscalled WinSCP.This allows you manipulate files on Sandbox using the file exploreras though they were local. You can edit them locally, and the programsaves them on Sandbox. (Note: Sandbox doesn't support FTP anymore.You must tell WinSCP to use the SCP or SFTP protocols.)
However you edit, whether running on your own machine or on Sandbox,save your file with a name ending in .c for plain C, or .cpp for C++, like fred.c or sally.cpp.After saving, get to a command prompt. If you're using pico, you can just exit, but it usually makes sense to have a separate window for commands.If you're connectingremotely, just make a second connection. On your own machine, just fireup a command shell window.(There is a summary of common Unix commandsbelow.)To compile a C program named fred.c, say:
[bennet@sandbox test]$ gcc fred.c Dev c++ website.
(Of course, [bennet@sandbox test]$ is the prompt; you typethe rest.)To compile a C++ program, say
[bennet@sandbox test]$ g++ fred.cc
The g++ command actually runs the same compiler as gcc, butwith different options directing it to compile C++.(You can compile C++ programs with the gcc command if youknow a bunch of obscure options to type on the command line,or you can just let g++ do it for you.)
If the compiler finds an error in your program, it willprint appropriate messages; re-edit and re-compileto repair the error. If there are no problems,the compiler will produce an executable file with thenot-too-obvious name a.out. This contains the translation of your program into machine language.To run the program, simply type a.out:
[bennet@sandbox test]$ a.outThe program will read any input from the keyboard, and write outputto the screen (but see below).
Windows EnvironmentWindows comes well-equipped for word processing and solitaire, but it is just barely possible to write programs with it.C compilers are available for Windows;the most popular free one is probablyDev-C++.Dev-C++ itself is actually the development environment.Make sure to get the edition that includes Mingw/GCC, which is thecompiler. This is a Windows port of the same compiler used on Linux systems.The install is straightforward.
To create a program, use File/New/Source File. (Happily, Dev-C++ doesn't force you to create a project when all you want is one file.)Edit your file and save it some place you can find. It's not a badidea to create a folder for your C code.
You can compile your program from the Execute/Compile. You can alsorun it from Execute/Run, but that won't work well for most of whatwe'll write in class, since the programs run in a window which pops upand disappears along with your output. You will want to start acommand shell, and change to the folder where your program isstored. Programs compile to plain .exe files with the samestem as the original program; fred.cpp compiles to fred.exe.You can run them simply by simply typing the file name:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp. C:Documents and Settingsbennet>cd DesktopC C:Documents and SettingsbennetDesktopC>dir Volume in drive C has no label. Volume Serial Number is 31D0-F15C Directory of C:Documents and SettingsbennetDesktopC 08/21/2007 12:32 PM <DIR> . 08/21/2007 12:32 PM <DIR> . 08/21/2007 12:31 PM 97 fred.cpp 08/21/2007 12:32 PM 474,990 fred.exe 2 File(s) 475,087 bytes 2 Dir(s) 35,202,727,936 bytes free C:Documents and SettingsbennetDesktopC>type fred.cpp #include <iostream> using namespace std; main() { cout << 'Hello, World!' << endl; } C:Documents and SettingsbennetDesktopC>fred Hello, World! C:Documents and SettingsbennetDesktopC> I/O RedirectionOne of the command-line joys is file redirection. We will use that often in class.This was created as part of Unix (adapted from Multics), andwas copied by DOS and successor command shells on Windows.
By default, the standard input facilities, such as C scanfor C++ cin,read characters from the keyboard.Likewise, standard output commands print onto the screen.Either (or both) of these can be changed by specifyingan input or output redirection.For instance, if you run your program with:
your program will read from the file named in.txt instead ofreading from the keyboard. No change in your program is required.The Windows command shell does the same thing, except you have thedistinctive program name instead of a.out:
C:Documents and SettingsbennetDesktopC>fred < in.txt
The same program can be run reading from the keyboard or from a file, depending on the presence of the redirectionspecification,
Dev C++ Program Examples< in.txt.Likewise, the program can be run so that its output will be placedin a file rather than printed on the terminal. For instance,will write its output to the file results.txt, rather thanthe screen. Input redirection can be useful when you have a long testinput you don't wish to type every time you run the program.You can use the text editor to place the input into a file, thenrun your program any number of times reading from that file.Output redirection can be helpful when you have a long outputyou wish to check carefully. After you create a file of youroutput, you can examine it with a text editor, or print it out.Run-Time Errors
[bennet@sandbox test]$ gcc fred.c
[bennet@sandbox test]$ a.out Segmentation fault (core dumped) [bennet@sandbox test]$
The notation 'core dumped' tells you that the system left a core file in your working directory. It's named core.nnnn,where nnnn is the process number your program had while it was alive.A core file can be read by the debugger; if you don't plan to use it,they are safe to delete.
The Windows version at rightis no more helpful, but somewhat more amusing.(Do you suppose that, if you send it a report, Microsoft willtell you what's wrong with your program?)
In either case, there's not much more than 'sorry, it broke.'One improvement is to add debugging outputto your program. If you do, be careful because core dumps andoutput redirection don't always mix well. Sometimes part of youroutput gets lost on the way to the file when the program does notterminate normally. This may lead you to believe the program died earlier than it actually did. If you need to capturedebugging output before a core dump, you may want to usethe script command.
Or, you can use the debugger, gdb. It can be useddirectly with the gdb command (on Windows you have to set the path first).But it's common to use it indirectly. On Sandbox, you might try the postmortem program, like this:
[bennet@sandbox test]$ gcc boom.c
[bennet@sandbox test]$ a.out Segmentation fault (core dumped) [bennet@sandbox test]$ postmortem Fatal error: #11: Segmentation fault. Usually a bad pointer or subscript. Call stack follows. Program died on line listed first. Line 1259 in vfprintf.c: Func _IO_vfprintf Line 31 in printf.c: Func printf Line ??? in ???: Func main Line 92 in libc-start.c: Func __libc_start_main Recompile with -g for more line number information. Suggestion: Check the arguments to your printf call(s). [bennet@sandbox test]$
Postmortem is showingthat the program died at line 1259 infile vfprintf.c, while running the function _IO_vfprintf.This is a system library function which was called from functionprintf at line 31, which was called from function mainin my program. But note that the line in the program isnot given. To get such information, you should compile your program with the -g option. Thepostmortem program suggests this.Thatlooks like this:
[bennet@sandbox test]$ gcc -g boom.c
[bennet@sandbox test]$ a.out Segmentation fault (core dumped) bennet 1006%postmortem Fatal error: #11: Segmentation fault. Usually a bad pointer or subscript. Call stack follows. Program died on line listed first. Line 1259 in vfprintf.c: Func _IO_vfprintf Line 31 in printf.c: Func printf >Line 4 in boom.c: Func main Suggestion: Check the arguments to your printf call(s). [bennet@sandbox test]$
Much better. The -g optiontells the compiler to leave extra information in thea.out file which postmortem can use.In fact, line 4 of boom.c is
printf('%s', 17);
which causes the program to die.
With Dev-C++, use Debug/Debug. If you haven't use the -g flag on your compile, it will offer to rebuild for you. You'll want to acceptthe offer. Then it will run the program and you'll see a pop-up message not unlikethe Unix one.You can clear this, and and click on the Backtrace tab nearthe bottom to show where the error occurs. (Make sure togo back to the Debug sub-tab and say Stop Execution when you are done,so Dev-C++ will do other stuff for you.)
The debugger can do a number of other things for you, suchas single stepping and setting break points. These are availableas tabs in Dev-C++, or as sub-commands if you run gdb under Linux orWindows. Use the help command to find out about the others.If you're running Linux locally, you can also use one of the GUIwrappers for GDB, such as ddd or kdbg.
Some Unix CommandsHere are a few Unix commands which you may find useful if youare running on Sandbox or on your own system.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |