Java to C Code Converter
Tool
Automatically converts simple Java to C, focusing on core elements.
Handles variables, print statements, and main method conversion.
Tool developed entirely using C programming language.
Goal & High-Level Workflow
Java Source Input Conversion Process C Source Output
Receives Java source file. Tool processes Java code. Generates equivalent C file.
Input: Java source file
Output: Equivalent C source file
Tracks variables and their types for proper C formatting.
Converts Java print statements to C's printf.
Dynamically adds necessary C headers.
Global Variables and Flags
int %d None
float %f None
char %c None
boolean %d (or %s for true/false) stdbool.h
String (char array) %s string.h
Arrays store variable names and types (int, float, char, bool).
Flags track required headers like
stdio.h stdbool.h
Ensures only necessary headers are included in output..
Variable Tracking with
trackVariable
1 Detect Declarations 2 Parse
Identifies variable Extracts variable names,
declarations in Java lines. ignores initial values and
spaces.
3 Handle Multi- 4 Stores
Declarations Stores names and types
Supports multiple declared for subsequent use in
variables in one line conversion.
e.g, int a, b, c;
Function: Getting Variable Types with getVarType
Searches stored variable list for a given name
Returns format type (e.g., d for int, f for float)
Default fallback type d (int) if not found
The getVarType function is crucial for correct printf formatting.
Function: Handling Print Statements
with handlePrintln
This function ensures accurate conversion of output.
Extract Content Convert to printf
Pulls text within Transforms to equivalent C
System.out.println() printf
statement.
Detect Concatenation Determine Specifier
Splits strings and variables joined by Finds correct format based on
variable type and content.
+
Function: Line Processing with
processLine
Header Feature Check
Checks if specific features requiring headers are present.
Main Method Conversion
Converts
main
method to C's
int main()
Brace Depth Tracking
Manages brace depth for function and block scope.
Output Formatting
Initiates formatting for function headers in C.
The processLine function is central to the conversion logic.
Function: Line Processing with processLine
Java Snippet C Snippet
public class Test { #include <stdio.h>
public static void main(String[] args) { int main() {
int x = 5; int x = 5;
System.out.println("Value is " + x); printf("%s%d\n", "Value is ", x);
} return 0;
} }
Converts variable declarations inside main
Converts print statements (println and print)
Manages block braces and adds return statement at main end
Handles boolean conversion to C bool type
File Handling and Header Inclusion
Read Input Java
Reads the Java source file character by character.
Temporary Buffer Write
Writes processed content to an intermediate buffer.
Output C File
Writes final C code to the output file.
Dynamic Headers
Includes only necessary headers based on analysis.
Rename Extension
Changes file extension from .java 4> .c
Error Handling
Includes robust checks for file operations.
Accuracy
Ensures reliable and correct file generation.
Summary & Future Enhancements
Core Conversion 1
Successfully converts basic Java
to C.
2 Variable/Print Handling
Accurately tracks variables and
formats prints.
Dynamic Headers 3
Includes only required C
standard library headers.
4 Future Scope
Extend support to loops,
methods, classes, error handling.
Potential Areas 5
Potential for GUI or command-
line interface improvements.