K & R C Chapter 2 Exercise Solutions

We have already provided solutions to all the exercises in the bookC Programming Language (2nd Edition) popularly known as K & R C book.

In this blog post I will give links to all the exercises from Chapter 2 of the book for easy reference.

Chapter 2: Types, Operators and Expressions

  1. Exercise 2-1.Write a program to determine the ranges of char , short , int , and long variables, both signed and unsigned , by printing appropriate values from standard headers and by direct computation. Harder if you compute them: determine the ranges of the various floating-point types.
    Solution to Exercise 2-1.
  2. Exercise 2-2.Write a loop equivalent to the for loop above without using && or || .
    Solution to Exercise 2-2.
  3. Exercise 2-3.Write the function htoi(s) , which converts a string of hexadecimal digits (including an optional 0x or 0X) into its equivalent integer value. The allowable digits are 0 through 9, a through f, and A through F .
    Solution to Exercise 2-3.
  4. Exercise 2-4.Write an alternate version of squeeze(s1,s2) that deletes each character in the string s1 that matches any character in the string s2 .
    Solution to Exercise 2-4.
  5. Exercise 2-5.Write the function any(s1,s2) , which returns the first location in the string s1 where any character from the string s2 occurs, or -1 if s1 contains no characters from s2 . (The standard library function strpbrk does the same job but returns a pointer to the location.)
    Solution to Exercise 2-5.
  6. Exercise 2-6.Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.
    Solution to Exercise 2-6.
  7. Exercise 2-7.Write a function invert(x,p,n) that returns x with the n bits that begin at position p inverted (i.e., 1 changed into 0 and vice versa), leaving the others unchanged.
    Solution to Exercise 2-7.
  8. Exercise 2-8.Write a function rightrot(x,n) that returns the value of the integer x rotated to the right by n bit positions.
    Solution to Exercise 2-8.
  9. Exercise 2-9.In a two’s complement number system, x &= (x-1) deletes the rightmost 1-bit in x . Explain why. Use this observation to write a faster version of bitcount .
    Solution to Exercise 2-9.
  10. Exercise 2-10.Rewrite the function lower, which converts upper case letters to lower case, with a conditional expression instead of if-else .
    Solution to Exercise 2-10.
You can purchase the book from here or here.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 2-9.

K and R C, Solution to Exercise 2-9:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition)You can learn and solve K&R C Programs Exercise.
In C two complement number system, x &= (x-1) deletes the rightmost one bit in x.
We take (x-1) and add 1 to it to produce x. The rightmost 0-bit of x-1 changes to 1 in the result x. Therefore, the rightmost 1-bit of x has a corresponding 0-bit in x-1. This is why x & (x-1), in a two’s complement number system, will delete the rightmost 1-bit in x. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/
int bitcount(unsigned x)
{
int b;
for(b = 0;x != 0;x &= x-1)
++b;
return b;
}
Read more Similar C Programs
C Basic

K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 2-8.

K and R C, Solution to Exercise 2-8:
 K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition) You can learn and solve K&R C Programs Exercise.
C Program that returns the value of the integer x rotated to the right by n bit positions. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

unsigned rightroot(unsigned x, int n)
{
int wordlength(void);
int rbit; //rightmost bit

while(n-->0){
rbit=(x & 1)<<(wordlength()-1);
x=x>>1;
x=x|rbit;
}
return x;
//word length computes the wor lengt of machine
int wordlength()
{
int i;
unsigned v = (unsigned~0;
for(i = 1;(v = v >> 1) > 0;i++)
;
return i;
}
//main function to test the program,, you can try in different ways!
#include <stdio.h>

int main(void)
{
unsigned x;
int n;

for(x = 0; x < 200; x += 25)
for(n = 1; n < 8; n++)
printf("%u, %d: %un", x, n, rightrot(x, n));
return 0;
}

Read more Similar C Programs
C Basic

K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 2-7.

K and R C, Solution to Exercise 2-7:
 K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition) You can learn and solve K&R C Programs Exercise.
C function invert(x, p, n) that returns x with the n bits that begin at position p inverted(i.e, 1 changed into 0 and vice versa), leaving the others unchanged.Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

unsigned invert(unsigned x, int p, int n)
{
return x ^ (~(~0U << n) << p);
}

/* Program for testing */

#include <stdio.h>

int main(void)
{
unsigned x;
int p, n;

for(x = 0; x < 500; x += 49)
for(n = 1; n < 8; n++)
for(p = 1; p < 8; p++)
printf("%u, %d, %d: %un", x, n, p, invert(x, n, p));
return 0;
}

Read more Similar C Programs
C Basic

K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 2-3.

K and R C, Solution to Exercise 2-3:
C Function htoi() which converts a string of hexa decimal digits into its equivalent integer value. K and R C Program. Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#define YES 1
#define NO 0
//HTOI: CONVERT HEXADECIMAL STRING S TO INTEGER
int htoi(char s[])
{
int hexdigit, i, inhex, n,i=0;
if(s[i]=='0'){
++i;
if(s[i]=='x' || s[i]=='x')
++i;
}
n=0;
inhex=YES;
for(;inhex==YES;++i){
if(s[i]>='0' && s[i]<='9')
hexdigit=s[i]-'0';
else if(s[i]>='a' && s[i]<='f')
hexdigit=s[i]-'a'+10;
else if (s[i]>='A' && s[i]<='F')
hexdigit=s[i]-'A'+10;
else
inhex=NO;
if(inhex==YES)
n=16*n+hexdigit;
}
return n;
}


Read more Similar C Programs
C Basic

K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 2-2.

K and R C, Solution to Exercise 2-2:
C Program to demonstrate for and while loops. K and R C Program. Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

/*Write the equivalent to the below for loop without using && or ||.*/

// Original

for(i=0; i<lim-1 && (c=getchar()) != 'n' && c != EOF; ++i)
{
s[i] = c;
}
// Equivalent:


while (i < (lim - 1))
{
c = getchar();

if (c == EOF)
break;
else if (c == 'n')
break;

s[i++] = c;
}

Read more Similar C Programs
C Basic

K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 2-1.

K and R C, Solution to Exercise 2-1:
C program to determine the ranges of char, short, int and long variables, both signed and unsigned, by printing appropriate values from standard headers and by direct computation.the ANSIstandard for C specifies that ranges be defined in <limits.h>. K and R C Program. Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>
#include <limits.h>

int
main ()
{
printf("Range of Char %dn", CHAR_BIT);
printf("Range of Char Max %dn", CHAR_MAX);
printf("Range of Char Min %dn", CHAR_MIN);
printf("Range of int min %dn", INT_MIN);
printf("Range of int max %dn", INT_MAX);
printf("Range of long min %ldn", LONG_MIN);
printf("Range of long max %ldn", LONG_MAX);
printf("Range of short min %dn", SHRT_MIN);
printf("Range of short max %dn", SHRT_MAX);
printf("Range of unsigned char %un", UCHAR_MAX);
printf("Range of unsigned long %lun", ULONG_MAX);
printf("Range of unsigned int %un", UINT_MAX);
printf("Range of unsigned short %un", USHRT_MAX);

return 0;
}

Read more Similar C Programs
C Basic

K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com