#include <stdio.h>
int main(void) {
printf("Hello, world\n");
return 0;
}
0
or 1
Letter | Binary Sequence | Letter | Binary Sequence |
---|---|---|---|
A | 00000 | B | 00001 |
C | 00010 | D | 00011 |
E | 00100 | F | 00101 |
G | 00110 | H | 00111 |
I | 01000 | J | 01001 |
K | 01010 | L | 01011 |
M | 01100 | N | 01101 |
O | 01110 | P | 01111 |
Q | 10000 | R | 10001 |
S | 10010 | T | 10011 |
U | 10100 | V | 10101 |
W | 10110 | X | 10111 |
Y | 11000 | Z | 11001 |
0
s and 1
s means, we can store and retrieve data!int
-> an integer, a whole number (1, -5, 100)char
-> a single character ('a', 'V', ' ')double
-> a floating point number (3.14159)int
-> 32 bits in C, 4 byteschar
-> 8 bits, 1 bytedouble
-> 64 bits, 8bytesint
-> -2,147,483,648
to 2,147,483,647
char
-> -128
to 127
charsdouble
-> -2,147,483,648
to +2,147,483,647
name
is different to nAme
first_name
int
char
double
To declare a variable, you use:
<type> <name>;
int age;
char first_initial;
double pi
#include <stdio.h>
int main(void) {
// declare an int.
int my_age;
// assign a value to the int.
my_age = 25;
// whoops, I wish... let's update
my_age = 28;
return 0;
}
printf
printf
The format specifier (%) indicates WHERE a value will output within the format string.
int my_age = 13;
printf("I am %d years!", my_age);
%c
for chars%d
for ints “decimal integer”%lf
for “long floating point number” (a double)printf
needs to know what type it should expect in what order, because...int diameter = 5;
double pi = 3.141;
printf("The diameter is %d, pi is %lf", diameter, pi);
scanf
%d
, %lf
, %c
are used in the same way&
symbol tells scanf where to store the data (more details later in term)#include <stdio.h>
int input;
printf("Please enter your age: ");
scanf("%d", &input);
scanf
scanf("%d", &my_int);
scanf("%c", &my_char);
scanf(" %c", &character);
#define <NAME> <value>
#define PI 3.1415
A lot of arithmetic operations will look very familiar in C
+
-
*
/
int age = 28;
int current_year = 2023;
int year_born = current_year - age;
printf("You were born in %d", year_born);
chars
are just ints
playing dress-up
char letter = 'b';
letter = letter + 1;
printf("%c\n", letter);
^^ Will print 'c'
If we add two large ints together, we might go over the maximum value, which will actually roll around to the minimum value and possibly end up negative