First Look at the Syntax of C
Authors
Section 1: Basic Operations with C- Arithmetic and Logicals
Most programming languages share a math-like syntax when doing basic calculator operations, and C and C++ (which builds extra features onto the C language) are no exception! Of course, there can be some surprises when using a new tool.
| Code | Description |
|---|---|
1 + 2 * 3 / 5 |
Arithmetic Operators |
10 % 3 |
Remainder after Integer Division Operator |
(3 > 5) == (2 <= 5) != (6 >= 4) |
Comparison Operators |
12 |
int (integer) |
12.0 |
double decimal value (needed for decimal math) |
true |
bool logical value |
Exercises
Let’s play around a bit with some math to get a feel for it! In each exercise, fill in the requested code below. If you’re stuck, use the table above to find helpful code.
Example: What is one plus one?
1 + 12Exercise: What is three plus five?
Solution
3 + 58Exercise: What is six times eight?
Solution
6 * 848Exercise: What is 12 divided by 4?
Solution
12 / 43Exercise: What is 9 divided by 5?
Solution
9 / 5.01.8000000Exercise: How many whole times does 20 go into 3? (in other words, integer division 20 divided by 3)
Solution
20 / 36Exercise: What is the remainder after dividing 20 by 3?
Solution
20 % 32Exercise: What is 4 + 5 * 2? Does C++ follow the right order of operations?
Solution
4 + 5 * 214Exercise: Is 0.1 + 0.2 - 0.3 equal to zero?
Solution
(0.1 + 0.2 - 0.3) == 0falseExercise: Is 0.1f + 0.2f - 0.3f equal to zero?
Solution
(0.1f + 0.2f - 0.3f) == 0trueExercise: Is four times six greater than three times seven?
Solution
4 * 6 > 3 * 7trueExercise: Is true equal to 1 in C++?
Solution
true == 1trueExercise: Is false equal to 0 in C++?
Solution
false == 0trueSection 2: Coding with Static Types- Identifiers (a.k.a. “Variables”)
We didn’t create any variables last time; that’s because C and C++ require that the type of the identifier (the name of the variable) be specified.
This extra bit of code helps the compiler make the code extremely efficient, as it takes a lot of guesswork out of the equation.
It also gives the programmer a lot of creative freedom to decide how the computer should manage its resources, which is useful when you have lots of data to process (like in data analysis), or
have little computing power to work with (like in Arduino microprocessors).
| Code | Description |
|---|---|
int x; |
“Make a new identifier called x, which references an integer in memory.” |
x = 42; |
“Store 42 at where the (previously-created) identifier x is stored.” |
int x = 42; |
“Make a new identifier called x, which references an integer in memory, and store 42 there.” |
short x = 10 |
b |
Exercises
Example: Set x to the value 3, and y to x plus 5. Encode both as int. Print the value of y.
int x = 3;
int y = x + 5;
y8Exercise: What if y was encoded as a double? Would it change what was printed?
Solution
int x = 3;
double y = x + 5;
y8.0000000Exercise: What if y was encoded as a float?
Solution
int x = 3;
float y = x + 5;
y8.00000fExercise: What if y was encoded as a char?
Solution
int x = 3;
char y = x + 5;
y'0x08'Different types require different amounts of memory; the more memory a type uses, the greater the range of values it can store. Reference the two tables at https://en.cppreference.com/w/cpp/language/types to create integers stored in different ways:
Example Create num, an integer that takes up only 16 bits, and is signed (it can store both positive and negative numbers).
short num;
num0Example: Set the value of num to 1000 (without changing its type). Does it store it properly?
num = 1000;
num1000Exercise: Set the value of num to negative 1000. Does it store it properly?
Solution
num = -1000;
num-1000Exercise: Set the value of num to a million. Does it store properly?
Solution
num = 1000000;
numinput_line_49:2:8: warning: implicit conversion from 'int' to 'short' changes value from 1000000 to 16960 [-Wconstant-conversion]
num = 1000000;
~ ^~~~~~~
16960Exercise: Now, instead of explicitly setting the value to 1 milliion, this time simply add one million to num. Does it store the number correctly? What’s different about what happens?
Solution
num = num + 100000;
num-14112The code below creates temp_k, a temperature stored in Kelvin, which means there should never be negative values. This time, let’s store it using the most memory we can: something with at least 64 bits of memory. (See the table here to find a good candidate: https://en.cppreference.com/w/cpp/language/types)
Solution
unsigned long long int temp_k;
temp_k0Exercise: Set temp_k to the highest value you can without getting an error.
Solution
temp_k = 999999999999999999;
temp_k999999999999999999Exercise: Check that temp_k is only for positive values, by trying to set temp_k to negative one. What value is temp_k?
Solution
temp_k = -1;
temp_k18446744073709551615Exercise: Can we store text as an integer? Try storing the string "hello!" in temp_k.
temp_k = "Hello!";
temp_kExercise: Create brightness, an image pixel value that stores a value between 0-255. That’s 256 values in total that the pixel could be set at. Since 256 is , let’s store it in one of the integer types in (https://en.cppreference.com/w/cpp/language/types) only using 8 bits.
Solution
unsigned char brightness;
brightness'0x00'Exercise: Store the number 66 in brightness. What’s weird about how the number is printed? (Note: the reason for this is because 8-bit integers are also used to store ASCII text data. To tell C++ that it can be “elevated” in its representation to a number, put a plus sign before the variable when printing it: (e.g. +brightness).
Solution
brightness = 66;
+brightness66Section 3: Introduction to C++ Arrays
| Code | Description |
|---|---|
char myString[10] = "Hello"; |
Store the characters ‘Hello’ in a char array of fixed-length of 10. In C, this is essentially a String. |
int myNums[5] = {1, 2, 3, 4, 5}; |
Store the integers 1-5 in an array. |
myNums[2] = 33; |
Put the value 33 in the third index of the myNums array. |
Exercises
Example: Put the numbers 10, 20, and 30 into an array of length 3 called cc. How does it look when printed?
int cc[3] = {10, 20, 30};
cc{ 10, 20, 30 }Exercise: Put the numbers 1, 2, and 3 into an array of length 10 called nums. How does it look when printed
Solution
int nums[10] = {1, 2, 3};
nums{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0 }Exercise: Replace the fifth value in nums with 555. Did it work?
Solution
nums[4] = 555;
nums{ 1, 2, 3, 0, 555, 0, 0, 0, 0, 0 }Exercise: Put the character 'i' in the 6th position in nums. Did it work?
Solution
nums[6] = 'i';
nums{ 1, 2, 3, 0, 555, 0, 105, 0, 0, 0 }Exercise: Make a new array called small, that contains only 8 boolean values. Don’t initialize it with any values. What’s the default?
Solution
bool small[8];
small{ false, false, false, false, false, false, false, false }Exercise: Use the sizeof() function. How many big is the small array?
Solution
sizeof(small)8Exercise: Use the sizeof() function. How big is the nums array? Is this what was expected? What does this mean about what sizeof() is calculating?
Solution
sizeof(nums)40Exercise: Make an array of integers called birthday that stores the year, month, and day of your birthday. Make it have the smallest sizeof() measurement you can, while still storing the data correctly.
short birthday[3] = {1992,5,8};
sizeof(birthday)6Exercise: Declare an array of chars called myName and initialize it with your name.
Solution
char myName[5] = "Nick";
myName"Nick"Exercise: Change the first character of the string to ‘A’.
Solution
myName[0] = 'A';
myName"Aick"Exercise: Change the last character of the string to ‘Z’
Solution
myName[4] = 'Z';
myName"AickZ"Exercise: Replace your name entirely with your last name. (Note that this doesn’t work the same way as with numbers; the strcpy(old, new) function is useful for this).
Solution
strcpy(myName, "Josephina");
myName"Josep"Section 4: Structures and Structure Arrays
Great code is self-documented, and the simplest way to compress your data into small packets and make your code readable at the same time is the handy struct!
| Code | Description |
|---|---|
struct MyStruct {int a; short b; unsigned char c;} |
Creates a structure called MyStruct with three named variables |
struct MyStruct x = {1, 2, 3} |
Puts data into the structure. |
x.a |
Gets the data from the a variable in the structure |
Exercises
Exercise: Make a Birthday struct containing the year, month, and day of a birthday.
Solution
struct Birthday {int year; int month; int day;}
BirthdayExercise: Store a birthday into a Birthday struct, called bday. Note that printing this will be tricky, you’ll get an error.
Solution
struct Birthday bday = {2022, 7, 23};Exercise: Access the month field from bday. Is it correct?
Solution
+bday.month7Exercise: Access the day field from bday1. Is it correct?
Solution
+bday.day23Exercise: How many bytes does bday1 take up?
Solution
sizeof(bday)12Exercise: Make a the Birthday2 struct so that it takes up as little possible space to store the information accurately. Could you get it smaller than the array you used before?
Solution
struct Birthday2 {ushort year; unsigned char month; unsigned char day;};
struct Birthday2 bday2 = {2022, 7, 23};
sizeof(bday2)4Exercise: Make an array of structs containing three birthdays, called bdays. Fill the array with the three birthdays’ data.
Solution
struct Birthday2 bdays[3];
bdays[0] = {1988, 1, 15};
bdays[1] = {2222, 3, 2};
bdays[2] = {3333, 11, 12};Exercise: Print the month of the first birthday in bdays:
Solution
bdays[0].month'0x01'Exercise: Print the day of the second birthday in bdays
Solution
bdays[1].day'0x02'Exercise: Print the year of the third birthday in bdays
Solution
bdays[2].year3333Exercise: How many bytes does bdays take up?
Solution
sizeof(bdays)12