Basic C Programming Language - Learning C by Example (2015)

Learning C by Example (2015)

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.

· int

· long

· float

· char

· double

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 = 100;

You also declare as below

int myvar1;

myvar1 = 100;

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 <stdio.h>

int main() {

int n;

int i,j;

n="hello";

return 0;

}

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 <stdio.h>

int main() {

int n = 10;

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 0;

}

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 <stdio.h>

int main() {

int a,b,c;

a = 10;

b = 6;

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 0;

}

Save this program into a file, called arith.c. Compile and run it.

Program run:

2.7 Mathematical Functions

C provides math library to manipulate mathematical operations. You must include math.h to implement mathematical operations.

Here is an illustration code for mathematical functions usage:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

int main() {

float a;

float b;

float pi = 3.14;

a = 0.25;

printf("value a = %.2f \n",a);

b = abs(-a);

printf("abs(a)=%.2f \n",b);

b = acos(a);

printf("acos(a)=%.2f \n",b);

b = asin(a);

printf("asin(a)=%.2f \n",b);

b = atan(a);

printf("abs(a)=%.2f \n",b);

b = atan2(a,5);

printf("atan(a,5)=%.2f \n",b);

b = cos(a);

printf("cos(a)=%.2f \n",b);

b = sin(a);

printf("sin(a)=%.2f \n",b);

b = tan(a);

printf("tan(a)=%.2f \n",b);

b = sqrt(a);

printf("sqrt(a)=%.2f \n",b);

return 0;

}


Save this code into a file, called mathdemo.c. Then compile and run it. Because we use math.h, we add -lm parameter on compiling.

$ gcc mathdemo.c -lm -o mathdemo

$ ./mathdemo

A program output can be seen in Figure below.

2.8 Comparison Operators

You may determine equality or difference among variables or values. Here is the list of comparison operatos:

== is equal to

!= is not equal

> is greater than

< is less than

>= is greater than or equal to

<= is less than or equal to

This is sample code for comparison usage

#include <stdio.h>

int main() {

int a,b;

a = 10;

b = 6;

printf("%d == %d = %d\n",a,b,a==b);

printf("%d != %d = %d\n",a,b,a!=b);

printf("%d > %d = %d\n",a,b,a>b);

printf("%d < %d = %d\n",a,b,a<b);

printf("%d >= %d = %d\n",a,b,a>=b);

printf("%d <= %d = %d\n",a,b,a<=b);

return 0;

}

Save this code as file, called comp.c.

Here is a sample of program output.

2.9 Logical Operators

These operators can be used to determine the logic between variables or values.

&& and

|| or

! not

To illustrate how to use logical operators in C code, you can write this code.

#include <stdio.h>

int main() {

int a,b;

a = 5;

b = 8;

printf("%d \n",a>b && a!=b);

printf("%d \n",!(a>=b));

printf("%d \n",a==b || a>b);

return 0;

}

Save this code into a file, logical.c. Compile and run it.

2.10 Increment and Decrement

Imagine you have operation as below

num = num + 1;

you can simply this syntax use ++.

num++;

using the same approach for this case

num = num - 1;

you can use -- syntax.

num--;

Now how to implement them in code. Let's write this code.

#include <stdio.h>

int main() {

int a = 10;

a++;

printf("%d \n",a);

a++;

printf("%d \n",a);

++a;

printf("%d \n",a);

a--;

printf("%d \n",a);

a--;

printf("%d \n",a);

--a;

printf("%d \n",a);

return 0;

}

Save this code into a file, called incdec.c.

Compile and run it. Here is sample of program output.

2.11 Decision

There are two approaches to build decision on C. We can use if..then and switch..case.

2.11.1 if..then

Syntax model for if..then can be formulated as below:

if (conditional) {

// do something

}else{

// do another job

}

conditional can be obtained by logical or/and comparison operations.

Here is the sample code:

#include <stdio.h>

int main() {

int a, b;

a = 5;

b = 8;

if(a>b || a-b<a){

printf("conditional-->a>b || a-b<a \n");

}else{

printf("..another \n");

}

return 0;

}

Save this program into a file, called ifdemo.c. Then, run this file.

Program run:

2.11.2 switch..case

switch..case can be declared as below:

switch(option){

case option1:

// do option1 job

break;

case option2:

// do option2 job

break;

}

Here is the sample code of switch..case usage:

#include <stdio.h>

int main() {

// you can obtain input value from keyboard

// or any input device

int input = 3;

switch(input){

case 1:

printf("choosen 1\n");

break;

case 2:

printf("choosen 2\n");

break;

case 3:

case 4:

printf("choosen 3\n");

break;

}

return 0;

}

Save this code into a file, called switch.c. Try to build and run it.

Program run:

2.12 Iterations

Iteration operation is useful when we do repetitive activities. We use for and while syntax

2.12.1 For

The simple scenario that illustrates iteration scenario is to show list of number. We do iteration until the number value less than 10.

#include <stdio.h>

int main() {

int i;

for(i=0;i<10;i++){

printf("data %d\n",i);

}

return 0;

}

Save this code into a file, called for.c. Compile and run it.

Here is a program output:

2.12.2 While

The following is the same code like 2.12.1 scenario but it uses while syntax.

#include <stdio.h>

int main() {

int num = 0;

while(num<10){

printf("data %d\n",num);

num++;

}

return 0;

}

Save this code into a file, called while.c. Try to compile and run it.

2.13 Struct

We can define a struct to declare a new data type. We use struct keyword. For illustration, we define new data type, called employee.

Create a file, called structdemo.c, and write this code.

#include <stdio.h>

// define a struct

struct employee{

int id;

char name[10];

char country[5];

};

int main() {

// declare struct variable

struct employee emp;

// set values

emp.id = 10;

sprintf(emp.name,"jane");

sprintf(emp.country,"DE");

// display

printf("id: %d, name: %s, country: %s\n",emp.id,emp.name,emp.country);

return 0;

}

Save this code.

Now you compile and run this program.

$ gcc -o structdemo structdemo.c

$ ./structdemo

A sample output of program can be seen in Figure below, Mac platform.

mac2-1