Saturday, January 15, 2011

Variables, Datatypes and Constants



Variable:


      For a computer in order to compute something it all needs is some input, some instructions which tells it what to do with that data. The Input can be given in variety of ways. Although every input has to be remembered by the computer, in-order perform some operations on it. Memory devices are used to achieve this. You can specify some statements which instructs the computer to store some value in memory.

    Before we store some value in the memory, its necessary to first reserve some space in memory to store the value and then store the value.

   In C we have concept called variables. Variables is nothing but a container which can hold value. In fact actually the value is stored at some location in memory. Therefore whenever specific value is needed then, we refer to that location using the name of the variable.



   A sample code is given below to declare a variable,

                      int var1;

   Here, var1 is the variable declared of type int(if you don't get it don't worry, keyword int will be discussed later in next few topics)


   General format is : data_type variable name;



Note : You can declare multiple variables in a single statement, this can be done by separating variable names by a comma(,)


   Once the variable is declared as shown above one can store and perform some operations on it. There are few restriction on the variable name, which is discussed below. Also few data types are discussed below. If you are familiar with these then you can move further to my next post.




Variable Name:
  • First letter has to be a character or under score 
  • Its recommended not to begin with under score – Because its normally used by libraries. 
  • Next characters followed by first character can be a normal chars, under score or digits 
  • Note that C is case sensitive language, therefore extra care to be taken while coding 
  • Traditionally variable names are lower case 
  • constant names are usually upper case 
  • keywords are reserved words cant be used 
  • Meaningful names are recommended, useful while analyzing the code and debugging 
  • Usually shorter names are used for local variables(usually loop index), longer names are used for other variables(normal and external variables)




Data-types :   
  • C has only few data types 
  1. char  (single byte) 
  2. int
  3. float  (single precision floating point)
  4. long  (double precision floating point)
  • In addition to these data types, number of qualifiers are present, that can be applied to these basic data types.
Qualifiers :
  • Qualifier : short, long
  • Eg : short int counter;
             long int counter;
  • However keyword int can be omitted,.. I.e, keyword int is optional if these qualifiers are use
  • Intention of using short, long is to provide different length of int..
  • Often short 16 bits, long 32 bits,
  • int either 16 or 32 bits  
  • Compiler is free to choose the size,.. But with few restrictions  
  1. size of short and int are at least 16 bits
  2. size of long is at least 32 bits
  3. size of short is no longer than int,
  4. similarly int is no longer than long, i.e, short  < =  int  < =  long  
Note : Here size is nothing but the allocated memory when variable of that type is declared
  • Qualifier : signed, unsigned
  • This qualifier can be applied to char or any integer 
  • variable of type unsigned can accept values of either positive or zero
  • similarly signed variable can accept either of positive or zero or negative
  • I.e signed char : -128 to +127 (Size of char is 1 byte, that is 8 bits)
  • I.e unsigned char : 0 to 255 (Size of char is 1 byte, that is 8 bits)
  • char are signed or unsigned by default,it is machine dependent.



Thursday, January 13, 2011

Introduction - History




       C is a general purpose language programming language. This language is not tied to any operating system or machine, i mean to say that this language is independent of operating system or machine. Most important ideas of C language is taken from language called BCPL, developed by Martin Richards.

       I hope this much history is enough to proceed further in learning. If one is more interested on it they can refer here


Monday, January 10, 2011

C Very Easy


Hi all..!! this is a dedicated blog, having posts which give a very nice, short, easy to learn, and of-course complete tutorial on C


Well, this tutorial will be kept as simple as possible, so that everybody can understand, irrespective of their technical background and skills. But that doesn't mean that topics will be covered half the way and rest left off, in air..!! no no no,..! i will try to give all possible details on each topics.


You can find a index here where you can find links to topics covered so far..


To try out any C code you can use any compilers. You can find hundreds of them if you Google it. Of-course there are few best compilers most of them recommend including me, I recommend j Grasp, Code Blocks.


I hereby request one an all to please do a reply about what you felt about this blog, or any queries on any topics covered so far, or any suggestions, or anything,..