Tuesday, 31 March 2020

Session 2a Data Types in C

c data types

C data types - Storage format for  a variable to store data to perform a specific operation.

Basic Data type in C language

Primitive Data Type: Inbuilt data type provided by C language
    - int, float, double, char and void

Non primitive data type : These are derived from the primary data type

     - arrays  , pointers

User Defined Data types
      - Structure, Union, Enumeration

Other way of classifying

Primary
    Character – char, signed char , unsigned char

    Integer
        signed – int, short int ,long int
        unsigned – int, short int, long int
    Float  - float , double , long doulbe
      
    Void

Derived
    Array , pointer, structure, union enum






Basic/Primary Type/ primitive

int, char, float, double
Enumerated Type

Enum

Void Type

void

Derived Type

pointer, array, structure, union


1) 

a)Integer

Integer data type is used to store numeric values in a variable.
int is the keyword used to refer integer data type.
The storage size of int data type is 2 , 4 or 8 bytes

This depends on the upon the processor in the CPU that we use. For a 16 bit processor, 2 bytes (16 bit) of memory will be allocated for int data type.
For 32 bit processor 4 bytes or 32 bits and for 64 bit processor 8 byte or 64 bits are allocated for int datatype.

Signed and Unsigned Variables

Signed variables, such as signed integers is used represent numbers both in the positive and negative ranges.

Unsigned variables, such as unsigned integers, is used to represent numbers in the positive
Two's complement Method 
Two's complement is the most common method of representing signed integers in computers

In two’s complement form, a negative number is the 2’s complement of its positive number

How to get 2's complement of a number

Assume we are using 8 bit form and want to represent  -28 in binary form.

a) 28 in binary form is : 00011100

b) Then we invert the digits. 0 becomes 1, 1 becomes 0.: 11100011

c) Then we add 1. 11100100
That is how one would write -28 in 8 bit binary.

For a 8 bit signed integer

127 is represented as 01111111
-127 is represented as 2’s complement of positive 127, ie 10000001

Get the Max and min values of signed and unsigned

Unsigned data types:

int max = pow(2, number of bits assigned to data types) — 1;

Signed data types:

int min = ( pow(2, number of bits assigned to data types) / 2) * -1;
int max = (pow(2, number of bits assigned to data types) / 2) - 1;

For 16 bit

Unsigned – > 2 power 16 - 1 = 65536 – 1 = 65535

Signed

int min = (2 power 16 ) /2 * (-1) = 65536/2 *(-1) = -32768

int max = ( 2 power 16 / 2) -1 = 32767




Integer Type 

Consider a 16 bit machine


Type Size
(Bytes)
Range


int or signed int
2


-32,768 to 32767
unsigned int

2

 0 to 65535

short int or signed short int
1


-128 to 127

unsigned short int
1


0 to 255
long int or signed long int 4


-2,147,483,648 to 2,147,483,647
unsigned long int 4


0 to 4,294,967,295

Consider a 32 bit machine


Type

Size(Bytes  Range

int

4



short int

2



long int

8






Refer: testint.c

b) Char


It stores a single character and requires a single byte of memory in almost all compilers.

Char is a data type which is  used in C programming for storing characters like letters and punctuation marks. However, it still remains to be an integer type. This is due to the reason that char type technically stores integers and not characters.

It makes use of a numerical code which represents characters by using integers. For example – ASCII code which is one of the most commonly used codes for carrying out such interpretations.

The transformation of char into int values is done automatically by C. However, it is still dependent on the machine which decides that the result would be negative or not. The upper case A is equivalent to integer value of 65.

The terms unsigned and signed used with char means that if the content of these eight bits is interpreted into an integer then these terms can make some difference. Character data type is usually of type unsigned by default. However, In C++ and ANSI C mode, there is an option to explicitly declare them as signed or unsigned char.

In unsigned char 8 bits are used as data bits, whereas in memory representation of signed char 1 bit (most significant bit) is used for signed bit and 7 bits are used as data bits. If the signed bit is 0 it means that number is positive. If signed bit is 1 then number is negative.

 
Type Storage Size Value Range
Char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 -255
Signed char 1 byte -128-127

The char data type can be either signed or unsigned. It depends on the compiler and platform.

While the character data type is commonly used to represent a character (and that's where it gets its name) it is also used when a very small amount of space, typically one byte, is needed to store a number. A signed char can store a number from -128 to 127, and an unsigned char can store a number from 0 to 255. When used to represent a character, however,  that signed or unsigned is irrelevant

Refer : testchar.c

c) float and double

float and double are two different data types in C for storing real numbers. double occupies twice the memory occupied by float

The size of float (single precision float data type) is 4 bytes. And the size of  double (double precision float data type) is 8 bytes.

 float: It is used to store decimal numbers (numbers with floating point value) with single precision.( Occupies 32 bits of memory.)


double: It is used to store decimal numbers (numbers with floating point value) with double precision. ( Occupies 64bits of memory )

2) Enumerated Data type

This is a user defined data type in C.

It can have only one value

3) void

Specifies that no value is available . It is used in the following cases

1. When a function does not return any value void test_fun(int input_val)

2. When a function does not take any input argument  int test_fun(void)

3. pointer to void

A void pointer is one which does not have any data type associated with , what is means is that  it can be assigned a value of any type.

It is capable of storing address of any data type. 

     void *ptr;
    int intval; char charval;

    ptr = &intval; //ptr changes to integer pointer as address of integer is   assigned to it

    ptr = &charval; //ptr changes to character pointer as address of character is assigned to it

Note: Null pointer is different from void pointer

A null pointer is one which is not pointing to anything, i.e. it is assigned a null value.
If there is no address to assign to a pointer, it is better to set it to null.

Syntax: <data type> *<variable name> = NULL;
Example: int *ptr = NULL;
          char *ptr = '\0';

ref: testvoidptr.c


Derived data types: Pointer, array, structure, 
We will see in further sessions 



      


Friday, 27 March 2020

Session 1 compilation , installation, structure of C program

C Compilation

The C source code needs to be "compiled", into machine language so that the CPU can actually execute the program as per the instructions given.  The compiler compiles the source codes into final executable programs. The most frequently used and free available compiler is the GNU C.

Installation on Linux

If you are using Linux or UNIX, then check whether GCC is installed on your system by issuing the following command

$ gcc -v

If gnu compiler is installed , then you will get the following message:
$ Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu

If gcc is not installed, install it from

https://gcc.gnu.org/install/



Structure of C Program

a) Comments Section

b) Pre processor Statements

      Header file Inclusion,  Macro Statements

c) global declaration section

d) main() function where the program execution begins

    The execution of the program begins at the opening brace '{' and ends       with  } the closing brace
      This has the local declaration of variables  used in the program
      The body of the program that has the Program Statements and Expressions

e) Return statement
     This refers to the return of the values from the function. The return vale depends on the return type of the function. A void function does not have a return statement

First C Program -> Hello.c

/* Author: s.Kalpalatha
Writes the words "Hai Hello, World!" on the screen
compile:  gcc hello.c -o hello
Run : ./hello
*/

#include<stdio.h>

int main()
{
    printf("Hai Hello, World!\n");
    return 0;
}
~ 









Saturday, 7 March 2020

Session 1a Steps involved during C compilation


About me

I am a technology enthusiast. I like programming and continuous learning. I also like to teach what I have learnt from  my academic program , my rich experience in the Industry and my research experience at Indian Institute of Technology , Chennai.

I like to listen to Carnatic Music. I  like to play the divine Musical Instrument , Veena which I had learnt during my school.

My primary interests are in the following areas:

1. C Programming
2. Data Structures and algorithms
3. Python Programming
4. Computer Networks
5. Java Programming
6. Wireless Lan Networks
7  Network Programming
8  Database
9  Operating Systems
10 Vi editor


My Dreams

1. Want to be a part of an organization that focuses of Network and Network Related products.

2. Contribute significantly to the organization and grow with the company.

3. Consolidate all my learnings and create materials and teach the student community

4. Continuously sharpen my skills and be a good developer always

5. Continue to learn Veena and eventually start teaching