# How's is this program working?

here is my code:

``````// Project 14 - Using long Variables
long number = 0;
long a = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
number = 0; // zero the incoming number ready for a new read
Serial.flush(); // clear any "junk" out of the serial buffer before waiting
while (Serial.available() == 0)
{
// do nothing until something comes into the serial buffer,
// when something does come in, Serial.available will return how many
// characters are waiting in the buffer to process
}
// one character of serial data is available, begin calculating
while (Serial.available() > 0)
{
// move any previous digit to the next column on the left;
// in other words, 1 becomes 10 while there is data in the buffer
number = number * 10;
// read the next number in the buffer and subtract the character 0
// from it to convert it to the actual integer number
// add this value a into the accumulating number
number = number + a;
// allow a short delay for more serial data to come into Serial.available
delay(5);
}
Serial.print("You entered: ");
Serial.println(number);
Serial.print(number);
Serial.print(" multiplied by two is ");
number = number * 2;
Serial.println(number);
}
``````

my question is that how this program takes multiple digits from input and specifically this area of code:

``````while (Serial.available() > 0)
{
// move any previous digit to the next column on the left;
// in other words, 1 becomes 10 while there is data in the buffer
number = number * 10;
// read the next number in the buffer and subtract the character 0
// from it to convert it to the actual integer number
// add this value a into the accumulating number
number = number + a;
// allow a delay for more serial data to come into Serial.available
delay(5);
}
``````

pl, explain how this program works?
thanks for ur help.

in each iteration of loop() it waits until there is serial data available and repeatedly reads it until there is no more, presumably a complete line

whenever is reads serial input, it multiplies number by 10, subtracts an ASCII zero from the character that is read, converting it to a single digit value and adds it to number.

when no more input is available it prints the value, returns from loop() which is reinvoked immediately

1 Like

thanks sir for your quick response
and one more thing what can i do to improve my arduino programming or maybe c programming .
Is there any book which u can advice me .

i would recommend the book written by the guy who invented C, Ritchie, The C Programming Language. it is concise, has good examples, but is written for experienced programmers.

but you may be having more problems understanding I/O concepts on the Arduino than just programming.

2 Likes

ok sir . thanks a lot .

You will not understand a lot of the Arduino code by reading this book. It is very old and describes the beginning of the C language.
Arduino is coded in C++ which ist a very big enhancement of the original C language.
There are many books and tutorials online. Search for â€śC++ book onlineâ€ť

1 Like

you certainly donâ€™t need to understand C++ to write Arduino programs and many OOP concepts can be implemented in C (C++ was first translated to C before a compiler was built).

1 Like

This alone is not C:

and you will not understand what the period means, if you only know C

Either the Arduino reference is enough for me, or I should look into C++ to get deeper into it. Learning only C in detail makes no sense.

1 Like

not sure what you mean by â€śaloneâ€ť. Of course the â€ś.â€ť means element of a structure

a little awkward, but demonstrates what C is capable of. File is named main.c, compiled with cc and outputsf â€śHello Worldâ€ť.

``````#include <stdio.h>

void pr (const char *s) {
printf (s);
};

typedef struct {
void (*print) (const char *s);
} Serial_s;

Serial_s Serial = { pr };

int
main ()
{
Serial.print ("Hello World\n");
return 0;
}
``````
1 Like

Ok, If you see it that way â€¦
I donâ€™t think itâ€™s a good idea to recommend an old C book to someone to learn the Arduino language (C++). There are so many improvements, even independent from OOP .

1 Like

I too still recommend that book. It is a model of clarity and exposition of the tiny language which C has become.

Get the 2nd edition, it has a few less cobwebby type things.

IMO getting a good grip on C, without and before all modern innovations, is essential, arguably more so in the context of writing small programs in resource-challenged environments.

You may notice that the first chapter or three of modern programming text books basically teach that languageâ€™s version of how to express the ideas from C in the new language!

But with nowhere near the care and deliberation of K&R.

Of course everyone anxious to get on with classes and objects and overloading and and and and, the contents of the next 11 chapters.

a7

2 Likes

itâ€™s what the language is capable of doing. you make it sound like iâ€™m cheating somehow. OOD/OOP has been around long before languages like C++. the approach i demonstrated shows one way of implementing OOD

I donâ€™t think itâ€™s a good idea to recommend an old C book to someone to learn the Arduino language (C++).

Arduino or C++? C is a systems language that is well suited for embedded applications. there no need to learn C++ to develop good Arduino programs

i think itâ€™s good to learn how to program from any well written book. Are there bad books? Elements of Style discusses flaws and how to fix them in programs from college textbooks teaching programming.

1 Like

The fact is that the Arduino Sketch is compiled by a C++ compiler - not a C compiler. And there are really differences between C++ and C compilers e.g. the name mangling.
And almost all libraries use classes, also the libraries of the Arduino core. Even if you donâ€™t write any yourself, you should know how to use them.

1 Like

isnâ€™t that the beauty of it, you donâ€™t need to understand c++ to use libraries written in it

2 Likes

donâ€™t want to drag this out, but iâ€™ve never seen a really well written C++ program that couldnâ€™t be done with C.

I was told one of our managers was against using c++ for embedded real-time applications until he met someone that explained how to use it properly for that type of work. presumably thereâ€™s a proper way to use the language. ( would love to talk with that someone).

in an interview, Bjarne Stroustrup said he felt it takes 10 years to become proficient in c++.

C++ Progamming Style discusses flawed C++ programs and how to make them better.

1 Like

That helps. Iâ€™m off the hook as I donâ€™t feel like I have ten years left. Sometimes I donâ€™t feel like I have ten monthsâ€¦

<â€” donâ€™t be calling the Good Samaritans, thx.

a7

1 Like

Of course, you can solve everything with C. You can also solve it with assembler, thatâ€™s not the point.
C++ provides many improvements to avoid errors that easily occur in C. You donâ€™t have to go deep into the OOP of C++ to take advantage of these improvements.
I started with C about 40 years ago ( I still own the K&R book ), but I now recognize and appreciate the advantages of C++. Anyone starting out should not overlook these benefits and start with C++ right away.

But everyone does it the way he likes and wants. I donâ€™t want to miss C++ anymore.

1 Like

i was suggesting that it was well written and easy to understand C code. a common complaint with C++ is abstraction makes it very difficult to read the code.

C++ provides many improvements to avoid errors that easily occur in C.

can you provide an example? sincerely

iâ€™m not suggesting not using a C++ compiler. at Bell Labs, many groups starting compiling code (i.e. lint) with the C++ interpreter to do better type checking among other things.

1 Like