Wednesday, 8 January 2014

C Programming Language Tutorial – I (Data types, Identifiers, Variables, Keywords, Comments, Constant, Storage classes)

Data Types
C supports several types of data, each of which may be represented differently within the computer memory. The data type specifies data in computer memory.
C provides variety of data types.
  • Primitive or simple or inbuilt data types
- int, float, char, double
  • Derive data types
- Array, pointer
  • User define data types
- structure, typedof, enumeration
Data Type
Size(bytes)
Range
char
1
-128 to 128
int
2
-32767 to 32768
float
4
3.4e-38 to 3.4e+38
double
8
1.7e-308 to 1.7+308
Identifiers
Identifiers are the name given to various programming elements by a programmer. It is fundamental requirement of programming language.
Keywords
Keywords are the reserved identifiers and have predefined meaning to the compiler. They are always written in lower case. They cannot be use as name for user define identifier. C has 32 keywords.
auto
case
const
default
double
enum
float
goto
int
register
short
sizeof
struct
typedef
unsigned
volatile
break
char
continue
do
else
extern
for
if
long
return
signed
static
switch
union
void
while
Comments
Comments are used to enhance the readability and understanding of a program.
These are not executable statements and they are ignored by the compiler at the time of compilation.
Single line comments – (//)
Multi lines comments – (/* -----*/)
Constants
Constants are those whose value is fixed throughout the execution of program.
Each constant has a type, determined by its form and value.
·         Integer Constants – contains sequence of digit.
·         Character constants – encloses in a single quotation mark.
·         Floating constants – consists of integer part and a decimal point. It can be suffixed by f.
String Literals
It is a sequence of character surrounded by double quotation marks.
Escape Sequences
C supports some special backslash character constants that are used in output functions. These character constants are known as escape sequence characters.
Character
Escape Sequence
New line
\n
Horizontal tab
\t
Vertical tab
\v
Backspace
\b
Carriage return
\r
Form feed
\f
Backslash
\\
Single quote
\’
Double quote
\”
Question mark
\?
Bell (alarm / beep)
\a
Symbolic Constant
The constant values that have been given a name are called symbolic constants. In C you can create symbolic constants using #define. The subsequent use of symbolic constants within the program will automatically replace the specified value before the compilation of program.
Variables
Variables are named locations in memory that are used to hold a value that may be modified by the program. Every variable has two parts, a name and a data type.
Declaration of variable
Data-type varable;
int age;
char sname;
float avg;
Rules for declaration
-   Name of the variable should be start from alphabet.
-   Space is not allowed within the variable name.
-   Space is compulsory in between data type & variable name.
-   Only underscore character is allowed in variable name.
-   Variables are declared under a single data type are separated by comma.
-   Semicolon is used at the end of variable declaration.
Initializing a variable
A process of assigning value to the variable for the first time after its declaration is known as initialization.
    int no=10;
Scope of variable
·   Block scope – A variable is visible only with in the block in which it has been declared.
·   Function scope – A variable is visible to the entire function in which it has been declared.
·   File scope – A variable is visible to all the functions within a file.
Storage Class
Variables in C not only have data type, but also storage class that provides information about their location and visibility. A storage class determines the portion of a program within which the variables are accessible. C provides variety of storage classes.
·   auto – It declares an automatic variable that is visible only in the block in which it is declared.
·   static – It initialized variable to 0 unless another value is specified. A variable retains previous value between calls to the functions.
·   extern – It is accessible throughout the program. It is allocated when the program begins and deallocated when the program ends.
·   register – the register keyword specifies that the variable is to be stored in a machine register, if possible.

The volatile keyword is also use for declaration of variable which tell to compiler that the variable value is sudden change at any time.

No comments:

Post a Comment