Variable definition exercise
Exercise:
Write a C program to calculate and print distance from A to C.
Distance from A to B and B to C are given.
First of all, you have to identify the data here. Here we have 2 data. data1 is 160 Km, and data2 is 40Km.
These are the steps you have to follow in order to do this task.
- Create the variables to hold these data
- Compute the sum of the data
- Store the result
- And then print the result as shown in the format(Figure 2)
First, let’s create two variables inside the main function. So, we have to store data1 and data2. Here, to store these two data, you must first select the data type. We can use unsigned char here. Because these are unsigned data, and these are not big data. These are less than 255. That’s why we use unsigned char.
After that, let’s use a variable to store the data. Let me call it distanceA2B. This is my variable name. distanceA2B = 160; here. That is my first variable definition and initialization. So, here the first variable is defined and, after it is initialized. You can do both the steps in one single line. No problem.
You can do either like this(look at below code snippet) or do something like this.
unsigned char distanceA2B;
distanceA2B = 160;
Instead of using two lines, you can do it in a single line.
Here we assign the value 160 to the variable distanceA2B, which is of type unsigned char. So, when compiles this code, the compiler generates the code to reserve 1 byte in memory for the variable distanceA2B.
After that, let’s create one more variable unsigned char distanceB2C = 40;
Let’s create one more variable to store the result unsigned char distanceA2C. This is a variable to store the result.
How to calculate the result?
distanceA2C = distanceA2B + distanceB2C;
We have to print the value of the distanceA2C variable. Let’s use printf for that.
#include <stdio.h> int main() { unsigned char distanceA2B = 160; unsigned char distanceB2C = 40; unsigned char distanceA2C; distanceA2C = distanceA2B + distanceB2C; printf("Total distance from A2C = "); return 0; }
Distance project exercise
Printf(āTotal distance from A2C = ā); Here you have to use a format specifier.
Here, you have to inform the printf to which format to print the contents of this variable.
So, do you want to print the contents of the distanceA2C variable as an integer or in the form of a hex number, octal number, or character? Which format do you want? You have to specify that.
We specify that using a format specifier.
Format specifiers in C
If you use %d, then printf will understand that you want the result to be printed in an integer format.
If you mention %f, the variable content will be printed in the form of float.
%c for the Character, %s for string, %u for unsigned integer, and if you want to print a long int, you can use %ld, like that.
If you want a hex format, you can use %x. If you want to print octal format, you can use %o, like that.
In this exercise, you can use either %u because you are printing an unsigned integer. You can either use %u or use %d because we are printing in the integer format.
%d would be more suitable here because we are dealing with unsigned data
%d is used when you want to print signed integer (+ve or -ve)
Let’s use %d here. That’s a format specifier we want.
#include <stdio.h> int main() { unsigned char distanceA2B = 160; unsigned char distanceB2C = 40; unsigned char distanceA2C; distanceA2C = distanceA2B + distanceB2C; printf("Total distance from A2C = %d Km\n", distanceA2C); return 0; }
After that, you have to give the argument for this format specifier.
“Total distance from A2C” is our message; you have to close the message using a double quote. You can even give \n here to move the cursor to the following line.
Then you have to give the argument for this %d format specifier so that we give it after you close the message. After you close the message, give a comma and the argument distanceA2C. After that, let’s give a semicolon here. That’s how you use printf to print the value of a variable.
Here let’s print the distance in kilometers. So, after %d, I’ll give space and write km.
Let’s compile and let’s see how this goes.
You can give this %d format specifier anywhere you want inside the message. For example, I can even write something like this ā%d is the total distance from A2Cā. So, there is one format specifier %d, and there should be only one associated variable distanceA2C. Let’s see how this goes. Let’s run.
Now see the output(Figure 5); it is printing like 200 is the total distance from A2C.
Now, if you want to print the content of distanceA2C variable in the form of hex, then you have to change the format specifier. If you’re going to print in hex format, you have to give the format specifier as X, either small x or capital X. If you use small x, then you can see that it prints c8, as shown in Figure 6. c8 is a hex equivalent of decimal 200. So, it is printed in small letters.
If I make it as capital X, then you can see that it will get printed in capital letters( as shown in Figure 7).
If you print this variable in octal format, give the format specifier %o. It doesn’t take capital O actually, so you have to provide a small o. 310 is the octal format of 200 decimal. (Figure 8)
And if you want to print the distanceA2C variable in the form of character, you can give %c. Let’s run. It prints some unknown characters for the value 200( as shown in Figure 9).
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1