Learning C for the first time
This is my first time trying to learn C, though I've learnt how to program in Rust, I never tried to even try to learn C.
I was much more interested in Rust's memory safety, but I guess it was time to try to learn C lol
I'm already familiar with how programming languages work, so I jumped right into pointers and memory allocation.
Trying to understand pointers.
I tried to understand them by reading this guide on pointers, but I just couldn't get it So I ended up reading this and this other video on Pointers, which they gave me a better idea on how they work, tho I'm still a bit confused on how they work lol
Memory on C.
Unlike Rust, you have to manually manage data on the Heap, using the malloc() function, for stuff like arrays which you do not know the size of at compile time, I guess that's not too hard right?
I, once again, read the learn-c.org guide on memory management, and also watched this video on malloc()
Are these the best resources to learn? Probably not, but that's what I (kinda) understood.
Practice!
It was time to put into practice what I (somewhat) learnt so far!
I wrote this first program.
#include <stdio.h>
#include <stdlib.h>
int main() {
float *my_box = (float *)malloc(sizeof(float));
if (my_box = NULL) {
printf("Error: No memory available!\n");
return 1;
}
*my_box = 10;
printf("Number %f in box\n", *my_box);
*my_box = 999;
printf("Now its: %f\n", *my_box);
free (my_box);
return 0;
}
Very, very basic, but it made me understand how mallloc and dereferencing works.
First this float *my_box = (float*)malloc(sizeof(float));, honestly I'm not quite sure what (float*) does? I probably should look into that lol, but at least I understand the rest.
I call malloc() to get the size of memory needed to store a float value.
Then in the if statement, I actually check if malloc() assiged me memory, which might not happen.
Then, I dereference *my_box to change its value, as it goes to that memory address and modifies its value.
At last, I free() the allocated memory.
Thats the first C program I made!
Second practice!
It was time to take what I learnt about malloc and see if I could make a list of boxes instead of just one!
I wrote this program to track temperatures over several days.
#include <stdio.h>
#include <stdlib.h>
int main() {
int days;
float sum = 0.0;
float average = 0.0;
printf("How many days to track? ");
scanf("%d", &days);
float *temperatures = (float*)malloc(days * sizeof(float));
if (temperatures == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
for (int i = 0; i < days; i++) {
printf("Enter temp for day %d: ", i + 1);
scanf("%f", &temperatures[i]);
}
printf("\n--- report ---\n");
for (int i = 0; i < days; i++) {
sum += temperatures[i];
printf("Day %d: %.2f\n", i+1, temperatures[i]);
}
average = sum / days;
printf("Average temp: %.2f\n", average);
free(temperatures);
return 0;
}
A bit more code this time, but it really helped me understand how dynamic arrays work.
First, I set up int days to hold the user input and float sum to do some math later. I ask the user how many days they want to track because I don't know the size of the array beforehand.
Then comes the big line: float *temperatures = (float*)malloc(days * sizeof(float));. This is just like the single box I did before, but this time I multiplied sizeof(float) by the number of days.
This gives me a block of memory big enough to hold all the float values side-by-side.
I still do the if (temperatures == NULL) check, to know if the computer is out of memory.
Then, I use a for loop to get the input. The cool thing is that even though temperatures is a pointer, I can use it just like an array: temperatures[i]. This puts the value into the correct "box" in that chunk of memory I allocated.
After that, I use another loop to calculate the sum by adding up all the values stored in temperatures[i].
I calculate the average using simple division: average=sum/days.
At last, I free(temperatures) to release that whole block of memory back to the system.
That's my first dynamic array in C using malloc() and pointers
That's it for today, but for a first day, I think it was a pretty decent chuck of C.
Pretty happy with what I've learned, but I still a lot to learn!