🔹Hi, I'm Behnam Jafari. Welcome to your channel! Learn C programming the right way! In this video, we’ll explore essential input and output functions in C with six hands-on exercises designed to improve your coding skills. From reading user input with scanf() to handling full-line input using fgets(), we cover everything you need to know to write efficient and reliable C programs.
🚀 What you’ll learn in this video:
✅ Reading input using scanf() (including integers and strings)
✅ The difference between scanf() and fgets() for string input
✅ Printing output using printf(), puts(), and putchar()
✅ Common pitfalls and best practices in C I/O functions
🔍 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
🎯 Whether you're learning C for embedded systems or general-purpose programming, these exercises will help you write better, bug-free code.
📌 Subscribe to PicoBit-Tech for more tutorials: @PicoBit-Tech
👍 Like & Share if you found this useful, and comment below with your questions! 🚀
Subtitle:
Exercise 1: Reading Input from the Keyboard Using scanf()
Then, we define a string variable that can store 11 characters plus the null terminator. Using printf(), we display a message prompting the user to enter a string, appending \n at the end to move to the next line.
Next, we use scanf() to read a string from the keyboard and store it in the str variable. Finally, we print the entered string to the console. To manually pause the debugger, we add an infinite loop (while(1)) at the end of the program.
After compiling the code, no errors or warnings appear. We then debug the program—previous videos cover the debugging process in detail, and the link is available at the top of the screen.
When we run the program, the message "Enter a string" appears. We type "Hello World" and press Enter. However, the output shows:
your string is: Hello
The reason is that scanf() reads input until it encounters a whitespace (such as a space or Enter key). Since "Hello World" contains a space, only "Hello" is stored in the variable.
Exercise 2: Reading an Integer Using scanf()
We define an integer variable named var, then prompt the user with:
Enter an integer number
The \n ensures the cursor moves to the next line for input.
Using scanf("%d", &var), we read an integer from the user and store it in var. The %d specifier tells scanf() to interpret the input as an integer.
If we forget to use &var, a warning appears because scanf() expects the memory address of the variable.
We switch to debug mode, run the program, and enter a number when prompted. If \n is included inside scanf(), it may interfere with input handling.
Finally, the entered number is displayed using:
printf() function;
Exercise 3: Reading a String and an Integer Simultaneously
We prompt the user to enter a string followed by an integer using printf().
In scanf(), we use two format specifiers:
• %s reads a string and stores it in str.
• %d reads an integer and stores it in var.
Important note:
For string variables, we do not need the & symbol because a string's name represents the address of its first character.
We input "Hello World" followed by a number, but the program outputs 0 for the integer.
This happens because "Hello World" contains a space, and scanf() stops reading the string at the first whitespace. The integer input is therefore not correctly read, causing an issue with the output.
To handle multi-word strings, we should use fgets() instead of scanf().
Exercise 4: Using puts() to Print a String
We use puts() to display a message:
Unlike printf(), puts() automatically moves to the next line after printing.
If we declare a variable but do not use it, the compiler issues a warning.
In embedded systems, where RAM is limited, it's best to comment out unused variables so the compiler ignores them, improving memory efficiency.
Exercise 5: Reading a Full Line of Input with fgets()
The fgets() function reads a complete string, even if it contains spaces. Its syntax is shown on the screen:
• str is the string variable to store input.
• num defines the maximum number of characters to read (including the null terminator).
• stream specifies the input source, typically stdin for keyboard input.
Since str can hold only 11 characters, entering more will truncate the input.
In embedded systems, memory management is critical. If memory constraints are relaxed, we can increase the buffer size, you can see a string with 100 character:
This allows longer strings to be safely stored.
Key difference between scanf() and fgets():
Unlike scanf(), which stops at the first space, fgets() correctly reads entire sentences, including spaces.