LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   So, I decided to try and learn C (https://www.linuxquestions.org/questions/programming-9/so-i-decided-to-try-and-learn-c-4175643886/)

jsbjsb001 12-08-2018 08:31 AM

So, I decided to try and learn C
 
1 Attachment(s)
Hi Guys/Gals.

So, as the title says, I'm currently trying to learn C. I guess first it probably helps if I give some background;

Background:

While I'm not new to computers, Linux, operating systems in general, etc, I can't really say the same for C. I did understand a tiny bit, but no where near enough to actually be able to write a program myself (and still am pretty green with it). Haven't had any real experience with it - or at least what most people I know would consider to be proper experience.

I'm doing this for a few reasons; the main reason is that, I'm at that stage of life where I want to try and find something that I can not only do, but ENJOY too. Sounds easy right? Well no, it isn't. So I figured if I commit myself to at least getting though the tutorial I'm currently following, and hopefully this can give me a clear cut answer to that question. And yes, I'm sick of looking at source code and not being able to understand the code properly.

Just in case I have anymore dumbass questions for ya's, I'll leave this thread open until I'm sure that I have no questions - rather that opening thread after thread.

My Questions:

1) I know there is a C function for opening files (the chapter I'm currently up to), but there is also a kernel system call that also opens files, being the open() function. I don't understand the overlap and I can't find anything that really makes a lot of sense to me at this point. Is there any reason for this? Which should I use? I don't get it, I'm sorry.

2) I've also attached some code that I've been playing with, but I'm just too green to really understand how good or bad it is - it does compile and work (I even fixed the loop in it too). So I guess I'm asking if people that know more (hopefully a LOT more) than myself could just have a look at it, and see if there is something that I missed/written incorrectly, etc. I know that sounds stupid, but it really would be good for me to get more than just my eyes reviewing it.

3) Is there anything you can recommend to make life simpler?

4) While this maybe a question for later on; is there any real benefit to using an IDE? As right now I'm just using Kwrite and gcc directly.

Please don't hold back if you can see something that could be better. But please remember that I am the newbie here in this case, so please treat me like I'm 5 years old and need it in simple terms. I've also tried to add my own comments as well - I basically took the example code, and added my own bits and pieces to it.

Don't worry, I'm not expecting to be spoon-fed here and I'm not asking for that. As that does nothing to help me learn - I want to be to do it one day and solve my own problems (as I pretty much already do with any Linux problems I may have). So I'll still be practising anyway.

It's this tutorial that I'm currently following, and I'm up to "C File I/O".

Thanks in advance for any help.

James

hazel 12-08-2018 09:28 AM

I did the same. I taught myself so you can too.
Quote:

Originally Posted by jsbjsb001 (Post 5934791)
My Questions:

1) I know there is a C function for opening files (the chapter I'm currently up to), but there is also a kernel system call that also opens files, being the open() function. I don't understand the overlap and I can't find anything that really makes a lot of sense to me at this point. Is there any reason for this? Which should I use?

The C library functions like fopen() are simply wrappers for the kernel functions like open() that actually do the work. They are more programmer-friendly than the raw system calls and also more portable, so you are recommended to use them
Quote:

2) I've also attached some code that I've been playing with, but I'm just too green to really understand how good or bad it is - it does compile and work (I even fixed the loop in it too). So I guess I'm asking if people that know more (hopefully a LOT more) than myself could just have a look at it, and see if there is something that I missed/written incorrectly, etc. I know that sounds stupid, but it really would be good for me to get more than just my eyes reviewing it.
I'll have a look at it, but in the mean time I'm posting a few general comments.
Quote:

3) Is there anything you can recommend to make life simpler?
Not really. C isn't a simple language. It was written to be powerful, not simple.
Quote:

4) While this maybe a question for later on; is there any real benefit to using an IDE? As right now I'm just using Kwrite and gcc directly.
I've never used a full-scale IDE but I find the geany editor ideal for writing code.

hazel 12-08-2018 09:52 AM

1 Attachment(s)
Here is your program with some comments

KenJackson 12-08-2018 11:24 AM

Quote:

Originally Posted by jsbjsb001 (Post 5934791)
4) While this maybe a question for later on; is there any real benefit to using an IDE? As right now I'm just using Kwrite and gcc directly.

