Adding 2 numbers  – real/floating point numbers


Exercise 1 : Modify your code from the previous example to use floating point numbers 5.1 and 6.1

  1. Teacher modifies variable declarations, but leaves prinf to output integer values.
  2. The erroneous result is observed and explained: the binary data stored at the variables are calculated and interpreted to show an integer result.  
  3. Teacher modifies the code of printf() to use %d and now the correct result is shown.

Exercise 2:

Observe what is output when %d is changed to each of the following, one at a time:

%d for int

%f for float

%lf for double

%c for char

%s for string

Video Demonstration

You can download the files here:


#include <stdio.h>
#include <stdlib.h>

int main()
   //declare and identify variables
   float a,b;
   float c;

   //Initialize the variables

   //Perform processing c <-- a+b

   printf("The sum of %.1f and %.1f is %.f \n", a,b,c);


© 2021  Vedesh Kungebeharry. All rights reserved. 

Leave a Reply

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

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

Facebook photo

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

Connecting to %s