Neha
Neha
Branch Unspecified
03 May 2009

'C' related problems

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

morphusis

Branch Unspecified
12 years ago
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

Branch Unspecified
12 years ago
And yea one more thing these operator are used in C++ as are the commands cout & cin
Neha

Neha

Branch Unspecified
12 years ago
I agree they are used in C++ but they are used in C too and are better known as Bitwise Operators.
pradypop

pradypop

Branch Unspecified
12 years ago
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

Branch Unspecified
12 years ago
Okay! I got my answer.Thanx.😀
Neha

Neha

Branch Unspecified
12 years ago
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

Branch Unspecified
12 years ago
answer to enum

hi dere !

the value of e is 7.

c =5

d = 6

e = 7.
Neha

Neha

Branch Unspecified
12 years ago
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

Branch Unspecified
12 years ago
output 4 d above

hi dere !

u will get syntax error.

some error like compound statement missing in function main.
Neha

Neha

Branch Unspecified
12 years ago
Incorrect Answer.😔

The answer is somewhat else.

Any Explanation??
sravik

sravik

Branch Unspecified
12 years ago
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

Branch Unspecified
12 years ago
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

Branch Unspecified
12 years ago
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

Branch Unspecified
12 years ago
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

Branch Unspecified
12 years ago
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

Branch Unspecified
12 years ago
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

Branch Unspecified
12 years ago
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

Branch Unspecified
12 years ago
Hi aditi,
I think the solution is :

0
1
2
10
11

Is this correct?
seeya302004

seeya302004

Branch Unspecified
12 years ago
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

Branch Unspecified
12 years ago
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

Branch Unspecified
12 years ago
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

Branch Unspecified
9 years ago
Re: Hi!!!

your answer is exactly correct in c++ but not in c....
pradeep_agrawal

pradeep_agrawal

Branch Unspecified
9 years ago
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

Branch Unspecified
9 years ago
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

Branch Unspecified
9 years ago
after compiling it in visual c++ 6 by (.c) extension

the result is:

0
10
11
20
21

Share this content on your social channels -

Only logged in users can reply.