If you're lucky enough to use the same IDE for every project you work on, then it has value. But if you jump back and forth between Linux and Windows, and especially if you work on a variety of embedded processors, then you probably won't have the option of using the same IDE on every project.

That means there'll likely be different key sequences to move the cursor, cut and paste text and save the current file. So instead of thinking about how you want to structure or access your program data, you'll have to stop and think how to move the cursor.

But if you choose one editor and stick with it everywhere, you'll never have to stop and think about how to zip the cursor to the start of the line. You'll just decide to do it and it'll happen because your hands know how to make it happen without being told.

jsbjsb001 12-08-2018 11:55 AM

Thanks so much guys/gals!

KenJackson, Thanks for your post.

Hazel, Thank you for the extra comments - I'll try and use them and change the code to suit.

I was actually thinking about doing some exit status, and would like to learn more about that. I'm not sure I really understand the differences between return() and exit() So here's what I'm thinking;

The return() function means that (as it's name suggests), it returns a value or to another function (like the previous function called).

The exit() function means that, anything other than exit 0 means that an error has happened, and the program stops executing

??

Once again, try not to give me the answer per se, but how I can work it out - or some hints (if I make any sense at all there).

Thanks again!

hazel 12-08-2018 12:23 PM

Yes, that's more or less right. return() returns your function to its caller. Its argument should be the value that the function is returning. exit() terminates the whole program and returns a value to the shell.

You can illustrate how the exit function works by using grep on a file and then typing "echo $?". This always gives the exit code of the previous command. If you grep for a string that occurs in the file, $? will be 0. If the string does not occur, $? will be 1. But in any case, the program will have ended.

astrogeek 12-08-2018 06:12 PM

Great initial replies!

I tried several times, long ago, to use IDE's, and always ended up thinking surely I am missing the point somewhere, AAAACCCCKKK!!

KenJackson's comments about learning and using a single editor effectively are exactly spot on, in my opinion! To which I will add that doing so with a terminal text editor as opposed to any flavor of gooey editor will multiply the advantages. You can always get a shell, but you cannot always work within a gooey tool.

"Standardizing" your development environment to a shell on a Linux/Unix platform means that you will alway be at or near "home", no matter where you are. Per KenJackson's comments on the value of reflex-like keystrokes in the editor, same and more goes for navigating your development tree with shell commands instead of gooey tools. Ditto for the compiler and other build tools - learn to use make and gcc from the shell first, not from the choices offered by an IDE.

Not only do you learn the keystrokes, but you end up organizing - and understanding - your own code structure instead of falling back on IDE defaults. I have been dumbfounded at times by otherwise talented programmers who had no idea where to actually find their source code on a filesystem outside their IDE.

My obvious lack of love for the gooey environment aside, use IDE's when appropriate for a project - QT development comes to mind. But form your habits and learn the language itself in its native form - pure textual structures, you will never regret it!

There are also some really good resources in the C/C++ sticky at top of the programming forum. Most of it is still good except for the inevitable broken links. As you learn, please feel free to add the most useful current resourcess you find!

Good luck!

syg00 12-08-2018 07:15 PM

Quote:

Originally Posted by astrogeek (Post 5934939)
My obvious lack of love for the gooey environment aside, use IDE's when appropriate for a project ...

:p
Many tears ago in a century long past, I discovered what an IDE was meant to be. Borland Delphi on Windoze - probably 3.1.
I had no experience of little-endian environments or windowing systems - I was blown away with Delphi as I had done a little pascal at Uni.

These days I'm looking at Dart and flutter for Android app developent - no way I'd consider not using an IDE to prop me up.

scasey 12-08-2018 07:30 PM

Quote:

Originally Posted by astrogeek (Post 5934939)
[snip]
But form your habits and learn the language itself in its native form - pure textual structures, you will never regret it!

^^+1.
I was once asked what tool I used to develop web pages. I replied, “Notepad”...which was not at all helpful to the questioner, of course.

I have since discovered ScITE, a GUI editor that runs on both Linux and Windows, so that gives me the constant look and feel wherever I am. No GUI? nano...

mina86 12-08-2018 07:40 PM

Quote:

Originally Posted by jsbjsb001 (Post 5934847)
The return() function means that (as it's name suggests), it returns a value or to another function (like the previous function called).

Just to clear one confusion: return is not a function but a statement, it does not need parenthesise and it doesn’t take arguments in the sense functions take arguments.

PS. One more thing I’ve noticed in hazel’s commentary: Firstly, there was a typo where it should read char **argv rather than char *argv. (Note that you can also see it written as char *argv[] but those two are equivalent when declaring function arguments).

Secondly, and this is a bit more pedantic I suppose, C standard explicitly allows int main(void) as a valid prototype of the main function so you don’t have to use int main(int argc, char **argv) if you ignore command line arguments.

I would also recommend, at least at the beginning, explicitly using foo(void) when you declare or define function which takes no arguments. void is not necessary in definitions but until you’re comfortable with what a declaration and what a definition is just stick to foo(void).

jsbjsb001 12-08-2018 09:27 PM

Thanks guys!

I think it's safe to say that I can skip the IDE at least for now. I was also thinking before that I've really gotta be able to read the code and understand it, otherwise it's not much good worrying about an IDE. I'll stick with Kwrite and using gcc directly, at least until I've got a better handle on this coding caper.

astrogeek, thanks for your post and stopping by. :) :thumbsup:

mina86, thank you for your post and correction - I'll try and remember that in future, thanks again for that (and for taking the time to help).

I'll try and re-jig my code to reflect the advice given to date. While I'll try and report back ASAP, it might be a good idea for me to re-read at least some of the chapters in the tutorial I'm following. So, I'm not ignoring anyone if I take a little time to report back. ;)

