Question:
Learning C programming/ having a java background?
Giovanni
2014-06-19 06:53:49 UTC
Hey there!
Quick question to the wise people of yahoo. I am a Senior CS Major and i have programmed in Java, Unix and in serval web programing languages. I start C programming next semester and have roughly 2 month til class begins again. i want to get a head start on this course as my instructor is known in the CS dept as "hardcore." I have the text we will be using, can anyone rougly write a simple C method that entails the principle concepts of C? i.e. pointers, or other important concepts. so i can study it and better understand how C works. any possible exercise problems i can find on the web to better my knowledge i would appreciate any advice and/or guidance. Have a good summer ! and program on!


Giovanni
Four answers:
?
2014-06-19 09:59:32 UTC
Wren gave a good summary of things to watch for.

An example of one of C's odd confusing syntax with pointers happens with structs.

If you have a struct ABC with three elements (a,b,c) you

access them with dot '.' notation:

ABC.a

ABC.b

ABC.c



But if you have a pointer to struct ABC you have to use a different

syntax:

pABC->a

pABC->b

pABC->c



C has several interesting syntax traps like this. And as the C ANSI standard has evolved, and newer compilers have implemented more stringent checks, you run into things that used to work, now give you compiler warnings. Usually these still work, but the warning tells you that you might have a problem on some systems.



Good luck.
SteveO
2014-06-19 07:17:35 UTC
There are more than abundant examples of C code in the wild, especially on Unix and Linux systems. Download the source code for Git and take a look at that to see how C is, or even the source code for the FreeBSD or Linux kernels.
Wren
2014-06-19 07:39:52 UTC
There are plenty of examples of C code, but I'm afraid I don't have personal experience with many of the tutorials.



This section "C Tutorial - C Made Easy" looks to be fairly decent from this site: http://www.cprogramming.com/tutorial.html

It looks like it talks about code and show examples, but also has a little "self-guided" quiz. I list other sites that might help in the "sources" section.



If you know Java, then you shouldn't have too much difficulty in picking up the syntax - an `if` statement in Java is basically the same as in C++ and basically the same as in C. However, while learning C, I would caution you to not skip over, or just "skim" when a tutorial or book covers these concepts. While similar, and probably easy to "read", each language has it's own nuances. Additionally, the tutorial might introduce a new concept while talking about such concepts. For example, in C and C++, an if statement doesn't use boolean, it uses integers. In fact, C does not have a boolean type at all! The `if` statement is considered "false" if the expression is equal to zero, and "true" if the expression is non-zero. Carefully learn about each concept, even if they appear to be "the same as in Java". Some differences are incredibly subtle, but will cause you grief if not learned properly.



The major difficulties will probably be as follows:

* Pointers - Java doesn't have them. Well, it does, but you probably didn't recognize them because the language insists on taking care of it for you. Did you ever really think about what a "NullPointerException" was? Pointers exist in Java, but aren't really used by the programmer. In C, the opposite approach is used - pointers are everywhere, and usually because you, the programmer, put them there.

* Reference/Dereference - this is basically the same idea as pointers. Still, these operators are something to look out for.

* Error handling - Java Virtual Machine catches exceptions and errors (like NullPointerException) and is incredibly helpful in attempting to debug a program. C and C++ is much more difficult to debug, especially when attempting to debug anything with a pointer - you will learn to hate the term "seg fault".

* Garbage collection - Java has its own garbage collection mechanism. You call "new Object()" all the time in Java, but you never actually "delete" it - Java does that for you. C and C++ will not "delete" or "free" anything unless you, the programmer, explicitly tell it to do so.

* Strings, data types - Java has the String object. C++ has something similar in one of it's libraries. C does not. It uses arrays of characters to accomplish anything like a "string". As mentioned before, C does not even have a boolean type - it uses integers to achieve the same effect.

* Low level - C is much more low level than Java is, and will probably frustrate you at the beginning. "I can do this in one line in Java, but I have to take ten to do the equivalent in C!" More lines of code means more lines you have to introduce a bug, and because debugging is more difficult in C, you will find it takes much more time to write a program in C, especially when starting out.



My suggestion for you is this: take it slow. You know how to program, but you don't know how to program in C. Take care in writing the code. Use a good IDE, if you can - it will help you catch errors and save you time in the long run.



By comparison, it's much easier for people to go from C to C++ or from C++ to Java, but going back down the ladder is difficult, because it requires relearning concepts you didn't fully understand, concepts you didn't even realize you had missed.



Good luck!
Kayleigh
2014-06-19 07:49:19 UTC
You can read books at safaribooksonline.com or read tutorials at lynda.com and codeacademy.com. Or you can watch tutorials in Youtube. You can also ask at StackOverflow for other resources and research on Google for other useful tutorials sites.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...