Question about basics; C

Go To Last Post
38 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

Im newcomer in coding (Only a bit foreknowledge) and Im trying to figure out the basics of C language. I have read some tutorials and example codes, but Im still wondering:

What are those buddies called int, char, long and etc.
As I understand, int, for example, is a part of a code that specifies how a variable is saved in the memory of MCU. Int gives variable type of integer, right? If this is wrong, please correct it.
But; When to use int, when to use char? What is the difference of using int or char? As I have understood in this case, they have only difference in capacity of memory (bits). Why wouldn't all be in same size? Does the code act in different way if I use char instead of int?
How about crossing different kind of variables? I mean:

int _k = 1
char _i
_i = _k

Is that illegal? Have I committed a crime?

If someone pro could explain more of these things, I would appreciate it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It is indeed legal. It's not very elegant, but it is legal. char only stores numbers between -128 and +127, so if _k were out of that range instead of being just 1, you would only get the 8 least significant bits of information. Also, the compiler may throw you some warnings about this depending on how it is configured.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, you should not add leading underscores to variables (putting underscores **inside** the name of a variable is perfectly legal).
if you do
int k=5*16 + 5; // with a terminating semicolumn : every C compiler will become mad, else
char i ;
i=k ;
i will be seen as a character the base 10 representation is 85 - or hexa representation 0x55-i.e 'U'-

C tolerates characters and integer to be equivalent (Pascal and Python do not).

Perhaps you should download a free C interpreter to play interactively with C on your PC... (before breaking an AVR) such as picoc (on GNUlinux) or pellesC on Windows -much more near a standard C)... .

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

technik90 wrote:
char only stores numbers between -128 and +127, so if _k were out of that range instead of being just 1, you would only get the 8 least significant bits of information.

That elucidates a lot. Thank you.

dbrion0606 wrote:
Perhaps you should download a free C interpreter to play interactively with C on your PC... (before breaking an AVR) such as picoc (on GNUlinux) or pellesC on Windows -much more near a standard C)... .

Good to know that!

Forgot to ask about void in first post. Void doesn't return anything, does it? It is used, for example, to make interrupts, right? How about this;

void a_variable(void);

There are two voids given to a_variable. What does the last void mean between ( )'s? -> a_variable=null? How about

void a_variable(int another_variable); or
int a_variable(int another_variable);

I know that these are boring questions but so is ignorance too.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

char is also used to represent characters, so you can write:

char c = 'a';

The numeric value of 'a' is 97, so this line is equivalent to:

char c = 97;

Also note that char comes in three varieties: char, signed char, and unsigned char. You should explicitly use signed or unsigned if you intend char to represent a numeric value and use just plain char if you intend the value to be interpreted as a character. You can also use signed and unsigned with int, but int and signed int are always treated identically by the compiler. This is not necessarily true with signed char and char.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For God's sake, do not try interrupts at the very beginning (traditional programs are a sequence of instruction -which can if needed be reated-; interrupts may occur at any time, and are more difficult to handle ).

There are two types of variable in a C (Python, Pascal) program: local variable, which are used only inside the function and global ones (every function sees) to make things simple.
A void function can modify a global variable (it is seen as ugly), or interact with hardware (but it might return a result code, which would be better)...
It has nothing to do with interrupts (arduino hides timer interrups, there is a global variable which tells how many timer interrupts occur and it can be managed in classical programming)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A couple of points. Data types ( like int, char and float ) describe how large a variable is and how it acts. In C, a simple value can be either integral ( int, short, char ) or floating-point ( float and double ). Integral data can, also, be either signed or unsigned. The combination of size and signed/unsigned determines how a value works in a program. Smaller data sizes have a smaller range of values that they can hold and computations that would produce a value too large to be stored in that type will produce overflow. It makes sense, however, to use smaller types to minimize memory usage and reduce the amount of computation needed ( e.g. it takes more instructions to add two 16-bit ints than it does to add two 8-bit unsigned chars on the AVR ).

Mixing different datatypes is done in C with precise "promotion" rules. If data of different sizes are combined, the compiler treats each value in a computation as equivalent -- the same as the larger size ( more or less ). That allows the compiler to combine values, generally, as expected but there are times that the rules can be confusing. Promotion is an important concept to understand in C.

There are two uses of void in function declarations ( prototypes ). If void is found as the return value type ( the very beginning of the function declaration ) it means that the function has no return value. Similarly, if a function is declared with a parameter list ( that stuff in the parentheses ) of void, it means that the function takes no parameters. Early style simply had empty parentheses.

There are a few other "little-niggles" with data types in C, of course; for instance, void* is a completely different beast than the voids we talked about above ( it is a pointer to data of undefined type ). There are pointers, and there are typedefs, but just understanding this little bit can get you a long way in C.

Martin Jay McKee

