'C' related problems

What for << and >> operators are used in C?

Replies

  • morphusis
    morphusis
    Hi!!!

    the << & >> operators are used for input & output... with cout & cin

    cout<<"THis way u can display text on the screen";
    cin>>a; //u can get vlaue this way
  • morphusis
    morphusis
    And yea one more thing these operator are used in C++ as are the commands cout & cin
  • Neha
    Neha
    I agree they are used in C++ but they are used in C too and are better known as Bitwise Operators.
  • pradypop
    pradypop
    neha
    I agree they are used in C++ but they are used in C too and are better known as Bitwise Operators.
    That's true. Infact both in C and C++ >> and << are bitwise-shift operators. In C++ these operators are overloaded to achieve redirection aswell. In C we don't have the concept of overloading of operators. So, we have to live with printf and scanf.

    These are bitwise shift operators.

    The syntax is: var1=var>>count;

    The var is shifted count number of times towards right and the result is stored in var1. << operator shifts towards left. What's shifting?

    lets take an example to demonstrate the usage:

    unsigned char a=154,b; //'a' is 10011010 at bit-level
    b=a>>1; //we intend to shift 'a' right once and store it in 'b', i.e 01001101 is stored in 'b'

    b is equal to 77 in this case after operation. << operator shifts in other direction.
  • Neha
    Neha
    Okay! I got my answer.Thanx.๐Ÿ˜€
  • Neha
    Neha
    One more..

    enum number { a= -1, b= 4,c,d,e}
    What is the value of e ?

    (a) 7
    (b) 4
    (c) 5
    (d) 15
    (e) 3
  • sravik
    sravik
    answer to enum

    hi dere !

    the value of e is 7.

    c =5

    d = 6

    e = 7.
  • Neha
    Neha
    Next One...

    Output of the following program is

    main()
    {int i=0;
    for(i=0;i<20;i++)
    {switch(i)
    case 0:i+=5;
    case 1:i+=2;
    case 5:i+=5;
    default i+=4;
    break;}
    printf("%d,",i);
    }
    }

    a) 0,5,9,13,17
    b) 5,9,13,17
    c) 12,17,22
    d) 16,21
    e) Syntax error
  • sravik
    sravik
    output 4 d above

    hi dere !

    u will get syntax error.

    some error like compound statement missing in function main.
  • Neha
    Neha
    Incorrect Answer.๐Ÿ˜”

    The answer is somewhat else.

    Any Explanation??
  • sravik
    sravik
    Explanation ๐Ÿ˜€

    hi dere !

    dis program when compiled will localize u with errors. check out d reasons.

    1. after the switch there is not opening. so dis will localize "case outside error".

    2. by looking at the program u can simply say by looking at the compunds that is by matching the { and }.

    3. the above does not have { and } proper matches.

    so from dis we can judge tat this program when compiled localized erros.
  • Neha
    Neha
    well, I noticed the question in one of IBM papers and the answer to the question mentioned was 16,21...though I agree with your points as well..That's why I have put it in this thread.
  • sravik
    sravik
    No

    hi neha !

    just forget bout tat. bcoz i am damn sure it will not give d result. u will get couple of errors on any C compiler.
  • crook
    crook
    sravik
    hi neha !

    just forget bout tat. bcoz i am damn sure it will not give d result. u will get couple of errors on any C compiler.
    Referring
    CCS#3: Avoid slangs in posts. Also avoid placing dots in between words. Take your time to write โ€˜greatโ€™ instead of โ€˜gr8โ€™. Please use good english. It creates a good impression.


    The above post should be written as -

    Just forget about that. Because I am damn sure it will not give the result. You will get couple of errors on any C compiler.


    No offence meant.

    Crooook
  • Neha
    Neha
    Next One...

    What will the following program do?

    void main()
    {
    int i;
    char a[]="String";
    char *p="New Sring";
    char *Temp;
    Temp=a;
    a=malloc(strlen(p) + 1);
    strcpy(a,p); //Line number:9//
    p = malloc(strlen(Temp) + 1);
    strcpy(p,Temp);
    printf("(%s, %s)",a,p);
    free(p);
    free(a);
    } //Line number 15//

    a) Swap contents of p & a and print๐Ÿ˜”New string, string)
    b) Generate compilation error in line number 8
    c) Generate compilation error in line number 5
    d) Generate compilation error in line number 7
    e) Generate compilation error in line number 1
  • sravik
    sravik
    solution to the above ๐Ÿ˜€

    hi dere !

    the above program generates an error like Lvalue required in function main at line number 8. (option b)

    you cannot change the base address of an array.

    here 'a' is an array and we are trying to allocate another address returned by malloc function. but we cannot overwrite the base address of an array by another address.

    Happy Programming.
  • aditi
    aditi
    what will be the output of this program? please post answers with explanation.
    main()
    {
    int *a, *s, i;

    s = a = (int *) malloc( 4 * sizeof(int));

    for (i=0; i<4; i++) *(a+i) = i * 10;

    printf("%d\n", *s++);
    printf("%d\n", (*s)++);
    printf("%d\n", *s);
    printf("%d\n", *++s);
    printf("%d\n", ++*s);
    }
  • Mahesh
    Mahesh
    Hi aditi,
    I think the solution is :

    0
    1
    2
    10
    11

    Is this correct?
  • seeya302004
    seeya302004
    Output of the following program is

    main()
    {int i=0;
    for(i=0;i<20;i++)
    {switch(i)
    case 0:i+=5;
    case 1:i+=2;
    case 5:i+=5;
    default i+=4;
    break;}
    printf("%d,",i);
    }
    }

    a) 0,5,9,13,17
    b) 5,9,13,17
    c) 12,17,22
    d) 16,21
    e) Syntax error

    Sorry Sravik! But i dont agree wid u,

    Explanation: When the value of i=0, den it will certainly satisfy the check condition of for(i.e. i<20) so it will enter into the for loop den, since value of i=0, "case 0" statements will start executing.(i.e. i=i+5 or i=0+5 => i=5) now note dat there is no break statement after case 0 : i+=5;
    => no unconditional end of the switch (when break is encountered by the compiler it comes out of the immediate body(which is swich here) in which it is written n is "not mandatory" to write it after every case statement) now since no break is encountered the compiler will go on executing the follwing statements irrespectiv of the fact dat whether i value is satisfying the case condition or not
    =>at case 1 :i becoms 7,den no break,=>at case 2: i becums 12 (no break) =>at default it becums 16.den break is encountered it will come out of the switch-body n will print the value of i which is 16, den it will increament its value by 1(for progression (i++))=>i=17 now.When it enters switch none of the case value matches wid value of i =>directly default statement will get executed => i = 17+4=21, again we found break => out of switch again ,print the value 21 n den increament its value by 1 i.e. i=22, but now the for condition which is i<20 is not satisfied hence program will get terminated...I hope u got it!
  • aditi
    aditi
    Mahesh
    Hi aditi,
    I think the solution is :

    0
    1
    2
    10
    11

    Is this correct?
    no.

    this is not the right answer.
  • reachrkata
    reachrkata
    Is the answer :

    0
    11
    10
    20
    21

    :!: ๐Ÿ˜’

    "Arguing with your Boss is like wrestling with a pig in mud.
    After a while you realize that while you are getting dirty,
    the pig is actually enjoying it."

    ๐Ÿ˜
  • bharathkumarp
    bharathkumarp
    Re: Hi!!!

    your answer is exactly correct in c++ but not in c....
  • pradeep_agrawal
    pradeep_agrawal
    Re: Hi!!!

    seeya302004
    Output of the following program is

    Sorry Sravik! But i dont agree wid u,

    Explanation: When the value of i=0, den it will certainly satisfy the check condition of for(i.e. i<20) so it will enter into the for loop den, since value of i=0, "case 0" statements will start executing.(i.e. i=i+5 or i=0+5 => i=5) now note dat there is no break statement after case 0 : i+=5;
    => no unconditional end of the switch (when break is encountered by the compiler it comes out of the immediate body(which is swich here) in which it is written n is "not mandatory" to write it after every case statement) now since no break is encountered the compiler will go on executing the follwing statements irrespectiv of the fact dat whether i value is satisfying the case condition or not
    =>at case 1 :i becoms 7,den no break,=>at case 2: i becums 12 (no break) =>at default it becums 16.den break is encountered it will come out of the switch-body n will print the value of i which is 16, den it will increament its value by 1(for progression (i++))=>i=17 now.When it enters switch none of the case value matches wid value of i =>directly default statement will get executed => i = 17+4=21, again we found break => out of switch again ,print the value 21 n den increament its value by 1 i.e. i=22, but now the for condition which is i<20 is not satisfied hence program will get terminated...I hope u got it!
    seeya302004, Sravik is correct. The program has syntax error. And the details you have provided does not hold true till program have syntax errors because with syntax error a program will not compile successfully and generate a executable.

    Below changes will remove the syntax errors:
    1. "{switch(i)" should be changed to "{switch(i){"
    2. "default i+=4;" should be changed to "default: i+=4;"

    Compile and run the program with these changes and it will give output "16,21," as described by you.

    -Pradeep
  • pradeep_agrawal
    pradeep_agrawal
    I feel the output of the code

    main()
    {
    int *a, *s, i;
    
    s = a = (int *) malloc( 4 * sizeof(int));
    
    for (i=0; i<4; i++) *(a+i) = i * 10;
    
    printf("%d\n", *s++);
    printf("%d\n", (*s)++);
    printf("%d\n", *s);
    printf("%d\n", *++s);
    printf("%d\n", ++*s);
    }
    
    will be:

    0
    10
    11
    20
    21

    For reason read below my comments against each statement of the code:

    int *a, *s, i;
    Comment: Declaring variables


    s = a = (int *) malloc( 4 * sizeof(int));
    Comment: Allocating memory equivalent to integer array of size 4. Initializing 's' and 'a' with the base address of the allocated memory.


    for (i=0; i<4; i++) *(a+i) = i * 10;
    Comment: Initializing the elements of integer array represented by the allocated memory with 0, 10, 20, and 30 respectively, i.e.,
    a[0] = 0
    a[1] = 10
    a[2] = 20
    a[3] = 30
    or we can say that
    s[0] = 0
    s[1] = 10
    s[2] = 20
    s[3] = 30
    Because both 'a' and 's' are assigned same base address.


    Before understanding the below code note that in a statement the operators will be evaluated in sequence '()', '*', '++'.


    printf("%d\n", *s++);
    Comment: "*s++" will first take the value present at 's' (i.e., s[0]) and print the value, then increment 's' to point to next address (i.e., address of s[1]). So it will print '0' and 's' will point to s[1] and the array will look like:
    s[0] = 10
    s[1] = 20
    s[2] = 30


    printf("%d\n", (*s)++);
    Comment: "(*s)++" will first take the value present at 's' (i.e., s[0]) and print the value, then increment the value present at 's' (i.e., increment s[0]). So it will print '10' and s[0] will become '11' and the array will look like:
    s[0] = 11
    s[1] = 20
    s[2] = 30


    printf("%d\n", *s);
    Comment: "*s" will take the value present at 's' (i.e., s[0]) and print the value. So it will print '11' and the array will look like:
    s[0] = 11
    s[1] = 20
    s[2] = 30


    printf("%d\n", *++s);
    Comment: "*++s" will first increment 's' to point to next address (i.e., address of s[1]) and then print the value present at 's' (i.e., s[1]). So 's' will point to s[1] and print '20' and the array will look like:
    s[0] = 20
    s[1] = 30


    printf("%d\n", ++*s);
    Comment: "++*s" will take the value present at 's' (i.e., s[0]) and increment the value, then print the value. So it will increment s[0] to '21' and print '21' .



    reachrkata
    Is the answer :

    0
    11
    10
    20
    21

    bharathkumarp
    your answer is exactly correct in c++ but not in c....
    Any reason for the output in C++ to be different than that of C? I feel the output should be same.

    -Pradeep
  • sheeko
    sheeko
    after compiling it in visual c++ 6 by (.c) extension

    the result is:

    0
    10
    11
    20
    21

You are reading an archived discussion.

Related Posts

Analogies are part of almost every placement paper and GRE tests. So, why not to crack them at CE. Here's first: DRIP:GUSH as A. cry:laugh B.curl:roll C.stream:tributary D.dent:destroy E.bend:angle
hi all hi all! im Rajeshwari 4m hyd.im in my final yr.g8 to b here๐Ÿ˜ ๐Ÿ˜
Hello CEans! In order to keep our forum neat & clean, the CE-Common Sense Guide version 2.0 has been released. CCS Guide now gets a permanant place on our main...
Hello CEans! We have a new ranking system in place. Members with upto 50 posts will be called 'New CE Team Members' & those with > 50 posts will be...
Well this is probably a already-very-much discussed topic still I would like to know what the crazy engineers have to say about it. Terrorism - Something that is as prominent...