Programming in C: 12 Steps (with Pictures)

Table of contents:

Programming in C: 12 Steps (with Pictures)
Programming in C: 12 Steps (with Pictures)

C is one of the older programming languages. It was developed in the 1970s but is still known as a powerful language as it is a low-level language that comes close to machine language. Learning C is a great introduction to programming in more complex languages, and the knowledge you gain is useful for almost any programming language, and can help you eventually get into app development. Read on to get started programming in C.


Part 1 of 6: The preparations

53403 1 1

Step 1. Download and install a compiler

C code must first be compiled by a program that can interpret the code and convert it into a language that the machine can understand. Compilers are usually available for free, and you can download different compilers for each operating system.

  • For Windows, Microsoft Visual Studio Express or MinGW.
  • For Mac, XCode is one of the best C compilers.
  • For Linux, gcc is one of the most popular options.
53403 2 1

Step 2. The Basics

C is one of the older programming languages, but very powerful. It was initially designed for Unix operating system, but eventually became common on almost every system. The "modern" version of C is C++.

C is made up of all functions, and within these functions you can use variables, conditional statements, and loops to store and manipulate data

53403 3 1

Step 3. Check out a few lines of simple code

Go through the (very) simple program below to get a first idea of ​​how the different parts of the language work together, and how programs function.

#include int main() { printf("Hello, World!\n"); getchar(); return 0; }

  • The #include command is placed at the beginning of a program, and loads libraries (code libraries) that contain the functions you need. In this example, stdio.h allows you to use printf() and getchar().
  • The int main() command tells the compiler that the program uses the "main" function and that it will return an integer (integer) after executing it. All C programs run as a "main" function.
  • The characters {} indicate that everything inside is part of the "main" function.
  • The printf() function displays the contents of the parentheses on the user's screen. The quotation marks ensure that the string is printed verbatim. The \n tells the compiler to move the cursor to the next line.
  • The sign; indicates the end of a line. Most lines of code should end with a semicolon.
  • The getchar() command tells the compiler to wait for a keystroke before continuing. This is useful because many compilers run the program and then immediately close the window. This prevents the program from closing until a key is pressed.
  • The return 0 command indicates the end of the function. Note that the "main" function is an int function. This means that it should return an integer once the program finishes. A "0" indicates that the program was executed correctly; any other number indicates an error has been detected.
53403 4 2

Step 4. Try to compile the program

Enter the code into your code editor and save it as a "*.c" file. Now compile this with your compiler, usually by pressing Build or Run.

53403 5 2

Step 5. Always include an explanation with your code

This should be a regular part of the program, but will not be compiled. This explanation helps to remember what the code is for, and as a guide for programmers looking at and/or wanting to use your code.

  • To add comments in C, put a /* at the beginning of the comment and a */ at the end.
  • Comment anywhere except the most standard parts of your code.
  • Comments can be used to quickly hide parts of the code without removing them. Surround the code by enclosing it in comment tags and then compile the program. If you want to use the code again, please remove the tags.

Part 2 of 6: Using variables

53403 6 2

Step 1. The function of variables

Variables allow you to store data, either results of calculations or user input. Variables must be defined before you can use them, and there are several types to choose from.

Some of the more common variables are int, char, and float. Each of these stores a different data type

53403 7 2

Step 2. Learn how variables are declared

Variables must first be given a certain type, or "declared", before they can be used in a C program. You declare a variable by specifying the data type, followed by the name of the variable. For example, the following declarations are all valid in C:

float x; charname; int a, b, c, d;

  • Note that you can declare multiple variables on the same line, as long as they are of the same type. The only thing is that you separate the variables with a comma.
  • Like so many lines in C, it is mandatory to separate each variable declaration with a semicolon.
53403 8 2

Step 3. Know where to declare the variables

Variables must be declared at the beginning of a code block (The parts of code enclosed in {}). If you try to declare a variable later, the program will not function properly.

53403 9 1

Step 4. Use variables to store user input

Now that you know the basics of how variables work, you can write a simple program that accepts and stores user input. You use another function of C for this, namely scanf. This function searches for special values ​​in a string.

#include int main() { int x; printf("Enter a number: "); scanf("%d", &x); printf("The number is %d", x); getchar(); return 0; }

  • The "%d" string/string tells scanf to search for an integer in the user's input.
  • The & before the variable x tells scanf where to find the variable to change it, and stores the integer as that variable.
  • The last command printf reads the variable and displays the result to the user.
