Programming in C: A Practical Approach Data Types, Variables and Constants
C is a:
High level programming language developed by Dennis Ritchie at the Bell Telephone Laboratories .
The selection of ‘C’ as a name of a programming language seems to be an odd choice but it was named C because it was evolved from earlier languages BCPL ( B asic C ombined P rogramming L anguage) and B .
Declaration statements in which more than one identifier is declared is known as shorthand declaration statement . e.g. int a=20, b=10;
The corresponding longhand declaration statements are:
e.g. int a=20; int b=10;
2. Can only be used to declare identifiers of the same type. e.g. int a=10, float b=2.3; is an invalid statement. 2. It can be used to declare identifiers of different types e.g. int a=10;float b=2.3; are valid statements.
An identifier is allocated some space in memory depending upon its data type and the working environment. This memory allocation gives rise to two important concepts known as L -value concept and R -value concept .
DATA OBJECT is a term that is used to specify the region of data storage that is used to hold values. Once an identifier is allocated memory space, it will be known as a data object.
L-value is a data object locator. It is an expression that locates an object. Variable is a sort of name given to the memory location (say2000). Variable here refers to L-value, an object locator. The term L-value can be further categorized as:
Modifiable L-value : A modifiable L-value is an expression that refers to an object that can be accessed and legally changed in the memory.
Non-modifiable L-value : A non-modifiable L-value refers to an object that can be accessed but cannot be changed in the memory.
---> L in L-value stands for “left” , this means that L-value could legally stand on the left side of assignment operator.
R in R-value stands for “right” or “read” , this means that if an identifier name appears on the right side of assignment operator it refers to R-value.
Consider the expression:
variable= variable +20
variable on the left side of assignment operator refers to L-value. variable on the right side of assignment operator (in bold) refers to R-value.
variable appearing on the right side refers to 20. 20 is added to 20 and the value of expression comes out to be 40 (R-value). This outcome (40) is assigned to variable on the left side of assignment operator, which signifies L-value.
Since, qualified constants are placed in the memory, they have l-value. But, as it is not possible to modify them, this means that they do not have modifiable l-value i.e. they have non-modifiable l-value. E.g.
int a=10; // It is possible to modify the value of a.
const int a=10;. // It is possible read the value placed within the
Integer literal constants are integer values like -1, 2, 8 etc.
The following are the rules for writing integer literal constants:
An integer literal constant must have at least one digit.
It should not have any decimal point.
It can be either positive or negative. If no sign precedes an integer literal constant, then it is assumed to be positive.
No special characters (even underscore) and blank spaces are allowed within an integer literal constant.
If an integer literal constant starts with 0, then it assumed to be in octal number system e.g. 023 is a valid integer literal constant, which means 23 in octal number system and is equivalent to 19 in decimal number system.
If an integer literal constant starts with 0x or 0X, then it is assumed to be in hexadecimal number system e.g. 0x23 or 0X23 is a valid integer literal constant, which means 23 in hexadecimal number system and is equivalent to 35 in decimal number system.
The size of integer literal constant can be modified by using a length modifier. The length modifier can be a suffix character l, L, u, U, f or F. If the integer literal constant is terminated with l or L then it is assumed to be long. If it is terminated with u or U then it is assumed to be an unsigned integer e.g. 23l is a long integer and 23u is an unsigned integer. The length modifier f or F can only be used with floating point literal constant and not with integer literal constant.
No special characters (even underscore) and blank spaces are allowed within a floating point literal constant.
A floating point literal constant by default is assumed to be of type double, e.g. the type of 23.45 is double.
The size of floating point literal constant can be modified by using the length modifier f or F i.e. if 23.45 is written as 23.45f or 23.45F, then it is considered to be of type float instead of double.
Following are valid floating point literal constants in fractional form:
-2.5, 12.523, 2.5f, 12,5F
The following are the rules for writing floating point literal constants in exponential form:
Non-printable character literal constants are represented with the help of escape sequences . An escape sequence consists of a backward slash (i.e. ) followed by a character and both enclosed within single quotes. An escape sequence is treated as a single character. It can be used in a string like any other printable character.
#include<stdio.h> is a preprocessor directive which includes standard input/output (i.e. stdio) header (.h) file. This file is to be included if standard input/output functions like printf or scanf are to be used in a program.
The following points must be remembered while writing preprocessor directives:
The preprocessor directive always starts with a pound symbol(i.e. #)
The pound symbol # should be the first non-white space character in a line.
The preprocessor directive is terminated with a newline character and not with a semicolon.
Preprocessor directives are executed before the compiler compiles the source code. These will change the source code usually to suit the operating environment (#pragma directive) or to add the code (#include directive) that will be required by the calls to library functions.