Tuesday, July 29, 2014

Storage classes in c

Generally we don't specify storage classes while declaring variables in c, but compiler assume a storage class of a variable based on where it is declared. So variables have default storage classes.
But wait  what is storage class???
Let me tell you storage class is something  by which compiler identifies some physical location within computer where variable will be stored, there two options for storing variable CPU register and Memory.
Storage class determine where to store variable.
There are 4 storage classes defined by c language:
               1. Automatic storage class
               2. Register storage class
               3. Static storage class
               4 . External storage class
These classes define where to store variable physically, it's initial value, scope.

External storage class:

Features:-
Storage-                             memory
Default initial value-            Zero
Scope-                              Global
Life-                                  As long as the program's execution

Global variable use external storage class. They persists during execution of program. Global variable can also shared among files in c. Ex
  1. /* File One.c*/
  2. #include<stdio.h>
  3. int i=9;

             
  1. /*File :  two.c*/
  2. #include<stdio.h>
  3. #include"prog1.c"
  4. extern int i;
  5. void main()
  6. {
     printf("i=%d",i);
  1.  }  

                Output: 9
When a variable is declared as extern it belong to external storage class and compiler do not allocate memory for it, assumes that it exists in some other file. If global variable in above program not declared as extern even now it will work because global variable are by default use external storage class.
                  

Automatic storage class:

Features:
Storage                                            Memory
Default initial value                           Garbage value
Scope                                             Local
Life                                                 Till the Control remains in the scope

      Ex:   auto int a;
       1.  auto int i=1;
2.  {
3.      auto int i=3;
4.      Printf(“%d”,i);
5.  }
 Note - variable i in line 3 is not same as line because both are in different scopes.


Monday, July 28, 2014

PRINTF MCQs


1. printf(1+”%d”);

     It starts printing the String Skipping the number of Characters specified before ‘+’ sign

2. Backslash Characters in C
  1. \n - Newline [ Moves Cursor Position to Next Line ]
  2. \b - Back Space [ Moves Cursor Position to 1 character Back ]
  3. \r - Linefeed [ Moves Cursor Position to First Position in Same Line ]
     Example:
     1.  #include<stdio.h>  
2.  void main()  
3.  {  
4.  printf("\nDiicket");  
5.  printf("\bter");  
6.  printf("\rCr");  
    }
    Outout  : Cricket  

3. Two Strings in Printf
      Ex : printf("Computer","Programming")
   1.    Only First String Will be Accepted
      2.    If number of Strings are Seperated by "Comma" then it will accept only
           first Stringf      
    
    4. %d format specifier
 printf("%d %d %d",50,050,0x50);Output:50 40 80

why?
  1. Any number preceded with Zero (040,050,030) are considered as Octal Numbers
  2. Any number preceded with [ 0x / 0X ] (0x40,0x50,0x30) are considered as Hexadecimal Numbers
5. Printf inside printf in C
    printf("%d",printf("%d",printf("%d",num)));  
 
 Output: 134241

Printf Returns total number of Digits 




Tuesday, April 1, 2014

Dictionary Encoding Data compression in python

#Dictionary Encoding
str=input("Enter text to be encoded ")
count=None
s1=str
lst=[]
def find(x,lst):
    for i in range(0,len(lst)):
        if x==lst[i]:
            
            return i
    
    return -1
lst=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
#print(lst)
s=s1[0]
s2=""
for i in range(0,len(s1)-1):
    c=s1[i+1]
    x=s+c
    #print("s=",s,"c=",c,"s+c=",x,"list=",lst)
    if find(x,lst)!=-1:
        s=s+c
        
    else:
        
        print(find(s,lst)+1)
        lst.append(s+c)
        s=c
print(find(s,lst)+1)
input()