53403 10 2

Step 5. Editing the variables

You can edit the data that you have stored in the variables using mathematical expressions. The most important distinction to remember for the mathematical expressions is that a single = captures the value of the variable, while == sets the values ​​on either side of the sign to verify that they are equal.

x = 3 * 4; /* assign "x" to 3 * 4, or 12 */ x = x + 3; /* this adds 3 to the previous value of "x", and sets the new value as variable */ x == 15; /* checks if "x" equals 15 */ x < 10; /* checks if the value of "x" is less than 10 */

Part 3 of 6: Conditional statements

53403 11 2

Step 1. Understand the basics of conditional statements

Conditional statements are what is at the heart of most programs. These are statements that are either TRUE or FALSE, and return a result based on that. The simplest of these statements is the if statement.

TRUE and FALSE work differently in C than what you may be used to. TRUE statements always end with equalizing a non-zero number. When you perform comparisons and the result is TRUE, a "1" is returned. If the result is FALSE, a "0" is returned. Understanding this helps to be able to work with IF statements

53403 12 2

Step 2. Learn the basic conditional operators

Conditional statements revolve around using mathematical operators that compare values. The following list contains the most commonly used conditional operators.

> /* greater than */ < /* less than */ >= /* greater than or equal to */ <= /* less than or equal to */ == /* equal to */ != /* not equal at */

10 > 5 TRUE 6 < 15 TRUE 8 >= 8 TRUE 4 <= 8 TRUE 3 == 3 TRUE 4 != 5 TRUE

53403 13 2

Step 3. The basic IF statement

You can use IF statements to determine what the program should do after the statement has been evaluated. You can combine this with other conditional statements to create powerful, complex functions, but we'll keep it simple for now.

#include int main() { if (3 < 5) printf("3 is less than 5"); getchar(); }

53403 14 2

Step 4. Use ELSE/ELSE IF statements to extend your conditions

You can build on the IF statements by using ELSE and ELSE IF statements to process different results. ELSE statements are only executed if the IF statement is FALSE. ELSE IF statements allow you to use multiple IF statements within the same code block to create more complex conditions. See the sample program below to learn how this works.

#include int main() { int age; printf("Enter your age: "); scanf("%d", &age); if (age <= 12) { printf("You're still a kid!\n"); } else if (age < 20) { printf("It's great to be a teenager!\n"); } else if (age < 40) { printf("You're still young at heart!\n"); } else { printf("With age comes wisdom.\n"); } return 0; }

The program takes the user's input and runs it through a number of IF statements. If the number satisfies the first statement, then the first printf statement is returned. If it doesn't match the first statement, it checks to see if any of the following ELSE IF statements match, until you find something that works. If none of the statements is satisfactory, the last ELSE statement is executed

Part 4 of 6: Loops

53403 15 2

Step 1. The operation of loops

Loops are one of the most important aspects of programming because they allow you to repeat blocks of code until certain conditions are met. This makes implementing repetitive actions very easy, and there is no need to write new conditional statements every time you want something to happen.

There are three different loops: FOR, WHILE, and DO…WHILE

53403 16 2

Step 2. The FOR loop

This is the most common and convenient loop type. This causes a function to continue to run until certain conditions are met, as indicated in the FOR loop. FOR loops require 3 conditions: to initialize the variable, the condition to be met, and the variable to be updated. If you don't need all of these conditions, you'll have to put an empty space with a semicolon, otherwise the loop will continue forever.

#include int main() { int y; for (y = 0; y < 15; y++;){ printf("%d\n", y); } getchar(); }

In the above program, y is set to 0, and the loop continues as long as the value of y is less than 15. Each time the value of y is printed on the screen, 1 is added to the value of y and the loop repeats. If y = 15, the loop will be interrupted

53403 17 2

Step 3. The WHILE loop

WHILE loops are a bit simpler than FOR loops. These have only 1 condition and the loop continues as long as that condition is met. There is no need to initialize or update a variable, but you can do that in the loop itself.

#include int main() { int y; while (y <= 15){ printf("%d\n", y); y++; } getchar(); }

The y++ command adds 1 to the variable y every time the loop is executed. When y has reached 16 (remember that this loop continues as long as y is "less than or equal to" 15), the loop will be stopped

53403 18 2

Step 4. The DO

