Home »
Programming Tips & Tricks »
C - Tips & Tricks
How to separate 'words' while declaring an identifier in C language?
By: IncludeHelp, on 17 MAR 2017
Learn: how to declare an identifier name that has more than one word (multi-words variable/identifier declaration)?
When, you are declaring an identifier like variable name, function name etc and there are two words in it. You should separate them by using underscore (_) or mixed uppercase and lowercase alphabets.
For example: if there is a variable to store product weight, variable identifier/variable name should be: product_weight, productWeight, product_Weight
Consider the below example: here we are reading product name and product weight and then printing them (it's just a simple program that will read and print the value; you have to focus on variable naming conventions).
#include <stdio.h>
int main()
{
char product_name[100]={0};
float product_weight;
printf("Enter product name: ");
scanf("%[^\n]s",product_name);
printf("Enter product weight: ");
scanf("%f",&product_weight);
printf("Product details:\n");
printf("Name: %s\nWeight: %.02f\n",product_name,product_weight);
return 0;
}
Output
Enter product name: Lenovo B40 Laptop
Enter product weight: 2.1
Product details:
Name: Lenovo B40 Laptop
Weight: 2.10
Do you know why we used "% .02f" to print the float value?
%.02f - will print the float value till 2 decimal places with zero (0) padding.