Personal computing discussed

Moderators: SecretSquirrel, just brew it!

  • 1
  • 2
  • 3
  • 4
  • 5
  • 10
 
whm1974
Gerbil Elder
Topic Author
Posts: 5761
Joined: Fri Dec 05, 2014 5:29 am

Re: GGC errors with compiling C code.

Wed Nov 08, 2017 4:55 pm

Redocbew wrote:
In that case, then yeah... go somewhere else and get a better tutorial. I've heard that w3schools wasn't a great resource, but I've never tried looking up one of their pages myself.

I'm wondering if I should buy C manuals in hardcopy? However they are somewhat pricey and I really don't that much room for a lot of books, which why I brought a Kindle Paperwhite in the first place.
 
Redocbew
Gold subscriber
Gerbil Jedi
Posts: 1599
Joined: Sat Mar 15, 2014 11:44 am

Re: GGC errors with compiling C code.

Wed Nov 08, 2017 5:03 pm

I still have a number of dead-tree books here, but I haven't used them in a while. These days I usually end up at stack overflow searching for a specific thing. If the kindle books work for you, go ahead and use that. Good reference material is a must-have, but the nice thing about looking stuff up online is you often don't have to pay for it if it turns out to be bad. When learning a new language I'll usually find some good reference material somewhere, bleed it dry, then find another, and so on until I feel comfortable with the language its self and can concentrate more on the task at hand than figuring out how the language will let me do it.
Do not meddle in the affairs of archers, for they are subtle and you won't hear them coming.
 
whm1974
Gerbil Elder
Topic Author
Posts: 5761
Joined: Fri Dec 05, 2014 5:29 am

Re: GGC errors with compiling C code.

Wed Nov 08, 2017 5:07 pm

Redocbew wrote:
I still have a number of dead-tree books here, but I haven't used them in a while. These days I usually end up at stack overflow searching for a specific thing. If the kindle books work for you, go ahead and use that. Good reference material is a must-have, but the nice thing about looking stuff up online is you often don't have to pay for it if it turns out to be bad.

Yeah that sort of thing will cause me to quit hard and fast if I have to start shelling out good money for crappy learning materials. :o
 
whm1974
Gerbil Elder
Topic Author
Posts: 5761
Joined: Fri Dec 05, 2014 5:29 am

Re: GGC errors with compiling C code.

Wed Nov 08, 2017 5:37 pm

Anyway it's dinnertime and I'm going to take a break for now.
 
just brew it!
Gold subscriber
Administrator
Posts: 51173
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GGC errors with compiling C code.

Wed Nov 08, 2017 6:04 pm

Yeah, looks like you need to avoid that site. Not only is the formatting mangled, grtch() is not even part of the standard C library. So it's a mangled rendering of an incorrectly transcribed example, and they didn't even bother to verify that what they posted on their site compiles. That's three strikes in my book.

What happened with that eBook the Manjaro people pointed you at? I didn't read the whole thing, but it looked pretty reasonable. Maybe stick with that until you've made it through the whole thing instead of just trying stuff from random web pages? Then you'll at least have enough of a grasp of the language to spot obviously messed up examples like that " crap.

Stay focused!
Nostalgia isn't what it used to be.
 
whm1974
Gerbil Elder
Topic Author
Posts: 5761
Joined: Fri Dec 05, 2014 5:29 am

Re: GGC errors with compiling C code.

Wed Nov 08, 2017 6:11 pm

just brew it! wrote:
Yeah, looks like you need to avoid that site. Not only is the formatting mangled, grtch() is not even part of the standard C library. So it's a mangled rendering of an incorrectly transcribed example, which they didn't even bother to test to verify that what they posted on their site compiles. That's three strikes in my book.

What happened with that eBook the Manjaro people pointed you at? I didn't read the whole thing, but it looked pretty reasonable. Maybe stick with that until you've made it through the whole thing instead of just trying stuff from random web pages? Then you'll at least have enough of a grasp of the language to spot obviously messed up examples like that " crap.

Stay focused!

Yeah I'm starting over with this manual that I put on my Kindle yesterday to read on my to and from the group I've been going to.
I'm going put several more of them my Ereader as well.
https://www.thecrazyprogrammer.com/2015 ... nners.html
 
just brew it!
Gold subscriber
Administrator
Posts: 51173
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GGC errors with compiling C code.

