String Input and Output in C

Learn how to handle string input and output in C programming using scanf and printf, with examples on Debian 12 using Vim.

String Input and Output in C Programming

In C, strings are handled as arrays of characters. You can read strings from input using scanf and print them using printf.

Reading Strings Using scanf

Use scanf(\\"%s\\", str); to read a string. It stops reading at whitespace.

char name[50];
scanf(\\"%s\\", name);

Printing Strings Using printf

Use printf(\\"%s\\", str); to print a string.

printf(\\"Name: %s\
\\", name);

Example Program

#include 

int main() 
    char name[50];

    printf(\\"Enter your name: \\");
    scanf(\\"%s\\", name);

    printf(\\"Hello, %s!\
\\", name);

    return 0;

Compile and Run

gcc string_io.c -o string_io
./string_io

Notes

scanf(\\"%s\\", str); reads input until the first whitespace. To read full lines with spaces, use fgets.

Summary

Use scanf and printf for simple string input and output in C.

Subscribe to Our YouTube for More

Download as PDF

https://blog.arashtad.com/updates/string-input-and-output-in-c/?feed_id=13412&_unique_id=689af22e31c06

Comments

Popular posts from this blog

Why Smart Contracts Are Revolutionary? (Smart Contracts Use Cases)

A Quick Guide to Tron Network Interaction using Python.

#36 Multi-Signature Wallet Smart Contract (Part 3)