C for Java ProgrammersGeorge FergusonSummer 2016(Updated Summer 2021)


Contents1Introduction72Overview of Java and C92.1What’s The Same? . . . . . . . . . . . . . . . . . . . . . . . . .92.2What’s Different? . . . . . . . . . . . . . . . . . . . . . . . . . .1034Development and Execution113.1Development and Execution in Java and C . . . . . . . . . . . . .113.2Setting Up Your Development Environment . . . . . . . . . . . .143.3Writing Your First C Program . . . . . . . . . . . . . . . . . . .163.4Compiling Your First C Program . . . . . . . . . . . . . . . . . .17Basic Expressions and Statements214.1Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214.2Primitive Types . . . . . . . . . . . . . . . . . . . . . . . . . . .224.3Producing Output . . . . . . . . . . . . . . . . . . . . . . . . . .234.4Operators and Expressions . . . . . . . . . . . . . . . . . . . . .244.5Variables and Assigment . . . . . . . . . . . . . . . . . . . . . .264.6Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .274.7Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .293

456CONTENTSControl Flow315.1Conditional Statements . . . . . . . . . . . . . . . . . . . . . . .315.2Iteration Statements . . . . . . . . . . . . . . . . . . . . . . . . .325.3Other Control Flow Statements . . . . . . . . . . . . . . . . . . .33Functions356.1Function Parameters and Arguments . . . . . . . . . . . . . . . .366.2Function Declarations . . . . . . . . . . . . . . . . . . . . . . . .377Structured Types398Memory Management438.1Variables, Addresses, and Pointers . . . . . . . . . . . . . . . . .438.2Passing Arguments by Reference . . . . . . . . . . . . . . . . . .468.3Memory Allocation . . . . . . . . . . . . . . . . . . . . . . . . .488.4Dynamic Memory Allocation in Java . . . . . . . . . . . . . . . .498.5Dynamic Memory Allocation in C . . . . . . . . . . . . . . . . .508.6Dynamic Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . .548.7Dynamic Data Structures . . . . . . . . . . . . . . . . . . . . . .568.8Function Pointers . . . . . . . . . . . . . . . . . . . . . . . . . .649Defining New Types10 Sharing Code: Files and Libraries697310.1 The C Preprocessor . . . . . . . . . . . . . . . . . . . . . . . . .7310.2 Separate Compilation, Libraries, and Linking . . . . . . . . . . .7510.3 Standard System Libraries . . . . . . . . . . . . . . . . . . . . .76

CONTENTS510.4 Project Development . . . . . . . . . . . . . . . . . . . . . . . .7710.5 Building Larger C Programs . . . . . . . . . . . . . . . . . . . .7911 Debugging a C Program8311.1 Debuggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8411.2 Compiler Options . . . . . . . . . . . . . . . . . . . . . . . . . .8411.3 valgrind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8512 Final Thoughts8713 References89


Chapter 1IntroductionWhen I teach introductory programming, I tell students that they are learning aforeign language: the language understood by the computer. The purpose of programming is to translate your ideas about how to solve a problem into a languagethat the computer understands so that it can follow your instructions.You are a Java programmer. You are already fluent in the Java programminglanguage. But now you find that you need to learn a new programming language,a language called “C.” This is just like learning a second (human) language.As I’m sure you know, some human languages are more similar than others. Ifyou know Spanish, you can learn French or Italian relatively easily. Many of theconstructions are the same, although there are some important differences in thedetails. On the other hand, if you know English, it’s of relatively little use to youin learning Chinese or Japanese—they don’t even use the same characters!Luckily for you, Java and C are closely related. In fact, Java was developed bystarting with C and adding features designed to help programmers develop complex programs more quickly and with fewer errors. Thus you will have no problemunderstanding the high-level structure of a C program. There are important differences that we will point out, starting in the next sections, but it really is a fairlyeasy transition.7

