C Programming β Standard Input/Output Functions (Hands-on Exercises)
πΉ Hi, I’m Behnam Jafari. Welcome to PicoBit-Tech!
Learn C programming the right way with practical exercises designed for microcontroller programming and embedded systems. In this video, we explore essential input and output functions in C with six hands-on exercises to improve your coding skills and understanding of I/O operations.
π What Youβll Learn
By the end of this lesson, you will master:
β
Reading input using scanf() (integers and strings)
β
Understanding the difference between scanf() and fgets() for string input
β
Printing output using printf(), puts(), and putchar()
β
Common pitfalls and best practices for C I/O functions
This tutorial emphasizes efficient and reliable coding practices, perfect for both embedded and general-purpose programming.
π Timestamps
00:00 β Introduction
01:09 β Exercise 1: Reading Input from the Keyboard Using scanf()
04:36 β Exercise 2: Reading an Integer Using scanf()
08:20 β Exercise 3: Reading a String and an Integer Simultaneously
14:05 β Exercise 4: Using puts() to Print a String
15:18 β Exercise 5: Reading a Full Line of Input with fgets()
20:16 β Exercise 6: Using putchar() to Print a Single Character
π‘ Exercise Highlights
Exercise 1: Reading Input with scanf()
-
Define a string variable (e.g., 11 characters + null terminator).
-
Prompt the user with
printf()to enter a string. -
Use
scanf()to read input. Note:scanf()stops at the first whitespace, so multi-word strings will be truncated. -
Debug with Eclipse IDE and use an infinite loop (
while(1)) for manual pause.
Exercise 2: Reading an Integer
-
Define an integer variable and prompt the user.
-
Use
scanf("%d", &var)to store the input. -
Ensure you pass the variableβs address (
&) to avoid warnings.
Exercise 3: Reading a String and Integer Together
-
Prompt the user for a string followed by an integer.
-
Use
%sfor the string and%dfor the integer. -
Note: Multi-word strings will break with
scanf(). Usefgets()for full-line input.
Exercise 4: Using puts()
-
Display strings with
puts()β automatically adds a newline after printing. -
Comment out unused variables in embedded systems to save RAM.
Exercise 5: Reading Full Lines with fgets()
-
fgets()reads entire strings including spaces. -
Syntax:
fgets(str, num, stdin) -
Manage buffer sizes carefully for embedded systems.
Exercise 6: Using putchar()
-
Print individual characters efficiently.
-
Useful for low-level or memory-constrained applications.
π― Why Watch This Video
-
Master standard input/output functions in C
-
Avoid common mistakes with
scanf()andfgets() -
Learn memory-aware programming practices for embedded systems
-
Build a foundation for real-world microcontroller programming