A question about file extentions of tabs in IDE.

I'm starting to write larger sketches and finding out how important the organization of code is the larger a program becomes. I'm using the Arduino IDE to compsose my code and started using tabs to break the code into semi-managable chunks. When I named the tabs I didn't give any extention to the names (didn't even know I could). But the IDE would treat the code as if it were just one big file.

Then I learned a little about making them into header files and "#include"ing them into the main sketch which enables me to isolate the different sections of code and localize the variables within them. Very useful!

Somewhere in the learning section on the Arduino site I recently came accross this -

Tabs, Multiple Files, and Compilation:

Allows you to manage sketches with more than one file (each of which appears in its own tab). These can be normal Arduino code files (no extension), C files (.c extension), C++ files (.cpp), or header files (.h).

My question is, what is the difference/use of using the .c or .cpp extentions in the tabs of the Arduino IDE. Is there any benifit in doing that. I know that libraries have to have both .h and .c files. Would I be better off learning how to write libraries. Which, by the way, is a little confusing. I can't seem to find much about how to write libraries that doesn't asume the reader has a fair knowledge of C language.

A little babbling about myself.
I've only been using C for 5-6 months. I used to write programs in machine language on my Commodore 64 (yes I'm that old). About the time windows 95 came out I started using PC's and there was so much software already available I didn't see the need to write programs anymore. But, I've been into electronics since I was 9. So when I came across the Arduino while browsing the internet the geek in me had to have one. Now I've got some ideas I'd like to develope, maybe even make a buck or two. So, now I have to start programming again ::).

P.S. I give thanks to the Arduino team for the devices they've created, which allow the rest of us to more easily and very affordably create our own devices of the future. (And thanks to everyone in the Open-Source movement)

what is the difference/use of using the .c or .cpp extentions in the tabs of the Arduino IDE.

I think that you've already learned the answer to that.

enables me to isolate the different sections of code and localize the variables within them. Very useful!

Isolating the code in separate files lets you concentrate, in one file, on one aspect of the program. Writing to an LCD? Why mix the code for that all through your code for reading a temperature sensor and wind speed indicator and lighting LEDs? Put it in one file, and call the functions in that file. If something doesn't work right, the LCD code is in one place, not spread all over the place.

Doing another project with the same LCD, but different input devices? No need to re-create the code to write to the LCD. You've already got that in a re-usable file. Also known as a library.

Thanks for the reply PaulS.
I need to clarify. I understand the use of .h as tab ext's. What is the use of/how do I use .c or .cpp as a tab ext. Just to see what would happen, I changed the extention of a tabbed file from .h to c. and changed the #ifndef, #define, #endif and #include to match and got errors(as I expected to).

A .h file (header file) is typically used to contain class definitions and/or function prototypes, depending on whether the code is C or C++.

The .c and .cpp files are where the implementation of the class or the functions go.

You would typically have a .c/.cpp file for each .h file. There are exceptions, when the header file just contains array declaration/initialization, for instance.

You include the .h file in the sketch, and the .c/.cpp file. When included in the sketch, the compiler determines that the corresponding .c/.cpp file needs to be compiled, too. This implies that the name of the .c/.cpp file must match that of the .h file, and that they must be in the same directory.

Thanks for the quick replies PaulS.
I'm pretty new to C, but I think I'm picking it up fairly quickly. If I understand you correctly, I put my function definitions into an .h file. Then my use of those functions into a .c/.cpp file. Is that a correct way of putting it? Or am I misunderstanding what you mean by implementation? What I've been doing is to put related function definitions and as much of the use of those functions as I can into a header file only.

Thanks again for taking the time to explain things to me. Is there any literature on this that is easy to understand. I pick things up quickly, but I've noticed when trying to research some new aspect of the C language, there's alot of stuff for beginers (very basic) or advanced users (very technical/unfamiliar terms used). I'm somewhere in the middle.

Again thanks for any help.
DigitalJohnson

Yes, the function definitions (or class definitions) go in the header file, and the function (the code that implements the defined function) goes in the source file (the .c/.cpp file).

I've noticed when trying to research some new aspect of the C language, there's alot of stuff for beginers (very basic) or advanced users (very technical/unfamiliar terms used).

When trying to create documentation, you have to decide if you want to explain all there is to know about a function (reference documentation) or how to use the function to perform some specific task.

If that task is not what you want to do, then that sort of documentation is not so useful.

The only way to see how documentation for using a particular function one way can help you use it another way is experience.

Beginner documentation explains how to use function X to perform a specific task. Advanced documentation either is reference material - here's what this function does, and what it's inputs and outputs are - or esoteric.

What makes esoteric understandable is experience.

I never thought of it that way. So, as an intermediate programmer, what I'm really looking for is more examples of use (the experience of others (people like you)). :stuck_out_tongue:

I'm off to work now. Thanks PaulS,
DigitalJohnson