Showing posts with label Variables in C Programming Language. Show all posts
Showing posts with label Variables in C Programming Language. Show all posts

Sunday, 24 December 2017

Constant in C/C++

Constant is a variables or values in C programming language which cannot be modified once they are defined. They are fixed values in a program. There can be any types of constants like integer, float, octal, hexadecimal, character constants etc.

Every constant has some range. The integers that is too big to fit into an int will be taken as a long. Now there are various ranges that differ from unsigned to signed bits. Under signed bit the range of an int, varies from -128 to +127 and under unsigned bit int varies from 0 to 255.

Defining Constants

In C program we can define constants in two ways as shown below:
  • Using #define preprocessor directive
  • Using a const keyword.
Let us now learn about above two ways in details:

Using #define preprocessor directive : This directive used to declare an alias name for existing variable or any value. We can use this to declare a constant as shown below:

#define VarName value

where Varname is the name given to constant and value is any value assigned to it.

Below is the C program to explain how to use #define to declare constants:

#include<stdio.h>
#define val 10
#define floatVal 4.5
#define charVal 'G'

int main()
{
printf("Integer Constant: %d\n",val);
printf("Floating point Constant: %f\n",floatVal);
printf("Character Constant: %c\n",charVal);

return 0;
}

 

Output:
 

Integer Constant: 10
Floating point Constant: 4.500000
Character Constant: G

 

Read Macros and Preprocessors directives in C.

 Using a const keyword: Using const keyword to define constants is as simple as defining variables, the difference is you will have to precede the definition with a const keyword. Below program shows how to use const to declare costants of different data types:

#include<stdio.h>
int main()
{
const int intVal = 10; // int constant
const float floatVal = 4.14; // Real constant
const char charVal = 'A'; // char constant
const char stringVal[10] = "ABC"; // string constant

printf("Integer constant :%d \n", intVal );
printf("Floating point constant : %f \n", floatVal );
printf("Character constant : %c \n", charVal );
printf("String constant : %s \n", stringVal);

return 0;
}

 

Output:
 

Integer constant :10
Floating point constant : 4.140000
Character constant : A
String constant : ABC

 

Learn about Const Qualifier in C.
 

String Literals : When string value is directly assigned to a pointer, in most of the compilers, it’s stored in a read only block (generally in data segment) that is shared among functions.
 

char *str = "GfG";
 

In the above line “GfG” is stored in a read only location, but pointer str is stored in a read-write memory. You can change str to point something else but cannot change value at present str. So this kind of string should only be used when we don’t want to modify string at a later stage in program.
 

The below program may crash (gives segmentation fault error) because the line *(str+1) = ‘n’ tries to write a read only memory.
 

int main()
{
char *str;

/* Stored in read only part of data segment */
str = "GfG";

/* Problem: trying to modify read only memory */
*(str+1) = 'n';
return 0;
}

Saturday, 23 December 2017

Variables in C


A variable in simple terms is a storage place which has some memory allocated to it. So basically a variable used to store some form of data. Different types of variables require different amounts of memory and have some specific set of operations which can be applied on them.

Variable Declaration:


A typical variable declaration is of the form:

data type variable_name;

for multiple variables declaration
data type vr1_name, var2_name, var3_name;

A variable name can consist of alphabets (both upper and lower case), numbers and the underscore ‘_’ character. However, the name must not start with a number.

Variable declaration and definition

Variable declaration refers to the part where a variable is first declared or introduced before its first use. Variable definition is the part where the variable is assigned a memory location and a value. Most of the times, variable declaration and definition are done together.

#include <stdio.h>
//extern int c;
int main()
{
char a = 'a'; // declaration and definition of variable 'a123'

float b;// This is also both declaration &definition/assigned garbage value.

printf("%c \n", a,c);

return 0;
}

 

Keyword extern is explained here.

Lvalues and Rvalues in C


There are two kinds of expressions in C:
 

Lvalue 

Expressions that refer to a memory location are called "lvalue" expressions. An lvalue may appear as either the left-hand or right-hand side of an assignment.
 

Rvalue 

The term rvalue refers to a data value that is stored at some address in memory. An rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right-hand side but not on the left-hand side of an assignment.
 

Variables are lvalues and so they may appear on the left-hand side of an assignment. Numeric literals are rvalues and so they may not be assigned and cannot appear on the left-hand side. Take a look at the following valid and invalid statements −
 

int g = 20; // valid statement

10 = 20; // invalid statement; would generate compile-time error


Geeks4Coding

Contributors

Popular Posts