scanf, printf, fgets, and More | C Programming for Microcontrollers

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 %s for the string and %d for the integer.

  • Note: Multi-word strings will break with scanf(). Use fgets() 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() and fgets()

  • Learn memory-aware programming practices for embedded systems

  • Build a foundation for real-world microcontroller programming

Leave a Reply

Your email address will not be published. Required fields are marked *