Your First C Adventure: Hello World in VS Code

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

Hey there, budding C programmer! Ready to embark on your coding journey? Let’s start with the classic “Hello World” program using Visual Studio Code. Don’t worry if you’re new to this – we’ll walk through it step by step!

Setting Up VS Code for C

Great job on setting up VS Code using the instructions from the official documentation. That’s an excellent first step! Now that your environment is ready, let’s write some code.

The Hello World Program

Create a new file in VS Code and save it as “hello.c”. Then, type in this code from “The Book of C”:

#include <stdio.h>

int main(int argc, char * argvar[]) {
    printf("Hello, World!\n");
    return 0;
}

Let’s break down this code and see what each part does:

  1. #include <stdio.h> This line tells the compiler to include the standard input/output library, which contains the printf() function we’ll use.
  2. int main(int argc, char * argvar[]) { This line declares the main function, where program execution begins. The argc and argvar parameters allow command-line arguments, though we won’t use them in this example.
  3. printf("Hello, World!\n"); This line prints “Hello, World!” to the screen. The \n adds a new line after the message.
  4. return 0; This statement indicates that the program has executed successfully.
  5. } This closing curly brace marks the end of our main function.

Running Your Program in VS Code

Now that you’ve written your code, let’s run it:

  1. You will click the Run icon in the upper right corner of the editor.

If everything went well, you should see “Hello, World!” printed in your terminal. Congratulations! You’ve just written and run your first C program in VS Code!

Program Output

Why This Matters

This simple program is your first step towards mastering C. You’ve learned about including libraries, defining the main function, and using printf to output text. These concepts will be the foundation for more complex programs you’ll write in the future.

Challenge: Make It Your Own!

Now that you’ve got the hang of it, why not experiment a bit? Here are some ideas:

  1. Change the message to greet yourself by name.
  2. Try printing multiple lines using several printf statements.
  3. Use escape characters like or tab or \ to print a backslash.

Remember, the key to learning programming is practice and curiosity. Don’t be afraid to make mistakes – they’re how we learn and grow as programmers!

Pro Tip: VS Code has great features for C programming. Try using breakpoints and the debugger to step through your code line by line!


Happy coding, and welcome to the exciting world of C programming with VS Code! I’m starting my journey with you, so let’s learn and grow together. If you have any questions or need help, feel free to leave a comment below. I’m here to support you every step of the way I can!

To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)