Introduction to C
Understanding C's relationship to Java
Introduction
In this lesson, you'll learn about introduction to c in C. Coming from Java, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In Java, you're familiar with understanding c's relationship to java.
C has its own approach to understanding c's relationship to java, which we'll explore step by step.
The C Way
Let's see how C handles this concept. Here's a typical example:
// C - procedural, manual memory
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}Comparing to Java
Here's how you might have written similar code in Java:
// Java - object-oriented, managed memory
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}You may be used to different syntax or behavior.
C is procedural, not object-oriented
You may be used to different syntax or behavior.
No classes in C - use structs and functions
You may be used to different syntax or behavior.
Must include header files for library functions
You may be used to different syntax or behavior.
main() returns int, not void
Step-by-Step Breakdown
1. No Classes
C doesn't have classes. Instead, you use structs for data and standalone functions for behavior.
class Person {
String name;
void greet() { ... }
}struct Person {
char name[50];
};
void greet(struct Person* p) { ... }2. Header Files
C uses #include to import functionality. <stdio.h> is like importing java.io.*
#include <stdio.h> // printf, scanf
#include <stdlib.h> // malloc, free
#include <string.h> // strcmp, strcpy3. Manual Memory
Unlike Java's garbage collector, C requires you to manually allocate and free memory.
// Java - automatic garbage collection
Person p = new Person();
// p is automatically cleaned up// C - manual memory management
Person* p = malloc(sizeof(Person));
// ... use p ...
free(p); // YOU must free it!Common Mistakes
When coming from Java, developers often make these mistakes:
- C is procedural, not object-oriented
- No classes in C - use structs and functions
- Must include header files for library functions
Key Takeaways
- C is procedural - no classes, use structs + functions
- Include headers for library functions
- Manual memory management is required