Structure of C Program

M4-R5.1 · Chapter 4: Building IoT applications · 2 min read

Structure Of C Program

Input and Output Statements in C 

1. Introduction

In C programming, input and output operations are handled using functions provided by the standard library. These functions are defined in the header file stdio.h. The most commonly used functions are printf() for output and scanf() for input.

2. printf() Function (Output)

The printf() function in C is a standard library function used to send formatted output to the standard output (usually the console screen).

Syntax:
printf("format string", argument_list);

Example:
printf("Hello, World!");
printf("Age: %d", age);

Key Points:

  • - It prints text and variable values.
  • - Format specifiers like %d, %f are used.
  • - \n is used for a new line.
  • - It is case-sensitive.

3. scanf() Function (Input)

In the C programming language, scanf() is a standard library function used to read formatted input from the standard input stream, which is usually the keyboard.

Syntax:
scanf("format string", &variable);

Example:
int age;
scanf("%d", &age);

Key Points:
- Reads input from keyboard.
- Uses address-of operator (&).
- Format specifier must match data type.

4. Format Specifiers

%d - Integer
%f - Float
%lf - Double
%c - Character
%s - String

5. Examples

Example Program:

#include <stdio.h>
int main() {
    int age;
    printf("Enter age: ");
    scanf("%d", &age);
    printf("Age is: %d", age);
    return 0;
}