C Primer plus Chapter 2 exercises

1. Write a program, call printf() function once, and print your first name and last name on one line. Call the printf () function again and print your first name and last name on two lines. Then, call the printf () function twice to print your first name and last name on one line. The output should be as follows (of course, replace the content of the example with your name):

Gustav Mahler ← content printed for the first time

Gustav ← content printed for the second time

Mahler ← is still the content printed for the second time

Gustav Mahler ← 3rd and 4th print

#include <stdio.h>
int main(void)
{

printf("Gustav Mahler\n");
printf("Gustav\n");
printf("Mahler\n");
printf("Gustav ");
printf("Mahler");

return 0;
}

Note: practice the line break '\ n' in this question   Usage of.

2. Write a program to print your name and address.

#include <stdio.h>
int main(void)
{

printf("Name:XXX\n");
pirntf("Address:XXXXXX\n");

return 0;
}

Note: this exercise uses the printf() function.

3. Write a program to convert your age into days and display these two values. There is no need to consider leap years here.

#include <stdio.h>
int main(void)
{
int y=37;  /*Age*/
int m=9;  /*month*/
int d=13;  /*date*/
int days;  /*Days*/

days=365*37+31+29+31+30+31+30+31+31+13;
printf("Ages:%d\n",y);
printf("Days:%d\n",days);

return 0;
}

Note: this exercise int type conversion explains the usage of% d.

4. Write a program to generate the following output:

For he' s a jolly good fellow!

For he' s a jolly good fellow!

For he' s a jolly good fellow!

Which nobody can deny!

In addition to the main() function, the program also calls two user-defined functions: one is called jolly(), which is used to print the first three messages, one at a time; Another function called deny() prints the last message.  

#include <stdio.h>
void Jolly(void);  /*Function declaration*/
void deny(void);   /*Function declaration*/
int main(void)
{
jolly();  /*function call*/
jolly();  /*function call*/
jolly();  /*function call*/
deny();   /*function call*/

return 0;
}

void Jolly(void)   /*Function definition*/
{
printf("For he is a jolly good fellow!\n");
}

void deny(void)   /*Function definition*/
{
printf("Which no body can deny!\n");
}

Note: this exercise exercises the declaration, call and definition of simple functions.

5. Write a program to generate the following output:

Brazil,Russia,India,China

India,China,

Brazil,Russia

In addition to main(), the program also calls two user-defined functions: one is called br(), which calls "Brazil,Russia" to print once at a time; The other is called ic(), calling "India, China" to print once at a time. The rest is done in the main() function.

#include <stdio.h>
void br(void);   /*statement*/
void ic(void);   /*statement*/
int main(void)
{

printf("Brizil,Russia,India,China\n");
ic();  /*function call*/
br();  /*function call*/

return 0;
}

void ic(void)  /*Function definition*/
{
printf("India,China\n");
}

void br(void)  /*Function definition*/
{
printf("Brazil,Russia\n");
}

Note: this exercise exercises the declaration, call and definition of simple functions.

6. Write a program to create an integer variable toes and set toes to 10. The program also calculates twice the toes and the square of the toes. The program shall print 3 values and describe them separately to distinguish them.

#include <stdio.h>
int main(void)
{
int tones=10;

printf("Tones:%d\n",tones);
printf("Tones*2:%d\n",tones*2);
printf("Tones*Tones:%d\n",tones*tones);

return 0;
}

Note: practice the usage of printf() function% d conversion description. You can convert the corresponding value or expression.

7. Many studies have shown that smiling has many benefits. Write a program to generate output in the following format:

Smile! Smile! Smile!

Smile! Smile!

Smile!

The program needs to define a function, which is called to print "Smile!" once at a time, and the function is used according to the needs of the program.

#include <stdio.h>
void smail(void);
int main(void)
{
smail();
smail();
smail();
printf("\n");
smail();
smail();
printf("\n");
smail();
printf("\n");

return 0;
}

Note: while practicing function declaration, call and definition, this exercise shows beautiful format output.

8. In C language, a function can call another function. Write a program and call a program named one_ Function of three(). This function prints the word "one" on one line, then calls the second function two(), and then prints the word "three" on another line. The two() function displays the word "two" on one line. The main() function is calling one_ Before the three() function, print the phrase "starting now:", and display the phrase "done!" after the call. Therefore, the output of the program should be as follows:

starting now:

one

two

three

done!

#include <stdio.h>
void one_three(void);
void two(void);
int main(void)
{

printf("Starting now:\n");
one_three();
printf("done!\n");

return 0;
}

void one_three(void)
{
printf("one\n");
two();
printf("three\n");
}

void two(void)
{
printf("two\n");
}

Note: this topic demonstrates the nested call of functions.

preface

I came across a sentence today and thought it was very reasonable: "teaching others is also a process of relearning." so I wrote this article.

summary

The exercises in the second chapter now seem to be a very simple topic. However, the author found some mistakes when looking at his previous answers, which proves that what he learned before is still not solid enough.

Keywords: C

Added by lill77 on Mon, 22 Nov 2021 17:54:08 +0200