8CHAPTER 1. INTRODUCTIONKeep in mind as you learn C that you are stepping back in the history of programming. C was developed in the early 1970’s when computers were much simpler(and less powerful) than today. Java appeared in the mid-1990’s and has beenevolving and expanding ever since. A fundamental thing to realize is that C provides much less support to the programmer. It is much easier to make mistakesand very often harder to figure out how to fix them. It requires some discipline touse correctly and effectively.So why would anyone use C? There are a couple of reasons. First, because asyou will see in Section 3, a C program runs in a much smaller memory footprint.This makes C the choice for embedded systems and other environments wherememory is at a premium. Second, because C programs run with less support, theymay also run faster. Although higher-level languages like Java and C# have gottenfaster, it is still probably the case that tightly coded C is as fast as you can getwithout writing assembly code, which is not only much harder but by definitionnot portable across platforms. If speed is important, C is often the choice.1 Athird reason to use C is that it is kind of the universal interchange language. Manyother languages interoperate with C, allowing you to connect components writtenin different languages into one application using C. Finally, because C is moreminimalist than many of its successors, it forces the programmer to confront someilluminating design and implementation questions and really think about whattheir code is doing.Please note that this guide is not a definitive manual for the C programming language. For that, you should get a copy of The C Programming Language, SecondEdition by Brian Kernighan and Dennis Ritchie. I will refer to this as “K&R” inthe rest of this document. Not only is it the definitive specification of the language,it’s one of the clearest, most useful books you will ever read. You will learn thingsabout programming and programming languages that you can apply to any language, including Java. Knowing C will make you a better Java programmer, aswell as having an additional very useful tool in your programming toolbox.1On the flip side, for many, possibly even most, programs, speed is not that important, andcertainly much less important than correctness.

Chapter 2Overview of Java and CLet’s start with a quick overview of the similarities and differences between Javaand C.2.1What’s The Same?Since Java is derived from C, you will find many things that are familiar: Values, types (more or less), literals, expressions Variables (more or less) Conditionals: if, switch Iteration: while, for, do-while, but not for-in-collection (“colon”syntax) Call-return (methods in Java, functions in C): parameters/arguments, returnvalues Arrays (with one big difference) Primitive and reference types Typecasts Libraries that extend the core language (although the mechanisms differsomewhat)9

10CHAPTER 2. OVERVIEW OF JAVA AND C2.2What’s Different?On the other hand, C differs from Java in some important ways. This section givesyou a quick heads-up on the most important of these. No classes or objects: C is not an object-oriented language, although it hasstructured types (struct and union) and there are ways of doing thingslike inheritance, abstraction, and composition. C also has a mechanism forextending its type system (typedef). Arrays are simpler: there is no bounds checking (your program just dies ifyou access a bad index), and arrays don’t even know their own size! Strings are much more limited, although the C standard library helps. No collections (lists, hashtables, etc.), exceptions, or generics. No memory management: You must explicitly allocate (malloc) and release (free) memory to use dynamic data structures like lists, trees, andso on. C does almost nothing to prevent you from messing up the memoryused by your program. This is the number one cause of problems for new(and old) C programmers. Pointer arithmetic: You can, and often have to, use addresses of items inmemory (called pointers). C allows you to change the contents of memoryalmost arbitrarily. This is powerful but also dangerous magic.Bottom line: When you program in C you are closer to the machine. This givesyou more flexibility but less protection. The next section explains this in moredetail by comparing the development and execution models of Java and C.

Chapter 3Development and Execution3.1Development and Execution in Java and CFigure 3.1 (page 12) shows the basic components involving in developing andexecuting a Java program. You should already be familiar with this process, so Iwill describe it only briefly. You write your program (source code) as a text file using the Java programming language. The name of your source file ends in “.java”. You compile your source code using the javac command (“Java compiler”) to create a Java class file containing “bytecode” (.class file). Bytecode does not use the native instruction set of any real computer. Instead, it uses an abstract instruction set designed for an abstract computercalled the “Java Virtual Machine” or “JVM.” To run your program, you run the java command, which simulates theJVM running on your actual computer. You tell it to load and run your classfile (bytecode), which results in your code being executed. That is, you have a “real” program, java, running on your computer, andit is simulating the execution of the bytecode of your program on the “notreal” JVM. This is slower than running native code directly on the computer,but also safer and better for development. Java programs can use other classes and libraries of other classes (jars) toprovide additional functionality. In fact, most of Java is library classes.11