Wed Nov 08, 2017 6:38 pm

What leads you to believe that it is better than the one recommended by the Manjaro forum people? I don't feel like subscribing to some random blogger's newsletter just to skim it to see if it looks reasonable, so if you want me to evaluate it you'll need to get me a copy somehow.
Nostalgia isn't what it used to be.
 
whm1974
Gerbil Elder
Topic Author
Posts: 5761
Joined: Fri Dec 05, 2014 5:29 am

Re: GGC errors with compiling C code.

Wed Nov 08, 2017 6:44 pm

just brew it! wrote:
What leads you to believe that it is better than the one recommended by the Manjaro forum people? I don't feel like subscribing to some random blogger's newsletter just to skim it to see if it looks reasonable, so if you want me to evaluate it you'll need to get me a copy somehow.

The link for a free copy should be there, but if you want the pdf I can send it to you. How do you want it sent?
 
whm1974
Gerbil Elder
Topic Author
Posts: 5761
Joined: Fri Dec 05, 2014 5:29 am

Re: GGC errors with compiling C code.

Wed Nov 08, 2017 6:47 pm

whm1974 wrote:
just brew it! wrote:
What leads you to believe that it is better than the one recommended by the Manjaro forum people? I don't feel like subscribing to some random blogger's newsletter just to skim it to see if it looks reasonable, so if you want me to evaluate it you'll need to get me a copy somehow.

The link for a free copy should be there, but if you want the pdf I can send it to you. How do you want it sent?

Edit:
I was reading it yesterday morning on my Kindle and so far it does look to be more straightforward then the other stuff I was looking at.
 
just brew it!
Gold subscriber
Administrator
Posts: 51173
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GGC errors with compiling C code.

Wed Nov 08, 2017 6:50 pm

whm1974 wrote:
The link for a free copy should be there

It wants me to subscribe to some sort of newsletter to get the link. I'd rather not, I get enough spam already.
Nostalgia isn't what it used to be.
 
whm1974
Gerbil Elder
Topic Author
Posts: 5761
Joined: Fri Dec 05, 2014 5:29 am

Re: GGC errors with compiling C code.

Wed Nov 08, 2017 6:55 pm

just brew it! wrote:
whm1974 wrote:
The link for a free copy should be there

It wants me to subscribe to some sort of newsletter to get the link. I'd rather not, I get enough spam already.

Can't blame as I have that problem as well. I sent you the pdf file to the address you gave me.
 
just brew it!
Gold subscriber
Administrator
Posts: 51173
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GGC errors with compiling C code.

Wed Nov 08, 2017 8:10 pm

whm1974 wrote:
just brew it! wrote:
whm1974 wrote:
The link for a free copy should be there

It wants me to subscribe to some sort of newsletter to get the link. I'd rather not, I get enough spam already.

Can't blame as I have that problem as well. I sent you the pdf file to the address you gave me.

Not impressed.</krogoth>

A few observations from quickly skimming the whole thing:

* Horribly stilted English. Example: "Learning programming is all about practical. So start doing practical from now."

* In the Introduction says not to use Turbo C because it is horribly outdated, then uses Turbo C in his example of how to compile a program.

* There's some misrendering, though not as egregious as on the web site you were using earlier. Example: Shows double-backquotes at the start of strings in some (but not all...) of the examples.

* Odd, non-standard (at least in the US) terminology. Examples: Calls the part of the file where you put the #includes the "Link Section"; calls statements "instructions". Learning from a text with non-standard terminology could cause confusion later.

* Use of same font for explanatory text and examples (makes it harder to read).

* Improper or no indentation in some of the examples, and difficult to follow even when it is correct due to use of single space (one column) indents and proportional font.

* Coverage of the standard C library is rather light.

Bottom line: It's better than the w3schools mess you got tripped up by earlier, but that's a low bar. It is far inferior to the one the Manjaro people sent you to. IMO its only saving grace is that it appears to be aimed at a more "newbie" audience, if that's what you need; but the stilted English and non-standard terminology are problematic (and quite frankly would drive me nuts and/or confuse me if I was trying to learn the language from it).
Nostalgia isn't what it used to be.
 
DancinJack
Grand Gerbil Poohbah
Posts: 3630
Joined: Sat Nov 25, 2006 3:21 pm
Location: Kansas

