In this post, we will learn how to convert kilobytes to bytes using C Programming language.
A Kilobyte also known as KB is a multiple unit used for measurement of binary data. A kilobyte is approximately 1,000 bytes(1024 bytes).
1 Kilobyte = 1024 Bytes
We will be using the above measurement data to convert kilobytes to bytes in our program.
So, without further ado, let’s begin this tutorial.
C Program to Convert Kilobytes to Bytes
// C Program to Convert Kilobytes to Bytes #include <stdio.h> int main(){ int bytes; double kilobytes; // Asking for input printf("Enter the no. of kilobytes: "); scanf("%lf", &kilobytes); // Conversion kilobytes to bytes bytes = kilobytes * 1024; // Displaying output printf("%.2lf Kilobytes = %d Bytes", kilobytes, bytes); return 0; }
Output
Enter the no. of kilobytes: 4
4.00 Kilobytes = 4096 Bytes
How Does This Program Work ?
int bytes; double kilobytes;
In this program, we have declared one int data type and one double data type variable which will store the values of bytes and kilobytes respectively.
// Asking for input printf("Enter the no. of kilobytes: "); scanf("%lf", &kilobytes);
Then, the user is asked to enter the value of kilobytes.
// Conversion kilobytes to bytes bytes = kilobytes * 1024;
We convert kilobytes into bytes by multiplying it by 1024.
// Displaying output printf("%.2lf Kilobytes = %d Bytes", kilobytes, bytes);
Finally, the result is printed using the printf() function. Here we have used %.2lf format specifier because we want to display the data only till 2 decimal places.
Conclusion
I hope after going through this post, you understand how to convert kilobytes to bytes using the C programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to solve your query.
Also Read: