From JS to C
How to run C
Go to your terminal, run
> gcc ./filename.c. It compiles to a
.exe or
.out binary file. Then just type
./a.exe.
The main function
Put the code you want to run first and sequentially in a function called main.
int main(void)
{
/* code */
return 0;
}
Functions
You must specify the return types and input types of a function upon declaration. Instead of “function”, just write the return type. Void if no return, and also void if no inputs:
int test(int a)
{
return 0;
}
Or
void test (void){}
You call functions in the same way as in JS. However, the number and types of arguments passed
must match the parameters.
If you want a variable number of parameters:
int test (int a, int b, …){
return 0;
}
The parameters and return value can be any type, like an array:
void test(int a[100], int b){}
Like Javascript, C will make a copy of the arguments passed into a function, to protect the original. Also like Javascript, there are some exceptions such as Arrays.
Printing
printf("Hello world\n");
Use
%i to insert an
int into the print string:
printf("Number: %i\n", 5);
Including other files
#include <stdio.h>
Using variables
Instead of
"let", put the type.
int sum = 5;
sum = 50 + 25;
int declared_not_used;
Variable types
-
int/long/long long/short/unsigned/signed for whole numbers
-
floatdouble for decimal numbers
-
char for a single character.
-
bool for a true or false value.
Change type of a value with casts:
float decimal = 42.3;
int num = (int) decimal;
You can also define symbolic constants, which are handled at compile time
#define ANSWER 42
...
printf("%i", ANSWER);
These are really wack because they aren't really variables. They are as if you typed that exact piece of text:
#define ANSWER 42); printf("Another statement!"
...
printf("%i", ANSWER);
Arithmetic
Same operators as JS. Nuances introduced because of static types.
Shorthand variable arithmetic works like in JS:
++i;
i++;
i += 5;
i *= 5;
Boolean operators
In C, 0 is falsey, everything else is truey. Boolean operators work the same as in JS:
5 >= 3;
3 != 4;
User input
int number;
printf("Insert a number: ");
scanf("%i", &number);
Loops
The exact same as JS.
for,
while and
do while work.
break and
continue as well.
Remember that if you define a new variable in the for loop, instead of
let or
var, you have to specify its type such as
int:
for(int i = 0; i < 5; i++){
}
Conditional statement
Same as in JS, including the normal if statement, the shorthand if and the switch.
Arrays
Kinda like JS.
Nth element:
lst[n];
Declaring an array with 100 integers, defaulted to 0:
int grades[100];
Initializing array with elements:
int integers[5] = { 0, 1, 2, 3, 4 }
So repeating the length is redundant:
int integers[] = { 0, 1, 2, 3, 4 }
Initializing array with only some elements, the rest will be set to 0:
int integers[5] = { 0, 1, 2 }
You can even do this (creates a list with 100 elements, 1, 0, 0…, 0, 0, 42):
int integers[] = { [0] = 1, [99] = 42 }
If you want a 2D array:
int integers[2][2] = { {1, 2}, {3, 4} }
Actually, C knows the size of the array beforehand so you don't need the inner braces:
int integers[2][2] = { 1, 2, 3, 4 }
Cool things to add infront of variable declarations
For example:
const int number = 5;
- const
- Same as JS, except it’s a “deep const”. For example, you can’t modify elements of an array initialized as const. Like applying Object.freeze on a constant variable.
- static
- Only initialized once. For example, if initialized in a function, it won’t be deleted when the function call terminates. It’s value will stay for the next function call, and it won’t be re-initialized to the initial value.
Comments:
Same as JS
Global variables
Any variable defined outside a function is global, and can be accessed from anywhere
Structures
Structures are like classes, but instead of assosiating data with methods, they just contain data. Declare a new type of structure like this:
struct date
{
int month;
int day;
int year;
};
Make a new
instance of this structure (the initial values must be constant, not in terms of another variable):
struct date today = { 2, 11, 2020 };
The
{ 2, 11, 2020 } is the value, which is casted into a
struct date, and stored in
today. You could also do the following:
struct date today;
today = (struct date) { 2, 11, 2020 };
As you can see, casting is perfectaly valid here as well.
Furthermore, this is cursed but it works:
struct date tomorrow = { .day = 12, .month = 2, .year = 2020 };
You can also just leave some values undefined, which are defaulted to 0:
struct date yesterday = { 2, 10 };
You can leave them all undefined:
struct date today;
And access and edit the properties of
today with dot notation:
today.month;
today.year = 2015;
Make an array of structs!!!!!!!!!
struct date birthdays[3] = { {2, 11, 2020}, {9, 6, 2002}, {3, 14, 1592} };
You can skip the inner braces, as if this was a two dimensional array:
struct date birthdays[3] = { 2, 11, 2020, 9, 6, 2002, 3, 14, 1592 };
If your brain is sufficiently massive, you can do this:
struct date birthdays[3] = { [1].day = 11, [1].month = 2 };
You can make structures, with structures in them!
struct event
{
struct date start;
struct date end;
};
Initializing an
event is quite reminiscent of a 2D array as well:
struct event planeRide = { { 2, 11, 2020 }, { 2, 12, 2020 } }
Making an
instance of a struct can be summarized as:
struct [struct type] [name];
Because of the fact that making a new struct type returns the
[struct type], you can do this:
struct date
{
int month;
int day;
int year;
} today;
We not only made a new struct type called
date, we also made an
instance of it called
today.
You can also use this to make an array of structs:
struct date
{
int month;
int day;
int year;
} birthdays[3];
And you can even make an
anonymous struct by omitting its name:
struct
{
int month;
int day;
int year;
} today = { 2, 11, 2020 };
Input and output
Every time you press the enter key on the terminal, the operating system adds these characters (plus a newline) to the stdin character stream.
You can pop off the next character:
char c = getchar();
And add it to the stdout stream (essentially printing the character to terminal):
putchar(c);
Characters
Characters in C are just small integers
char c = 'A';
int n = 65;
n == c; //this is true
printf("%i", c); //65
printf("%c", n); //A