As with most things in engineering, the answer is an unabashed, "It depends."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A couple of things:

1) have a read about . If you are just learning it's as well to do it properly form the start. is an improvement over standard C types and is better for readability and maintenance.

2) don't use _ or __ as prefix for a variable name in C To avoid name pollution symbols with the prefix '_' are used for the C library and symbols with the prefix '__' are used for the C compiler. This means your 'counter' will not clash with the library's _counter or the compiler's __counter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bontz07 wrote:
Why wouldn't all be in same size?

Because larger variables generally take both more memory and more CPU time to process. Use the smallest variable you can. I have a mantra:

Don't use 16 bits if 8 will do
Don't use 32 bits if 16 will do
Don't use floats if integers will do
Don't use signed if unsigned will do

Quote:
Does the code act in different way if I use char instead of int?
How about crossing different kind of variables? I mean:

int _k = 1
char _i
_i = _k

Is that illegal? Have I committed a crime?


It is legal. It is also a prolific source of hard-to-find bugs.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
void a_variable(void);

There are two voids given to a_variable. What does the last void mean between ( )'s? -> a_variable=null?

That is not the format of a variable, that is the format of a function. It sounds to me like you are just looking at code and guessing what it means. You need to get a book on C and read it.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Some guy at the phone company wrote [It has been explained to me that the tutorial formerly linked here is no good, so I have removed it. Thank the people who would much rather sh1t on someone trying to help, than to help.]

A little later he co-wrote this book http://cm.bell-labs.com/cm/cs/cb... which is available in 28 languages. That book is not (legally) online; you have to pay money for it. There are many other free online C books and references listed at https://www.avrfreaks.net/index.p... .

I hope this helps!

Last Edited: Sat. Mar 23, 2013 - 09:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

http://www.lysator.liu.se/c/bwk-tutor.html
...excellent link, thank you.

"I have read and understood http://www.lysator.liu.se/c/bwk-... (*)" should be a checkbox on the 'freaks membership application form :)

(*) - or I refuse to acknowledge the existence of C and will only deal in ASM

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

GeococcyxC wrote:
Some guy at the phone company wrote http://www.lysator.liu.se/c/bwk-... almost 40 years ago. It's still a pretty good introduction.
I disagree.
The disclaimer there is important and very true. That "tuorial" is not a good introduction for nowadays C beginners. E.g.:

count(buf, size)
   int size, buf[ ]; {
...
}

If someone learns C with that document, then it is no wonder when he has no idea what "void function (void) {" should mean.

That document is simply too outdated and too far away from nowadays C. That document should not be the first contact with C for anyone these days.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:
That is not the format of a variable, that is the format of a function. It sounds to me like you are just looking at code and guessing what it means. You need to get a book on C and read it.

Im looking forward reading a C book as soon as I find one good. Any book suggestions for a beginner? And yes, it is highly guessing when trying to understand codes, but the fact is I like learning via reading separate codes, just dont know how effective it is. But some basedata wouldn't be any bad.

Going to check those links sent here.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, you should first learn with a PC (already wired and working) and classical books.

If you want to learn both things at the same time
Smiley's (advertises here) seems very good (I read some free chapters and its contents).
I use http://www.elektor.fr/products/b... (is translated from German, you can find at elektor.de instead of elektor.fr)

As it has been translated (and code comments, too) by a very competent man, one might bet there are very few errors, if any).

