Skytopia > Articles > Quick reference to the most common commands used in C/C++ and Java    (article first created 05/07/2006 and updated 1/9/06).

Quick conversion and reference to the most
common commands used in C/C++ and Java

Throw all those C and Java books away! This is your one-stop shop to the basic commands found in the Java and C/C++ programming languages. No need to hunt through pages and pages of documentation, or tutorials - just copy and paste (and learn 100x times faster with the terse but informative comments!). Also, once you've had experience programming in those, everything else like Javascript, PHP and visual basic is super easy!

This article/reference is useful for beginners and experts .....
  • If you wish to quickly learn or remember how to do a simple command or operation in C/C++ or Java.
  • If you are converting from C (and some C++) code to Java (or vice versa), and want to see the equivalent commands.
  • If you are new to C/C++ or Java, and are learning the ropes such as array creation, file handling, passing to functions, or string manipulation.

    Basically, I would have died for a web page like this when I first started out learning C (I came from a Java background, but found C many times faster for my first application conversion!). Hope you all find it useful! If you'd like to comment on this page, please email me

  • Basic program template for C/C++ and Java
  • Basic operators for C/C++ and Java
  • Control flow commands & loops for C/C++ and Java
  • Most common commands and functions for C/C++ and Java
  • Arrays, malloc, passing to functions etc.
  • File opening, saving, etc.
  • Math functions
  • Special characters for use in strings



  • If the info on this site has been of sufficient interest, a small donation would be appreciated:
    Amount you wish to contribute:

    Basic program template for C/C++ and Java:

    C/C++

    #include <stdio.h>
    int main(int argc, char *argv[])
    {
    	printf("Hello World!");
    	return 0;
    }
    

    Java

    class MyClass
    {
    	public static void main(String[] args)
    	{
    		System.out.println("Hello World!");
    	}
    }
    

    Basic Operators and syntax for C/C++ and Java:

    + * - / % Simple math functions same as usual. Plus, multiply, subtract, divide, and modulus (which means take the 'remainder' (e.g. 5 % 17 = 2)). Use of the term '++' in n++ for example, is shorthand for n=n+1.
    // hidden 'Comment' or 'quote out' a line from that point on, to make it 'invisible' to the compiler.
    /* hidden */ Like above, but quotes out a whole section. /* Begins the section, and */ ends it.
    && AND operator
    || OR operator
    ! NOT operator
    = Means simple assignment. For example: n=10
    == Means 'equals to'. 1==2 is false etc. Be very careful not to mix this up with assignment (=). Also in the case of Java, you need to use the a.equals(b) method to test whether strings are the same.
    != Means 'not equals to'. 1!=2 is true etc.
    < Less than. For example, 3<7 is true. Also use <= for 'less than or equals to'.
    > More than. For example, 3>7 is false. Also use >= for 'more than or equals to'.


    Control flow commands & loops for C/C++ and Java:

    if (a==b) { blah... } If a equals b, then do whatever is in the curly brackets.
    else { blah... } Put under the "if" section, this executes if the previous "if" failed.
    while (a<b) { blah... } For as long as a is less than b, continue to execute (loop) whatever is inside the {} brackets.
    for (int n=0 ; n<50 ; n=n+1 ) { blah... }       This will execute whatever is in the curly brackets, 50 times. Also n will go from 0 to 49.
    break; Breaks out of a loop or switch section prematurely.
    continue; Skips the current iteration of a loop section.

    Most common commands and functions for C/C++ and Java:


    Useful libraries to include in your code. Many of the commands below rely on these:

    C/C++
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sys/timeb.h>
    #include <time.h>
    #include <math.h>
    #include <iostream>
    #include <windows.h> // (Slow to import, and Windows only)
    Java
    import java.io.*;
    import java.lang.Math;
    import java.util.*;


    Please click the checkboxes below to suit the table to your requirements:
    Select blue to learn just about the c/c++ commands or orange to learn only about Java commands, and select both to compare them side by side. Also, you may want to click the white button to save line space (at the cost of needing to scroll the screen).
    Display C / C++ commands
    Display Java commands
    Wrap lines

    Declaring Variables (for next section)

    C / C++ Java C/C++ explanation Java explanation
    int i; int i; Declare an integer Declare an integer
    long l; long i; Declare a long (allows bigger numbers than an int usually) Declare a long (allows bigger numbers than an int)
    double d; double d; Declare a double Declare a double
    char c; char c; Declare a char (one character string). Declare a char
    char mystring[100]; String mystring Declare string of 100 chars big (but use the malloc method instead for super-large arrays). This is the way to get a string in c. Declare a string. Java handles all the difficult work at the cost of speed.
    char array2d[20][100]; String[] stringArray = new String[20]; Declare 20 strings of 100 length (but use the malloc method instead for super-large arrays or if you want to pass to a function) Declare 20 strings.
    struct newtype { int o; char p[5]; }; --- Create a new 'structure' to contain sub-variables (similar to an object in an object-orientated language like Java). In this example, each variable of type "newtype" will contain the subvariables 'o' (an integer), and 'p' (an array of chars). All structs must be above (and outside) any functions. Java doesn't support structs. Use classes/objects instead.
    newtype ntype; --- Using the struct created above, declare a variable of type newtype. This goes into a function (eg. main () ). ---

    Many of the commands below rely on the variables as declared above, and should go into a function (eg: main)

    C / C++ Java C/C++ explanation Java explanation
    printf("Hello\nWorld"); System.out.print("Hello\nWorld"); Prints stuff ("\n" means newline) Prints stuff ("\n" means newline)
    printf("%s",mystring); System.out.print(mystring); Prints char array (mystring var), eg., a string. Prints string
    printf("%c",c); System.out.print(c); Print a char from the char declared above Print a char from the char declared above
    printf("%c hello %s",c,mystring); System.out.print(c+" hello"+mystring); Print a char followed by "hello", followed by the mystring variable. Print a char followed by "hello", followed by the mystring variable.
    printf("%c",mystring[99]); System.out.print(mystring.charAt(99)); Print last char from the string declared above Print last char from the string declared above
    printf("%s",array2d[19]); System.out.print(array2d[19]); Print last string from the string array (declared above under title "Declaring Variables"). Print last string from the string array (declared above under title "Declaring Variables").
    printf("%c",array2d[19][99]); System.out.print( array2d[19].charAt(99) ); Print last char from the last string in the string array. Print last char from the last string in the string array.
    mystring[99] = 'z' --- Change last character in mystring string to 'z' No direct char editing in string
    mystring = "hello" mystring = "hello" Obvious, but illegal! You can't directly assign a string to a char array. Use strcpy instead. Unlike in C, this is fine, and just assigns "hello" to the mystring String.
    array2d[19][99] = 'y' --- Change last character in last string to 'y' No direct char editing in a string
    strcpy(mystring, "hello"); mystring = "hello" Copy "test" to the "mystring" string as declared earlier. Assigns word "hello" to the mystring String.
    strcpy(array2d[5], "hello"); stringArray[5] = "hello" Copy "test" to the 5th string in "array2d" as declared earlier. Copy "test" to the 5th string in "stringArray" as declared earlier.
    strncpy(array2d[5], "hello", 5); stringArray[5] = "hello".substring(0, 5) Use strcpy instead usually. This is used if you want to define the number of chars to copy. Use substring (truncate) to define a limited section of the string.
    strncpy(array2d[5], mystring+10, 50); stringArray[5] = mystring.substring(10, 60) Takes a substring of mystring (start from 10th char and do 50 of them), and puts it into the array2d[5] string. Takes a substring of mystring (start from 10th char and do 50 of them), and puts it into the array2d[5] string.
    strcat(mystring,"hello"); mystring = mystring + "hello" Concatenate (add/append) "test" to the string, along with the escape code '\0' to signify the end Concatenate (add/append) "test" to the string. However, for large amounts of concatenations, use Java's StringBuffer() method, as it is much quicker.
    strcmp(a,b); a.compareToIgnoreCase(b) Compare two strings. Returns zero if same, -1 if a is less than b, and 1 if a is more than b. Compare two strings. Returns zero if same, Less than zero if a is less than b, and more than zero if a is more than b.
    strcmpi(a,b); a.compareTo(b) Case sensitive version of above Case sensitive version of above
    strlen(mystring); mystring.length() Number of chars so far stored in the string (everything up to escape code '\0') Number of chars so far stored in the string.
    int i = '7'-48; int i = '7'-48; Convert char to int, providing the char is a number from 0-9. Convert char to int, providing the char is a number from 0-9.
    int i = 'a'; int i = 'a'; Convert char to int code Convert char to int code
    char c = 7 +48; char c = 7 +48; Convert int to char, providing the number is from 0-9. Convert int to char, providing the number is from 0-9.
    char c = 97; char c = 97; Convert int code to char ('a' in this case). If you don't want to declare a new char, then use the wrapper type "(char)" next to 97. Convert int code to char ('a' in this case). If you don't want to declare a new char, then use the wrapper type "(char)" next to 97.
    int i = atoi("456"); int i = Integer.parseInt("456") Convert string to int. Use 'atof' to convert string to double/float, and 'atol' for converting string to long. Convert string to int. Use Double.parseDouble to convert string to double, and Long.parseLong for converting string to long.
    sprintf(mystring, "%d", 123); mystring = String.valueOf(123) Convert int to string (or double to string, using %f instead of %d) Convert int to string (or double to string)
    itoa(123, mystring, 10); --- Convert int to string base 10 (nonstandard, so use above instead usually) ---
    int i = sizeof(mystring); int i = mystring.length Find (full) length of array (returns 100 in this case) Find (full) length of array (returns 20 in this case)
    exit(0); System.exit(0); Quit program Quit program
    system("PAUSE"); System.in.read(); "Press any key to continue" (Windows only) Pause program until return key is pressed. You must use allow to throw an IOException though.
    Sleep(500); try {Thread.sleep(500); }
    catch(Exception e) { }
    Wait 500 milliseconds (Windows only) Wait 500 milliseconds.
    char mystring2[]="hello"; String mystring2="hello"; Declare and initialize string in one go Declare and initialize string in one go
    int i2=99; int i2=99; Declare and initialize an integer in one go eclare and initialize an integer in one go
    ntype.o = 37; --- Assign the number 37 to the 'o' part of the ntype variable. No structs with Java. Use objects instead.
    ntype.p[3] = 'z'; --- Assign the char 'z' to the 3rd element of the array in the 'p' part of the ntype variable. No structs with Java. Use objects instead.
    long l = clock(); --- Assign to l, the number of milliseconds of CPU time used on the program since its execution. No measuring of CPU time in Java.
    long l = time(NULL); long l = System.currentTimeMillis()/1000; Assign to l, the number of seconds since 00:00:00, January 1, 1970. Assign to l, the number of seconds since 00:00:00, January 1, 1970.
    timeb ti; ftime( &ti );
    double l = ti.time*1000.0 + ti.millitm;
    long l = System.currentTimeMillis() Assign to l, the number of milliseconds since 00:00:00, January 1, 1970. Assign to l, the number of milliseconds since 00:00:00, January 1, 1970.
    time_t mytime = time(0);
    strcpy(mystring, ctime(&mytime) );
    ? Assign to mystring, the local date and time in ascii format. ?
    srand(77); Random r = new Random(77); Randomize seed for random numbers (77 used here, but use srand((unsigned)time(NULL)) instead
    for different random numbers each time the program is run)
    Create random object, with a seed of 77. Remove '77' to create different random numbers every time the program is run.
    100 * rand()/(RAND_MAX+1) Random r = new Random();
    i = r.nextInt(100);
    Compute random integer number between 0 and 100 (0 <= r < 100). Compute random integer number between 0 and 100 (0 <= r < 100).
    68 * rand()/(RAND_MAX+1) + 32 Random r = new Random();
    i = r.nextInt(68)+32;
    Compute random integer number between 32 and 100 (32 <= r < 100). Compute random integer number between 32 and 100 (32 <= r < 100).
    (float) rand()/(RAND_MAX+1) Random r = new Random();
    d = r.nextFloat()
    Compute random floating number between 0 and 1 (0 <= r < 1). Compute random floating number between 0 and 1 (0 <= r < 1).
    100*(float) rand()/(RAND_MAX+1) Random r = new Random();
    d = r.nextFloat()*100.0
    Compute random floating number between 0 to 99.999... inclusive (0 <= r < 100). Compute random floating number between 0 to 100
    scanf("%s", &mystring); BufferedReader r = new
    BufferedReader(new
    InputStreamReader(System.in));
    mystring = r.readLine();
    Read from console input into string Read from console input into string
    char* token;
    token = strtok (str," ");
    while (token != NULL)
    {
    token = strtok (NULL," ");
    }
    ... Split the string "str" into pieces or 'tokens'. One could use any symbol to split by, such as a comma or space (as used here), or any arbitrary letter or number. In the while loop, you'll want to do something with each token such as print it out. We use "NULL" (instead of the previously used "str") as the first parameter for future strtok calls to indicate that we're going from where we previously left off, otherwise we'll keep getting the first token of a string each time. ...


    Arrays, malloc, passing to functions etc.:

    Declaring Variables (for next section)

    C / C++ Java C/C++ explanation Java explanation
    int i = 5; int i = 5; Create an integer Create an integer
    char j = 'z'; char j = 'z'; Create a character Create a character
    int myIntArray[10]; int[] myIntArray = new int[10]; Create an integer arrayCreate an integer array
    int*myIntArrayB = (int*)malloc(100000*sizeof(int)); int[] myIntArrayB = new int[100000]; Create an int array, myIntArrayB, of size 100000. Use instead of "int myIntArrayB[100000];" for large values like 100000. (Same goes for other array types like char too). When done with, use "free(myIntArrayB)" to free up the memory created. To access/change the array, simply use myIntArrayB[i] as usual. Create an int array, myIntArrayB, of size 100000.
    char*mystring = (char*)malloc(100000*sizeof(char)); char[] mystring = new char[100000]; Create a char array, mystring, of size 100000. Use instead of "char mystring[100000];" for large values like 100000. (Same goes for other array types like int too). When done with, use "free(mystring)" to free up the memory created. To access/change the array, simply use mystring[i] as usual. Create a char array, mystring, of size 100000.
    char**array2d = (char**)malloc(2000*sizeof(char*));
    for (int n=0 ; n<2000 ; n++) { array2d[n]=(char*)malloc(1000*sizeof(char)); }
    char[][] array2d = new char[2000][1000]; Declare 2000 strings of 1000 length. Use instead of "char array2d[2000][1000];" for large values, or if you want to pass to a function. To access/change the array, simply use array2d[i][j] as usual. Declare 2000 strings of 1000 length.
    char**array2d = (char**)malloc(2000*sizeof(char*));
    for (int n=0 ; n<2000 ; n++) { array2d[n]=(char*)calloc(1000,sizeof(char)); }
    --- Like above, except all are initialized to null. ---
    char***array3d = (char***)malloc(200*sizeof(char**));
    for(int i = 0; i < 200; i++) array3d[i] = (char **)malloc( 100 *sizeof(char*));
    for(int i=0; i<200; i++) {
    for(int j=0; j<100; j++) array3d[i][j]=(char*)malloc( 3 *sizeof(char));
    }
    char[][][] array3d = new char[200][100][3]; Allocate for a 3 dimensional array (in this case, it's 200*100*3). Obviously char can be replaced with int, float etc. To access/change the array, simply use array3d[i][j][k] as usual. Allocate for a 3 dimensional array.
    struct newtype {
        int amount;
        char strings[10][10];
    };
    --- Similar to an object in Java, create a new 'structure' to contain sub-variables. Each variable of type "newtype" will contain an integer - "amount", and a 2D array of chars - "strings". All structs must be above (and outside) any functions. ---
    newtype ntype; --- Using the struct created above, we have created a variable of type 'newtype'. ---
    newtype ntypeArray[10]; --- Using the struct created above, we have created a variable array of type 'newtype'. ---

    The commands below rely on the variables as declared above, and should go into a function (eg: main)

    C / C++
    Java
    C/C++ explanation Java explanation
    int z = addTwo(5);
    int addTwo(int x) {
        int n=x+2 ; return n ;
    }
    int z = addTwo(5);
    static int addTwo(int x) {
        int n=x+2 ; return n ;
    }
    The first part in red (which should go in a function like main() {...} ) passes 5 to the "addTwo" function (outside of main() {...} !), which then returns 5+2. Then z (in red), becomes 7 as a result. The int in "int addTwo(" means that the type to churn out is an integer. The first part in red (which should go in a function like main() {...} ) passes 5 to the "addTwo" function (outside of main() {...} !), which returns 5+2. Then z (in red), becomes 7 as a result. The int in "int addTwo(" means that the type to churn out is an integer.
    myFunc(i,j);
    void myFunc(int i, char j) {
        i=5 ; int i2 = i;
    }
    myFunc(i,j);
    static void myFunc(int i, char j) {
        i=5 ; int i2 = i;
    }
    How to pass 2 variables to a function (integer i and char j in this case). Since the variables are not arrays, they are passed by value instead of reference. Therefore the vars inside the "void myFunc(...)" and "{..}" bits are completely separate from the i and j vars outside the function (including the caller bit: "myFunc(...)"). 'void' means that nothing is returned to the caller. How to pass 2 variables to a function (integer i and char j in this case). Since the variables are not arrays, they are passed by value instead of reference. Therefore the vars inside the "void myFunc(...)" and "{..}" bits are completely separate from the i and j vars outside the function (including the caller bit: "myFunc(...)"). 'void' means that nothing is returned to the caller.
    myFunc(&i);
    void myFunc(int *i) {
        *i=5 ; int i2 = *i;
    }
    --- Unlike above, this time, the variable is passed by reference. This means that the 'i' variable inside the function is the same as the 'i' variable outside, and not a copy. In other words, actual changes to 'i' inside the function affect 'i' globally. Inside, the * symbol is used to 'dereference' the variable, so we can work with it. Java does not support passing non-array variables by reference directly, but a hack involves create a 1 element array, and using that instead.
    myFunc(myIntArray);
    void myFunc(int *array) {
        array[5]=99;
    }
    myFunc(myIntArray);
    static void myFunc(int[] array) {
        array[5]=99;
    }
    Pass an array to the function. Arrays are always passed by reference, so changing "array" inside the function will change "myIntArray" outside. Pass an array to the function. Arrays are always passed by reference, so changing array2d inside the function will change myIntArray outside.
    myFunc(mystring,array2d);
    void myFunc(char* data, char** data2) {
        data[99]=5;
        data2[99][99]=5;
    }
    myFunc(mystring,array2d);
    static void myFunc(char[] data, char[][] data2) {
        data[99]=5;
        data2[99][99]=5;
    }
    How to pass the 1D and 2D 'malloced' arrays to a function. By default, arrays are passed by reference (see above for explanation). How to pass the 1D and 2D arrays to a function. By default, arrays are passed by reference (see above for explanation).
    myFunc(ntype);
    void func5(newtype in) {
        in.strings[3][3]='z';
        data2[99][99]=5;
    }
    --- Pass the ntype variable (type newtype) to the func5 function. The function takes the passed variable, creates a copy of it, and inside the curly brackets, gets the "strings" variable part of ntype, and changes one of its chars. ---
    myFunc(&ntype);
    void myFunc(newtype *in) {
        (*in).strings[3][3]='z';
        data2[99][99]=5;
    }
    --- Like above, but access the variable directly - don't make a copy. ---
    myFunc(ntypeArray);
    void myFunc(newtype *in) {
        in[5].strings[3][3]='z';
    }
    --- Pass a "newtype" array to the function. Just like when passing normal arrays (like int[] and char[] ), by default, they are passed by reference. ---


    File opening, saving, etc.:

    Declaring Variables (for next section)

    C / C++
    Java C/C++ explanation Java explanation
    FILE *fs = fopen("outputfile.txt", "w");
    if (!fs) { printf("Can't write file"); exit(0); }
    File fs = new File("outputfile.txt");
    FileWriter fw = new FileWriter(fs);
    BufferedWriter mywriter;
    mywriter = new BufferedWriter(fw);
    Create file (on the hard disk usually) ready for writing to. Use "a" instead of "w" for append instead. The second line just checks if the file could be created. Create file (on the hard disk usually) ready for writing to. The BufferedWriter isn't strictly necessary, but speeds up the process of writing considerably. Use FileWriter(fs, true); instead to append data instead of overwrite it. Don't forget to allow for an IOException throw (i.e. by wrapping it in a try/catch block, or putting "throws IOException" right next to the method declaration).
    FILE *fl = fopen("input.txt", "r");
    if (!fl) { printf("Can't open file"); exit(0); }
    char *array=(char *)malloc(getSize(fl));
    fread(array, 1, getSize(fl), fl);
    rewind(fl);
    File fl = new File("input.txt");
    FileReader fis;
    fis = new FileReader(fl);
    BufferedReader myreader;
    myreader = new BufferedReader(fis);
    byte[] array=new byte[(int)fl.length()];
    fis.read(array);
    Prepare a file ready for reading. The second line makes sure the file exists. This is now ready for use. However, if you want super fast memory access, then the green part comes in handy. The first green line creates an array ready for the file to be transferred, and the second, transfers it (getSize() is a custom function - see below). Finally, the rewind function rewinds the file position indicator back to the start of the file. Prepare a file ready for reading. The BufferedReader part isn't strictly necessary, but speeds up the reading process significantly. This is now ready for use. However, if you want super fast memory access, then the green part comes in handy. The first green line creates an array ready for the file to be transferred, and the final line transfers it.
    int getSize(FILE* fin) { fseek(fin,0,2); int size=ftell(fin); rewind(fin); return size; } fl.length() Useful function to retrieve the size of a file. Retrieve the size of a file (fl was declared above).
    FILE *vFile1 = fopen ("myfile.bin","wb");
    FILE *vFile2 = fopen ("myfile.bin","rb");
    ... Declaring write and read files ready for binary writing to a file named "myfile.bin". ...


    The commands below rely on the variables as declared above, and should go into a function (eg: main)

    C / C++
    Java C/C++ explanation Java explanation
    char ch = array[100]; char ch = array[100]; If the green section was included above, then this will read one character from the array (which has the file data in!) and put it into ch. If the green section was included above, then this will read one character from the array (which has the file data in!) and put it into ch.
    char ch = fgetc(fl); char ch = myreader.read(); If you didn't want to create a massive array to put the file data in (i.e. you excluded the green code), then this command will read characters directly from the file. After each fget, the file position indicator is advanced ready to read the next character. If you didn't want to create a massive array to put the file data in (i.e. you excluded the green code), then this command will read characters directly from the file. As it is, it will return ints, so put "(char)" in front of "myreader.read()" to return chars. After each read(), the file position indicator is advanced ready to read the next character.
    bool b = fgets (mystring , 10000 , fl); String s=myreader.readLine(); Like above, but reads a whole line from the file instead of just a single character. The first argument (mystring) is the string (char array) which to write to. The second is the maximum amount of characters allowed before cutoff, and the third is the file from which to read the line from. bool b just returns whether the operation was successful (useful for end of file checking etc.) Like above, but reads a whole line from the file instead of just a single character. If the end of the file is reached, then readLine returns null.
    fputs("Hello", fs); mywriter.write("Hello"); The fputs command appends a string to the file. Also, the fclose function (see below) makes the file available for the OS to use. You might want to try: fwrite("Hello",1, sizeof("Hello"), fs); instead if you have problems (i.e. if you want to write zeros to a file). The 1 represents the size of the variable type (char = 1 byte), and sizeof(hello) is how many elements there are in "hello". The write command writes a string to the file. Finally, the close function (see below) makes the file available for the OS to use.
    fprintf(fs,"%s %f %i","Hello",1.7,2); ... Similar to above, but works more in line with the printf syntax. Nice and flexible, since floats etc. can be printed as chars too (without going through intial conversion). ...
    fclose(fs); mywriter.close(); Close file to release to the OS, making it ready to use. Close file to release to the OS, making it ready to use.
    fseek(fl, 100, 0); --- Move the file position indicator to the 100th character of the file. ---
    fwrite(&i, sizeof(int), 1, vFile1);
    fwrite(mystring, sizeof(char), 20, vFile1);
    for(int n=0; n<6; n++)
      { fwrite(array2d[n], sizeof(float), 4, vFile1);}
    fclose (vFile1);

    // At this point, the file of variable data is on
    // hard drive, ready for use another day.

    fread (&i, sizeof(int), 1, vFile2);
    fread (mystring, sizeof(char), 20, vFile2);
    for(int n=0; n<6; n++)
      {fread(array2d[n], sizeof(float), 4, vFile2);}

    // Now you can do stuff with
    // i, mystring[] and array2d[][]

    fclose(vFile2);     // Finally, close the file.
    ... Write variable data (like integers, arrays or strings) to a file, ready to load back another time. In our case, "i" is a singler integer, "mystring" is a 20 char string (i.e a 1d array of chars), and "array2d" DESPITE POSSIBLE APPEARANCES TO THE CONTRARY, is a 2D array of floats (6x4 elements).

    Once we have used fwrite to write the variables to a file, we can later use fread to load them back in again at a later time (even from another run of the program).

    The first parameter in fwrite (or fread) is the pointer to the data, the 2nd is the type size, the 3rd is the number of elements to write, and the 4th is the FILE pointer (declared earlier).

    (Note that we haven't declared the variables (i, mystring, and array2d), or put anything inside them - that's up to you).

    ...


    Math functions:

    See the cppreference.com site for more maths functions.

    C / C++
    Java C/C++ explanation Java explanation
    int i = abs(-20); int i = Math.abs(-20); Calculate the absolute value (turns negative numbers to positive. Calculate the absolute value (turns negative numbers to positive.
    double d = pow(3.0, 7.5); double d = Math.pow(3, 7.5); Calculate 3 to the power of 7.5 Calculate 3 to the power of 7.5
    double d = sin(50.0); double d = Math.sin(50); Calculate the sine of 50 (in radians). Use 50*pi/180 instead to get degrees. Calculate the sine of 50 (in radians). Use 50*pi/180 instead to get degrees.
    double d = cos(50.0); double d = Math.cos(50); Calculate the sine of 50 (in radians). Use 50*pi/180 instead to get degrees. Calculate the sine of 50 (in radians). Use 50*pi/180 instead to get degrees.
    double d = sqrt(50.0); double d = Math.sqrt(50); Calculate the square root Calculate the square root
    double d = tan(50.0); double d = Math.tan(50); Calculate the tangent Calculate the tangent
    double d = atan(50.0); double d = Math.atan(50); Calculate the inverse tangent Calculate the inverse tangent
    double d = log(32.0)/log(2.0); double d = Math.log(32.0)/Math.log(2.0); Calculate the base 2 logarithm of 32 (which is 5). Calculate the base 2 logarithm of 32 (which is 5).


    Special characters for use in strings

    \n	Newline	
    \?	Question mark	
    \'	Single quote	
    \"	Double quote
    \t	Horizontal tab
    \a	Bell (alert)	
    \b	Backspace	
    \\	Backslash
    

    links

  • Jazillion - includes links and an excellent guide to convert from C to Java.
  • Frequently Asked Questions on C
  • Eddie's basic guide to C programming
  • C Programming Notes
  • Groogle - A web based peer code review tool providing a range of features aimed at easing the code review process.


    Found this site of use, or want to say something? Please email me




    Skytopia > Articles > Quick reference to the most common commands used in C/C++ and Java    (article first created 05/07/2006 and heavily updated 01/09/2006).





    This page is copyright 2006 onwards Daniel White.
    If you wish to duplicate any of the information from this page, please contact me for permission.