Week 1: Diving into C Programming: A Beginner's Adventure

Week 1: Diving into C Programming: A Beginner's Adventure


After my first week in this computer science course, I quickly realized the importance of establishing a strong foundation in programming concepts. Starting with Scratch, I familiarized myself with fundamental concepts that would later serve as building blocks for my exploration of more advanced languages. However, it wasn't until I delved into the world of C programming that I truly began to grasp the power and versatility of coding.

0ae4600a7495519de00183e2762f8fd55ca1952d64e20e5d92fac33e82fc0960.jpg

Hello World in C:

Armed with Visual Studio Code (VS Code) as my trusty companion, I took my first steps into the world of C programming. Commands like `code`, `make`, and `./hello` became second nature as I learned to write, compile, and run programs with ease. The simplicity of the `printf` function allowed me to print my first "Hello, World!" message onto the screen, marking the beginning of my coding odyssey. It became evident that attention to syntax and the inclusion of necessary header files were paramount in ensuring the success of my endeavors.

Hello World in C:

#include <stdio.h>


//////Hello World!//////
int main(void)
{
   printf("Hello World!\n");
}

Hello World output:

3d0d07a54e1fc1aa104ad11b84b6da8291641d71e019fc128dd42d8046c76f6a.png

Functions and Libraries in C:

As I delved deeper into the intricacies of C programming, I discovered the power of custom functions and the utilization of libraries. The `cs50.h` library proved to be a valuable asset, simplifying my code and enhancing its readability. With each function I defined and each library I incorporated, my understanding of modular programming and code reuse grew exponentially.

 

Variables and Conditionals:

The concept of variables and conditionals introduced me to the backbone of programming logic. From declaring and utilizing variables of various data types to implementing conditional statements such as `if`, `else if`, and `else`, I learned to navigate the intricacies of decision-making within my code.

 

Loops in C:

With the introduction of loops, a whole new realm of possibilities opened up before me. Whether it was using `while` loops for repetitive tasks or employing `for` loops to print intricate patterns, I marveled at the efficiency and elegance of loop structures in C programming.

While loop in C:

#include <stdio.h>
#include <cs50.h>


int main(void)  ////The loop that makes most sense to me.
{
   int j = 0;
   while (j < 4)
   {
       printf("#");
       j++;
   }
   printf("\n");
}

While Loop output:

83769f0b2574c559bcd4787e92c336ec1275a42476c779e5a795b925de06348c.png

Operators and Abstraction:

The utilization of mathematical operators for calculations allowed me to perform complex computations with ease. By abstracting my code into functions, I gained a newfound appreciation for modularity and clarity in programming, laying the groundwork for more sophisticated algorithms in the future.

Pyramids in C:

#include <stdio.h>
#include <cs50.h>
  //////New Function Prototype//////
void lprint_row(int length);
void buildPyramid(int height);


   //////Main Function//////
int main(void)
{
   //////Left Aligned Pyramid//////
   int lheight = get_int("How many # tall would you like your Left Aligned pyramid? ");


   for (int i = 0; i < lheight; i++)
   {
       lprint_row(i + 1);
   }


   //////Right Aligned Pyramid//////


       // Initialize the variable height
   int height;


       // Run the loop to get a value of height between 1 and 8, inclusive, from the user
   do
   {
       height = get_int("How many # tall would you like your Right Aligned pyramid? ");
   }
   while (height < 1 || height > 100);


       // Call the function and pass height to it as a parameter
   buildPyramid(height);
}


   //////Create Function//////
void lprint_row(int length)
{
   for (int i = 0; i < length; i++)
  {
       printf("#");
  }
  printf("\n");
}


void buildPyramid(int height)
{
   // Loop to add a new line
   for (int i = 0; i < height; i++)
   {
       // Loop to add spaces
       for (int k = height - i; k > 1; k--)
       {
           printf(" ");
       }
       // Loop to add hashes
       for (int j = 0; j <= i; j++)
       {
           printf("#");
       }
       printf("\n");
   }
}

Pyramids quickly became one of my favorite codes so far because it was fun and challenging to figure out! I loved utilizing what I learned about creating functions for this project. Pyramids output: 

6d78c531284e1da5d169e25c44baffae61c53d30ef62261fd9ffe5b105e471e8.png

Linux and Command Line:

Basic Linux commands became my allies as I familiarized myself with file manipulation and directory navigation. Interacting with VS Code's terminal provided me with invaluable insights into the inner workings of my codebase, empowering me to manage files and execute commands with precision.

 

Comments and Types:

I quickly learned the importance of comments in documenting my code and enhancing its readability. An overview of data types, from `bool` to `int`, deepened my understanding of memory management and data manipulation, equipping me with the tools necessary to tackle complex programming challenges.

 

Conclusion:

As I reflect on my journey through the realms of C programming, I am filled with a sense of accomplishment and excitement for the adventures that lie ahead. From mastering the basics of syntax to delving into the intricacies of algorithms, each lesson learned has brought me one step closer to becoming a proficient programmer. With a solid foundation in programming concepts and a thirst for knowledge, I eagerly anticipate the challenges and discoveries that await me on my continued journey into the world of computer science. 

How do you rate this article?

7


LunaFey
LunaFey

Check out my latest blog for early releases at https://lunafey627.blogspot.com/. Coming Soon - I've recently embarked on a HarvardX CS50 course, an introduction to Computer Science, and I'm excited to share my journey, notes, and projects with you soon!


CodeQuest: A Journey of Rediscovery
CodeQuest: A Journey of Rediscovery

Description: Join me on CodeQuest, a blog chronicling my journey of rediscovery through coding. Follow along as I dive into online courses, tackle projects, and share insights on my path to mastering programming languages.

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.