Languages [TCS Placement]: Sample Questions 87 - 88 of 131

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 87

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:
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. intmain () {
  4.     charstr[30] ="123456 This is example";
  5.     char ⚹ptr;
  6.     longret;
  7.     ret =strtol(str, &ptr, 10);
  8.     printf("The number is %ld", ret);
  9.     printf("String part is |%s|", ptr);
  10.     return(0);
  11. }
  • Output:

The number is 123456

String part is|This is example |

Question 88

Edit

Describe in Detail

Essay▾

Given the bitwise operators in c

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:
  1. #include<stdio.h>
  2. intmain()
  3. {
  4.     unsignedcharx =7, y =12;
  5.     printf("x =%d, y =%d", x, y);
  6.     printf("x&y =%d", x&y);
  7.     printf("x|y =%d", x|y);
  8.     printf("x^y =%d", x^y);
  9.     printf("~x =%d", x =~x);
  10.     printf("y≪1 =%d", y1);
  11.     printf("y≫1 =%d", y1);
  12.     return0;
  13. }
  • Output:

x = 7 , y = 12

x&y = 4

x|y = 15

x ^ y = 11

~x = 248

Y << 1 = 24

y >> 1 = 6

Developed by: