# Languages-C & C Plus Plus [TCS Placement]: Sample Questions 77 - 78 of 119

Get unlimited access to the best preparation resource for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

## Question 77

C & C Plus Plus

Edit

### Describe in Detail

Essay▾Which of the following standard C library converts a string to a long integer and reports any leftover numbers that could not be converted.

### Explanation

- strtol () converts a string to a long integer and reports any leftover numbers that could not be converted.
- function long int strtol (const char ⚹ str, char ⚹⚹ endptr, int base) converts the initial part of the string in str to a long int value according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.
- This function returns the converted integral number as a long int value, else zero value is returned.
- Example:

`#include <stdio.h>`

`#include <stdlib.h>`

`int main () {`

`char str[30] ="123456 This is example";`

`char ⚹ptr;`

`long ret;`

`ret =strtol(str, &ptr, 10);`

`printf("The number is %ld", ret);`

`printf("String part is |%s|", ptr);`

`return(0);`

`}`

- Output:

The number is 123456

String part is|This is example |

## Question 78

C & C Plus Plus

### Explanation

- In C, following 6 operators are bitwise
- & (bitwise AND) : Takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
- | (bitwise OR) : Takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1.
- ^ (bitwise XOR) : Takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.
- << (left shift) : Takes two numbers, left shifts the bits of the first operand determined by the second operand.
- >> (right shift) : Takes two numbers, right shifts the bits of the first operand as defined by second operand.
- ~ (bitwise NOT) : Takes one number and inverts all its bits
- Example:

`#include<stdio.h>`

`int main()`

`{`

`unsigned char x =7, y =12;`

`printf("x =%d, y =%d", x, y);`

`printf("x&y =%d", x&y);`

`printf("x|y =%d", x|y);`

`printf("x^y =%d", x^y);`

`printf("~x =%d", x =~x);`

`printf("y≪1 =%d", y≪1);`

`printf("y≫1 =%d", y≫1);`

`return 0;`

`}`

**Output**:

x = 7 , y = 12

x&y = 4

x|y = 15

x ^ y = 11

~x = 248

Y << 1 = 24

y >> 1 = 6