Brush up your c basics part-1

                                               DEBUGGING

1.   main()
     {       int x = 10,y=2,z,ab=11;
             z=x/*y+y*/+y+a/*add ab also*/b;
             printf("%d\n",z); }
2.   main()
     {       int a[] = {0,1,2,3,4};
             int *p[] = {a,a+1,a+2,a+3,a+4}; int **pp = &p;
             printf("%d, %d, %d ", *pp-a, pp-p, **pp);
             pp++; pp++;;
             ++pp; *++pp;
             printf("%d, %d, %d ", pp-p, *pp-a, **pp); }
3.   register int i;
     int main()
     {       i=10;
             i++; i+=2;
             printf("%d\n",i); }
4.   int main()
     {       int i=puts("Hello"); printf("%d\n",i);
             i=(printf("sourabh"),printf("jain"));
             printf("%d\n",i);
             char *p="hello";
             printf("%s\n",*&*&p); }
5.   main()
     {       char as[] = "\\0\0"; int i = 0;
             char ch='a';
             do
             {        switch( as[i++])
                      {      case '\\' : printf("A"); break;
                             case 0 : printf("B"); break;
                            default : printf("C"); break; }
           } while(i<3);
           printf("%d\n",'a');
           switch(ch)
           {        case 97 : putchar('a'); break;
                    case 98 : putchar('b'); break; } }
6. int b;
   int fun(int x)
   {       static int a;
           if(a<b)
           {        a=a+x; x=a+b; }
           else
           {        b=b+x;
                    int a;
                    a=a+x; a=a+b; }
           b=a+b; a=b+a;
           a=a+x; a++;
           return a; }
   int main()
   { printf("%d %d\n",fun(5),fun(4)); }
7. int main()
   {       char *arr="\\0\1\8234\0"; int i=0;
           while(arr[i])
           {        switch(arr[i])
                    {
                            case '0': printf("is no"); break;
                            case '00': printf("is debugging\n"); break;
                            case 0: printf("It is Avishkar\n"); break;
                            case '\\': printf("This "); break;
                            case '\1': printf("t s"); break;
                            case '8': printf("o s"); break;
                            case '2': printf("imp"); break;
                            case '3': printf("le as"); break;
                            case 2: case 3: case 4:
                            case 8: printf("This "); break;
                            default: printf(" it seems\n"); break;}
                    i++; } }
8. int main()
   {       char arr[]="\\0\0\0834\213\xab\088\n";
           printf("%d %d\n",sizeof(arr),strlen(arr)); }
9. int main()
   {       int i=10;
           int *j=(int *)11;
           j+=i;
           printf("%d %d\n",i,j); }
10. int fun(int a,int *b,int **c)
    {       *b+=1; **c+=1;
            int z=**c;
            int y=*b;
            int x=a; return x+y+z; }
    int main()
    {       int a=5;
            int *b,**c;
            b=&a; c=&b;
            printf("%d\n",fun(a,b,c)); }
11. class Y
    {       int i;
            public:
            Y(int i):i(i){}
            void change_value()
            {        i++; }
            void get()const
            {        cout<<"Initial Value: "<<i<<endl;
                     change_value();
                     cout<<"After changing Value: "<<i<<endl; } };
    int main()
    {       Y y=2; y.get(); }
12. class Y
    {       static int j;
            int counter;
            public:
                     Y(int i):counter(i){}
                     void change_value()
                     {       j++; }
                     static void counter_show() {
                             counter++;
                             cout<<"Current Counter: "<<counter<<endl; }
                     void show_value()
                     { cout<<"Value is: "<<j<<endl; } };
    int Y::j=10;
    int main()
    {       Y y=2;           y.change_value();    }
13. class Rainbow {
            public:
                     Rainbow() { cout << "Rainbow()" << endl; }
                      ~Rainbow() { cout << "~Rainbow()" << endl; } };
    jmp_buf kansas;
    void oz() {
            Rainbow rb;
             for(int i = 0; i < 3; i++) cout << "there's no place like home\n";
             longjmp(kansas, 47); }
    int main() {
             if(setjmp(kansas) == 0) {
                      cout << "tornado, witch, munchkins...\n";
                      oz(); } else {
                      cout << "hello" <<endl; } }
14. class Except1 {};
    class Except2 {
             public:
                      Except2(Except1&) {} };
    void f() { throw Except1(); }
    int main() {
             try { f();
             } catch (Except2)
                      cout << "inside 2”<<endl;
             } catch (Except1)
                      cout << "inside 1”<<endl;} }
