K & R C 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 chapters of the “C Programming Language (2nd Edition)” popularly known as K & R C book for easy reference.

  1. Chapter 1: A Tutorial Introduction
  2. Chapter 2: Types, Operators and Expressions
  3. Chapter 3: Control Flow
  4. Chapter 4: Functions and Program Structure
  5. Chapter 5: Pointers and Arrays
  6. Chapter 6: Structures
  7. Chapter 7: Input and Output
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

C Aptitude Questions and answers with explanation

C Aptitude 24
C program is one of most popular programming language which is used for core level of coding across the board. C program is used for operating systems, embedded systems, system engineering, word processors,hard ware drivers, etc.

In this site, we have discussed various type of C programs till date and from now on, we will move further by looking at the C aptitude questions.

The C aptitude questions and answers for those questions along with explanation for the interview related queries.

We hope that this questions and answers series on C program will help students and freshers who are looking for a job, and also programmers who are looking to update their aptitude on C program.
Some of the illustrated examples will be from the various companies, and IT industry experts.
Read more about how to prepare and write the C Aptitude Exams?
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

 

Predict the output or error(s) for the following:

C aptitude 24.1

main()

{

char str[4]="test";

printf("%s",str);


}

Answer: test#$634278621@!

Explanation: The character array has the memory just enough to hold the string “test” and doesn’t have enough space to store the terminating null character. So it prints the test correctly and continues to print garbage values till it accidentally comes across a NULL character.

C aptitude 24.2

 
main()

{

char * str = "hello";

char * ptr = str;

char least = 127;

while (*ptr++)

least = (*ptr <least ) ?*ptr :least;

printf("%d",least);

}

Answer: 0

Explanation: After ‘ptr’ reaches the end of the string the value pointed by ‘str’ is ‘’. So the value of ‘str’ is less than that of ‘least’. So the value of ‘least’ finally is 0.

C aptitude 24.3

     main()

{

int i = 257;

int *iPtr = &i;

printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) );

}

Answer: 11

Explanation: The integer value 257 is stored in the memory as, 00000001 00000001, so the individual bytes are taken by casting it to char * and get printed.

Read more Similar C Programs 
 
Learn C Programming
C Aptitude
C Interview questions
 

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

C Program to change the text colors.

Write a C Program to change the text colors.
In this program, we give the example of changing the text colors, background colors using conio.h library.
Syntax: void textcolor(int_color);
Here color is the integer variable, you can specify a color name also, but it should be a proper color name in capital letters.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

/***********************************************************
* 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<conio.h>
int main()
{
clrscr();
textcolor(BLUE); // Change font colour to blue
textbackground(WHITE); //change the background colour to white
cprintf("Color is Blue with white backgroundnn");
clrscr();
textcolor(RED+BLINK);//this one is blinking the text
cprintf("Color is RED with blinkingnn");
return 0;
}
Read more Similar C Programs
 
Graphics in C

C Strings 
 
C Aptitude 
 

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

C Aptitude Questions and answers with explanation

C Aptitude 23
C program is one of most popular programming language which is used for core level of coding across the board. C program is used for operating systems, embedded systems, system engineering, word processors,hard ware drivers, etc.

In this site, we have discussed various type of C programs till date and from now on, we will move further by looking at the C aptitude questions.

The C aptitude questions and answers for those questions along with explanation for the interview related queries.

We hope that this questions and answers series on C program will help students and freshers who are looking for a job, and also programmers who are looking to update their aptitude on C program.
Some of the illustrated examples will be from the various companies, and IT industry experts.
Read more about how to prepare and write the C Aptitude Exams?
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

 

Predict the output or error(s) for the following:

C aptitude 23.1

void main(){
int res;
res= 56>76 ? return 0:return 1;
printf("%d",res);
}

Answer: Compilation error.

Explanation: We cannot use return keyword in conditional operator.

C aptitude 23.2
Note: here input for command line argument is, example 5 4 7 8

 
/* example.c */
#include

int main(int argc, char *argv[])
{
int j;
j = argv[1] + argv[2] + argv[3] + argv[4];
printf("%d", j);
return 0;
}

Answer: Error

Explanation: Command line arguments are string types, we can’t add or do the arithmetic operations on string types in C, we have to convert the argv[] to the integer types using atoi function.

C aptitude 23.3

     int main()
{
int num1 = 123;
float num2 = 123.0;
if(num1 == num2)
printf("num1 and num2 are equal");
else
printf("num1 and num2 are not equal");
return 0;
}

Answer: num1 and num2 are equal

Explanation: This is the tricky question, in if statement we are comparing 123=123.0, condition satisfied, so the result is num1 and num2 are equal.

Read more Similar C Programs 
 
Learn C Programming
C Aptitude
C Interview questions
 

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