12CHAPTER 3. DEVELOPMENT AND cutable “compile tonative code”“run”CPUFigure 3.1: Java Development and Execution le”cc“link”.oldobjectcode.o.a .so.dll.dyliblibrariesexecutable“run”CPUFigure 3.2: C Development and Execution Environment

3.1. DEVELOPMENT AND EXECUTION IN JAVA AND C13 To partially compensate for the slowdown due to interpreting bytecode, Javaincludes a feature called a “Just In Time” or “JIT” compiler. This part ofthe java runtime system detects pieces of Java code that are executed frequently and compiles them into the real instruction set of the host computer(native code). This is faster than interpreted bytecode, but usually not quiteas fast a native code. Finally, note that the Java compiler, javac, is itself a Java program, writtenin Java! That is, running javac means running java (the Java runtime)and telling it to run the class for the Java compiler. I’ll let you think abouthow the first Java compiler was written. . .By comparison, Figure 3.2 shows a similar component diagram for developmentand execution in C. I’ll point out the main differences briefly. In C, you write your program as a text file using the C programming language. The name of your source file ends in “.c”. You then compile your code into processor-specific object code (“.o”) using the cc command (for “C compiler”). The C compiler actually consists of two phases. The first is a “pre-processing”phase which can transform the text of your program in various useful ways.Then the compiler phase translates the resulting C program text into nativeobject code. Although it is technically possible to run just one or the otherphase of the C compiler, you will never do that in practice. Before your code can be run, it needs to be “linked” with any libraries thatit uses, including the C runtime. The result is an executable file containingnative object code. You can run your executable directly from the command line. If it crashes,your program dies. Sometimes this happens without even a message unlessyou are using a debugger. In the not-so-good old days, you might even crashthe entire machine. A compiled C program runs at full speed on the processor for which it wascompiled.

14CHAPTER 3. DEVELOPMENT AND EXECUTIONIn general, the key difference between Java and C is that in C you are runningmuch closer to the machine. When computers were less powerful and had lessmemory, it was vital to squeeze out every drop of performance in order to doeven simple computing tasks. The history of programming languages is a continual evolution from low-level object code and assembly language to higher-levellanguages like Fortran, Algol, Lisp, Smalltalk, Java, Javascript, and all the othermodern languages. C sits somewhere in the middle, not as low-level as assemblylanguage, but without too many of the powerful (but possibly slow) features ofhigher-level languages. Interestingly, C was actually predated by some higherlevel languages. And some of the earliest languages had features that have onlyrecently been rediscovered.But as a Java programmer coming over to C, your development and executionprocess should be quite familiar. You write your code as one or more text files.You compile your code until the compiler is happy with it. There are IDEs for Cas for Java should you want to use them (see next section). There are libraries thatmake your life easier, and you can develop your own libraries and reusable code.Then you run your program and see if it works. If you need to debug it, there aredebugging tools for C as for Java. In C as in Java, these can catch fatal errors andallow you to post-mortem the execution rather than simply crashing.3.2Setting Up Your Development EnvironmentDevelopment environments, whether for Java or for C, are a deeply religious topic.You can opt to use an “Integrated Development Environment” (IDE), like Eclipse(cross-platform), Visual Studio (Windows), or XCode (Mac). Or you can worklike the pioneers did, using separate tools that they could combine in ingeniousways. For simple projects, an IDE is fine. For complicated projects that includesome amount of automated compilation or testing, it helps to know how thingswork under the hood so that you can build your own tools.I have collected some tips on getting setup to program in C: C ProgrammingResources (for CSC173 and beyond). Depending on when you are reading this, itmight be very out-of-date.In any case, most Computer Science students should become comfortable workingfrom the terminal (command shell). If that’s not yet you, then this is your chance