Re: GCC errors with compiling C code.

Wed Nov 08, 2017 8:14 pm

FWIW, that code should really look like this. You might get a warning or two about types, but that should compile and run. (haven't worn the C hat in a long time, and it just reminds how much more I like C++)

#include<stdio.h>

main()
{
  int number;
  printf("Type a number: ");
  scanf("%d", number);

  /* check whether the number is negative number */
  if (number < 0)
  {
    /* If it is a negative then convert it into positive. */   
    number = -number; 
    printf("The absolute value is  %d", number);
  }
  return 0;
}
i7 6700K - Z170 - 16GiB DDR4 - GTX 1080 - 512GB SSD - 256GB SSD - 500GB SSD - 3TB HDD- 27" IPS G-sync - Win10 Pro x64 - Ubuntu/Mint x64 :: 2015 13" rMBP Sierra :: Canon EOS 80D/Sony RX100
 
whm1974
Gerbil Elder
Topic Author
Posts: 5761
Joined: Fri Dec 05, 2014 5:29 am

Re: GCC errors with compiling C code.

Wed Nov 08, 2017 8:22 pm

OK I'm posting this as a way to stay on track and to help me to learn to write proper and portable code from the very start. So there are 32 keywords that C has and they are:

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

Did I miss anything? Is goto still used? I thought everyone quite using it a while back.
 
just brew it!
Gold subscriber
Administrator
Posts: 51173
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GCC errors with compiling C code.

Wed Nov 08, 2017 8:25 pm

DancinJack wrote:
FWIW, that code should really look like this. You might get a warning or two about types, but that should compile and run. (haven't worn the C hat in a long time, and it just reminds how much more I like C++)

#include<stdio.h>

main()
{
  int number;
  printf("Type a number: ");
  scanf("%d", number);

  /* check whether the number is negative number */
  if (number < 0)
  {
    /* If it is a negative then convert it into positive. */   
    number = -number; 
    printf("The absolute value is  %d", number);
  }
  return 0;
}

You need a '&' in front of 'number' in the scanf() call, and a '\n' at the end of the string in the printf would be good, otherwise the next CLI prompt will appear on the same line.
Nostalgia isn't what it used to be.
 
whm1974
Gerbil Elder
Topic Author
Posts: 5761
Joined: Fri Dec 05, 2014 5:29 am

Re: GCC errors with compiling C code.

Wed Nov 08, 2017 8:28 pm

Thanks JBI I'll start using the ones the Manjaro guys sent me. Was those the wikibooks.org ones? This having to keep starting over is getting annoying. But I'll keep at it. And thanks for the corrected code example DancinJack.
 
just brew it!
Gold subscriber
Administrator
Posts: 51173
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GCC errors with compiling C code.

Wed Nov 08, 2017 8:31 pm

whm1974 wrote:
OK I'm posting this as a way to stay on track and to help me to learn to write proper and portable code from the very start. So there are 32 keywords that C has and they are:

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

Did I miss anything? Is goto still used? I thought everyone quite using it a while back.

Goto is still used occasionally, but generally frowned upon. There are situations where it can arguably make sense, but I avoid it in my own coding.

There's really not much point to memorizing all of the keywords up front (other than maybe to avoid trying to accidentally use them as variable names). Learn them as you go.

You probably won't use register or volatile until you're much more advanced (if ever); register is probably largely superfluous given current optimizing compilers anyway. The auto storage class was superfluous (effectively a no-op) in C and has a completely different meaning in recent dialects of C++.
Nostalgia isn't what it used to be.
 
just brew it!
Gold subscriber
Administrator
Posts: 51173
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GCC errors with compiling C code.

Wed Nov 08, 2017 8:35 pm

whm1974 wrote:
Thanks JBI I'll start using the ones the Manjaro guys sent me. Was those the wikibooks.org ones?

Yes.

whm1974 wrote:
This having to keep starting over is getting annoying.

That's self-inflicted. What made you go off and start trying to use those other sites/eBooks when you already had a recommendation from a source that (arguably) should have a clue (the Manjaro forums)? A majority of the content on the web is junk, and that includes tutorials/eBooks.

Stay focused!

whm1974 wrote:
But I'll keep at it. And thanks for the corrected code example DancinJack.

Please note my corrections to his corrections as well!
Nostalgia isn't what it used to be.
 
whm1974
Gerbil Elder
Topic Author
Posts: 5761
Joined: Fri Dec 05, 2014 5:29 am

Re: GCC errors with compiling C code.

Wed Nov 08, 2017 8:40 pm

just brew it! wrote:
whm1974 wrote:
Thanks JBI I'll start using the ones the Manjaro guys sent me. Was those the wikibooks.org ones?

Yes.

whm1974 wrote:
This having to keep starting over is getting annoying.

That's self-inflicted. What made you go off and start trying to use those other sites/eBooks when you already had a recommendation from a source that (arguably) should have a clue (the Manjaro forums)? A majority of the content on the web is junk, and that includes tutorials/eBooks.

Stay focused!

whm1974 wrote:
But I'll keep at it. And thanks for the corrected code example DancinJack.

Please note my corrections to his corrections as well!

Yeah 80% of everything is crap, and it is looking like I'm learning that the hard way. :roll: :o
 
DancinJack
Grand Gerbil Poohbah
Posts: 3630
Joined: Sat Nov 25, 2006 3:21 pm
Location: Kansas

Re: GCC errors with compiling C code.

Wed Nov 08, 2017 8:43 pm

just brew it! wrote:
You need a '&' in front of 'number' in the scanf() call, and a '\n' at the end of the string in the printf would be good, otherwise the next CLI prompt will appear on the same line.


Well, that's what I get for trying to C from a 2004 memory.
i7 6700K - Z170 - 16GiB DDR4 - GTX 1080 - 512GB SSD - 256GB SSD - 500GB SSD - 3TB HDD- 27" IPS G-sync - Win10 Pro x64 - Ubuntu/Mint x64 :: 2015 13" rMBP Sierra :: Canon EOS 80D/Sony RX100
 
just brew it!
Gold subscriber
Administrator
Posts: 51173
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GCC errors with compiling C code.

Wed Nov 08, 2017 8:48 pm

Just spotted another (possible) fix. The printf() should probably be outside the if (assuming you want it to print the number even when it was positive to begin with).

Also, if you want to be fully C99 compliant main() needs to have a return type (int). Gcc will issue a warning if you compile it in c99 mode without the int return type.

Final version:
#include<stdio.h>

int main()
{
  int number;
  printf("Type a number: ");
  scanf("%d", &number);

  /* check whether the number is negative number */
  if (number < 0)
  {
    /* If it is a negative then convert it into positive. */
    number = -number;
  }
  printf("The absolute value is  %d\n", number);
  return 0;
}
Nostalgia isn't what it used to be.
 
JustAnEngineer
Gold subscriber
Gerbil God
Posts: 18066
Joined: Sat Jan 26, 2002 7:00 pm
Location: The Heart of Dixie

Re: GCC errors with compiling C code.

Wed Nov 08, 2017 8:56 pm

Your coding looks fine, but "a number" describes a set that cannot be stored in an int. Shouldn't that be a float or a double?
i7-8700K, H100i v2, RoG Strix Z370-G Gaming, 16 GiB, RX Vega64, 960Pro SSD, 5TB HDD, Define Mini-C, SS-660XP2, C32HG70, RK-9000BR, MX518
 
Redocbew
Gold subscriber
Gerbil Jedi
Posts: 1599
Joined: Sat Mar 15, 2014 11:44 am

Re: GCC errors with compiling C code.

Wed Nov 08, 2017 9:40 pm

Possibly. I don't remember if C would do an implicit conversion when comparing a float or double against integer 0, but I'm guessing the original author just didn't think of it. Like JBI said earlier, it doesn't seem like there was much if any review before posting the code.
Do not meddle in the affairs of archers, for they are subtle and you won't hear them coming.
 
whm1974
Gerbil Elder
Topic Author
Posts: 5761
Joined: Fri Dec 05, 2014 5:29 am

Re: GCC errors with compiling C code.

Wed Nov 08, 2017 9:43 pm

Redocbew wrote:
Possibly. I don't remember if C would do an implicit conversion when comparing a float or double against integer 0, but I'm guessing the original author just didn't think of it. Like JBI said earlier, it doesn't seem like there was much if any review before posting the code.

Yeah I just learn the hard way to be careful where I get my example code from. :roll: :evil:
 
Redocbew
Gold subscriber
Gerbil Jedi
Posts: 1599
Joined: Sat Mar 15, 2014 11:44 am

Re: GCC errors with compiling C code.

Wed Nov 08, 2017 9:52 pm

The really depressing part is how much production code is out there which is pretty bad also, but you don't have to worry about that yet. Stay on it with your wikibook, and you should be headed in the right direction.
Do not meddle in the affairs of archers, for they are subtle and you won't hear them coming.
 
whm1974
Gerbil Elder
Topic Author
Posts: 5761
Joined: Fri Dec 05, 2014 5:29 am

Re: GCC errors with compiling C code.

Wed Nov 08, 2017 9:54 pm

Redocbew wrote:
The really depressing part is how much production code is out there which is pretty bad also, but you don't have to worry about that yet. Stay on it with your wikibook, and you should be headed in the right direction.

I have downloaded it as a PDF file and I am reading it now.
 
just brew it!
Gold subscriber
Administrator
Posts: 51173
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GCC errors with compiling C code.

Wed Nov 08, 2017 10:17 pm

Redocbew wrote:
Possibly. I don't remember if C would do an implicit conversion when comparing a float or double against integer 0, but I'm guessing the original author just didn't think of it. Like JBI said earlier, it doesn't seem like there was much if any review before posting the code.

It'll do an implicit conversion in that situation. You could make it handle decimals by replacing int with float, and the %d in both format strings with %f. (Or use double, and %lf for the formats, if you want double precision.)

Or you could just change the prompt string to say "Type an integer". :wink:

TBH the example also has yet another, more subtle flaw: If the user types something other than a valid numeric input, the value of number will be undefined. You might get 0 in this case, or you might get random garbage. The return value of scanf() should be checked (it returns the number of values successfully read and parsed), to verify that a valid number was input.

Redocbew wrote:
The really depressing part is how much production code is out there which is pretty bad also, but you don't have to worry about that yet. Stay on it with your wikibook, and you should be headed in the right direction.

Unless you're dealing with a development organization with good internal software QA processes (which is much less common than you would think), or code that is subjected to formal certification (e.g. the FAA certification that is required for any avionics code that affects flight safety), code quality out in the real world is generally pretty abysmal. The "not checking the return value of scanf() and other similar library functions" error I noted above is pretty common, for example.
Nostalgia isn't what it used to be.
 
whm1974
Gerbil Elder
Topic Author
Posts: 5761
Joined: Fri Dec 05, 2014 5:29 am

Re: GCC errors with compiling C code.

Wed Nov 08, 2017 10:19 pm

just brew it! wrote:
Redocbew wrote:
Possibly. I don't remember if C would do an implicit conversion when comparing a float or double against integer 0, but I'm guessing the original author just didn't think of it. Like JBI said earlier, it doesn't seem like there was much if any review before posting the code.

It'll do an implicit conversion in that situation. You could make it handle decimals by replacing int with float, and the %d in both format strings with %f. (Or use double, and %lf for the formats, if you want double precision.)

Or you could just change the prompt string to say "Type an integer". :wink:

TBH the example also has yet another, more subtle flaw: If the user types something other than a valid numeric input, the value of number will be undefined. You might get 0 in this case, or you might get random garbage. The return value of scanf() should be checked (it returns the number of values successfully read and parsed), to verify that a valid number was input.

Well at least I'm learning to avoid writing bad code.
 
just brew it!
Gold subscriber
Administrator
Posts: 51173
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GCC errors with compiling C code.

Wed Nov 08, 2017 10:28 pm

In the real world, the process we just went through with that simple program would've been called a "code review". Code reviews are part of the process in well-run development groups. In a nutshell, you get one or more developers other than the original author of the code to analyze it, looking for potential bugs.
Nostalgia isn't what it used to be.
 
whm1974
Gerbil Elder
Topic Author
Posts: 5761
Joined: Fri Dec 05, 2014 5:29 am

Re: GCC errors with compiling C code.

Wed Nov 08, 2017 10:37 pm

just brew it! wrote:
In the real world, the process we just went through with that simple program would've been called a "code review". Code reviews are part of the process in well-run development groups. In a nutshell, you get one or more developers other than the original author of the code to analyze it, looking for potential bugs.

I can imagine that it is nearly impossible for one person to write code for highly complex and large program without being really buggy.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 10

Who is online

Users browsing this forum: No registered users and 2 guests