User avatar
woodystanford
Posts: 122
Joined: Sun Feb 19, 2017 8:17 am

Learning C in 5 minutes

Sun Feb 26, 2017 8:47 am

OK, listen up. Python people been saying they want to know C/C++. I'll have you a C programmer in 5 minutes (maybe a little longer...ten minutes).

If you are Windows, I want you to download (its free) MinGW and install it. It has the GNU C compiler aka "GCC" in it...THE compiler. If you are on UN*X then GCC is already there...just type gcc and see what it says. Amazing, huh? (Remember UN*X is case-sensitive)

OK C is a compiled language...this means its fast as the dickens. Interpreted languages are slower because they keep on reading from the "source" or "source code" to execute the program. A compiler does this once and expresses the resulting "executable" in machine language. We won't talk about the linker at this stage, just know there is a linker. The UN*X kernel is coded in this language.

The reason why you feel that C is throwing you is because of the ++ part. So what we'll do is focus on just the C part. C++ came later and when you attach all of these advanced concepts, a lot of people believe that it is beyond their capability. But in the beginning, there was only C.

Open pico and start typing...

OK, what you put in any C program is this line at the very top. This is a core element of our UN*X faith.

#include <stdio.h>

This gives you the ability to read and write to "streams" (important concept in C) of which is stdout (standard out) and stdin (standard in). When you are on the command line, whatever you output goes to the screen, and whatever you input is what was typed on the keyboard. All other devices work on the same principle.

Then you put in the main routine, like this:

#include <stdio.h>
main()
{

}

this is the minimal C program, and it does nothing. Let's make it do something...

Put this line between the squiggly brackets:

printf("Hello there human\n\n");

Printf outputs text to your "terminal" or your CLI (command-line interface) so very important to know and use. This constitutes you very first program in C. It does something albeit quite minimal.

Save your program (in pico its with a <ctrl>x and look at the bottom of the screen) to file, with name test.c, and exit out to shell.

OK to execute it, you have to compile it. There is a reason why I said to use GCC at the beginning and you will see why. Now type

gcc test.c -o test

(in Windows: gcc test.c -o test.exe)

The -o "switch" specifies the name of the executable file (your test.c file is called a C file or your source file). That is why that's there. If you typed in the program perfectly then it should compile right.

Then you execute your program with:

./test

(in Windows you can just type "test")

Most of the time you'll find that your programs don't compile without errors the first time; this is normal. Then you have to "debug" its called...basically going back into pico and double-checking everything ... typos ... syntax errors ... errors in logic. Again, this is normal...in fact abnormal if you are THAT good lol. Then just recompile.

Now you know the main elements of C...

