LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-15-2018, 09:18 AM   #91
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063

Thanks for that GazL.

Was it called "Programming in C 3rd edition by Stephen G. Kochan" ? As that's the pdf I've just downloaded. I think it's the one you mean. Is it?

It's just I don't have the money to buy any books - I've only got the Internet really - if you know what I mean.
 
Old 12-15-2018, 04:54 PM   #92
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
Quote:
Originally Posted by GazL View Post
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.
 
2 members found this post helpful.
Old 12-15-2018, 05:10 PM   #93
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
Quote:
Originally Posted by jsbjsb001 View Post
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.
 
3 members found this post helpful.
Old 12-16-2018, 12:32 AM   #94
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
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!
 
2 members found this post helpful.
Old 12-16-2018, 03:26 AM   #95
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
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
 
1 members found this post helpful.
Old 12-16-2018, 04:05 AM   #96
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
You're very welcome

Everything looks correct except:

Quote:
int main (void)
Means the main function returns an integer value:

Quote:
return 0<--- the integer value main returns
and (void) means the main function accepts no arguments.

Last edited by Mechanikx; 12-16-2018 at 04:19 AM. Reason: Grammar.
 
2 members found this post helpful.
Old 12-16-2018, 04:17 AM   #97
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
Quote:
Originally Posted by jsbjsb001 View Post
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.
If you'd like to see what gets added try:

Code:
gcc -Wall -E myprogram.c -o myprogram.i
 
3 members found this post helpful.
Old 12-16-2018, 04:22 AM   #98
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
Quote:
Originally Posted by Mechanikx View Post
You're very welcome

Everything looks correct except:
...
Means the main function returns an integer value:
...
and (void) means the main function accepts no arguments.
Thank you Mechanikx, it seems I need to do some more reading. Thanks again.

And thank you for post #97 - I never knew that. I'm going to have to have a look at that. Thanks again!
 
1 members found this post helpful.
Old 12-16-2018, 04:26 AM   #99
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
You're welcome. Enjoy the journey
 
1 members found this post helpful.
Old 12-16-2018, 07:28 AM   #100
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,572
Blog Entries: 19

Rep: Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451
Quote:
Originally Posted by rtmistler View Post
Everyone has different code format preferences.

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.

I find that this makes the program more readable.
 
1 members found this post helpful.
Old 12-16-2018, 01:26 PM   #101
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
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.

Last edited by GazL; 12-16-2018 at 01:29 PM.
 
3 members found this post helpful.
Old 12-16-2018, 02:32 PM   #102
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
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.
 
2 members found this post helpful.
Old 12-17-2018, 05:21 AM   #103
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,572
Blog Entries: 19

Rep: Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451
That goes for names too. Use CamelCase or snail_case and don't mix them. But you can use one for variables and the other for functions.

Some people put type prefixes on their variable names (for example fSize) so they can see at a glance what the type is, in this case float.
 
1 members found this post helpful.
Old 12-17-2018, 05:40 AM   #104
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by hazel View Post
That goes for names too. Use CamelCase or snail_case and don't mix them. But you can use one for variables and the other for functions.

Some people put type prefixes on their variable names (for example fSize) so they can see at a glance what the type is, in this case float.
It also makes it harder to identify the variable, which is why many put it at the end.

I dislike both because they make the code harder to read. Use good names to start with and the type becomes obvious.
 
2 members found this post helpful.
Old 12-17-2018, 06:02 AM   #105
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
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.
 
1 members found this post helpful.
  


Reply

Tags
c programming, learning c



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Finally decided to try to get libinput to work Timothy Miller Linux - Hardware 3 01-04-2018 08:04 PM
Decided to try Lubuntu 14.04 on my netbook... pcninja Ubuntu 4 04-20-2014 08:18 PM
Finally decided to get serious & learn a302svt LinuxQuestions.org Member Intro 1 07-19-2007 12:27 PM
Decided to try Debian some guidance required ninadb Debian 2 08-20-2004 11:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration