8 Mar 2013

C Tutorial 2

                                              Tutorial-2

In this tutorial, we are going to learn about variables,different data-types and other related stuff.

What are data-types?
In C-programing, a data can be represented in different formats.
an example might be as follows, in mathematics, 12 and 12.0 are same, however in computer science, the memory required by 12 and 12.0 is different.
The number of bits required to store an integer is not the same as the number of bits stored by a floating point value(decimal point value).
So the question arises how to tell the compiler to allocate the memory for different types of data?
The answer to this is by telling the complier the data-type of the value or data which is the type of the data.
C language data types can be classified as
 

·         Primary data type
·         Derived data type
·         User-defined data type
Concept of : signed,unsigned,short and long.
This is pretty simple as signed data-type means that it will cover the positive and the negative values as well.
Unsigned data-type will cover only the positive values starting from 0.
short datatype will allocate either same or less than the main type of the data and definitely lesser memory as compared to long-datatype of the same.
short int <= int <= long int
float <= double <= long double




Type 
Size (Bits)   
Range 
Char or Signed Char
8
-128 to 127
Unsigned Char
8
0 to 255
Int or Signed Int
16
-32768 to 32767
Signed Int
16
0 to 65535
Short Int or Signed int
8
-128 to 127
Unsigned short int
8
0 to 255
Long int or signed long int
32
-2147483648 to 2147483647
Unsigned long int
32
0 to 4294967295
Float
32
3.4 e-38 to 3.4 e+38
Double
64
1.7e-308 to 1.7e+308
Long Double
80
3.4 e-4932 to 3.4 e+4932

The above values are for a 16-bit machine.

We shall discuss derived data-types and user-defined data-types in their dedicated tutorials.
As of know, knowing just the simple data types is more than enough.

What are
 variables?
Variables in computer science is something that symbolizes the known or unknown data.
Variable names are independent of the data-type of the variable.
Variables can be changed in the course of execution under proper conditions.
Variable names are nothing but temperory names given to some part of memory.

Now we are in a position to write our first program which involves usage of variables.
In this program, we are going to add two numbers and store them in a variable and display this variable.

Code:
/*Author : ks*/
#include
#include

int main(int argc,char*argv[])
{
    int number1=5;
    int number2=10;
   
    int answer=number1+number2;//Add the two numbers
    printf("The answer is %d\n",answer);
    system("PAUSE");
    return 0;
}
In the above code,number1,number2,answers are called as variables.
int number1=5, this tells the compiler that the variable number1 is going to contain an integer,so allocate memory required to store an integer.
same is with number 2 and answer.
Take a look at the statement 'printf("The answer is %d\n",answer);'
 
As stated in tutorial-1, printf() takes the format specifier, "%d" is used when we expect to print an integer value.

Here is a list of format specifiers:

%c
The character format specifier
%d
The integer format specifier.
%i
The integer format specifier (same as %d).
%f
The floating-point format specifier.
%e
The scientific notation format specifier.
%E
The scientific notation format specifier.
%g
Uses %f or %e, whichever result is shorter.
%G
Uses %f or %E, whichever result is shorter.
%o
The unsigned octal format specifier.
%s
The string format specifier.
%u
The unsigned integer format specifier.
%x
The unsigned hexadecimal format specifier.
%X
The unsigned hexadecimal format specifier.
%p
Displays the corresponding argument that is a pointer.
%n
Records the number of characters written so far.
%%
Outputs a percent sign. 

try it out yourself.

Next to format specifier, you shall find "\n". This is called as an escape sequence.
An escape sequence is combination of a backslash '\' and a letter. Its taken as one single character.

The following is a list of commonly used escape sequences.
 
\n
Newline 
\t
Horizontal Tab
\v
Vertical Tab 
\b
Backspace 
\r
Carriage Return 
\f
Form feed 
\a
Audible Alert (bell)
\\
Backslash
\?
Question mark
\’
Single quote 
\”
Double quote 



Exercise:
 
Write program to subtract,multiply and divide(this is slightly tricky, grasp the knowledge of data-types first).
Write program to find average of two numbers.
Write many simple programs till the time,either you are bored or you get proper hang of the syntax.;)
Play with different data-types and escape sequences.
****************************************************

No comments:

Post a Comment











































































































Support From Kuldeep Sheoran