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.

Wednesday, 27 July 2016

A Simple C Program

Lets write a basic C program.

In order to write up your first C program, You have to download IDEs to compile and run your programs. There are various C programs available on the internet. I'm currently using Turbo c++.
Here's a link
So, lets get started.


Compiling and Running C Programs

Compiling C program requires working with four kinds of files ---


  • Regular Source Code Files - These files contain function definitions and have an extension '.C '.
  • Header Files - These files contain declarations (also known as function prototypes) and various pre-processor statements. They are used to allow source code files to access externally defined functions. Header files have '.h ' extension.
  • Object Files - These files are produced as the output of the compiler. They consist of various function definitions in binary form, but they are not executable by themselves.object files have '.obj ' extension.
  • Binary Executable - These are produced as the output of a program called  'Linker'. The linker links together a number of object files to produce a binary file that can be directly executed. They have no suffix in UNIX but have '.exe ' on Windows.

Monday, 25 July 2016

Learning Red hat - A Linux OS

An operating system is a program that manages the computer hardware. It also provides a basis for application programs and acts as an intermediate between the user and the computer hardware.


A computer system can be divided roughly into four components:
  • The Hardware
  • The Operating System
  • The Application Programs
  • The Users
The Hardware - provides the basic computing resources with the help of the Central Processing Unit (CPU), the memory, the input/output devices.
The Application Programs -  They define the ways in which these resources are used to solve user's computing problems. For instance, word processors, spreadsheets, compilers, web browsers, etc.
The Operating system is similar to a government. Like a government, it performs no useful function by itself. It simply provides an environment within which user or other programs can do useful work.


Most of us are acquainted with the Windows operating system, provided by Microsoft. Aside this, there are other operating systems like Linux, Unix, Solaris, etc.
Each OS(operating system) is different from another as they use different scheduling algorithms, solve their critical section problems differently, that is, they perform the tasks in different ways.




So, lets start understanding Linux operating system. In the next posts on this topic we will be using Red Hat OS (v 7.1).


In 1969 three software engineers,
  • Dennis Ritchie
  • Richard Stallman
  • Ken Thompson
developed an operating system named 'UNIX'.




UNIX was based on C programming language. It was not open source but hardware dependent like HP to HP, or IBM to IBM, etc. It was set up in Bell Labs at AT&T (American Telecom and Telegraph) Company.




In  1991, Linus Torvalds, a student of Helsinki University developed "MINIX" kernel and developed a new OS known as LINUX.




LINUX is an open source OS. It is not hardware dependent.