Anda di halaman 1dari 5

Table of Contents

Learn the Basics

Hello, World!
Variables and Types

Arrays

Strings

For loops

While loops

Functions

Advanced

Pointers
Structures

Function arguments by reference

Dynamic allocation

Recursion

Linked lists

Binary trees

Unions

Pointer Arithmetics

Function Pointers

Hello, World!
Introduction
The C programming language is a general purpose programming language, which relates closely to
the way machines work. Understanding how computer memory works is an important aspect of the

C programming language. Although C can be considered as "hard to learn", C is in fact a very simple
language, with very powerful capabilities.
C is a very common language, and it is the language of many applications such as Windows, the
Python interpreter, Git, and many many more.
C is a compiled language - which means that in order to run it, the compiler (for example, GCC or
Visual Studio) must take the code that we wrote, process it, and then create an executable file. This
file can then be executed, and will do what we intended for the program to do.

Our first program


Every C program uses libraries, which give the ability to execute necessary functions. For example,
the most basic function called printf, which prints to the screen, is defined in the stdio.h header file.
To add the ability to run the printf command to our program, we must add following include derivative
to our first line of the code:
#include <stdio.h>

Execute Code
The second part of the code is the actual code which we are going to write. The first code which will
run will always reside in the main function.
int main() {
... our code goes here
}

Execute Code
The int keyword indicates that the function main will return an integer - a simple number. The number
which will be returned by the function indicates whether the program that we wrote worked correctly.
If we want to say that our code was run successfully, we will return the number 0. A number greater
than 0 will mean that the program that we wrote failed.
For this tutorial, we will return 0 to indicate that our program was successful:
return 0;

Execute Code
Notice that every line in C must end with a semicolon, so that the compiler knows that a new line has
started.
Last but not least, we will need to call the function printf to print our sentence.

Exercise
Change the program in the bottom in a way so that it prints to the output "Hello, World!".

Start Exercise

Variables and Types


Data types
C has several types of variables, but there are a few basic types:

Integers - whole numbers which can be both positive and negative. Defined
using char, int, short, long orlong long.
Unsigned integers - whole numbers which can only be positive. Defined using unsigned
char, unsigned int,unsigned short, unsigned long or unsigned long long.
Floating point numbers - real numbers (numbers with fractions). Defined
using float and double.
Structures - will be explained later, in the Structures section.

The different types of variables define their bounds. A char can range only from -128 to 127, whereas
a long can range from -2,147,483,648 to 2,147,483,647.
Note that C does not have a boolean type. Usually, it is defined using the following notation:
#define BOOL char
#define FALSE 0
#define TRUE 1

Execute Code
C uses arrays of characters to define strings, and will be explained in the Strings section.

Defining variables
For numbers, we will usually use the type int, which an integer in the size of a "word" the default
number size of the machine which your program is compiled on. On most computers today, it is a 32bit number, which means the number can range from -2,147,483,648 to 2,147,483,647 (same
as long).
To define the variables foo and bar, we need to use the following syntax:
int foo;
int bar = 1;

Execute Code

The variable foo can be used, but since we did not initialize it, we don't know what's in it. The
variable bar contains the number 1.
Now, we can do some math. Assuming a, b, c, d, and e are variables, we can simply use plus, minus
and multiplication operators in the following notation, and assign a new value to a:
int a = 0,b = 1,c = 2,d = 3, e = 4;
a = b - c + d * e;
printf("%d", a); /* will print 1-2+3*4 = 11 */

Execute Code

Exercise
In the next exercise, you will need to create a program which prints out the sum of the numbers a, b,
and c.

Arrays
Arrays are special variables which can hold more than one value using the same variable, using an
index. Arrays are defined in a very straightforward syntax:
/* defines an array of 10 integers */
int numbers[10];

Execute Code
Accessing a number from the array is done using the same syntax. Notice that arrays in C are zerobased, which means that if we defined an array of size 10, then the array cells 0 through 9 (inclusive)
are defined. numbers[10] is not an actual value.
int numbers[10];

/* populate the array */


numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;

numbers[5] = 60;
numbers[6] = 70;

/* print the 7th number from the array, which has an index of 6 */
printf("The 7th number in the array is %d", numbers[6]);

Execute Code
Arrays can only have one type of variable, because they are implemented as a sequence of values in
the computer's memory. Because of that, accessing a random array cell is very efficient.

Exercise

The code below does not compile, because the grades variable is missing.
One of the grades is missing. Can you define it so the grade average will be 85?

Anda mungkin juga menyukai