[C language] Introduction to C language (Chapter 2)


The second chapter in Stephen Prata’s C Primer Plus. Here the first practical tasks already appear; albeit extremely simple, for memorizing the basic syntax; therefore, I will not give their output separately. Before the tasks, a small reminder:

5 Types of Operators in C Language

  • Declaration statement
  • assignment operator
  • Function
  • Control operator
  • Empty statement

Programming exercises

1) Write a program that uses the first printf() to print your first and last names on one line, the second printf() to print your first and last names on two lines, and two more printf() calls to print your first and last names in one line. The output should look like this (but include your personal details):

Ivan Ivanov ← The first output operator
Ivan ← Second output statement
Ivanov ← Still the second inference operator
Ivan Ivanov ← The third and fourth output operators

Program:

#include <stdio.h>
int main (void)
{
printf("Ivan Ivanov\n");
printf("Ivan\nIvanov\n");
printf("Ivan ");
printf("Ivanov");
getchar();
return 0;
}

2. Write a program that displays your name and address.

#include <stdio.h>
int main (void)
{
printf("My name is Ivan Ivanov\n");
printf("My address is Moscow");
getchar();
return 0;
}

3. Write a program that converts your age in completed years to number of days and displays both values on the screen. Ignore leap years.

#include <stdio.h>
int main (void)
{
int years, days;
years = 34;
days = years * 365;
printf("I live already %d years which is %d days", years, days);
getchar();
return 0;
}

4. Write a program that produces the following output:
He is a fun guy!
He is a fun guy!
He is a fun guy!
Nobody can deny it!

In addition to the main() function, the program must use two user-defined functions: jolly(), which prints the message “He’s a good fellow!” once, and deny(), which prints the message on the last line.

#include <stdio.h>
void jolly();
void deny();
int main (void)
{
jolly();
jolly();
jolly();
deny();
getchar();
return 0;
}
void jolly()
{
printf("He is funny good lad!\n");
}
void deny()
{
printf("No one can't deny it!\n");
}

5. Write a program that produces the following output:
Brazil, Russia, India, China
India, China,
Brazil, Russia
In addition to the main() function, the program must use two user-defined functions: br(), which prints the string “Brazil, Russia” once, and ic(), which prints the string “India, China” once. The main() function must take care of any additional inference tasks.

#include <stdio.h>
void br();
void ic();
int main(void)
{
br();
printf(", ");
ic();
printf("\n");
ic();
printf("\n");
br();
printf("\n");
getchar();
return 0;
}
void br()
{
printf("Brazil, Russia");
}
void ic()
{
printf("India, China");
}

6. Write a program that creates an integer variable called toes. The program must set the variable toes to 10. Along with this, the program must calculate twice the value of toes and the square of toes. The program should display all three values, providing them with appropriate explanations.

#include <stdio.h>
int main(void)
{
int toes = 10;
int toes_2x;
int toes_sqrt;
toes_2x = 2 * toes;
toes_sqrt = toes * toes;
printf("Human got %d toes\n", toes);
printf("Two humans got %d toes\n", toes_2x);
printf("Ten humans got %d toes", toes_sqrt);
getchar();
return 0;
}

7. Many studies show that smiling contributes to success. Write
a program that produces the following output:
Smile! Smile! Smile!
Smile! Smile!
Smile!
The program must define a function that displays the string
“Smile!” once. This function should be called as many times as necessary

#include <stdio.h>
void smile();
int main(void)
{
smile(); smile(); smile();
printf("\n");
smile(); smile();
printf("\n");
smile();
printf("\n");
getchar();
return 0;
}
void smile()
{
printf("Smile!");
}

8. In C, one function can call another. Write a program that calls a function named one_three ( ) . This function should print the word “one” on one line, call the two() function, and then print the word “three” on the same line. The two() function should display the word “two” on the same line. The main() function should print the word “start:” before calling the one_three() function and the word “order!” after it is called. So the output should look like this :
getting started:

one
two
three
order!

#include <stdio.h>
void one_three();
void two();
int main()
{
printf("start:\n");
one_three();
printf("good!\n");
getchar();
return 0;
}
void one_three()
{
printf("one\n");
two();
printf("three\n");
}
void two()
{
printf("two\n");
}

I will be glad to see your comments!


This entry was posted in C language (en). Bookmark the permalink.

Leave a Reply

🇬🇧 Attention! Comments with URLs/email are not allowed.
🇷🇺 Комментарии со ссылками/email удаляются автоматически.