Basic C Programming – 2

constvar

Hello everyone …. how you guys doing?? I hope you guys are doing well. So, lets come straight to the point, in this post I am going to explain constants and variables in C language.

So, guys. every language has its own grammar, C language also has its own. this grammar consist of functions, keyword, variables and constants. constants and variables play very important role in C language because they are the very basic building blocks of any C program.

For the processing of information we need data. a piece of information is nothing but a collection of data and constants and variables are the base of any data in C.

C constants

Constant is an entity that does not change. In C there are different types of constants –

Integer constant

Real constant

Character constant

String constant

First we will discuss Integer constant. Integer constant is nothing but a sequence of digits. like 12, 149, 7765, etc. there are three types of integer constant in C namely decimal integer, octal integer, hexadecimal integer. octal and hexadecimal integers are not prevalent, we seldom use them.

decimal constant lies b/w 0-9, we can use – or + sign to represent negative or positive integer respectively. maximum value of an integer constant depends upon the machine on which you are working, means it may be different for 32-bit or 64-bit operating system. generally for 16-bit machine it lies b/w -32768 to +32767. for 32-bit machine it lies b/w -2147483648 to +2147483647. an integer constant must contain at least one digit. there should not be any blank space, comma or period b/w the digits.

e.g

int a = 23 ;

int amount = 4500 ;

int age = 28 ;

above are some valid examples of declaring an integer constant. in the first example ‘a’ is a variable which can hold only an integer constant, in this case 23. likewise ‘amount’ and ‘age’ are the variable which can hold only integer type of constant.

int a = 10,00 ; (wrong)

int b = 10 34 ; (wrong)

above are some wrong declarations as we have inserted comma and space b/w the digits. also, you should avoid using decimal point(real number notation) while declaring integer constant. the thing is, whenever you declare a variable of integer type it will only store an integer value, not the real value. it will avoid the digits after the decimal and will only store the value before the decimal.

e.g int amount = 4568.324

the compiler will not give any error message when you compile the program but the memory allocated for the variable ‘amount’ will only store 4568 in it.

Now, The Real constant..

In real world there are variety all over, there are things which may or may not be in fixed proportion like height, weight, temperature, speed. a train may run with the speed of 75 kmph or it may run with the speed of 81.75 kmph. that’s the thing, In C we can store the real value, this provide the flexibility in the representation of data, overcoming the limitations of integer constant.

e.g
float a = 23.678 ;

float salary = 6254.894 ;

float cost = 5089.289 ;

above are some examples of how we store real constant in C. in first example 23.678 will get stored in ‘a’. likewise 6254.894 and 5089.289 will get stored in ‘salary’ and ‘cost’ respectively. we can also write a floating point value (real value) in exponential form, C provides this functionality.

e.g

float a = 23.678 (conventional form)

float a = 0.23678e2 (exponential form)

here, e2 represents 10² (10 base to the power 2). this notation also known as scientific notation. the general form is –

mantissa e exponent

OR

mantissa E exponent

the digits before e/E are known as mantissa and that come after e/E are known as exponent.
C Character constants

In C, a character constant is a single character stored in a memory. C provides this functionality to store a single character in the memory. C also has the capability to store a string of character, known as ‘string’. we will discuss that in a later part of this blog series.

every character in C has a numeric value associated with it known as ASCII value. whenever we store any character, the ASCII value of it get stored in the memory. the representation of character constant is as follows –

e.g

char name = ‘B’

char ans = ‘a’

char post = ‘b’

above are some of the examples of representation of character constant in C. character constant should be enclosed within a pair of Sigle quote mark. note that in the above example there is a difference b/w the representation of ‘B’ and ‘b’. they are not the same. C is a case sensitive language, the ASCII value of ‘B’ is different from that of ‘b’. when we store ‘B’, the ASCII value of ‘B’ gets stored in the memory which is different form that of ‘b’. the ASCII value of ‘B’ is ’66’ and that of ‘b’ is ’98’. we use ‘char’ keyword to store the character constant in C.

Variables in C

A variable in C is nothing but a logical name given to the memory location to store data value. opposite to that of constant, its value may change at different point of execution.

e.g

int a = 23 ;

float b = 48.3778 ;

char name = ‘F’

in the above examples ‘a’, ‘b’ and ‘name’ are the variables. in first, ‘a’ is an integer type of variable which can store only integer value. in second, ‘b’ is a real/float type of variable which can hold only real values and in third, ‘name’ is a character type of variable which can hold only character constant. whenever we declare a variable, a separate memory segment is created in the computer’s memory according to the type of variable. we can use letters, numerical values and underscore to create a variable name. a variable name should not contain any special character except underscore. we can use both upper case or lower case letters, lower case letters are prevalent. a variable name can only be initiated with letters or underscore, also we should avoid using blank space while defining a variable.

e.g
Valid Declaration Invalid Declaration

float a = 39.568 ; float %a = 21.49 ;

int salary_month = 4 ; int 12ft = 24 ;

char ch = ‘G’ ; char c*h = ‘O’ ;

the characters limit to define a variable is compiler dependent although we should avoid using lengthy variable names as it may affect the readability of the program. Note : avoid defining variable names similar to that of keywords.

Now, we will take some small programs.

  1. in this program we are defining three different types of variables. also, we have stored values in it. on execution this program will print the values stored in each of the variables.
#include
#include
int main ()
{
int a = 10 ; \\ declaration of variable
float b = 34.25 ;
char ch = ‘G’ ;
printf(“value of a = %d \n”, a); \\ printing the values on the consol
printf(“value b = %f \n”, b);
printf(“ch holds = %c”, ch);
}
Output :
2019-01-09 (2)

2. In this program we have declared the variable and its type but the values will be put by keyboard. on every point of execution the variables may store different values.

#include
#include
#include
int main()
{
int a ;
float b ;
char ch ;
printf(“enter the value of a :”);
scanf(“%d”, &a);
printf(“enter the value of b :”);
scanf(“%f”, &b);
printf(“enter a character for ch :”);
scanf(” %c”, &ch);
printf(“\n \n”);
printf(“value of a = %d \n”, a);
printf(“value of b = %f \n”, b);
printf(“character in ch = %c \n”, ch);
}
Output:
2ndpro
I hope via this post, you guys would understand the concepts of constants and variables. for program execution I am using Code: Blocks IDE with MinGw compiler on my 64-bit windows 10 OS. In the next post I’ll explain Data types and its properties. till then..
This is Sachin, Signing off !!!

Leave a comment