.c and .h scripts - what is the difference?

I’m totally new to “C” and I’m a more than bit confused.
I see there are any number of scripts with the same name but either with the .c or .h extension in the example I downloaded to see what is going on.
I also looked in some of the scripts in the “donor” scripts and only found .h references (so far anyway).

Question 1:
What is going on there?

Question 2:
What is the CORRECT folder structure to be used for a project?

When I first created the sketch and saved it, it created 2 folders, “overview” and “data”.
What should be placed in these folders?

My project is named “afbot” and the script “afbot.ino” has almost a 1000 lines of comment about what I want to do and ideas on how I can possibly do it but it only has one un-commented command which is “#include <afbot.h>”.

Question 3:
Should “afbot.h” be in the same folder as the “afbot.ino” script and if not, where should I place these scripts and how do I make sure that the “#include <afbot.h>” command can find it?

In some languages there is a set of paths that the compiler will look in to find an included script.
Is C like that or would I have to modify the “#include <afbot.h>” line to get the compiler to the right script if it should be in a different folder/sub-folder?

I first got into programming in 1985 and bitter experience has taught me that if you do not do things as you should from day 1, sooner or later something is going to bite you in the butt and I do not want to make those sorts of mistakes when learning C.

Question 4:
Languages such as PHP allow you to create a “class” object so that you can store related information or commands in a structured and defined place.
You simply create a new instance of the class for each requirement.
I know that “normal” C can implement that sort of object orientated approach, can the flavor of C that will run on a DUE support that and if it does, is the overhead required to implement such objects much greater than doing it the “old fashioned way”?

The project is CNC related but I have 6 axis (so far but it is probably going to be 7) to work with so creating an axis object and creating a copy for each axis makes big sense to me instead of inline data and code to do what has to be done.

I have put a brief synopsis of the project in my profile if anyone is interested.

Thanks for reading my question and ANY good advice will be extremely welcome.

Sorry people but it seems like the synopsis got truncated! The project is a swiveling robot arm with a router end effector and a turntable for the work piece. First got the idea a long time ago but after blowing up about 30 expensive (back then) PIC chips I gave up. When Arduino came on the scene I had a look but the Uno was too light for all the reverse kinematics calculations. Recently I discovered the Due and decided to dust the project off and give it a go.

Here is what I originally posted on my profile - maybe it is still in for moderation...

I am a retired engineer and website back end developer. Started coding in 1985 but never in "C" ;>{ In 2007 I got my project idea but I am "electronically challenged" and after blowing up more than 30 expensive PIC controllers the project got shelved. Then Arduino came along with the Uno but the specs were too "lite" so the project remained shelved. When the Due came along I dusted the project off and bought one. So what is my "project"? A 3 segment robot arm mounted on a swivel base with a wrist and articulating router as the end effector along with a rotary table. All driven by G-Code !!! All based on the Scorbot 3 design with its positioning motors in the base and belts running up the arm to actuate the joints. I want to be able to carve statues out of solid wood. Ambitious? YOU BET !!! Just look at my "handle"... The mechanical side is the easy part for me as I have constructed many machines in my career. Now that the Due is available, most of the electronic section is sorted. Now I need to develop the program (I believe the correct term is "sketch") to take G-code, process it and control the arm. However, to do that needs "reverse kinematics" which I could not wrap my brain around until I found Angela Sodemann on YouTube - SHE IS "THE LADY"... and very helpful !!! Check her channel out. I then installed the latest version of the IDE (1.8.4) and now I have to learn how to do things. I downloaded GRBL to see how they do things and as a total newbie to "C" I am in dire need of mentorship. When I learn a new programming language I do not simply copy/paste someone elses code (well, sometimes I do) until I know the "what,when,where, how and why" of it. And that applies to the language commands as well. I need to understand stuff... So look forward to some seemingly dumb and idiotic help requests from me if I cannot find things out.

Welcome on board

for your questions I think the best answer is to go read the basics of C and C++. There are tons of resources on line, such as this one (not a recommendation, I learnt C and C++ long before the web was there so did not read this :) )

For your questions about the .h files, have a look at Class code and header files for example

PS/ we don't call those "scripts", but really program source files.

Ok.

The .h stands for “header”. It is often the case that if a program is made from a bunch of separate .c files, then they need to share some common definitions and whatnot. These common definitions go into a .h file. Then each .c file will “include” the .h file using an #include directive. The #include directive basically reads the .h file and treats it as if it were written into the .c file being compiled.

The arduino programming environment comes with some standard libraries, each with a .c containing the bulk of the code and a .h containing the stuff that any one using that code needs to have.

Ideally, you don’t have mush code in a .h file. I mean - there’s nothing stopping from putting all your code in a .h and then having a .c that’s just one #include directive, but that’s not what .h files are for. A complication is that C++ was written after C, and consequently class definitions often need code in them.

