Tuesday, 16 August 2016

Variable Declaration and Initialization

Variable Declaration

  • Declaration introduces one or more variables within a program. it reserves memory for the variable.
  • A declaration statement begins with the type, followed by the name of one or more variables.

The general form is-
data_type variablename1, variablename2, variablename3, ..., variablenameN;

  • Variables are declared a three basic places.
  1. When these are declared inside a function, they are called local variables.
  2. When the variables are declared in the definition of the function parameters, these are called formal parameters.
  3. When variables are declared outside all functions, the are called global variables.
for example-

Local Variable Declaration
Formal Parameter

Global Variable Declaration

  • Variables when used in expressions are also referred to as operands.

Initialization 

When a variable is declared, the C compiler doesn't assign any value to the variable, unless it is instructed to do so. Such declaration is called a tentative declaration. When such declaration is made garbage value(a random value) is stored in the variable.

for example-
  int i;      // This declaration is tentative
  int x;
  x=i+5;

Variable is not assigned any known value and contains a garbage value. Therefore the vlaue of x is not defined. This is a bug in the program.

To prevent such pitfalls, always assign a value to the variable during the declaration of the variables. This is known as Initialization.

for example-
  int i=10;
  int x;
  x=i+5;
In  this declaration the value of x is 15.

Saturday, 13 August 2016

Representation of a variable in memory

  • It may be mentioned that a computer provides a Random Access Memory (RAM) for storing the executable program code and the data the program manipulates. 
  • This memory can be thought of as a contiguous sequence of bits, each o which is capable storing a binary digit .
  • Typically, the memory is also divided into groups of eight consecutive bits (called bytes). These bytes are sequentially addressed. 
  For example :-
 
           int salary = 65000;
It causes the compiler to allocate a few bytes to represent salary. The binary conversion of 65000 is 1111110111101000. the compiler uses the addresses of the first byte at which salary is allocated to refer to it. As it is int  2 bytes of memory is allocated.

A two-byte integer whose address is 1215

Thursday, 11 August 2016

Data Types of C



Classification Of data types

  • C has five Basic data types -
  1. Character - keyword used is char.
  2. Integer - keyword used is int.
  3. Floating Point - keyword used is float.
  4. Double precision floating point - keyword used is double.
  5. Void - keyword used is void.
  • C has four type qualifiers also known as type modifiers. They precede the basic data types.
  1. short
  2. long
  3. signed
  4. unsigned
  • A type modifier alters the meaning of the base data type to yeild a new type. The following points should be noted.
  1. Each of these modifiers can be applied to the base type int .
  2. The modifiers signed and unsigned can also be applied to the base type char.
  3. In addition long can be applied to double.
  4. When the base type is omitted from a declaration, int is assumed.
  • A complete list of possible data types is mentioned below.
  1. bool
  2. char
  3. unsigned char
  4. signed char
  5. int
  6. unsigned int
  7. signed int
  8. short int
  9. unsigned short int
  10. long int
  11. unsigned long int
  12. signed long int
  13. float
  14. double
  15. long double

  • Then there are three types of Derived data types -
    1. Array
      • It is a collection of similar type of data (or data types) stored in contiguous memory location
    2. Function
      • A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions. You can divide up your code into separate functions.
    3. Pointer
      • A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type.

  • Then there are also three types of User-defined data types -
    1. Union
      • A union is a special data type available in C that allows to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple-purpose.
    2. Structure
      • A structure in the C programming language is a complex data type declaration that defines a physically grouped list of variables to be placed under one name in a block of memory, allowing the different variables to be accessed via a single pointer or variable of type struct. "Class" is c++ is an advanced version of structure.
    3. Enumeration
      • Enumerated Types allow us to create our own symbolic names for a list of related ideas. The keyword for an enumerated type is enum.


Monday, 8 August 2016

Tokens, Keywords, Identifiers and Variables

Tokens 
In a passage of text and usual words and punctuation marks are called Tokens. In a C program the smallest individual unit are also known as Tokens.




Keywords 
  1. Keywords have predefined uses and cannot be used for any other purpose in a C program.
  2. They are used by compiler to compile the program.
  3. They are always written in lowercase letters.
  4. There are Thirty two (32) keywords defined in C.

  Identifiers
  
An identifier is a sequence of characters created by the programmer to identify or name a specific object. The sequence of characters may be letters, digits, and the special character ('_' )known as an underscore.  They include variables, arrays,functions, pointers, etc.

Rules for creating Identifiers
  1. Identifiers are case sensitive.
  2. Only alphabets can be used as the first character of the identifier. 
  3. An identifier should not be a keyword  

Variables
  • A variable is an identifier for a memory location in which data can be stored and recalled. 
  • Variables are used for holding data values so that they can be utilized in various computations in a program.
  • All variables have two different attributes -
  1.  A data type is established when a variable is defined. Once defined, the type of C variable cannot be changed.
  2.  The value can be changed by assigning new value to the variable. The type of the values depends upon the data type of the variable.