How to use Variables and Format Specifiers In C
Hello there! Gain with another lesson with the Express Learning programming C language tutorial. Here is the story about variables in the C language and how to use variables. First of all, welcome back to the brand-new lesson, and without any huge talk, let’s dive into the topic of Comments in C.
What is a variable?
The variable is a storage address or some memory location. It will allow us to store some known or unknown data with specific data types. That means variables can hold some value with a specific data type. We can use less code, and it will help to handle our program easily.
Related Article: How To Install GCC On Windows.
In the C first, we need to specify what the datatype of our variable is. And then we must give the identifier to our variable. The identifier is used to define a label or name for a variable. And then use the “=” symbol. Finally, on the right side, we can put a value. Look at the code given below.
data_type identifier = value
Now I’m going to create a simple integer-type variable and, I will show you how to read that value. here is the code.
#include <stdio.h>int main() { int num = 10; printf("%d", num); return 0; }
I’m only explaining things in the main function. If you don’t have any idea about that, please refer to our previous lesson (Let’s Create our First Program in C) before reading this.
How to Read Variables in C?
Look at the “printf();
” function. Something different in there. The first thing is “%d
“, It is called format specifiers.
Format Specifiers
format specifiers used to define what type of data is used in Standard input and Standard output using “printf();” & “scanf();” functions. Here are some basic format specifiers, See what datatype defines in “%d“, It was a Signed integer. Don’t think about that, I will explain that later.
Format Specifier | Type |
---|---|
%c | Character |
%d | Signed integer |
%e or %E | Scientific notation of floats |
%f | Float values |
%g or %G | Similar as %e or %E |
%hi | Signed integer (short) |
%hu | Unsigned Integer (short) |
%i | Unsigned integer |
%l or %ld or %li | Long |
%lf | Double |
%Lf | Long double |
%lu | Unsigned int or unsigned long |
%lli or %lld | Long long |
%llu | Unsigned long long |
%o | Octal representation |
%p | Pointer |
%s | String |
%u | Unsigned int |
%x or %X | Hexadecimal representation |
%n | Prints nothing |
%% | Prints % character |
Finally, after the”,
” symbol. Call to the variable using our variable name. let’s compile and run it you can see it will provide what is the value in the variable. In the next lesson, we are going to learn what are the data types we can use in GNU C Programming Language.