c programming for Linux 2 – data type

In c variable has data type, that define the type of data they contain.
C data type are very similar to java primitive. For integer number we are using the data type int.
int is 4 byte in size and contain only whole number.
Let see an example:


#include 

int main(int argc, char *argv[]){
    int k;
    k = 24; 
    printf("value of k = %d\n", k);
    printf("Hello world\n");

    return 0;
}

So we have declare an int number variable white the name k.
Then we assign to it the number 24.
Eventually we print it, the “%d” in the printf mean decimal, and after the comma, we tell it that it should be k, so it will print:
value of k = 24
c allow us to do mathematical calculation * is used for multiply, / is used to divided it to the largest integer that give a result equal or small from the number.

Example of program and output of the execution, the program code is between the “===”


start.c
======================
#include 

int main(int argc, char *argv[]){
    int k;
    k = 24; 
    int r = 11, f = 2, h = 21, s;

    s = r / f;

    int t = h * f;

    printf("value of k = %d, value of s = %d, value of t = %d, value of h = %d\n",k,s,t,h);
    
    printf("value of k = %d\n", k); 
    printf("Hello world\n");

    return 0;
}
========================

oren@oren-computer:~/c-programming  
$ gcc  start.c -o start
oren@oren-computer:~/c-programming  
$ ./start 
value of k = 24, value of s = 5, value of t = 42, value of h = 21
value of k = 24
Hello world



char is anther data type in c. It used to contain one charter, and when we want to do an assign we should use single quote ‘ and not double. Inside the printf if we want to print char we use %c (c short for char).

Example for program that use char data type:


start.c
======================
#include 

int main(int argc, char *argv[]){
    char c = 'c';
    
    printf("char c value is %c\n", c); 

    return 0;
}
=====================
oren@oren-computer:~/c-programming  
$ gcc  start.c -o start
oren@oren-computer:~/c-programming  
$ ./start 
char c value is c

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s