Friday, March 18, 2016

Program to convert Fahrenheit temperature to Celsius temperature.

Hey guys in this post we are going to discuss about variables and arithmetic expressions in C. Before discussing further lets make a simple program which prints a table with conversion of Fahrenheit to Celsius. Let me show you the output first:

FahrenheitCelsius
0-17
20-6
404
6015
8026
10037
12048
14060
16071
18082
20093

Lets write the program:

#include<stdio.h> //As C does not provide the I/O functionality we use STDIO.H library

/*Print Fahrenheit-Celsius Table */

void main(){
   
  int fahr, celsius; // variable declaration
  int lower, upper, step;
  
  lower = 0; /*lower limit of temperature table*/
  upper = 200; /*upper limit*/
  step = 20;
  
  fahr = lower;
  

  while(fahr <= upper){
     celsius = 5 * (fahr - 32) / 9;
     printf("%d\t%d\n", fahr, celsius);
     fahr = fahr + step;
}


Lets discuss various parts of the program:

#include<stdio.h>
One of the interesting fact about C told by sir Dennis Ritchie is, this programming language does not provide any kind of facility to take input and output. So how do we accomplish these two tasks? The answer is we use a library to perform Input and Output operations in C i.e. STDIO.H. Basically it stands for "Standard Input Output". It is a header file with extension .h and included using #include directive.

void main(){}
The main function is the entry point of the C programs. So every program of C must consist main(). Functions in C must return a value and those functions which do not return any value must be defined or declared with void as we can see above main does not return any value so it is defined using void.

int fahr, celsius; // variable declaration
int lower, upper, step;
Variables in C must be declared before their use. Variables basically are containers which consist specific type of value which is decided at the time of declaration like above, we declared for and Celsius as an integer so they can hold only integer type values.

lower = 0; /*lower limit of temperature table*/
upper = 200; /*upper limit*/
step = 20;
Initialization of variables is one of the important aspect of every programming language. Every variable must of initialized, it makes your program more reliable. How? Well if you don't initialize variables, they automatically initialized with garbage values which increase the probability of unexpected errors in your program or your program may crash. The above statements also assignment statements we are assigning a value to a variable using '=' operator.

while(fahr <= upper){}
Its while loop. To print the table, we iterate using while loop until the value of fahr will become greater than upper. In the body of the while loop, we are increasing the value of fahr variable by 20 with the help of statement fahr = fahr + step.

celsius = 5 * (fahr - 32) / 9;
Okay the main logic falls here. The general formula of converting fahrenheit to celsius i.e. C = (5/9) (F-32) is converted into C expression. So that is how we are calculating the celsius from fahrenheit.

printf("%d\t%d\n", fahr, celsius);
This is just a simple statement to print the calculated values. The function printf exists in header file stdio.h which we are including at the top of the program so that it will be available in our program.

Output:
0-17
20-6
404
6015
8026
10037
12048
14060
16071
18082
20093

Observations and Corrections:

  1. The converted temperature is in integer format but it should be in decimal format like the converted value of 0 should be like  -17.8. The correction to this problem is declare fahr and celsius variable as float instead of  int i.e. float fahr, celsius;

Conclusion:
In this post, we learn about the following:

  1. Variables
  2. Expressions
  3. Functions
  4. Loop

Thanks guys for reading my blog post. Please let me know if I made any mistake in writing this blog post so that I can write better blogs.

C++ and Java, say, are presumably growing faster than plain C, but I bet C will still be around.  ~ Dennis Ritchie

Thursday, March 17, 2016

How to install C on ubuntu

Install C on Ubuntu.

Hi guys in this post I am gonna tell you how to install C compiler on Ubuntu.

Step 1: Update your OS.

sudo apt-get update

Step 2: In order to install C compiler we need to install build-essential package. This package consist tools to build packages for Ubuntu.

sudo apt-get install build-essential

Step 3: Check configurations to make sure that compiler is installed run the following commands.

gcc -v
make -v

Step 4: Testing installation by creating and compiling a simple program to print "Hello World!!".

mkdir ~/C && cd ~/C
touch hello.c && vim hello.c

To exit VIM editor type: press esc then write :wq hit enter

Step 5: Compile the program.

 cc hello.c -o hello
Above command will generate a file with name hello. You can see the files in the terminal with command 'ls'.


Step 6: Run the C program.

To run c program write: ./hello
OUTPUT: Hello World!!
UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity. ~Dennis Ritchie