Skip to content

2.1 C Similarity to Java Syntax

This note serves to show the similarities between C and Java, meant to give the reader an understanding of C syntax from prior knowledge. Get back to toc CS61C or 2. C Programming

C Programs

C programs inspired the syntax of Java, so if you do find some intuitiveness in C, then this is the reason why. Here, we will look at familiar syntax that will get you started with programming in C, however, you'll need to understand these next three chapters - 2. C Programming, 3. Memory, and 4. More C Programming - to get used to C programs in general. It is much lower level than Java, so things like where memory is stored (2.4 Stack, Heap, Static, Code), dedicating memory and freeing it is also important to know (2.2 Memory Allocation and 2.3 Pointers and Arrays).

C programs are compiled into assembly code, you'll learn more about assembly in 5. RISC-V Programming. From Assembly, we will go through two more 3 more processes to get it into something you can run on your device, covered in

Variable Declarations, Strings

Like in Java, one must declare the type of the variable and the name it will be referenced by. One can either just declare the type, or declare and assign a value. For example,

int x = 4; # declaration of a variable and value

int y; # declaration of variable
y = 1; # assignment of value

We can also announce arrays in this syntax, which is slightly different from Java:

int arr[] = {1, 2, 3}; # Compiler will infer the size of the array
int arr2[2]; # Explicit declaration of array size
arr2[0] = 1; # Assignment of values in array. 
arr2[1] = 3

There are other types you might have seen, bool , double, float, long, etc.

Strings are weird in C, there is no such thing as a String type, like in Java. Instead a string in C is thought of as an array of char types. One can create the equivalent of a string in many ways, but so far, there are two ways to make it based on the knowlegde we know right now.

char str[] = {'h', 'e', 'l', 'l', '0', '\0'};
char str2[] = "hello";

First off, char types are delimited by single quotes, and strings are delimited by double quotes. Secondly, we have to end the char array with a null terminator: \0. The reason why is because when we invoke function calls like determining the string length, C needs to know when it ends. When you declare a string like we did in str2, the null terminator is tacked on by C when you compile it. However, an array must include a null terminator. The null terminator is a part of the char array size that you declare, so something like

char three[4] {'d', 'o', 'g', '\0'};

has a length of three even though we had to instantiate an array with length 4. The function to determine the size of a char array is strlen(<char array>);

There actually is not really any other way to get the size of an array, there is no arr.length like there is in Java. If you ever want to iterate through something like an int array, you probably also need to know the size and pass it in as a parameter to a function.

Because of that fact, this brings me up to something that is notorious in C: it doesn't have any checks on if you go out of bounds in an array. Doing something like

int bad[] = {1, 2, 3};
bad[4] = 5;

is possible to accidentally do. You might be able to get away with this in C, but Java and Python has error checking for this type of stuff. Maybe, you might get lucky, and the compiler will produce a segmentation fault (also known as seg fault).

Another point to bring up. C is not really great at telling you what you did wrong. When you code in C, the answer you might get once it gets past a compiler is segmentation fault. We will learn where this happens when we talk about Operating Systems, but essentially, you just attempted to access a restricted area of memory that the Operating System knows about. Unfortunately, sometimes the access you make is out of bounds, but is not restricted, so you won't receive an error. This is what makes programming in C a bit annoying.

Function Signatures and Control

The classic java syntax for the main function

public static void main(String[] args) {
    <code>
}

is heavily simplified in C functions. First off, we won't be worried too much about the public and static parts of C functions too much, we still keep track of the return type and the parameter types. The C main function therefore looks a bit more simpler:

int main() {
    <code>
    return 0;
}

we have to return 0 at the end. There are things you can include at the C header to have stuff like command-line arguments, like String[] args in Java, but I won't include much detail here; you can jsut look it up if you want to.

Other functions are just like this main function: note the return type, indicate the name of the function, indicate the types and names of the parameters of the function. For example, a simple recursive factorial function might look like

int factorial(int n) {
    if (n == 0) {
        return 1;
    } else {
        return n * factorial(n-1);
    }
}

You can also see, control is just like Java; there the syntax is exactly the same for while, for, if/else if/else. You might see incrementation as i++ or ++i, or i += 1. Decrementation is the exact same way: i--, --i, i -= 1.

Summary

So these are the building blocks of C programs, but now it is time to get into all the details of a low-level programming language like C. We won't really get into programming in C so much as we did in Java or Python, since the skills of coding carry over from language to language. We are more focused on the concepts and using C as the vehicle to play around with the concepts. So in the next section, we will be looking at things you take for granted in languages like Java or Python.

Go to next section 2.2 Memory Allocation