(Why are people afraid of C? Because of C++ and all of the things that are attached to it standard. The amount of work and understanding that goes into just printing out a single line of text is monumental because you have to manage your windows, buttons and such, as most computers these days are GUI. GUI's are actually pretty monsterous under the hood because its a complex process to time and coordinate everything correctly, using events, objects, properties, methods and interfaces. If its your job and you go to school for it, then not so intimidating, however if you are just interested, quite the hurdle. C, the base language, is actually pretty easy to learn and understand.)
Last edited by woodystanford on Tue Mar 21, 2017 11:15 pm, edited 1 time in total.

User avatar
woodystanford
Posts: 122
Joined: Sun Feb 19, 2017 8:17 am

Re: Learning C in 5 minutes

Sun Feb 26, 2017 9:12 am

OK, you programmed your first C program in about 5 minutes. Coolness. How about input? How do I get something in from the keyboard. Enter function scanf()...I guess the opposite of printf.

Here is how scanf works. I want you to cut and paste this code into a new text file (let's call it test2.c).

Code: Select all

#include <stdio.h>
main()
{
   int a,b,sum; //note that this only adds whole numbers
   printf("Please enter the first number you want to add: ");
   scanf("%d",&a);
   printf("Please enter the second number you want to add: ");
   scanf("%d",&b);

   sum=a+b;

   printf("The sum of %d and %d is equal to %d\n\n",a,b,sum);

}
OK a bunch of new elements have been introduced. I told you I would have you programming quickly so let's cover what each of these things do.

For now, have kind of a cut and paste view on these "functions". "How I read in an integer is by typing:
scanf("%d",&a); " Usually a teacher will cover why all the strange symbols, however if you just remember that to get a number into variable a, you type that, you are good for now.

To read in a sequence of letters and/or numbers, called a "string" (just what its called), you type this: scanf("%s",str1); where the string is saved in variable str1. I want you to look at the stange symbols between the two versions of scanf and see if you can logically figure out what's going on (ie. why the symbols). If you can't, that's cool and I will explain.

Variables:

C requires you to "define" your variables before hand (a lot of computer languages don't but in the long run C is right that you should for reasons of clarity).

The line: "int a,b,sum;" defines the variables a, b and sum as ints or "integers" (positive or negative whole numbers within the range something like -30,000 to 30,000...so just about every number you would find in an everyday situation). There are variable types that allow you to put in decimals and stuff.

Here is a declaration that allows you to use a string large enough to hold 255 characters: "char str1[256];"

Arithmetic straightforward in C. You just type the math operation you want to perform. "sum=a+b;" just adds the contents of variables "a" and "b" and puts it in variable "sum". Study it out in your mind how it works. An example of a more complex arithmetic: "c=sqrt(a*a+b*b);". Can you figure out what this means and which famous equation it expresses?

So now you know how input and print something...and how to do basic math with C.

jahboater
Posts: 4686
Joined: Wed Feb 04, 2015 6:38 pm

Re: Learning C in 5 minutes

Sun Feb 26, 2017 9:26 am

I have often thought that each language in the Programming section of this forum should have a sticky at the top showing how to create, build if needed, and run, a simple hello world program. Just to get people started.

Most people on the Pi know pico as "nano".
The line: "int a,b,sum;" defines the variables a, b and sum as ints or "integers" (positive or negative whole numbers within the range something like -30,000 to 30,000...so just about every number you would find in an everyday
Might as well say: -2147483648 to 2147483647 for the Pi - and any normal modern computer.

User avatar
r3d4
Posts: 967
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: Learning C in 5 minutes

Sun Feb 26, 2017 9:46 am

*
Last edited by r3d4 on Mon Mar 20, 2017 7:32 pm, edited 1 time in total.

User avatar
woodystanford
Posts: 122
Joined: Sun Feb 19, 2017 8:17 am

Re: Learning C in 5 minutes

Sun Feb 26, 2017 10:17 am

J: Might as well say: -2147483648 to 2147483647 for the Pi - and any normal modern computer.

W: I will have you know sir *faux arrogance* that that is a "long integer" in C. A C integer is only -32K<=x<=32K and the unsigned version is 0<=x<=64K.

"Longs" are defined with "long". And J is correct.

J: Most people on the Pi know pico as "nano".

W: Yes (but most people find that strange).

J: Recently i found this : ryanmjacobs/c -Compile and execute C "scripts" in one go!

W: I checked it out. My first instinct was "This is VERY cool." then I realized that Mr. Torvaldes would see my soul rot in hell for thinking that.

jahboater
Posts: 4686
Joined: Wed Feb 04, 2015 6:38 pm

Re: Learning C in 5 minutes

Sun Feb 26, 2017 10:46 am

woodystanford wrote:W: I will have you know sir *faux arrogance* that that is a "long integer" in C. A C integer is only -32K<=x<=32K and the unsigned version is 0<=x<=64K.
Those are minimum's from the standard, on the Pi and other normal desktop computers, int is always 32-bits.
On the Pi in 32-bit mode, long is the same as int (ILP32), but in 64-bit mode long is 64-bits (LP64) while int remains 32-bits.

For Intel computers, there is a small penalty for 16-bit numbers, but for the Pi (ARM) it can only load and store them, and all arithmetic is 32-bit regardless. "short" is 16-bits.

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: Learning C in 5 minutes

Sun Feb 26, 2017 10:55 am

I agree,
int on my Pi is 32 bits long (== 4bytes) , the same is long, and int32_t, and so is size_t,
i.e. te range is -2147483648 to 2147483647

just short is 16 bit size (== 2Bytes), and int16_t,
range is -32768 to 32767

and long long is supposed to be 64 bits long (== 8 bytes) (edit: == int64_t then of course)
range is –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Last edited by 1dot0 on Sun Feb 26, 2017 11:41 am, edited 1 time in total.

jahboater
Posts: 4686
Joined: Wed Feb 04, 2015 6:38 pm

Re: Learning C in 5 minutes

Sun Feb 26, 2017 11:18 am

1dot0 wrote:I agree,
int on my Pi is 32 bits long (== 4bytes) , the same is long, and int32_t, and so is size_t,
i.e. te range is -2147483648 to 2147483647
You have it, use <inttypes.h> where it really matters.

User avatar
rpdom
Posts: 15212
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Learning C in 5 minutes

Sun Feb 26, 2017 12:45 pm

woodystanford wrote:J: Most people on the Pi know pico as "nano".

W: Yes (but most people find that strange).
I don't see why. Nano is more commonly used than Pico nowadays. Not just in Raspbian/Debian.

User avatar
PeterO
Posts: 5023
Joined: Sun Jul 22, 2012 4:14 pm

Re: Learning C in 5 minutes

Sun Feb 26, 2017 12:46 pm

woodystanford wrote:OK, listen up. Python people been saying they want to know C/C++. I'll have you a C programmer in 5 minutes (maybe a little longer...ten minutes).
As a C programmer with over 30 years experience I can only tell you that you are delusional if you think you can teach anyone C in 5 or 10 minutes.
Now you know the main elements of C...
:lol: :lol: :lol: :lol:
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
DougieLawson
Posts: 36158
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Learning C in 5 minutes

Sun Feb 26, 2017 1:52 pm

PeterO wrote: As a C programmer with over 30 years experience I can only tell you that you are delusional if you think you can teach anyone C in 5 or 10 minutes.
As someone who doesn't write C for a living (I'm one of those wonks that still writes S/370 (aka zSeries) assembler), but has hacked around with it for ten years or more. I wholeheartedly agree with Peter. Five or ten years is more realistic. Every time I go anywhere near a C program I learn something new.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Learning C in 5 minutes

Sun Feb 26, 2017 1:53 pm

rpdom wrote:
woodystanford wrote:J: Most people on the Pi know pico as "nano".

W: Yes (but most people find that strange).
I don't see why. Nano is more commonly used than Pico nowadays. Not just in Raspbian/Debian.
Pico is the editor that comes with pine and now alpine email programs - it's built into those, but it is available as a standalone package. However there are (maybe were?) licensing issues with it, so the FoS community dropped it and wrote their own called nano. If you check your Pi's then you'll see:

Code: Select all

pi@raspberrypi:~$ which pico
/usr/bin/pico
pi@raspberrypi:~$ ls -l /usr/bin/pico
lrwxrwxrwx 1 root root 22 May 27  2016 /usr/bin/pico -> /etc/alternatives/pico
pi@raspberrypi:~$ ls -l /etc/alternatives/pico
lrwxrwxrwx 1 root root 9 May 27  2016 /etc/alternatives/pico -> /bin/nano
Everyone should break their old pico habit and switch to nano.

-Gordon
--
Gordons projects: https://projects.drogon.net/

jahboater
Posts: 4686
Joined: Wed Feb 04, 2015 6:38 pm

Re: Learning C in 5 minutes

Sun Feb 26, 2017 3:50 pm

PeterO wrote:As a C programmer with over 30 years experience I can only tell you that you are delusional if you think you can teach anyone C in 5 or 10 minutes.
I agree, and ditto the 30+ years professional use of C.

On the face of it C is a fairly simple language, and you can learn to write small programs in a short time. But to write a large and robust program, that is correct, secure, portable, long lasting, resilient, easily maintained, etc, and fully understand the nuances of the C99 and C11 standards, takes many many years.

Having said that, there is a place for the little "hello world" program - the beginner gets to create/edit the program, compile it, and run it which is a good start before learning the language. Some people by the sound of it don't even know that Raspbian contains a (good and free) C compiler.

User avatar
woodystanford
Posts: 122
Joined: Sun Feb 19, 2017 8:17 am

Re: Learning C in 5 minutes

Mon Feb 27, 2017 5:09 am

Anyways, let's talk about language constructs. There are no idiots here so I'll just talk about them:

You've heard of loops. We have a few of them for you.

FOR Loop

for (a=0;a<1000;a++)
{ [statements here] }

This runs the statements in the squiglies 1000 time. Can you figure out why? We use integer variable a to keep track of the current count, and the first term in the brackets (the a=0) sets the inital value for a. The second term is how many times it gets looped (technically as long as this term stays logical TRUE it loops). The third term increments the variable a for each iteration.

WHILE Loop

While loops are simple to understand:

While ([expression])
{
printf("You are in a WHILE loop\n");
}

While the [expression] is true, it will continue to execute the statements within the squiglies.

There are no other loops than this. :D

Well, one more, and you can pull this one out on your buddies:

a = 10;
label1:
if (a!=0)
{
a--;
[put statements here]
goto label1
}

This will run the [statements] ten times. Can you figure out logically what != means in C? Also remember that to measure the equality of an expression the operator is == . A single = means "assignment" (ie. a=1+3*4;)


Let's talk about conditionals. There is only one conditional in C and that is the IF statement.

if (x==0)
{ [runs this code if x is equal to zero] }
else
{ [runs this code if x is not equal to zero] }

Can you figure out why?

You can run an IF statement without the ELSE btw, like this:

if (x==0)
{ [runs this code if x is equal to zero] }

Forget what I said about there not being more than one conditional. I was just joking, you know that.

Check out a SWITCH:

switch(c)
{
case 'A': [put statements to be run here]
break;
case 'B': [put statements to be run here]
break;
case 'C': [put statements to be run here]
break;
case 'D': [put statements to be run here]
break;
default: [put statements to be run here]
}

This is so simple you can easily figure it out. You are a smart person! You might not realize how this now but the switch will become your best buddy.

OK, we are about half the way there. And then you will know C language....the actual core language.

If you have any questions, feel absolutely free to ask them. I only request that you think it through first and then when you get stumped then ask.

User avatar
woodystanford
Posts: 122
Joined: Sun Feb 19, 2017 8:17 am

Re: Learning C in 5 minutes

Mon Feb 27, 2017 5:58 am

Where are we now? Here are the reserved words in C:

auto break case char const continue default do double else enum
extern float for goto if int long register return short signed sizeof
static struct switch typedef union unsigned void volatile while

Ok here are the words you can ignore: auto, const, continue, maybe do (who uses do loops?), enum, extern, register (oh god!), signed, sizeof, static, typedef, union (oh my goodness!) , unsigned, maybe void, definately volitile

So we have covered break, case, char. ,default, double, else, float, for, goto, if, int, long, switch, while

Which leaves: return, struct (ok and you'll hate me for explaining struct, that's an advanced technique) and a few other "language constructs" or whatever they are called.

I guess we are more than half the way there. Serious serious, after this you can tell people you know C language. I promise.

RETURN Statement

return returns you from a function (we still have to talk about functions, main() is technically a function so you already know how to do functions...kinda). Just remember to return from a function call you just type the word "return".

We won't cover STRUCT now because you brain really isn't rated to learn something like C this quickly so remember to take breaks and just let is sink in. Sleep on it.

functions()

Functions work like this:

int getcolorofsky(int hr)
{
}

Where the function's name is "getcolorofsky" and the variables you pass to it are in the brackets. Its like a math function where you have various symbols that all work together to return an answer. In this case the hr that we are defining for the purposes of this example to be the hour of the day.

You can pass back something like a color with a numeric code like BLACK=0, BLUE=1, YELLOW/ORANGE=2, WHITE=3 and so on and so forth.

Again you can use return to return to where the function was called.

In you code, you just call a function like this:

Code: Select all

#include <stdio.h>

int GetColorOfSky(int hr)
{
//maybe you can code this yourself...now you know how to do it
//but we'll return the hr as the color code so it will do something when you enter something
   return hr;
}

int main()
{
int hr;

printf("Please enter the hour of the day in military style: ");
scanf("%d",&hr);

switch(GetColorOfSky(hr))
{
case 0: printf("The sky is black\n");
break;

case 1: printf("The sky is blue today\n");
break;

case 2: printf("The sky is yellow/orange\n");
break;

case 3: printf("The sky is white\n");
break;

default: printf("Program doesn't support the color code\n");
break;
}

}
OK, then you already know how to compile and link, a la:

gcc test3.c -o test3 -lm

and execute:

./test3

addendum: ok got that problem in the code fixed. I do apologize. Working with several languages at the same time and I tend to slur them together sometimes.
Last edited by woodystanford on Wed Mar 22, 2017 12:33 am, edited 2 times in total.

User avatar
buja
Posts: 498
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: Learning C in 5 minutes

Mon Feb 27, 2017 6:17 am

It takes more than 5 minutes, but Simon Long already did a good job on teaching the basics on C:
https://www.raspberrypi.org/magpi/issue ... ials-c-v1/

(well, apart from the void main() ;))

User avatar
woodystanford
Posts: 122
Joined: Sun Feb 19, 2017 8:17 am

Re: Learning C in 5 minutes

Mon Feb 27, 2017 6:26 am

buja wrote:It takes more than 5 minutes, but Simon Long already did a good job on teaching the basics on C:
https://www.raspberrypi.org/magpi/issue ... ials-c-v1/

(well, apart from the void main() ;))
I am impressed.

Quickly skimmed this book and it seems really, really good (kind of along the lines of this tutorial thread) and will teach you C in minimum time.

Free download on it (and its the full version in PDF). Would recomment downloading it and stuffing it in your "bag of tricks".

Image

jahboater
Posts: 4686
Joined: Wed Feb 04, 2015 6:38 pm

Re: Learning C in 5 minutes

Mon Feb 27, 2017 9:54 am

woodystanford wrote:Where are we now? Here are the reserved words in C:

auto break case char const continue default do double else enum
extern float for goto if int long register return short signed sizeof
static struct switch typedef union unsigned void volatile while
and some new(ish) ones: restrict, _Atomic, _Bool, _Alignas, _Alignof, _Noreturn, and probably others I don't use.
woodystanford wrote:Ok here are the words you can ignore: auto, const, continue, maybe do (who uses do loops?), enum, extern, register (oh god!), signed, sizeof, static, typedef, union (oh my goodness!) , unsigned, maybe void, definately volitile
I give up!

Well quickly
"auto" and "signed" yes you can ignore. The rest are all very useful and very commonly used.
Who uses do loops ? you must know that do while is more efficient than while if you, or the compiler, knows it will be executed at least once. I suspect volatile is commonly used on the Raspberry Pi when dealing with memory mapped GPIO registers. Register, yes not generally needed except for some kinds of inline asm (svc calls require them).

The other keywords are too obviously necessary to discuss.

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: Learning C in 5 minutes

Mon Feb 27, 2017 10:33 am

jahboater wrote:Well quickly
"auto" and "signed" yes you can ignore. The rest are all very useful and very commonly used.
Who uses do loops ? you must know that do while is more efficient than while if you, or the compiler, knows it will be executed at least once. I suspect volatile is commonly used on the Raspberry Pi when dealing with memory mapped GPIO registers. Register, yes not generally needed except for some kinds of inline asm (svc calls require them).
The other keywords are too obviously necessary to discuss.
I agree, but IMO no need at all to discuss anything about that:
reserved is reserved is reserved, regardless of personal taste or habits or preferences.

User avatar
PeterO
Posts: 5023
Joined: Sun Jul 22, 2012 4:14 pm

Re: Learning C in 5 minutes

Mon Feb 27, 2017 10:40 am

1dot0 wrote:reserved is reserved is reserved, regardless of personal taste or habits or preferences.
You really don't understand! If you are going to teach C then teach C, not some personal preference sub-set that you think is all that is needed.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23686
Joined: Sat Jul 30, 2011 7:41 pm

Re: Learning C in 5 minutes

Mon Feb 27, 2017 11:29 am

woodystanford wrote:Where are we now? Here are the reserved words in C:

auto break case char const continue default do double else enum
extern float for goto if int long register return short signed sizeof
static struct switch typedef union unsigned void volatile while

Ok here are the words you can ignore: auto, const, continue, maybe do (who uses do loops?), enum, extern, register (oh god!), signed, sizeof, static, typedef, union (oh my goodness!) , unsigned, maybe void, definately volitile

So we have covered break, case, char. ,default, double, else, float, for, goto, if, int, long, switch, while

Which leaves: return, struct (ok and you'll hate me for explaining struct, that's an advanced technique) and a few other "language constructs" or whatever they are called.

I guess we are more than half the way there. Serious serious, after this you can tell people you know C language. I promise.
I am loath to interject when people are clearly trying to help, but there have been a number of reports on this and similar threads. I have cleaned some posts out of this thread.

When teaching it's important to ensure that what you teach is right. I have to take task with a number of statements above.

I would say that some of the reserved words you dismiss are VITAL to a C programmer who actually have to write C programs. Enum, struct, typedef, static, signed, void etc are all essential learning. They cannot be dismissed out of hand. Yes, there are some reserved keywords that are rarely used. These are not them!

You cannot "know" the C language without knowing those.

So please ensure that what you are trying to get across is right.


And why do I think I can comment on this? 25 years as a C and C++ programmer, working from baremetal to apps and everything in-between. Currently working on Linux kernel debugging. Couldn't do that without knowing a LOT more than is stated above.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

User avatar
buja
Posts: 498
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: Learning C in 5 minutes

Mon Feb 27, 2017 11:52 am

I doubt a forum is the right platform for teaching anything at all because the lessons are mixed with corrections and opinions, so in the end who is to say what is right and what is wrong? Not very useful for a beginner.

User avatar
Paeryn
Posts: 2673
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Learning C in 5 minutes

Mon Feb 27, 2017 3:10 pm

woodystanford wrote: In you code, you just call a function like this:

Code: Select all

#include <stdio.h>

int GetColorOfSky(int hr)
{
//maybe you can code this yourself...now you know how to do it
//but we'll return the hr as the color code so it will do something when you enter something
   GetColorOfSky=hr;
   return(hr);
}
I'll ignore the rest of the code at this point since that function is invalid. You can't assign hr to GetColorOfSky if GetColorOfSky is the name of a function.

Code: Select all

pi@rpi3:~/Programming/woody $ gcc col.c
col.c: In function ‘GetColorOfSky’:
col.c:7:14: error: lvalue required as left operand of assignment
 GetColorOfSky=hr;
              ^
And whilst I'm at it, why put brackets around the return value? return doesn't need brackets around the value to return, they are superfluous. Not that it's an error, just unnecessary, unless you try returning from a void function with return(); which would be an error.
She who travels light — forgot something.

User avatar
PeterO
Posts: 5023
Joined: Sun Jul 22, 2012 4:14 pm

Re: Learning C in 5 minutes

Mon Feb 27, 2017 3:21 pm

Paeryn wrote: You can't assign hr to GetColorOfSky if GetColorOfSky is the name of a function.
Maybe he's a old ALGOL-60 programmer :-)

Code: Select all

REAL PROCEDURE F(X)'  VALUE X' REAL X'
BEGIN

   F := M1*X+(M0-M1)/2*(MOD(X+1)-MOD(X-1))'

END'
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: Learning C in 5 minutes

Mon Mar 13, 2017 3:19 am

woodystanford wrote:
Ok here are the words you can ignore: auto, const, continue, maybe do (who uses do loops?), enum, extern, register (oh god!), signed, sizeof, static, typedef, union (oh my goodness!) , unsigned, maybe void, definately volitile
Yes lets gut the language and make it useless.

volitile Used often, and needed when dealing with HW that can be changed by an external source.
void Extremely important, especially in dealing with pointers to differing data types.
union There are many times you need to access a datum as multiple different formats, this makes it easier, think linked lists and trees of data.
typedef Technically you could ignore it, tough it makes life a lot easier.
static If you need a variable to hold its state between function calls, you need static. If you need a global variable not to be visible outside the current source module, you need static.
sizeof Many uses, do not ignore what you need.
signed When working in systems that default to a unsigned char type for byte size data you are going to need to specify when you are using signed bytes.
register Used to be very helpful, until most compilers started ignoring it.
extern As it is implicit in all cases where needed yes it can be ignored.
enum Definitely ignorable.
continue Many cases where skipping part of a loop is needed conditionally.
do For loops that are known to be executed at least one time this makes life a lot easier.
const Arguable, though I agree that it can be ignored.
auto The way things are done by compilers now, yes it can be ignored.

You see if you ignore these keywords you castrate the language.
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

Return to “C/C++”