Both come with real hardware and both books seem very imiler (from Smiley 's contents...).

AVRfreaks tutorials are very good, too , but maybe more as a further reading from {Elektor's|Smileys -alph.order.) book (depend on the pedagogic skills of the writer, and the audience he seeks).

Another solution (it is C++, very simplified) would be to ... study arduino's démos and documentation...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bontz,

If you are serious about learning C, you need to get a FEW good text books about C programming.

There are MANY C programming books available in the marketplace, but only a few are worth anything to a beginner like you. The book I recommend to beginners is written by Chris Jamsa entitled "1001 C/C++ Tips" (or similar). Jamsa has written a number of books about C, I also like his "C/C++ Bible" (or similar). I am sure that other Freaks will have their own favorites - by Jamsa or others.

"1001" is not perfect or complete, but it is well-written and I find it easy to understand (as have those I have recommended it to). He presents the material in bite-size "topics", with simple do-it-yourself C-code examples for each one. The "bible" is a mpre in-depth reference book - you'll want to have it on hand, but it would be a mistake to use it to learn C from.

If you go onto Amazon, you will find used copies of "1001" for 17 cents plus $3.99 shipping - the best $4.16 you'll ever spend with your pants on! The "Bible" can also be had for similarly ridiculous prices. If you find one dealer with both books, you'll save on shipping too.

I have ordered dozens of used books with Amazon. I have has very few problems. I like their "partner" method of selling used books because I use my Amazon credit card "account" (i.e. Visa, MasterCard, PayPal) and only Amazon has access to my credit card info - not the 3rd party I'm actually purchasing the book from.

The online C tutorial are good, but they only go so far. Nothing like a real paper book(s) at your side as you're learning this stuff.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Any book suggestions for a beginner?

Every C programmer should have a copy of:

While some people say it's not the best "tutorial" (though I thought it was pretty good!) it is undoubtedly the "bible" of C programming probably because one of the two authors wrote C.

(this is the book GeococcyxC mentioned above - I'm simply emphasising how important it is that all C programmers have a copy - presumably the authors get royalty so it's also your way of saying "thanks for C" ;-))

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sternst wrote:
GeococcyxC wrote:
Some guy at the phone company wrote http://www.lysator.liu.se/c/bwk-... almost 40 years ago. It's still a pretty good introduction.
I disagree.
The disclaimer there is important and very true. That "tuorial" is not a good introduction for nowadays C beginners. E.g.:

count(buf, size)
   int size, buf[ ]; {
...
}

If someone learns C with that document, then it is no wonder when he has no idea what "void function (void) {" should mean.

If somebody doesn't understand why char and int are not the same size, they need to fix that problem first before worrying about K&R vs ANSI function declarations. Also, the compiler a lot of people use with AVRs will still accept K&R style functions as valid.

For what it's worth, I did note this problem when I posted here: https://www.avrfreaks.net/index.p...

Quote:
That document is simply too outdated and too far away from nowadays C.
Because newer is always better, and all the old things should always be thrown away and ignored. Right.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

will still accept K&R style functions as valid.

Yeah but you don't want to teach the youth of today to use 30 year old syntax?!?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Quote:

will still accept K&R style functions as valid.
Yeah but you don't want to teach the youth of today to use 30 year old syntax?!?

1. Syntax isn't the hard part! The hard part is understanding how to structure a program, local vs global variables, what stuff should be functions vs inline code, how to conserve RAM if you don't have much, etc.

2. Syntax is the hard part for n00bs, but only if they haven't programmed before. These days, with lint built into the compilers, the compiler is pretty good at catching n00b syntax errors - all you have to do is read the error output and believe it. (Which admittedly can be a challenge to get people to do.)

The reason why I like to link to that document is that in my experience, people asking n00b questions are not going to run out and buy a copy of K&R*. Maybe they should, but they won't. I paid $40 for a legal copy at the bookstore in the US in about 1993; currently it's $47 from Amazon and probably $60 in bookstores. People with n00b questions are either going to look online (and find a mix of good and awful tutorials), or buy one of the "phonebook style" C tutorials at the bookstore (and find a mix of good and awful tutorials). If I can give them a link to IMHO a pretty good tutorial, I think that will help.

(*Exceptions: they live in a country with a legal cheap edition, or a country that doesn't care about copyright, or they are good at finding things on BitTorrent or FTP.)

Matt Roberds

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

GeococcyxC wrote:
Quote:
That document is simply too outdated and too far away from nowadays C.
Because newer is always better, and all the old things should always be thrown away and ignored. Right.
No, because learning a syntax that is used by absolutely NOBODY these days leads to unnecessary additional confusion for the beginner when looking at real world code. And with that document he is not only learning old C style programming, but also even bad C style programming (not using any prototypes, relying on defaults for function return types, etc.). He unnecessarily learns things which he has to revise afterwards.

Quote:
If somebody doesn't understand why char and int are not the same size, they need to fix that problem first before worrying about K&R vs ANSI function declarations.
And you expect that a beginner knows what parts of the document are of value for him and which parts are rather not?

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Agreed. To tell him to learn an outdated style, only to have to relearn the correct way is asinine, particularly when an important part of learning the language is looking at other peoples code.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also, the difference between old-style and new-style function declarations is an IMPORTANT one when it comes to writing good code. The old-style format means no function prototypes, and thus no type-checking of arguments or return values. Which almost everyone agrees is a bad idea.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sternst wrote:
GeococcyxC wrote:
Because newer is always better, and all the old things should always be thrown away and ignored. Right.
No, because learning a syntax that is used by absolutely NOBODY these days leads to unnecessary additional confusion for the beginner when looking at real world code.
So, were you going to provide a link to a good tutorial, or are you just here to sh1t on the people that are trying to help?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:
Agreed.
So, were you going to provide a link to a good tutorial, or are you just here to sh1t on the people that are trying to help?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

westfw wrote:
Which almost everyone agrees is a bad idea.
So, were you going to provide a link to a good tutorial, or are you just here to sh1t on the people that are trying to help?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

GeococcyxC wrote:
or are you just here to sh1t on the people that are trying to help?
Yes, exactly that is my motivation.
What is yours? To insult everyone who dares to disagree with one of your opinions?

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sternst wrote:
GeococcyxC wrote:
or are you just here to sh1t on the people that are trying to help?
Yes, exactly that is my motivation.
Good to know.

Quote:
What is yours?
My motivation is trying to help. If I don't know the answer, I try not to say anything. If I think I can post something helpful, I do. I don't mind if somebody looks at what I wrote and says "no, that's not helpful" or "it won't work that way", if they then also say "read this, it will explain the problem" or "doing it like this might work better". Posting only "no, that won't work, don't do it that way" and nothing else is just being an asshole.

Quote:
To insult everyone who dares to disagree with one of your opinions?
Note that I didn't challenge the people who posted alternative answers to the original query - only the people whose sole purpose in the thread was to say "don't do it that way" and then sit there and bask in their superior knowledge, without sharing any.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

GeococcyxC wrote:
Posting only "no, that won't work, don't do it that way" and nothing else is just being an asshole.
No, it is also helpful for the OP, by saving him from wrong tracks. And in this case by informing him that some of the help might not be as helpful as it appears to be.
On the other hand, insulting others isn't helpful for anyone.

GeococcyxC wrote:
Note that I didn't challenge the people who posted alternative answers to the original query - only the people whose sole purpose in the thread was to say "don't do it that way" and then sit there and bask in their superior knowledge, without sharing any.
Bullshit. What about Steve? The only criteria for your challenge was "disagreed with my opinion".

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please also realize that there is a difference between using C with PCs and using C with microcontrollers like the AVR. A PC has a high-definition screen, gigabyte memory, gigahertz processor, and 101-key keyboard to present information to you. A microcontroller is an integrated circuit that has pins that switch between Vcc and ground in predictable and programmable patterns. That's all that it does.

There is a big difference between the two. Problems that are easy to identify and fix on the PC when using C language can be and usually are much more difficult to identify and fix on an AVR.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Problems that are easy to identify and fix on the PC when using C language can be and usually are much more difficult to identify and fix on an AVR.

That entirely depends on the debug tools available. Yesterday I was trying to get some FAT memory card reading code working on a mega32 and had the luxury of a JTAGICEMKII and a GUI debugger. Meanwhile I was also trying to develop some file processing code on the PC but couldn't easily use anything but command line driven gdb and printf statements. It was far worse developing the PC code than the AVR code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you download the imagecraft compiler demo, the help file has a chapter called 'c in 21 pages' that is a succinct summary.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

this is the book GeococcyxC mentioned above - I'm simply emphasising how important it is that all C programmers have a copy - presumably the authors get royalty so it's also your way of saying "thanks for C" Wink)

Well , not all the authors will get royalties (and a French lawyer, who is aware he is endebted for C -he had a very intersting blog, and now twitters- , wrote an homenage to Ritchie http://www.maitre-eolas.fr/post/...

Last Edited: Thu. Mar 28, 2013 - 06:40 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Broken link:
Document non trouvé
The document you are looking for does not exists.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, I should have checked the link (and, now, seems to work)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sternst wrote:
GeococcyxC wrote:
Note that I didn't challenge the people who posted alternative answers to the original query - only the people whose sole purpose in the thread was to say "don't do it that way" and then sit there and bask in their superior knowledge, without sharing any.
Bullshit.
I'm sorry your reading skills are so poor.
Quote:
What about Steve? The only criteria for your challenge was "disagreed with my opinion".
You, clawson, Koshchi (Steve A.), and westfw didn't like the link I originally posted:
https://www.avrfreaks.net/index.p...
https://www.avrfreaks.net/index.p...
https://www.avrfreaks.net/index.p...
https://www.avrfreaks.net/index.p...

However, clawson pointed to a better source of information: https://www.avrfreaks.net/index.p... , but you, Koshchi (Steve A.), and westfw did not.

I didn't challenge clawson in my followups. I did challenge you, Koshchi (Steve A.), and westfl.
https://www.avrfreaks.net/index.p...
https://www.avrfreaks.net/index.p...
https://www.avrfreaks.net/index.p...

It is true that both clawson and Koshchi (Steve A.) tried to assist the original poster earlier in the thread, before the idea of consulting some kind of reference or tutorial was brought up:
https://www.avrfreaks.net/index.p...
https://www.avrfreaks.net/index.p...
So maybe challenging Koshchi was incorrect.

I stand by my challenge of you and westfl.

I have also removed the offending link from my original post, and from a place where I posted it in another thread.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Matt, this whole argument makes for boring reading. Is anything of use going to be resolved?

Sometimes its just easier to let go. This advice came in handy for me when i was jumping out of an airplane.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is a simple rule here that you should only post if it would help the OP, otherwise don't post. If there's any more of this irrelevant nonsense I'll simply delete the noise.

Moderator