Pure "C" Hello World

I have IDE 1.8.19 and it is not possible to compile classes (or C++) in .c files, command line option -std=gnu11 hints the reason. error: unknown type name 'class'; did you mean 'labs'?.

Far, far too lenient. 'nuff said.

Arduino IDE 2.x accepts all the same file extensions as 1.x. Every sketch must contain a .ino file, just as it has always been, but you can add .h, .cpp, .c, and .S files. This can be done easily by clicking the ●●● icon on the right side of the editor toolbar and then selecting "New Tab" from the menu.

If you find you are unable to do that, please provide detailed instructions I can follow to reproduce the problem and I will be happy to investigate. I just tried it out and found it works as expected.

What do you mean by "alphas"? The allowed characters are specified in the Arduino Sketch Specification:

https://arduino.github.io/arduino-cli/latest/sketch-specification/#sketch-folders-and-files

In fact, Arduino IDE 2.x currently allows more characters than provided for in the specification simply because the developers haven't gotten around to writing the code to enforce specification compliant file names. So any sketch filename you can use in Arduino IDE 1.x, you can use in Arduino IDE 2.x. If you find otherwise, please provide detailed instructions I can follow to reproduce the problem and I will be happy to investigate.

@SamBrownADK
I think you have struggled with this because of a lack of understanding of some basic concepts.
Things like

  • what does the IDE do.
    The IDE's main functions are to take your source code compile it, link with some startup & runtime libraries, upload the image built to a board, and provide a rudimentary serial monitor.
    The IDE is just a GUI based fancy builder tool wrapper. It takes your .ino source code module massages it a bit to be compliant C++ code then calls various gnu embedded tools to compile it and then link it with some gnu provided startup library code, as well as some libraries provide by the Arduino core platform to provide the portable Arduino environment (for the target board) like including all the Arduino APIs and which provide various i/o capabilities including async serial i/o.
    The IDE itself does not provide any printing capabilities; any output like serial i/o is performed by the board itself, and the IDE can display serial output from the board on its serial monitor.

  • The "language" used by Arduino
    With respect to this, the Arduino.cc team has made a confusing wreck of this that goes back many years.
    There is no such thing as "the Arduino language". The Arduino development environment for .ino files uses C++
    It is just that simple.
    All the APIs provided provided by the Arduino environment are implemented to work with your C++ code in your .ino file.
    What the Arduino environment does do with a lot help from the IDE, is allow users to write C++ code that is a bit "simpler" by allowing users to write C++ code that would not compile by allowing users to leave out certain things that are necessary to compile.
    The main thing being function prototypes for forward references.
    The IDE scans your .ino code and essentially corrects it to be proper C++ code, it also figures out (guesses) which libraries your code appears to be using so those can be compiled and linked into the final image.
    While some of the low level code Arduino environment platform i/o code might be implemented in C code, it is callable from C++ (which is your .ino code)
    As mentioned above, your .ino code is massaged by the IDE to make it compilable by the gnu C++ compiler. If you are calling things like Serial.print() you are using C++ and referencing the C++ Serial object in the serial library. Same as calling something like Wire.begin() in the C++ Wire library.

The basic concept of Arduino is portability. i.e. creating an environment that allows a user to write code to a defined set of APIs that can be built to work on any board that provides an Arduino core platform.
And they have done a really good job of that.
IF you step outside of the Arduino environment by not using the defined APIs and do things like direct register accesses, then you are no longer writing Arduino code and the code will no longer be portable across boards.
i.e. the IDE will allow you to build code that is not limited to using the Arduino environment APIs.

If you are wanting to not use the Arduino core/platform library for serial support, it is possible, but... It means that you must now write all the code that touches and controls the hardware directly and the code will no longer be portable across boards.

It is not a trivial task, and get quite involved if you want bi-directional support with interrupt driven buffering. You essentially have to implement much of what is in the HardwareSerial library.

This is not normally done in "hello world" examples.
For example, in the typical C hello world example from unix that goes back 50 years now

#include <stdio.h>

main()
{
    printf("hello, world\n");
}

There is a lot of library code that goes along with that to make the example that simple.
In the case of unix there is an OS, C runtime startup code, all the standard i.o. code, the printf() library, and more.

When using Arduino the Arduino environment, through the core platform library, provides a startup and runtime environment. The equivalent for Arduino is to use Serial.print() because that is what the portable Arduino environment provides.
While you can step out of that, it means you are on your own to write all the low level i/o code to control the serial h/w and also the higher level formatting output code that sits on top of that.

And if you REALLY do want to only use C, I'm not sure you can do that with the Arduino IDE, in that even if you put a main() in your .ino file, the IDE will still build it as C++

--- bill

