c programming for Linux 3 -data type

In C if we want to work with rational numbers that include floating point then we have the data type float, if we want double precision then can use double.
In order to print floating number we use %f inside the printf.

For example (program between ======)


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

int main(int argc, char *argv[]){
    float num1 = 23.5, num2 =15.7;


    printf("23.5 / 15.7 = %f\n", num1 /num2);

    return 0;
}
==================================
oren@oren-computer:~/c-programming  
$ gcc  start.c -o start
oren@oren-computer:~/c-programming  
$ ./start 
23.5 / 15.7 = 1.496815

We can use the sizeof function to get the size (in memory) in byte of variable or data type.
The return value of sizeof is of type size_t, and we need %zu inside the printf double quote.

For example let print the size in memory in byte unit of num1 and of the data type int:


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

int main(int argc, char *argv[]){
    float num1 = 23.5, num2 =15.7;


    printf("The size in the memory of num1 in byte unit is %zu\n",sizeof(num1));
    printf("The size in memory of the data type int in byte unit is %zu\n", sizeof(int));

    return 0;
}
==================================
oren@oren-computer:~/c-programming  
$ gcc  start.c -o start
oren@oren-computer:~/c-programming  
$ ./start 
The size in the memory of num1 in byte unit is 4
The size in memory of the data type int in byte unit is 4

For a large integer number we can use long int.
For example:


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

int main(int argc, char *argv[]){
    long int bigNumber = 999999999;
    printf("the memory size of long in byte is %zu\n",sizeof(bigNumber));
    printf("the value of bigNumber is %ld\n", bigNumber);
    return 0;
}
========================
oren@oren-computer:~/c-programming  
$ gcc  start.c -o start
oren@oren-computer:~/c-programming  
$ ./start 
the memory size of long in byte is 8
the value of bigNumber is 999999999

As we can see the memory size of long on this machine is 8 byte.
The %ld is used to print long integer.

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