3.2. SETTING UP YOUR DEVELOPMENT ENVIRONMENT15to learn something new. Here are some things to look for: You’ll need an editor for writing your C programs: vim, Emacs, NotePad,TextEdit, BBEdit, . . . Text editors are another subject on which programmers have deeply-held religious views. You’ll need a C compiler and linker. The standard open-source tool is the“GNU Compiler Collection”, better known as gcc.– On Linux distributions, gcc is standard or can be easily installed usinga package manager.– On Apple platforms, you need either XCode or the Apple DeveloperCommand Line Tools (CLT). Apple originally used gcc, but nowprefers a compiler suite called clang/llvm (although the name gccstill works).– On Windows, it is possible to get gcc working, but it’s a bit tricky.Microsoft expects its developers to use Visual Studio. Check out theMinGW or Cygwin projects if you want an alternative. You’ll probably want a debugger. I’m a big believer in print statements fordebugging, but sometimes you do need to watch a program as it breaks. TheGNU debugger is gdb, for llvm the debugger is lldb, and on Windowsit’s Visual Studio or gdb. For anything but the simplest projects, you will need some kind of buildsystem to automate compilation and testing. The classic tool is make, andit is still frequently used for C development. More modern (but also waymore complicated) tools include ant and gradle, both of which are morecommonly used for Java development than for C.Finally, whether you use an IDE or a separate toolchain, you should learn tolove some kind of version control system. The original tools on UNIX wereSCCS (“Source Code Control System”) and CVS (“Concurrent Versions System”). These were replaced by Subversion (svn), which is still popular. Moremodern version control systems include Git, Mercurial (hg), and Bazaar (bzr).Note that you don’t have to store your files “in the cloud” (e.g., on GitHub) to benefit from a version control system. You can use it locally to track changes, makebranches, and revert to a prior state if you break your code five minutes beforeit’s due. Of course, you should also be sure to backup your files, but you alreadyknow that. . .

16CHAPTER 3. DEVELOPMENT AND EXECUTION/** File: hello.c*/#include stdio.h int main(int argc, char *argv[]) {printf("Hello world!\n");}Figure 3.3: “Hello World” Program in C3.3Writing Your First C ProgramFigure 3.3 shows the classic “Hello World” program which, by the way, first appeared to my knowledge in The C Programming Language. Let’s break it downby comparing it to the Java equivalent with which I’m sure you’re already familiar(otherwise check out “Hello World” Program at Wikipedia).First the comment at the top: block comment just like in Java.Next: #include: Semantically, this is like an import statement in Java, making some library functionality available to your program. In C, it is implementedquite differently, as described more fully in Section 10. In this case, we are including the definitions of the C “Standard Input/Output” (stdio) library so thatwe can use the printf function to produce output later in the program.Now, notice that there is no “main class.” Indeed, there are no classes at all in C.Instead, functions are defined at the toplevel of the file, as can be done in Python,for example, and are visible to any following code.The next block of code is the definition of the function main. Just like in Java,each program needs a main function and this is the function that is called whenthe program starts (Java inherited this from C, as a matter of fact).The function definition should look familar. It says that main is a function thatreturns an int (Java uses a static void method) and has two parameters.In Java you get an array of Strings passed to your program containing anyarguments specified on the command-line. In C, as you’ll see in Section 4.6,arrays don’t know their length, so the first parameter will be set to the number of

3.4. COMPILING YOUR FIRST C PROGRAM17arguments (traditionally named argc for “argument count”). Strings are definedas char* (“pointer to char”, see Section 4.7), and the parameter argv (for“argument vector”) is an array of these. So this looks a bit different but it meansexactly the same thing as in Java.And finally, the one-line body of the main function in curly brackets. Insteadof calling System.out.println (or format), we are calling the functionprintf, which is part of the C Standard input/output (I/O) library and defined instdio.h. But the function call itself is the same in C as in Java: the name of thefunction, followed by comma-separated values for the arguments in parentheses.There is only one argument in this call to printf: a string literal containing thetext we want to print. String literals use double quotes just like Java. The “\n”at end of the string might be new to you. It indicates a “newline” character, sinceprintf in C behaves like print in Java and does not print a newline the wayprintln does. There is no println in C. Furthermore, printf in C cantake additional arguments, and substitutes them for placeholders in the first stringargument, just like Java’s format methods. You will use this often since, unlikeJava, there is no string concatenation ( for strings) and there are no toStringmethods.And finally, a semi-colon at the end of every statement, just like Java.That’s the whole program. Just like Java, the program exits (stops running) whenthe main function is done. That is, just like Java unless you’re using multithreading either explicitly or implicitly, as with Swing graphics. And those things arenot so easy to do in C.This C program should be understandable to any Java programmer. The familyresemblance between the languages is very strong. Yes there are some importantdifferences, but you are not starting from scratch.3.4Compiling Your First C ProgramSo you’ve written your first C program, now you want to compile and run it. Ifyou’re using an IDE, it may have already done part of this for you. DifferentIDEs work differently. Some compile constantly in the background to warn youof errors; with others you need to tell it to compile. Either way, you edit your

