The Basics

We'll be going through this fairly quickly since most of these are things you should already know or will be familiar with from other languages.

The main() Function


int main(int argc, char** argv){
    return 0;
}

No more public static void or String[] args. Just return type, main, and parameters for command line arguments.

argc gives you the number of command line arguments passed through when running your program (For example: $./test.o arg1 arg2 will have argc=3).

argv is your list of arguments.

argc will always be at least 1. Why? The name of the program (in our case it's test.o) is considered an argument. Subsequently, it's also always the first argument in argv.

Remember to return something every time. This isn't a void function since the return value from main is used to determine error codes of your program on exit. Anything that isn't zero is considered an error.

Including Other Packages


#include <package.h>
#include "some/path/to/package.h"

int main(...){}

Two ways of including packages:

  1. The first is any installed packages/libraries on the machine which are found in your "search path" (probably /usr/include/ on the iLabs).
  2. The second way is to include any packages that aren't in your search path - you have to give the path to the header file (can be relative or full path).

For the projects in this class, you most likely won't need to worry about figuring out which packages to include since we already give them to you. You will need to include any header files that you make yourself though.

Printf


#include<stdio.h>

int main(...){

    printf("<format>", arg1, arg2, ...);
    return 0;
}

Yep, you need to include a package to use printf(). This should be simple enough - you provide a format and for every format identifier, you need to provide an argument that corresponds to it (ie: printf("%d\n", 5); %d is the identifier for ints, 5 being arg1).

Types


Some standard primitives you'll be using in this class:

  • int
  • char
  • float
  • long
  • unsigned int (or long, double, float, char, etc.)

Some types you might see popup from stdint.h:

  • int32_t (32-bit int)
  • uint32_t (unsigned 32-bit int)

Why do we need these other types? Sometimes a machine might not conform the same standards. An integer on the iLabs is 4-bytes, but that's not the case for every machine out there (honestly most machines use the same standard, but better safe than sorry).

Anything With Conditionals


We'll be going through these quickly since these are straightforward

//if-else statement
if(conditional){}
else if(conditional){}
else{}

//all the loops
do{}
while(conditional);

while(conditional){}

int i; //The default standard on GCC (gnu17? iLabs might have it set to something else) might make it so you can now declare inside the loop
for(i=0; i < x; i++){} 

conditional here is a boolean statement.

Some Useful Syntax Using Conditional Parameters


Something cool you can do is initialize values inside a conditional parameter and use it as part of your conditional. This might save you some lines.

int x;
if((x = foo()) == 0){
    ...
}

while((x = bar()) < MAX){
    ...
}

Operators


Nothing out of the ordinary here. These are the same as Java. We'll get into bitwise operators later.

Functions


We'll only be going over function headers here and save the rest for pointers

int foo(){
    return 0;
}

void bar(){
	return;
}

There's no public, private, protected, etc. Those concepts come from the OOP in Java. Just a return type, your function name, and parameters.

Just as a word of warning, sometimes the compiler let's you compile a program with functions that don't return anything despite having a return type. Using -Wall argument at compile time(with gcc) should stop this, but this isn't a required flag (and sometimes gets annoying).

Function Prototypes


USE THESE - not enough students use function protoypes - though for the most part the projects will force you to use them one way or another (because header files).

Function prototypes are useful since they let you globally reference functions, regardless of what order you write them in.

Why does this matter? Well, unlike Java, C doesn't let you write functions in any order and use them.

int main(...){

    foo(5, 'f');  //The compiler will complain that foo() doesn't exist or there's an implicit declaration of a function.
    return 0;
}

void foo(int a, char b){}

So how do we fix this? Write a function prototype before all of your functions. We'll get into how to clean this up later in header files for when you have too many function prototypes.

void foo(int, char); //you don't have to put the names of the parameters, just the types

int main(...){
    ...
}

void foo(...){}

You could also just write every function above the one you call it in, but function prototypes have other uses (again we'll expand on this in header files).