You can put main in a .c file if you like. It is true that the IDE will always be compiling at least one C++ file, but your .ino file can be empty and you can implement the entire sketch code in the .c files, as was done in the nice programs @westfw shared.

Bill: I think I have a good handle on what the IDE (as a wrapper and provider of basic resources to be used at a high level, etc) is meant to do and provides. I wanted to be able to do a direct hello world to see the underlying code (and hack around with it) as i begin my journey in C. I just figured hello world was the traditional starting point.

I learn (even as a kid) by looking "inside" and taking things apart and mucking around (much to the chagrin of my mother and her Electrolux and my dad and his transistor radio).

1 Like

Bill: Yep... I realize that now. A correct analogy to the customary beginners hello world in the micro controller world is the "blink.ino": to actually have more that that is not a hello world program: it is more involved. But I did not realize all that till i tried (and received the help / advice / corrections / explanations of others here on this thread).

THIS IS 100% WHAT I HAD HOPED TO GET WHEN I STARTED THIS THREAD! There is no "Serial" or any other "Arduino" stuff in it that i can see. It appears (to my limited eye) to be C and nothing else. And it prints out "Hello World" on the Arduino terminal emulator. It uses almost no program space and global vars. To do the same "bear bones" hello world with Serial and setup() , main() so on (ie: a Arduino "sketch") uses much more resources.

Thank you Bill!! This is EXACTLY what i had hoped to acquire. But clearly this absolute minimum proves there is no straight forward "hello world" for a newbie given the way I had hoped to do it. A total beginner will grasp the "blink.ino" conceptually, this code is more involved.

So... now I have my jumping off point: only I realize now (as opposed to my OP) that the "jumping off point" realy isn't what I thought it would be / is a lot more involved.

Thank you Bill / westfa again!!!

@SamBrownADK,

I learnt C for microcontrollers using Microchip's MPLABX and associated C compilers. No danger of contamination with C++ because the compilers don't support it. If you do this then you will have to write your own drivers for the hardware.

If you read any of my code on this forum you will realise I mostly write as if I am using C, not C++. I realise C++ is not exactly a superset of C and sometimes the differences cause me problems, but never so much I can't figure out how to adapt my code to work.

Edit: your reply #95 appeared while I was typing this.

removed by poster

Code and schematic or it didn’t happen. :grinning:

I will pass: As I have seen earlier: it will only open a whole new can of worms with snide remarks.

I think people are pretty kind when someone shows that they are making an effort.

The way it’s used, This would be better as
void uart_putString(const char *s) {
or the compiler might issue a warning depending on settings.

After all this time, it's still not clear to me why you want to do this; I've more than likely missed it. Unless you write everything in "pure" C, I personally don't see the need to do this.

But to add a little bit of information: the datasheet of the 328P (and probably the datasheets of the other AVR based processors) has all the information (and examples) that you need :wink:

How much setup does MPLABX need to code in C?
How big is the D/L?

If I pay for PRO, my compiler will make faster code, it says. A flag is waving right there.

I've stuck with the IDE to help beginners here, even stayed for this CRAP version of the forum itself.

Good news, MPLABX doesn't -require- Winblows to run.

1 Like

Download it, install it.
Download the C compiler you want to use with it (different versions for 8, 16 and 32 bit micro-controllers) install that.
Create at least one C and one h file and away you go.

Be prepared to realise just how much hidden stuff is done for you in Arduino.h. You will be starting with the blankest of blank pages. There is a configuration tool to help you set up the basics you need for a micro-controller to do anything at all (clock speed for example). You will need a programmer, such as PICKit 4 (which, so far as I know, also works with ATMEL chips now Microchip own ATMEL).

Windows reports 3.72GB for the folder containing it.

It is a lot and I don't have huge time but Perry.. I expect that there is or will be some equivalent to the Arduino Playground from the ones who use this, or should be!

If I can remember, I'll PM you. Buty if I 'lose my keys' I may need a reminder.

Never found or used Arduino playground, don't know what it is. Don't know if there's anything equivalent for MPLABX. I learnt to program PICs on the old MPLAB in assembly language, then moved to MPLABX, again assembly. Then decided I needed to learn C. I had no clue about what C involved and was absolutely delighted when I first managed to read one port and copy what was there as output to another port. I did have the advantage of 2 friends who know more about programming than I ever will. Oddly the one who gave me the most help does not use or even like C, and knows little about micro-controllers.

You only need to learn your way around MPLABX and you need to read the data sheet for the target controller so you can figure out what to put in the various control registers.

There's GOLD in this hill! The fast DIV10 alone....

I joined the Forum and in the week or so before I got a board I went through the Arduino main site and bookmarked the links pages and what looked good.

Of course every so often the company rearranges the site so ALL your posts that have links become obsolete. They live in a fascist country now, karma over dogma.