JV
C

Java to C

10 lessons

Progress0%
1Introduction to C2Data Types3Pointers4Strings5Memory Management6Structs vs Classes7Preprocessor and Header Files8Multi-File Programs9Input / Output and Command-Line Args10Arrays: Sorting and Searching
All Mirror Courses
JV
C
Introduction to C
MirrorLesson 1 of 10
Lesson 1

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.

Mirror Card
JV
From Java:

In Java, you're familiar with understanding c's relationship to java.

C
In C:

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
C 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:

JV
Java (What you know)
// Java - object-oriented, managed memory
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

C
In C:

C is procedural, not object-oriented

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

C
In C:

No classes in C - use structs and functions

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

C
In C:

Must include header files for library functions

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

C
In C:

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.

JV
Java
class Person {
    String name;
    void greet() { ... }
}
C
C
struct Person {
    char name[50];
};
void greet(struct Person* p) { ... }
Common Pitfall
You'll need to pass struct pointers to functions that modify data - there's no 'this' pointer.

2. Header Files

C uses #include to import functionality. <stdio.h> is like importing java.io.*

C
C
#include <stdio.h>   // printf, scanf
#include <stdlib.h>  // malloc, free
#include <string.h>  // strcmp, strcpy

3. Manual Memory

Unlike Java's garbage collector, C requires you to manually allocate and free memory.

JV
Java
// Java - automatic garbage collection
Person p = new Person();
// p is automatically cleaned up
C
C
// C - manual memory management
Person* p = malloc(sizeof(Person));
// ... use p ...
free(p); // YOU must free it!
Rule of Thumb
Every malloc() must have a corresponding free(). Memory leaks are a major source of bugs in C.

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
Common Pitfall
Don't assume C works exactly like Java. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • C is procedural - no classes, use structs + functions
  • Include headers for library functions
  • Manual memory management is required
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Java code in C to practice these concepts.
OverviewNext