18CHAPTER 3. DEVELOPMENT AND EXECUTIONsource code until it compiles successfully.If you are not using an IDE, you need to be comfortable working with a terminalwindow and command interpreter (or “shell”). To compile your C program, youneed to invoke the C compiler and tell it to compile your file. The simplest formof this is something like the following, which you would type at your commandshell prompt:cc hello.cThis assumes that your source file is named “hello.c” and that it is in the“current working directory” of the terminal (shell). It also assumes that your Ccompiler is named “cc”, which is traditional. You might need to try gcc orclang or whatever is right for your platform and development environment.When you run the C compiler, you will either get error messages or perhaps nooutput. If there is no output, you should be left with an executable program file inthe current working directory. For historical reasons, this file is called “a.out”regardless of what the source file was named. That isn’t usually so helpful, so youcan tell the C compiler to call the executable something else with the “-o” option:cc -o hello hello.cNow your executable will be called “hello” (on Windows it’s traditional to nameexecutables with names that end in “.exe”, but not on Unix-like platforms). Youshould see your new file if you type ls on Unix platforms, dir on Windows, orfind it in a window showing the directory (folder) with your source file in it.Now you need to run your program. With an IDE, this usually means some kind of“Go” button. Without an IDE, you need to tell your terminal command interpreter(shell) to run the newly-created executable file. For reasons that don’t concernus now, you probably can’t just type its name the way you did the cc command.Instead you probably have to give the pathname of the executable file. The easiestway to do this is:./helloThe “dot” (“.”) means “the current working directory, whatever it is,” so thispathname refers to the file named “hello” in the current working directory.

3.4. COMPILING YOUR FIRST C PROGRAM19Either way, your program should run and you should see its output printed eitherto the console of your IDE or to your terminal. Congratulations! You are now a Cprogrammer.The rest of this document builds on this basic program. You can use it as a template for trying the code examples. Try different variations and find out whatworks and what doesn’t. There’s only one way to learn a foreign language, andthat’s to get out there and use it to communicate. For learning programming languages, this means getting onto a computer and writing programs.


Chapter 4Basic Expressions andStatementsLet’s dive a bit deeper into the C programming language, emphasizing similaritiesto and differences from Java. As always, you’ll want to consult K&R for the finalword on C.As you already know from your prior programming experience: Comments are a crucial part of any program. All computer programs compute values. Values come in several types. Values can be written as literals (“literally this value”) or as expressions thatuse operators to combine values into new values.Java’s comments, values, types, and expressions are derived from C’s, so they arevery similar. Let’s take a look at each of these in a bit more detail.4.1CommentsThe syntax for comments in Java was taken from that for C. Text enclosed between/* and */ is ignored by the compiler. Modern C compilers also understand the// syntax that was introduced in C and is also used in Java.21

224.2CHAPTER 4. BASIC EXPRESSIONS AND STATEMENTSPrimitive TypesTable 4.1 shows the primitive types in both Java and C. As you can see, all thebasic numeric types in Java are inherited from blecharchar (see text)byteN/Aboolean N/ATable 4.1: Comparison of Java and C primitive typesHistorically these types were specified with somewhat less precision than in Java.For example, int was whatever was most natural for a given machine, and couldbe 16 bits, 32 bits, or something else. I’m not going to dwell on these details. Ifyou need to know how many bits are in an int, you can lookup how to do that.Two of Java’s primitive types do not exist in C: byte and boolean. That’s ok.Most people don’t use byte anyway. In C, use an int (or a short if you’rereally concerned about memory usage) and just don’t assign it a value less than-128 or greater than 127. I told you C gave you less protection than Java.Now booleans are genuinely useful, but if you think about it, they simply represent a true-false, yes-no, 1-0 value. So again, in C, use an int. C uses theconvention that 0 means “false” and any non-zero value means “true.” Booleanvariables and functions that return Boolean values are traditionally declared int.The stdbool library provides support for more explicit booleans in modern Ccompilers.

