Friday, August 10, 2012

Java and C parallels : C Introduction

These are some notes from a java/c class I took 
  • Transition from java
    • Java main: public static void main (String[] args)
    • c main: int main(int argc, char *argv[])
    • Java doesn't count program as argument, but c does
    • Java convert string to int: int lo = Integer.parseInt(args[0])
    • c convert string to int: lo = atoi(argv[1])
    • Java print: System.out.println("test") //java automatically puts newline in
    • c print: printf("test\n")
    • Import library Java: import java.util.*
    • Import library c: include <stdio.h>//preprocessor directive, use quotes for user header files
    • Java error device: System.err.print("error"); System.exit(1)
    • c print device: fprintf(stderr,"error\n"); exit(1); //included in stdio.h
    • Each include statement is textually replaced by code from the header file
    • Java just includes what it uses
    • You can use a function in c as long as you place the function or function prototype before its use
    • If you do not this you get implicit declaration errors
    • You overload functions in java, but not c
    • Java constant: public static final int DEFAULT_WIDTH = 5;
    • c constant: const int DEFAULT_WIDTH =5;
    • There is no boolean type in c
    • Define boolean in c: typedef enum {false=0, true} boolean;
    • An object can be modelled in c by creating a source file for such an object just like you would create a java file
    • gcc -o rect rect.c //compile and link a c source file in one step
    • gcc -o rect rect.o //link
    • if you reference a function not present in the file you are linking (gcc -o), then you will get an error
    • You can reference a function not present in the file you are compiling (gcc -c)
    • If you compile this c file for execution, that has no main function, gcc will complain about it having no reference to main

No comments:

Post a Comment