15. class Base {
             public:
                      virtual void what() {
                              cout << "Base" << endl; } };
    class Derived : public Base {
             public:
                      void what() {
                              cout << "Derived" << endl; } };
    void f() { throw Derived(); }
    int main() {
             try { f();
             } catch(Base b) {
                      b.what(); }
             try { f();
             } catch(Base& b) {
                      b.what(); }}
16. struct s
    {        int a:16; int b:35;
             int c:2; float f:21;};
    int main()
    {        cout<<sizeof(struct s)); }
17. class Y
    {        mutable int i; int j; static int counter;
             public:
                      Y(int i):i(i),j(i){}
                      static void counter_show()
                    {       counter++;
                            cout<<"Current Counter: "<<counter<<endl; }
                    void show_value()const
                    {       i++; j++;
                            cout<<"Value is: "<<j<<endl; } };
    int Y::counter=0;
    int main()
    {       Y y=2; y.show_value(); }
18. class Y;
    class X
    {       public:
                    operator Y()const{cout<<"In X"<<endl;} };
    class Y
    {
            public:
                    Y(const X &){cout<<"In Y"<<endl;} };
    void fun(Y y)
    {       cout<<"In fun"<<endl; }
    int main()
    {       X x;
            fun(x); }
19. class X;
    class Y
    {
            public:
                    Y(const X & x){cout<<"In Y"<<endl;}
    };
    class X
    {
            public:
                    operator Y()const{cout<<"In X"<<endl;}
    };
    void fun(Y y)
    {     cout<<"In fun"<<endl; }
    int main()
    {     X x;
         fun(x); }
20. int main()
    {
            enum debuggers{ravi,sandy,avi=0,sourabh};
            char str1[]="Hello";
            char str2[]="Hello";
            if(str1==str2)
                    printf("%d %d\n",ravi,sourabh);
            else
                     printf("%d %d\n",sandy,avi);
            return 0;
    }
21. This is a code to insert nodes in Binary Search Tree. Correct it.
    int insert(Btree **head,int data)
    {
            if(head==NULL) return 0;
            Btree *n,*t;
            n=( Btree *) malloc(sizeof(Btree));
            n->left=n->right=NULL;
            n->data=data;
            if(*head==NULL) {
                     *head=n;
                     return 1;
            }
            t=*head;
            do {
                     if(t->data<data)
                             t->right?(t=t->right):t->right=n;
                     else
                             t->left?(t=t->left):t->left=n;
            } while( t!=n );
            return 1;
    }
22.
1-> main()
    {       int i=3;
            printf("%d %d\n",i++,++i);
            return 0;}
2->
    main()
    {
            volatile int i=3;
            printf("%d %d\n",i++,++i);
            return 0;
    }
    What will be the output in the two cases given above and explain your answer.

Comments

  1. Hi Priyaranjan,Awesome job dude.Bythe please put some lights on Problem no 18 and 19. I am not getting it.

    Also in problem no 7 I m getting output as "This is not so simple as it seems".
    Here '\8' is an invalid escape sequence.Then why it is going to the case '8' and also at the last why '\0' is going to the case default??

    ReplyDelete
  2. Breaking down the string initialization we have:

    \\ == literal \ character
    \1 == octal representation of integer "1"
    \8 == technically invalid standard C, microsoft compilers will treat an
    unsupported (i.e. non-octal, non-escape, non-hex-specifier) character after
    a slash as if the slash were not present, so this is ascii '8'
    2 == ascii '2'
    3 == ascii '3'
    4 == ascii '4'
    \0 == literal zero

    arr[ 0 ] = '\\'; // literal backslash
    arr[ 1 ] = '0'; // ascii '0'
    arr[ 2 ] = 1; // integer 1
    arr[ 3 ] = '8'; // using microsoft (non-portable) syntax
    arr[ 4 ] = '2'; // ascii '2'
    arr[ 5 ] = '3'; // ascii '3'
    arr[ 6 ] = '4'; // ascii '4'
    arr[ 7 ] = 0; // integer 0

    The loop prints for each successive value of i:

    i == 0: "This "
    i == 1: "is no"
    i == 2: "t s"
    i == 3: "o s"
    i == 4: "imp"
    i == 5: "le as"
    i == 6: "it seems"

    hope it is clear now...

    ReplyDelete
  3. Thank you.
    Please put some lights on problem no 18 and 19.I am struggling there.

    ReplyDelete

Post a Comment

Popular posts from this blog