4.3. PRODUCING OUTPUT23In Java, a char represents a 16-bit Unicode character (a “code point”). In C, achar is an 8-bit byte representing an ASCII character. The difference mattersif you’re working with modern text sources which can include foreign characters,accents, math symbols, and, yes, emoji. There is some library support for handlingunicode in C, but it is not a strength of the language. On the other hand, a Cchar is exactly a byte, so you could use it for that if you wanted (there aresome signed/unsigned math considerations, and C has unsigned numeric types,including unsigned char).Literals for primitive types are just like in Java. Numeric representations for integer and floating point numbers. Character (char) literals in single-quotes. Stringliterals enclosed in double-quotes. Inside a character or string literal, use backslash for escapes, including \’, \", and \n.4.3Producing OutputWe’ve already seen the printf method to produce output in our “Hello World”program. In that example, we passed in the string that we wanted to print. Thismakes it seem like the Java method println from the In fact though, it’s more like the Java method format which uses the classjava.util.Formatter. Python 2 (and 3) has the “%” operator, and Python3 has the function format. All of these are derived from C’s printf.The printf function always takes at least one argument. That first argument toprintf is called the format string because it controls the format of the output.Any character in the format string other than a percent sign (“%”) or backslash(“\”) is simply printed as is.We’ve already seen that the backslash (“\”) is used to introduce characters thatcan’t be easily typed in a string, such as the newline \n. A backslash in front ofa percent sign “escapes” its special meaning (described next), meaning that \%prints as just “%”.An unescaped percent sign indicates that the value of the next argument to thefunction call is to be converted to text and printed at that point in the output.Unlike in Java, there is no automatic conversion of values to strings. Instead, thecharacter following the percent sign says what type of thing it is. For example“%d” indicates an integer (“decimal”) number, “%f” a floating-point number, and

24CHAPTER 4. BASIC EXPRESSIONS AND STATEMENTS“%s” a string. For a quick example:printf("%d %f %s\n", 123, 3.14159, "abc");will print123 3.14159 abcWe will see more examples when we look at expressions and variables.Another thing we will see shortly is that there is no string concatention in C. Ifyou are used to writing println statements using “ ”, you will need to breakthat habit and get used to specifying a format string and the arguments that fill the% specifiers.FYI: Wikipedia has an extensive description of printf format strings.4.4Operators and ExpressionsMost of the operators in Java were also derived from C, so they will be familiarto you. The basic C operators are shown in Figure 4.1. Note that Java took“ ” for “equals” comparison from C. Pascal (which came after C), used “: ”for assigment and plain “ ” for “equals”, which actually makes more sense, butunfortunately never caught on.Precedence rules (order of operations) for the operators are as in Java, and parentheses can be used to group sub-expressions.As noted above regarding the Java boolean type, comparison and logical operators in C use the assumption that 0 means “false” and any non-zero value means“true.” You can check the examples shown in Figure 4.2 in your own program.There is one important difference regarding Java and C operators: there is nostring concatenation using the operator. Strings are not primitive types in C orin Java. We discuss this further in Section 4.7.

4.4. OPERATORS AND EXPRESSIONS25Arithmetic , -, *, /, %Comparison , ! , , , , Logical!, &&, Increment/Decrement , -- (pre- and post-fix)&, , ˆ, , , BitwiseFigure 4.1: Basic C "%d\n",3211001

executing a Java program. You should already be familiar with this process, so I will describe it only briefly. You write your program (source code) as a text file using the Java program-ming language. The name of your source file ends in “.java”. You compile your source code using the javac command (“Java com-File Size: 510KBPage Count: 89