ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Small world. I learnt from the 1988 "Revised Edition" (green cover) of that book, which was pre-ANSI K&R syntax. I still keep it on the bookshelf beside me. It's a very gentle introduction to the language well suited to beginners.
Small world indeed C was my first programming language and for the reason you mentioned, and others, this book was perfect. I liked his teaching style so much that after I was done I bought "Unix Shell Programming" which I found to be equally as good.
Absolutely agree with you GazL, that tutorial is just crap. Good to hear from you.
I've just been doing some re-reading of that same near useless tutorial I started off with, and have been trying to understand what didn't make sense to me before, particularly the different "data types". Now, under "Enumerated types" it talks about "discrete integer values", I've been trying to understand exactly what that means, and I still don't get it.
So I looked up the meaning for "discrete", and it says;
That by itself, I get it. But in relation to "discrete integer values", I still don't understand exactly what that means. I get that an "integer" is a number without the fractional part. But I still don't understand why I would use that or what that exactly means. I've tried looking at various sites (some of which I ended up just closing, because they just confused me even more), but it's still not clear to me exactly what that term means. Please bear in mind that I'm absolutely hopeless when it comes to maths, so it's just going to fly straight over my head if you are not clear about exactly what I'm failing to understand here. Could someone give me an example/explain exactly what that term means in relation to C? Why would I use an "discrete integer value" data type? I don't get it.
I'm assuming this is the definition you're refering to:
Quote:
Enumerated types
They are again arithmetic types and they are used to define variables that can only assign certain discrete integer values throughout the program.
The enumerated data type allows you to create your own data type. For instance, if you wanted to create a data type called "days" so that when a variable is declared to be of this type, the only valid values that it could accept are the days of the week, you would use the enumeration data type, like so:
Code:
enum days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; // define the data type 'days'
Code:
enum days daysOfTheWeek; // define a variable to be of type 'days'
The names of the days between the curly braces are called "identifiers". Now here's the thing. The compiler actually assigns integer constants, in sequential order starting at zero, to the identifiers. For example:
Code:
Sunday = 0
Monday = 1
Tuesday = 2
...
This is what I believe that tutorial means by "...can only assign certain discrete integer values throughout the program". In my example the only integer values that are assigned to the identifiers are 0-7. The values are either 0, 1, 2, 3, 4, 5, 6, 7 or are not valid. This is discrete and not continuous.
Also, you have the option to specify which integers you would like to associate with which identifiers:
Code:
enum days { Sunday = 3, .... };
If I were to write a program using the enumerated data type 'days' I could refer to the identifiers by their integer constant. So, if I needed to use 'Tuesday' I could just specify '2'. But it's more efficient to just rely on the symbolic name 'Tuesday', because if I wanted to change the integer it's associated with I would only have to make that adjustment in one place rather than everywhere in the program I refer to 'Tuesday'.
I hope this helps to clear up any confusion.
For what it's worth, I didn't learn about enumerated data types until a few months into learning programming.
Last edited by Mechanikx; 12-15-2018 at 05:47 PM.
Reason: Added an additional thought at the bottom.
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
Thank you so much Mechanikx!
Yes, that's exactly what I was referring to. Just reading the first part of your reply made a LOT of sense. While I think I understand it now; I'll keep reading both your reply, as well as, the pdf I downloaded that GazL mentioned before.
I have already read at least a couple of chapters of that book/pdf and already things are much clearer. There are even exercises in it too. As I was looking at the source for the ls command, and that's (from what I can see) filled with "enumerated data types".
Thank you both again - things are becoming much more clear to me now. I've ditched that near useless tutorial I started off with, and am not even going to bother with that tutorial anymore - it's just not detailed enough. Thank you guys!
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
Hi guys,
I have been looking a one of the examples in the pdf I'm reading, and I want to make sure I'm understanding EXACTLY and completely (as astrogeek suggested) what each and every line actually means, not just what it does.
Code:
#include <stdio.h>
The above line gives me access to the "printf" function. And adds the code from that header to my program.
Code:
int main(void)
The above line is the "main" function, that is of "integer data type", that returns nothing (hence the "void" part).
Code:
int answer, result;
The above line means I'm declaring the "answer" and "result" variables as "integer data types".
Code:
answer = 100;
result = answer - 10;
The above line means that the variable called "answer" equals "100". The variable "result" equals the variable "answer" minus "10".
Code:
printf("The result is: %i\n", result + 5);
The above line means that I'm calling the "printf" function to display "The result is:" followed by the answer to "100" minus "10", plus "5" - which is "95". Which therefore will give me the output of "The result is: 95".
Code:
return 0;
The above line means that my program is finished successfully, and returns an "0" to the operating system. To tell the OS my program has finished WITHOUT error.
(I know the "%i" in the "printf" function dictates what kind of value "printf" is supposed to display, in this case I *think* that means an "integer" - a number.)
Is there anything I've missed? And is that correct?
Last edited by jsbjsb001; 12-16-2018 at 03:34 AM.
Reason: more typos and additions
I have been looking a one of the examples in the pdf I'm reading, and I want to make sure I'm understanding EXACTLY and completely (as astrogeek suggested) what each and every line actually means, not just what it does.
Code:
#include <stdio.h>
The above line gives me access to the "printf" function. And adds the code from that header to my program.
I prefer to indent 3 or 4 white spaces, never use tabs in C code, and put code in brackets even if it is a one line if-statement.
I use tabs all the time (laziness!) I'd be interested to know what you have against them.
My rules (for what they're worth) are:
1) Start function headers and comments in column 1. Temporary debug statements also start in this column.
2) Un-nested permanent code is indented one tab space.
3) Paired braces are always in the same column as the start of the surrounding code.
4) The code between the braces is indented one further tab space.
Using tabs for alignment can get messed up when different people view the same file with differing tab stop values. It's not too bad with the initial block indent at the start of a line as the relative alignment of lines in the block will remain intact, but continuation lines and manually aligned comments can often end up misplaced.
On the whole I just find indenting with spaces less problematic, especially as emacs' c-mode inserts the correct number of them for me when I press <tab> so I don't need to worry about it.
P.S. I also prefer an offset of 4. I find 8 far too wide.
I also use spaces for the reasons mentioned by GazL.
Also, whether you choose to use tabs or spaces it's important to pick one and stick with it. In other words, don't mix tabs and spaces within the same program.
snake_case for local variables, camelCase for functions, and Hungarian only for pointers seems to be what I do, but I admit I'm still a little inconsistent with that and it's something I need to work on to improve the quality of my coding style.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.