DEV Community

darkmage
darkmage

Posted on

Introduction to Programming in C

Introduction to Programming in C

This is designed to be a quick blast through "Hello, World" as well as primitive datatypes and variables. We'll add some more terms necessary to have discussions about programming as they are needed. I will make attempts to italicize special terms, such as primitive, datatype, and variable. Special terms to come include function, method, data structure, among others.

#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("Hello, World!\n");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

That's it. That is your basic "Hello, World" program in C.

If you already know C, then it is clear what is happening here.

If you only know how to program in other languages, you can probably infer a lot about C syntax from this example.

However, if you don't know programming at all, then there are some basics that you need to pick up. The good news is that the basics picked up here can transfer into other languages as well.

So, whether or not you're an experienced programmer or a novice, I hope to convey or expose some new ways of thinking, and if not, then hopefully I am helping to reinforce things that you already know.


The first line is an #include statement, which is a command to the C preprocessor, a program that runs on your code first before it is actually compiled, that tells it to include code from the specified header file. In this case, it is "stdio.h", which is short for "standard input output". The .h means "header".

The second line is the "main" function declaration and definition. Every program has a starting point, an entry point, and most of the time, it is going to be called "main". There are very slight exceptions to this rule, and many can be found in embedded programming. The return type of this function is int, short for integer, and its parameters are the "argument count" (argc) and the "argument vector" (argv). argv is an array of strings (char *) passed in from the command line, or however your program is executed, and can be used to pass optional parameters to be processed by your program.

The third line, printf, is short for "print format". The string to be printed is "Hello, World", with a newline character at the end. Every statement ends in a semicolon (generally).

The fourth line, "return 0", designates the end of this function and is returning 0, or our "success" value, to the calling program (wherever this program is ran).

The last line, a lone closing curly brace, designates the end of this function definition.


As I get started blogging again, expect the material-level to increase based on what I am comfortable writing about. I have been writing a lot of C lately for my game, so I felt like dumping out a quick, super-basic, intro to C. I hope you have enjoyed reading this, and look forward to more content in the future.

Top comments (2)

Collapse
 
dynamicsquid profile image
DynamicSquid

Cool! Just a tip though, I wouldn't recommend teaching beginner's this:

int main(int argc, char *argv[]) {
Enter fullscreen mode Exit fullscreen mode

As it's a little confusing to explain

Collapse
 
therealdarkmage profile image
darkmage

This is fair, but it is also important to be in the habit of knowing about CLI arguments early on.

Back in High school when I was taught both C++ and Java, teachers would have us type in public static void main(String args[]) without really understanding why, either.

Beginners do not need to understand everything right away in order to simply "do" something. In another Universe, a C student goes his whole life without learning about the argument vector and misses an important concept early on :D