Thanks again to all!

jsbjsb001 12-09-2018 04:54 AM

1 Attachment(s)
So, I've just tried to do my exit status, as well as implementing the suggests so far, but well... I've broken it and now cannot compile it and get a stack of warnings. I moved my variables from being global variables to local variables, which is when my problems started. I was getting errors as well, but I managed to fix them, but I have no idea what I've done wrong here.

I haven't changed scanf() yet, as I'll have to read up more about what Hazel suggested instead.

I've attached my "updated" code, and here's gcc's output below;

Code:

[james@jamespc devel]$ gcc test.c -o test
test.c: In function ‘main’:
test.c:39:5: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
    printf("This program uses variables, if statements and functions to display which numbers you have entered and carry out mathematical operations on the numbers entered\n");
    ^
test.c:44:5: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
    scanf( "%d %d", &a, &b );
    ^
test.c:81:7: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
      exit(1); }
      ^
test.c: In function ‘value_less_than_100’:
test.c:99:3: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
  printf("Moved to function value_less_than_100()'\n");
  ^
test.c:101:3: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
  scanf("%d %d %d", &e, &f, &g );
  ^
test.c: In function ‘value_not_less_than_100’:
test.c:129:3: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
  printf("Moved to function value_not_less_than_100()\n");
  ^
test.c:137:4: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
    scanf("%s %d", str, &i);
    ^

Any pointers about what I'm doing wrong would be great. Thanks.

hazel 12-09-2018 06:15 AM

Those are warnings, not errors. I'm not sure what's causing them but they shouldn't stop the compilation from completing. Are there messages that actually contain the word "Error"? Those are the ones you need to look at.

Now another piece of advice. The printf function is very useful for printing out mixtures of text boilerplate and variables (for example on line 46) but it's overkill for plain strings. Try replacing it with puts(). This is the function you usually use for that kind of thing. And you don't need separate statements for all those newlines. Just put them in at the end:
Code:

puts("ramble, ramble, ramble\n\n\n");
The simpler your code, the less likely it is to annoy the compiler.

jsbjsb001 12-09-2018 06:22 AM

Yeah, you're right, they are indeed only warnings, but the thing is, it's still returning to the CLI after spitting out the warnings. I thought that (and I'm sure I've seen it with other things I've compiled before) if it's just a warning, then it would still compile anyway, but it doesn't.

Thanks for the advice, I'll have a look at puts() hopefully later on tonight or tomorrow. Thanks again for your help Hazel, it's been very helpful to me. I'm in your debt. Thank you. :)

hazel 12-09-2018 06:42 AM

Sorry but I don't understand your answer. Why shouldn't gcc return to the cli when it's finished compiling? Have you tried launching the compiled program? That's the test. And btw, when recompiling, always get rid of the old compiled code first to be safe.


All times are GMT -5. The time now is 04:11 PM.