An .ino file is a .c file that is treated specially by the Arduino programming environment. The Arduino builder does some stuff and then after that stuff is done the result is compiled as though it were a .c file. IT does this because … reasons. Basically, the damn engineers that use arduinos can’t be bothered to properly forward declare their functions (like how real C programmers have been doing for decades). The IDE does it. Mostly, the IDE gets it right because it isn’t really all that hard to do.

All of the c files - the ones you write, the ones in the libraries, the processed .ino - then get “linked” together into a single big executable program suitable for upload to the microprocessor.

MadAubrey:
My project is named “afbot” and the script “afbot.ino” has almost a 1000 lines of comment about what I want to do and ideas on how I can possibly do it but it only has one un-commented command which is “#include <afbot.h>”.

Question 3:
Should “afbot.h” be in the same folder as the “afbot.ino” script and if not, where should I place these scripts and how do I make sure that the “#include <afbot.h>” command can find it?

Yes.

There are two forms of the include directive. If you use quotes, the compiler will look for your include file in the source directory, and then in the places where library files are kept. If you use angle brackets, then the compiler will only search the standard libraries.

So:

  • put your afbot.h in the directory where your afbot.ino is; and
  • Use
#include "afbot.h"

at the head of your .c file.

MadAubrey: Question 4: Languages such as PHP allow you to create a "class" object so that you can store related information or commands in a structured and defined place. You simply create a new instance of the class for each requirement. I know that "normal" C can implement that sort of object orientated approach, can the flavor of C that will run on a DUE support that and if it does, is the overhead required to implement such objects much greater than doing it the "old fashioned way"?

Yes. The arduino compiler is a C++ compiler, and C++ has classes.

The main overhead in using classes is that every (non-static) class method will have a "hidden" extra parameter - a reference to the instance that it is being executed against. All accesses of instance variables will be offsets against this reference. There is some overhead associated with this.

For a PHP programmer (and for a java programmer), you will need to get used to the fact that classes are not key/value maps. They are chunks of memory, and the class variables are offsets into the chunk. Most especially, a class variable inside another class variable is present by value, not by reference. This affects assignment semantics. Of course, C++ has references too, but you have to explicitly use them.

There are many, many pages on the topic, including my one in my sig block below. If you are not a C++ programmer, you just might want to avoid them for now. But, looking at your response further it's clear that you will probably need to go there.

I'd suggest doing a C++ tutorial and doing the tutorial sketches in the IDE (make an LED blink) before attacking the GRBL codebase, which I guess is a G-code interpreter that you will need to hack up a bit to apply it to your specific hardware.

Hi J-M-L Message understood! But I think I am still going to have some "dumb" questions on the future - please be patient with me. But I will get there - eventually. It took me more than 3 or 4 years to properly understand the how and why of reverse kinematics. Thanks.

No pb - take your time !

PaulMurrayCbr
Now the water is a lot less muddy!
The ino file is where the whole application starts off from (much like index.php)
The .c program source files can be stored in different folders (to make things easier and neater) but then the “include” directive needs to include the path to the resource so that the compiler can find it.
The .c contains the higher level code and (if the .h is needed) must include the the include directive for the .h code which contains the lower level (and sometimes long and complicated) code.

As an aside, see that the .h script has the following:

#ifndef gcode_h
#define gcode_h

#endif

This looks like it is emulating the PHP “include_once” call to prevent all sorts of “duplicated” errors when compiling and you have included a script twice by accident somewhere.

Classes:
I use classes all day every day and am very comfortable with using objects.

There were 2 reasons I “waited” until a board was available that had better specs than the Uno:

  1. I knew I would be needing a much more powerful CPU as far as speed is concerned to handle all the math involved with reverse kinematics.
  2. I knew that because of the complex nature of what I want to develop that it would not fit into the Uno program memory which is I suspect why GRBL has not gone any further recently as there is no more space to put anything more into the program. The program memory available on the Due is also much more than on the Uno.

But that brought up the whole 5v / 3v3 issue but my nephew has assured me that that is easily sorted out.

“All” he wants is for me to build him a copy of my final arm when the project is over which is fine with me as long as he supplies the motors (hacked windscreen wiper motors set up as servos).

PaulMurrayCbr, thank you very much for the help and advice - and now I have a few tutorials to go through.

All the best
Aubrey

I downloaded GRBL to see how they do things and as a total newbie to “C” I am in dire need of mentorship.
Question 1: What is going on there?

GRBL is an Arduino “library.” This means that:

  • It’s directory structure is NOT AT ALL like an ordinary Arduino user “sketch”
  • It contains examples as well as code intended for use by user sketches. The Examples are actually like normal user sketches, aside from being buried in the library directory structure.
  • The .ino extension is for the user sketch itself, and libraries will not contain .ino files - they’ll have .c, .cpp, and .h files instead (except in the Examples.)
  • Creating libraries is normally an “expert” task, so the documentation is harder to find and more cryptic.
  • You probably should NOT look too closely at a library, if you’re trying to figure out how to create a normal sketch.

