Copyright
Learning C by Example
Agus Kurniawan
1st Edition, 2015
Copyright 2015 Agus Kurniawan
Table of Contents
Preface
This book is a practical book to get started with C language. It describes all the elements of the language and illustrates their use with code examples.
Agus Kurniawan
Depok, March 2015
1. Development Environment
This chapter explains how to start with development environment using C.
1.1 Getting Started
C is a general-purpose, imperative computer programming language. It supports structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations.
In this chapter, we prepare development environment to develop C program. You can write C program with your program. We focus on Windows, Linux and Mac platforms.
1.2 Compilers
There are many C compilers to develop C program. In this book, we use GCC as C compiler for Linux, Mac and Windows. For Windows users, you also can use Visual C++ compiler.
1.2.1 Linux
Installation of GCC C is easy. For Ubuntu Linux, you can do it using console and write this script
$ sudo apt-get install build-essential
If installation already finished, you can check GCC version on console and write script as below
gcc --version
You will see gcc version, for instance shown in Figure above.
1.2.2 Windows
If your computer has Windows platform, you can use Cygwin. Download it on http://cygwin.com/setup.exe and then run it.
On setup dialog, you choose Devel package as follows.
After installed, you will get Cygwin terminal. Run it.
You can check GCC version. The following is a sample of output for GCC version
1.2.3 Mac
To install GCC, you can install Xcode so you get GCC. After installed, you can verify it by checking GCC version.
1.3 Development Tools
Basically you can use any editor tool for instance, vi, vim, gedit, Eclipse. The following is vi editor in Ubuntu.
Here is gedit editor
1.4 Hello World
Now we start to write the first program using C.
Firstly, open your text editor and create new file, called hello.c
Let's write this code
#include int main () { printf( "Hello C \n " ); return ;}
Save this file.
Now open your Terminal and compile this file.
sudo gcc hello.c
If you check, you will get file a.out. GCC will generate a.out file if you don't specify an output file name. You can define the output compiled file as follows
sudo gcc hello.c -o hello
After compiled, you can run the compiled file by writing this command (for instance, the compiled file is hello.out)
./hello
A sample output of program is run on Mac platform.
2. Basic C Programming Language
This chapter explains the basic of C programming language.
2.1 Common Rule
In C language if you write a line of code we must write semicolon (;) at the end of code. Here is the syntax rule:
syntax_code;
2.2 Declaring Variable
To declare a variable called myvar1 , write the following:
int myvar1;
int is data type.
The following is the list of common data type you can use in C language.
We can also write many variables as follows:
int myvar1, myvar2;
Once declared, these variables can be used to store data based on its data type.
2.3 Assigning Variables
Variable that you already declare can be assigned by a value. It can done using the equals sign (=). For example, variable myvar1 will assign the number 100, you would write this:
int myvar1 = ;
You also declare as below
int myvar1;myvar1 = ;
You must assign a value on a variable properly based on data type. If not, you will get error on compiling process, for instance, write this code
#include int main () { int n; int i,j; n = "hello" ; return ;}
Save this code as file, called var1.c.
Try to compile this file. You will get a warning message as shown Figure as below
2.4 Comment
You may explain how to work on your code with writing C. To do it, you can use // and /* */ syntax. Here is sample code:
// bank account char accountCode; /* parameters*/ int p1, p2, p3, p4;
2.5 Input & Output on Console
You may want to show message on console using C. You can use printf() to write message on console. printf() also can be passed parameters inside using %d for integer numeric and %f for floating numeric. For char data type, we can use %c for passing parameter.
Here is a sample code
#include int main () { int n = ; float m = 1.245 ; char c = 'A' ; printf( "%d \n " ,n); printf( "%f \n " ,m); printf( "%.2f \n " ,m); printf( "%.3f \n " ,m); printf( "%c \n " ,c); return ;}
Save this code into a file, called inout.c.
Compile and run this code
You can see on %f that wrote floating data with 6 decimal digits. You can specify the number of decimal digit by passing numeric after dot (.), for instance, %.2f and %.3f.
2.6 Arithmetic Operations
C supports the four basic arithmetic operations such as addition, subtraction, multiplication, and division. The following is the code illustration for basic arithmetic:
#include int main () { int a,b,c; a = ; b = ; c = a + b; printf( "%d + %d = %d \n " ,a,b,c); c = a - b; printf( "%d - %d = %d \n " ,a,b,c); c = a * b; printf( "%d * %d = %d \n " ,a,b,c); float d = a / b; printf( "%d / %d = %.2f \n " ,a,b,d); float e = ( float )a / b; printf( "%d / %d = %.2f \n " ,a,b,e); return ;}