1Learning Outcomes¶
Use a “Hello World” program to intuit C program structure.
Do a cursory comparison of C and Java.
🎥 Lecture Video
2Hello World¶
C program: hello_world.c
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Hello World!\n");
return 0;
}Java program: hello.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}2.1Highlights¶
In C, we import libraries using
#include. Here, we includestdioforprintf(), which prints to stdout (here, the command-line).There is a
mainfunction.C is function-oriented; unlike Java, this is not an object’s method.
The return type of
mainis notvoid; it’s an integer.By convention, C programs return
0on success. (The main rationale is it is easier to check equality to zero; we return to this soon)
2.2Run Demo¶
The below instructions are mostly for reference. Refer to this section to understand details.
Compile and run C
To run this program, use the command-line program, gcc, to compile the program. This creates a binary program with the default name a.out (why the naming?). Then, run the binary program.
$ gcc hello_world.c
$ ./a.out
Hello World!In practice, rename the binary to something more meaningful, like hello_world:
$ gcc -o hello_world hello_world.c
$ ./hello_world
Hello World!You will also find it useful to generate debugging symbols for gdb, our debugger.
$ gcc -d -o hello_world hello_world.c
$ gdb hello_world3C vs. Java¶
Table 1 below is adapted from the C Programming vs. Java Programming table, created for Princeton University’s introductory CS sequence. Hover over the footnote for more information about each row.
Table 1:(a) C vs. Java; (b) similar operators
| Feature | C | Java |
|---|---|---|
| Language Paradigm[1] | Function Oriented (programming unit: function) | Object Oriented (programming unit: Class = Abstract Data Type) |
| Compilation[2] | gcc hello.c creates machine language code | javac Hello.java creates Java virtual machine language bytecode |
| Execution[2] | ./a.out loads, executes program | java Hello interprets bytecodes |
| Dynamic Memory Management[3] | Manual (malloc, free) (more later) | Automatic garbage collection; new both allocates and initializes |
| Variable declaration | Typed declaration; declare before you use it | (same) |
| Function declaration | Use curly braces. void means no return value | (same) |
| Accessing a library | #include <stdio.h> | import java.util.* |
| Comments | Multiline: /* ... */, end-of-line: // ... | (same) |
| Operator | C and Java |
|---|---|
| arithmetic | +, -, *, /, % |
| assignment | = |
| augmented assignment | +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= |
| bitwise logic | ~, &, |, ^ |
| bitwise shifts | <<, >> |
| boolean logic | !, &&, || |
| equality testing | ==, != |
| subexpression grouping | () |
| order relations | <, <=, >, >= |
| increment and decrement | ++, -- |
| member selection[4] | ., -> |
| conditional evaluation[5] | ? : |
4More Highlights¶
1. Variable naming convention: In C, use snake_case[6], NOT camelCase [7].
2. Command-line arguments: In our hello_world.c program, the main function can take in command-line arguments with two parameters:
argcis an integer count of how many arguments you have. The executable itself counts as one argument. If you run something like./hello_world my_file,argcis2.argv: is a pointer to an array of the arguments themselves, as C strings. We discuss pointers, arrays, and strings in more detail next time. For now, if you run./hello_world my_file, the first[8] argument is the name of the program itself (hello_world) and the second argument is the stringmy_file.
Command-line arguments: What looks familiar about array syntax?
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Received %d args\n", argc);
for(int i = 0; i < argc; i++) {
printf("arg %d: %s\n", i, argv[i]);
}
return 0;
}3. Curly braces: The C language allows for some omission of curly braces for single-line statements—even for control structures like if-else and for. This is the same as in Java, but we didn’t tell you. :-)
But just because you can, doesn’t mean you should. Because subsequent lines the control structure are considered outside of the body, omitting curly braces leads to many debugging errors[9]:
Omitting curly braces: Which lines are printed?
#include <stdio.h>
int main(int argc, char *argv[]) {
int x = 0;
if (x == 0)
printf("x is 0\n");
if (x != 0) // careful!
printf("x not 0 line 1\n");
printf("x not 0 line 2\n");
return 0;
}Java is an object-oriented language; C is mostly a functional language. In C, the core idea is the function, whereas in Java, it’s the class or abstract data type. While it is possible to write object-like code in C, if programs required objects then you should really be using C++.
We’ve discussed this in detail in a previous section.
Java manages memory for you with garbage collection. In C, all the safety belts and padded rooms are gone; you do all the memory management yourself, and you can get in trouble very quickly. More next time.
Slightly different than Java because there are both structures and pointers to structures, more next time
cond ? body_true : body_falseLike Python, C arrays and string are zero-indexed.