Member • Aug 14, 2006
-
NehaWhat for << and >> operators are used in C?
-
Member • Aug 15, 2006
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 wayAre you sure? This action cannot be undone. -
Member • Aug 15, 2006
And yea one more thing these operator are used in C++ as are the commands cout & cinAre you sure? This action cannot be undone. -
Member • Aug 15, 2006
I agree they are used in C++ but they are used in C too and are better known as Bitwise Operators.Are you sure? This action cannot be undone. -
Member • Aug 15, 2006
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.nehaI agree they are used in C++ but they are used in C too and are better known as Bitwise Operators.
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.Are you sure? This action cannot be undone. -
Member • Aug 15, 2006
Okay! I got my answer.Thanx.😀Are you sure? This action cannot be undone. -
Member • Aug 15, 2006
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) 3Are you sure? This action cannot be undone. -
Member • Aug 16, 2006
answer to enum
hi dere !
the value of e is 7.
c =5
d = 6
e = 7.Are you sure? This action cannot be undone. -
Member • Aug 17, 2006
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 errorAre you sure? This action cannot be undone. -
Member • Aug 17, 2006
output 4 d above
hi dere !
u will get syntax error.
some error like compound statement missing in function main.Are you sure? This action cannot be undone. -
Member • Aug 18, 2006
Incorrect Answer.😔
The answer is somewhat else.
Any Explanation??Are you sure? This action cannot be undone. -
Member • Aug 18, 2006
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.Are you sure? This action cannot be undone. -
Member • Aug 18, 2006
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.Are you sure? This action cannot be undone. -
Member • Aug 18, 2006
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.Are you sure? This action cannot be undone. -
Member • Aug 18, 2006
Referringsravikhi 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.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.
CrooookAre you sure? This action cannot be undone. -
Member • Aug 25, 2006
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 1Are you sure? This action cannot be undone. -
Member • Aug 25, 2006
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.Are you sure? This action cannot be undone. -
Member • Nov 12, 2006
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);
}Are you sure? This action cannot be undone. -
Member • Nov 13, 2006
Hi aditi,
I think the solution is :
0
1
2
10
11
Is this correct?Are you sure? This action cannot be undone. -
Member • Nov 14, 2006
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!Are you sure? This action cannot be undone. -
Member • Nov 14, 2006
no.MaheshHi aditi,
I think the solution is :
0
1
2
10
11
Is this correct?
this is not the right answer.Are you sure? This action cannot be undone. -
Member • Nov 14, 2006
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."
😁Are you sure? This action cannot be undone. -
Member • Apr 30, 2009
Re: Hi!!!
your answer is exactly correct in c++ but not in c....Are you sure? This action cannot be undone. -
Member • May 1, 2009
Re: Hi!!!
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.seeya302004Output 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!
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.
-PradeepAre you sure? This action cannot be undone. -
Member • May 1, 2009
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' .
reachrkataIs the answer :
0
11
10
20
21
Any reason for the output in C++ to be different than that of C? I feel the output should be same.bharathkumarpyour answer is exactly correct in c++ but not in c....
-PradeepAre you sure? This action cannot be undone. -
Member • May 3, 2009
after compiling it in visual c++ 6 by (.c) extension
the result is:
0
10
11
20
21Are you sure? This action cannot be undone.