..WHILE loop.

This loop is very useful for loops that you want to make sure that they are completed at least once. In FOR and WHILE loops, the condition is checked at the beginning of the loop, meaning whether or not the loop is looped. DO…WHILE loops only check at the end whether the condition is met and are therefore executed at least once.

#include int main() { int y; y = 5; do { printf("The loop is running!\n"); } while(y != 5); getchar(); }

  • This loop will show the message even though the condition is FALSE. The variable y is set to 5 and the WHILE loop will continue as long as y is not equal to 5, after which the loop ends. The message was already shown on the screen, because only at the end is checked whether the condition is met.
  • The WHILE loop in DO…WHILE must end with a semicolon. This is the only time a loop ends with a semicolon.

Part 5 of 6: Using features

53403 19 1

Step 1. The Basics of Functions

Functions are self-contained blocks of code that can be called from another part of a program. It makes it a lot easier to iterate code and makes programs easier both to read and to modify. Functions use all the techniques described above, and even other functions.

  • The main() line at the beginning of all previous examples is a function, as is getchar()
  • Features are intended to make reading and writing code more efficient. Make good use of features to streamline your program.
53403 20 2

Step 2. Start with a short description

Functions are best designed by first describing what you want to achieve, before you start the actual coding. The basic syntax of a function in C is "return_type name (argument1, argument2, etc.);". For example, to create a function that adds two numbers, do the following:

int add(int x, int y);

This creates a function for adding two integers (x and y), and returns the sum as an integer

53403 21 1

Step 3. Add the function to a program

You can use the short description to create a program for adding two user-entered integers. The program will define how the "add" function works and use it to process the numbers entered.

#include int add(int x, int y); int main() { int x; int y; printf("Enter two numbers to add: "); scanf("%d", &x); scanf("%d", &y); printf("The sum of the numbers is %d\n", add(x, y)); getchar(); } int add(int x, int y) { return x + y; }

  • Note that the short description is at the beginning of the program. This tells the compiler what to expect when the function is called and what it will return. This is only necessary if you want to define the function later in the program. You can also define add() in front of the main() function so that the result is the same as without the short description.
  • The operation of the function is defined at the end of the program. The main() function obtains the integers from the user and then passes them to the add() function for processing. The add() function then returns the result to main()
  • Now that add() is defined, it can be called anywhere within the program.

Part 6 of 6: Keep learning

53403 22 2

Step 1. Go through some books on programming in C

This article only covers the basics, which is just the tip of the iceberg called C and everything that comes with it. A good book will help you solve problems and save you a lot of headaches later on.

53403 23 2

Step 2. Join a group

There are many groups, both online and in the real world, that are dedicated to programming and all kinds of programming languages. Find a few like-minded C programmers to exchange code and ideas with, and you'll find that you've learned a lot more in a short time than you thought possible.

Go to some hack-a-thons, if possible. These are events where teams and individuals have to come up with the solution and the associated program for a problem within a certain time, something that requires a lot of creativity. You can meet a lot of good programmers there, and hack-a-thons are organized all over the world

53403 24 2

Step 3. Take a course

You really don't have to go back to school to train as a programmer, but it can't hurt to take a course and speed up your learning pace. Nothing can beat direct help from people who are very well versed in a particular matter. You can often find a course nearby, or try searching for an online course.

53403 25 2

Step 4. Also consider learning C++

Once you've mastered C, it doesn't hurt to move on to C++. This is the more modern version of C, and offers much more flexibility. C++ is designed for working with objects, and being able to work with C++ allows you to write powerful programs for almost any operating system.


  • Always comment on your programs. Not only does this help others understand your source code, but it also helps you remember what you coded and why. Maybe you know what you're doing now, but after a month or 2-3 there's a good chance you won't have any idea.
  • Remember to end statements such as printf(), scanf(), getch(), etc with a semicolon (;) but never place them after statements like 'if', 'while' or 'for' loops.
  • When you encounter a syntax error while compiling and you get stuck, use your favorite search engine to find out what the error message means. There is a good chance that someone else has already posted a solution for the same problem.
  • The source code must have a *.c extension so that the compiler knows it is a C file.
  • Remember, practice makes perfect. The more you practice writing programs, the better you will become. So start with simple, short programs until you get a foothold, then move on to the more complex programs.
  • Learn more about logic. This helps to solve various problems while you are coding.

Popular by topic