Variables & Constants in C are formed by combining alphabets, numbers, and special symbols. Let us see what are ‘constants’ and ‘variables’ in C.
The syntax for declaring a variable:
dataType variable_name;
Example:
int age;
float sum;
Here, age and sum are variables whereas, int and float are data types in C.
Constants | Examples |
Integer constants | 426,+78,-2900 |
Real or Floating-point constants | +34.09,-98.345,88.456 |
Character constant | ‘a’, ‘5’, ‘=’, ‘*’ |
Octal Constant | 021, 033, 046 |
String Constant | “c”, “c program”, “c in javatpoint” |
Hexadecimal Constant | 0x2a, 0x7b, 0xaa |
There are two ways to define constant in C programming.
const keyword:
#include<stdio.h>
int main(){
const float PI=3.14;
printf("The value of PI is: %f",PI);
return 0;
}
output:
The value of PI is: 3.14
here,
const float PI=3.14
as a result, the value of variable PI is set to 3.14 which cannot be changed during program execution.
#define preprocessor: The #define preprocessor is used to define constant. We will know more about the #define preprocessor directive later on.
#include<stdio.h>
#define PI 3.14
int main(){
float area=PI*5*5;
printf("The Area of circle is: %f",area);
return 0;
}
output:
The Area of circle is: 78.5
note: also read about Identifiers in C & Keywords in C.
next: Data Types in C
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…
Problem Statement (Asked By Airbnb) Given a list of integers, write a function to compute…