ANSI C IDE?

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
Does anyone have a source for an ANSI C compiler/IDE? I am trying to find a link, but there are an overwhelming amount of entries! I have tried several already, but they are combination C/C++. I am reading K&R's C and would like to compile some of the examples and edit them, but the C/C++ IDE's tend to use different syntax and I don't want to get confused. For instance, Visual Studio's stdio library is names differently. I am currently looking at Eclipse.

Any suggestions?

Thanks!
 

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
C is a subset of C++. Typically, a C/C++ compiler will compile either depending on the extension you use. .c means it is a C file. .cpp means it a C++ file.

MS uses the standard stdio library if you are coding in C. You can also use it in C++ if you want.

Also, C compilers will typically an ANSI switch that will disable any non-standard extensions.
Bob
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
Not sure if this will help you, but I found a really nice, compact, free C (not C++) development system for Windows. It includes an IDE and other development utilities, and can build command-line and GUI executables for 32-bit and 64-bit Windows. I converted an old MS-DOS app to Windows with it, and it was very quick and painless. I highly recommend it for "plain C" development under Windows.

http://www.smorgasbordet.com/pellesc/

(Eclipse is widely used, but I found it unintuitive and hard to get to grips with. Perhaps I'm just thick, or too old, or not Unixy enough. I found the NetBeans IDE similar to, but much easier to use than, Eclipse.)
 

shumifan50

Jan 16, 2014
579
Joined
Jan 16, 2014
Messages
579
Eclipse works nicely under Linux, but is a bit of a nightmare when you start using extensions(like for C++, databases etc). Mostly it is a problem with versions and plugin compatibility, but if you stick with what is available under Ubuntu, then you should be fine (don't install from external sources as Ubuntu lags behind).

You can set the ANSI flag in MS Dev Studio to force ANSI compliance, but even ANSI compliance might be different from K&R as things have evolved a lot. Mostly what you will find as that ANSI C will compile without the flag set, but when the flag is set, checking is much more stringent. The problem with most compilers is to set the PATHs correctly to find the header and library files, so when you install it is best to let these default.
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
You guys are awesome! Thanks Kris, I am downloading that and I will check it out :)
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
I gave MS Visual Studios another try because the IDE looked simple and there was pretty good support info on the web.... turns out my main problem was a linking issue caused by the need for a SP1 update to the Studios prog. !!! That'll drive you batty :-(
If I hadn't any experience in C++ at all I think I would have quit!

This was quite helpful:

1. Open Visual Studio. From the menu bar select File -> New -> Project.
2. New project window will pop-up. There will be multiple project types. Search for Visual C++ (mine was under other languages) and select Win32 under Visual C++.
3. Select Win32 console application from the templates pane.
4. Enter the name for your project. Click "Next" button.
5. A new pop-up window titled "Win32 Application Wizard - <your project name>" will show up. <your project name> is the name you gave to your project in the previous step. Click "Next" button.
6. You should see "Application settings" in the popup window. Make sure that "console application" is checked under application type.
7. This is important step: Under additional options, UNCHECK "Precompiled header" and CHECK "Empty project".
8. Your project should be created by now (if everything went correctly). You should see 3 folders in your project -- header files, resource files and source files. If solution explorer is not visible, you can make it visible by selecting View -> Solution Explorer from the menu bar.
9. Right click on the "source files" folder from solution explorer. Select Add -> New item.
10. You will see "Add new item" popup window. From categories pane on the left, select "Code" under "Visual C++".
11. From the "templates" pane on the right, select C++ file. (Don't worry! see below)
12. This is important step: Create the name of the file with ".c" extension. For e.g., Program.c. Click "Add" button.
13. If the file is successfully added, you will see the name of the file under "source files" in solution explorer.
14. Double click the file you added from solution explorer. Type in your C code. Save the file.
15. From menubar, select View -> Output
16. From solution explorer, right click on the solution name and select "Build solution" to compile your project.
17. The output of the build will be displayed in the "output window" you opened in step 15. The very last line should be something like "1 succeeded, 0 failed, 0 skipped". This means, build was successful.
18. In your source code, right click on the very last curly brace and select Breakpoint -> Insert Breakpoint. You will see a solid red circle next to the curly brace indicating that the breakpoint is set.
19. From solution explorer, right click on the project (not the solution -- the project will be just below the solution) and select Debug -> Start new instance.
20. Your program will execute until the breakpoint and stop there. A console window will be opened (but it may have been minimized. Check your minimized windows). You can verify that whatever printfs you issued in your program appeared in this console window.
21. Select Debug -> Continue from the menu bar. You should see from the output window something like (xxx has exited with code 0. 0x0). This means your program ran normally.

Notes 1:
The main function should always be declared as
(a) int main(void)
or
(b) int main(int argc, char *argv[0])
Don't forget to insert "return 0" at the end of the program, as you declared "main" returning an "int".
Corollary to this is "void main()" or "main()" are not C standard and known to give problems.

Note 2:
Adding multiple source files to your project is easy - right click on "source files" folder in solution explorer. select "Add -> New item" or "Add -> Existing item".

Note 3:
It's been a while since I used explorer edition. If you don't see option to create new project under "File" menu, check under "Project" menu.


Note 4:
#include<stdio.h> worked for me. In case you see problems, try #include <stdlib.h>

main() does not work? I guess it has been superseded for whatever purpose with the need the assign an integer value to main and then return 0; ?
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
(This post is in reply to post #8 below!)

You definitely don't need to use a VM and Linux!

If you want to practice C programming on Windows, making executables that run under Windows, you can use MS Visual C, or Pelles C that I recommended above, or lcc, or any of many options. I believe there's even a free version of Borland C that compiles Windows apps! (I learnt C with the Borland compilers as well...) but I wouldn't recommend it.

Regarding the declaration of main(), in K&R the rules are pretty relaxed. ANSI is much more strict (and this is generally a good thing). The normal declaration is int main(int argn, char * argv[]). No '0' between the square brackets - that's a typo. That's for environments where there are command-line parameters. For a program that runs on the PIC, there is nowhere to get command-line parameters from, and no use for a return value, so the declaration would just be void main(void).

Programming for the PIC does not use .exe files. Files for download into PICs are in .hex format. And executables that run on Windows will not run on the PIC. The PIC has a few hundred bytes of RAM and a few k of ROM, an 8-bit architecture and a current consumption of a few milliwatts; a Windows machine has gigabytes of RAM, a 32/64-bit architecture and a power consumption of hundreds of watts!

To program the PIC, you need a "cross-compiler" - that is, a compiler that RUNS on Windows but COMPILES code for the PIC. There are several options but the general starting point is the XC8 compiler from Microchip, which is free for the basic version (with many of the optimisations disabled). It will be fine for your purposes.

Microchip's development chain runs under an IDE (integrated development environment) called MPLAB. The current version is MPLAB X ('X' as in 10) which is based on the NetBeans IDE (similar to Eclipse). MPLAB X and the XC8 compiler are free downloads from microchip.com. Install MPLAB X first, then install the XC8 compiler. You can test out your programs on a virtual PIC, but you will need a hardware device to program a real PIC. I'm using the PICkit 3 but they still make the PICkit 2 which has (or had) some advantages. Google PICkit 2 vs. PICkit 3.
 
Last edited:

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
sorry about that - wrong thread:

As far as programming is concerned:

I am having a hard time weeding through the plethora of information on C and C++ For the purposes of understanding and programming PIC's (not saying that I will be able to do it!!) what is the better learning environment? Should I download a VM, load linux into it and learn from the command line or can I do the same from the IDE in windows? If I was to code in windows, how would I build the .exe for the PIC's? I have noticed that some of the functions are different when run in windows - the need for an argument in main despite it being void? Are these features going to cause issues when compiled and built for the PIC? I thought the great thing about C was portability! I am somewhat lost and things have changed alot since my experience with C on Borland!! (talk about dated, eh?)

Thanks in advance for your advice and letting me rant. :)
 

shumifan50

Jan 16, 2014
579
Joined
Jan 16, 2014
Messages
579
C and portability:
The basic C language is totally portable, but has very little statements. The actual power comes with the libraries. Over the years many libraries have become 'standard', e.g. stdlib, however, not all these libraries are available on all platforms and in some cases, even when they are generally available on PICs for example, they require more memory than is available on some PICs, so they have to be avoided (e.g sprintf takes a lot of flash).
If you specifically want to learn C for PICs then you are best off using the Microchip (free) IDE MPLABX and Microchip compilers and libraries - it is an integrated environment for developing a program, compiling it, flashing it onto the target PIC and, in the bigger PICs, allowing interactive debugging; set break points and look at variables, but bear in mind that depending on where you set your breakpoints, it could disturb timing.
For learning C in general, Windows Visual Studio is fine, but be aware that the various IDEs differ wildly and if you intend to primarily develop for PIC projects, then it will be better to use MPLABX and get used to that interface.
If you use MPLABX it is also useful, although not absolutely required, to get a PicKit2/3 as it integrates nicely with the IDE for programming and debugging on a PIC. My PicKit came with a development board with example programs for the various stuff on the board: ADC from a pot, several LEDs and some buttons.

Note: PICs don't have .exe files, the compiler generates a .hex file that is flashed onto the PIC and run on the PIC. MPLABX automatically builds a .hex file to program onto the PIC.

Also note that PICs run NO OPERATING system, so Linux does not enter the picture. The MPLABX IDE runs on Windows, Linux and MAC. A PIC program normally has the structure:

Code:
//Initialisation code/functions

void main(void) {
    Initialise(); //calling intialisation code
    while(1) {   //where the program spends all its time - it is a never ending loop
         ......code to do what you want.....
   } //end while
}  //end main
 
Last edited:

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
Install MPLAB X first, then install the XC8 compiler. You can test out your programs on a virtual PIC, but you will need a hardware device to program a real PIC. I'm using the PICkit 3 but they still make the PICkit 2 which has (or had) some advantages. Google PICkit 2 vs. PICkit 3.

I have installed them, but haven't had a chance to run through them yet. I expect to have loads of questions soon! I checked out Dave Jones' take on the PIC3 and the equally funny reply from the folks at Microchip - it looks like they fixed some of the issues(LED and decontented material put back in), so the PIC3 is on the wish list, right behind a soldering station and multimeters ;-)
 
Top