Question 2: What is the CORRECT folder structure to be used for a project?

Normally, if you create a new sketch (FILE/NEW/SAVE) you’ll be prompted for a sketch name, and the IDE will create a directory with that name containing a single “xxx.ino” file, where xxx also matches the name you provided. Arduino is very fussy about the directory name matching the .ino file name.
You can then add additional files of various types using the “new tab” menu (or create them with an external editor, in the same directory as the .ino file.) You can also use the SKETCH/ADD LIBRARY menu, for libraries that you have already added to the IDE using the library manager (or that came with the IDE or a board support package.) Using a library does not add files or change the directory structure of the Sketch folder itself.

When I first created the sketch and saved it, it created 2 folders, “overview” and “data”.

Thats … very strange! Which IDE are you using?

My project is named “afbot” and the script “afbot.ino” has almost a 1000 lines of comment about what I want to do and ideas on how I can possibly do it but it only has one un-commented command which is “#include <afbot.h>”.

Typically, an Arduino sketch would contain all of the code within the .ino file, and only use #include to reference libraries. As sketches get more complex, this may be undesirable, but it sounds like the ways typically used to split a sketch or C program are different than what you’re used to.

  • other .c and .cpp files in the same folder as your sketch are compiled separately and “linked” with the binary of your .ino file (plus libraries, plus the arduino core) before it is uploaded. Thus, support functions can be put in separate files. The separate compilation means that you’ll have to include various system definitions (#include <Arduino.h> and more) as well as your own code.
  • additional .ino files can be created in the same directory, and they’ll be concatenated (in source form) with the main sketch file BEFORE preprocessing and compilation. This has certain advantages and disadvantages.
  • The style you see with some languages where your “top” file includes a bunch of separate source files for different functional units is generally not done in C, C++, or Arduino. Mostly this is because the combination is done after compilation, at link time. Some of it is just stylistic differences.
  • The .h “header” files that you DO see include are usually supposed to be files that “define” things, but don’t actually implement anything, and theoretically don’t generate code or data by themselves (again: stylistic!) You mentioned classes - a C class or structure would typically be defined in a .h file, but any actual objects of the class would be created in .c, .cpp, or .ino fles.
  • #include” is a strict text file inclusion, so the file that it references can have any name. .h is traditional for C defintions (as in (4)) You COULD do #include “subfunction.c”, but it would be counter to prevailing style, and it would break arduino sketches because subfunction.c would be included in both the source of whichever file, AND compiled separately as a .c file by the IDE.
  • Some of the issues you are running into are due to the differences between an interpreted language that has to have access to all of its “libraries” in source form, vs a compiled language that prefers binary libraries.

In some languages there is a set of paths that the compiler will look in to find an included script.

In Arduino, those are provided automatically by the IDE based on which libraries you’ve used, and the paths are not user-changeable.

The .c contains the higher level code and (if the .h is needed) must include the the include directive for the .h code which contains the lower level (and sometimes long and complicated) code.

No it's really best to only consider the .h as declarative and keep the code into the .c or .cpp files. Potentially simple code could make its way there - but remember the "h" is for "header". You might want to read about "Declare vs Define in C and C++" and think of the .h where you would declare many things and the .c where you would define them.

This looks like it is emulating the PHP "include_once" call to prevent all sorts of "duplicated" errors when compiling and you have included a script twice by accident somewhere.

you got it. that's a smart trick to avoid including many times the same stuff. (two .h that you need might refer to a shared third one that you need too... this way it's included only once

But that brought up the whole 5v / 3v3 issue but my nephew has assured me that that is easily sorted out.

you have a smart nephew :)

westfw:
GRBL is an Arduino “library.” This means that It’s directory structure is NOT AT ALL like an ordinary Arduino user “sketch”

I wasn’t familiar with GRBL. The whole point of a library is that you don’t go ferreting through the code - you just read the docs and it all works happily exactly as described. /s

MadAubrey:
The .c contains the higher level code and (if the .h is needed) must include the the include directive for the .h code which contains the lower level (and sometimes long and complicated) code.

No.

The .h file contains declarations - variables, functions, common types.
The .c files contain the code.

Here’s an example:

// FOO.H

class Foo { int bar; };

extern void adjustMyFoo(Foo &foo);

----------------------------------
// FOO.C

#include "foo.h"

void adjustMyFoo(Foo &foo) {
  foo.bar = 9;
}

----------------------------------
// MYPROG.C

#include "foo.h"

Foo someFoo;

void setup() {
  adjustMyFoo(someFoo);
}

MadAubrey:
As an aside, see that the .h script has the following:

#ifndef gcode_h
#define gcode_h

#endif

This looks like it is emulating the PHP “include_once” call to prevent all sorts of “duplicated” errors when compiling and you have included a script twice by accident somewhere.

Yup.

MadAubrey:
I use classes all day every day and am very comfortable with using objects.

Do keep in mind my warning that C objects don’t work the same as objects in scripting languages where everything is held by reference.