Saturday, October 10, 2026

Format Specifiers and Their Usage in C

C Programming: Format Specifiers and Their Usage

In C programming, format specifiers are used with functions such as printf() and scanf() to specify the type of data being displayed or accepted as input.

Common Format Specifiers

Format specifier

Data type

Example value

Usage

%d

int

25

Integer numbers

%i

int

25

Integer input/output

%f

float

12.5

Floating-point numbers

%lf

double

12.567

Double input using scanf()

%c

char

'A'

Single character

%s

char array

"Hello"

String

%u

unsigned int

25

Non-negative integer

%ld

long int

123456L

Long integer

%lld

long long int

123456789LL

Very large integer

%x

Unsigned integer

255

Hexadecimal in lowercase

%X

Unsigned integer

255

Hexadecimal in uppercase

%p

Pointer

Address

Memory address


In printf(), use %f to display a double value. In scanf(), use %lf to accept a double value.

C Program Demonstrating Format Specifiers

#include <stdio.h>

int main()
{
    int age = 18;
    float marks = 85.5f;
    double salary = 25000.75;
    char grade = 'A';
    char name[] = "Jiya";

    printf("Name: %s\n", name);
    printf("Age: %d\n", age);
    printf("Marks: %f\n", marks);
    printf("Salary: %f\n", salary);
    printf("Grade: %c\n", grade);
    printf("Percentage: %.2f%%\n", marks);

    return 0;
}

Output

Name: Jiya
Age: 18
Marks: 85.500000
Salary: 25000.750000
Grade: A
Percentage: 85.50%

Explanation
  • %s displays the student's name.
  • %d displays an integer.
  • %f displays a floating-point value.
  • %c displays a single character.
  • %.2f displays a floating-point number with exactly two digits after the decimal point.
  • %% displays the % symbol.
  • \n moves the output to the next line.

Format Specifiers in scanf()

The scanf() function accepts input from the keyboard. The format specifier must match the variable's data type.

#include <stdio.h>

int main()
{
    int age;
    float marks;
    double salary;
    char grade;
    char name[30];

    printf("Enter your age: ");
    scanf("%d", &age);

    printf("Enter your marks: ");
    scanf("%f", &marks);

    printf("Enter your salary: ");
    scanf("%lf", &salary);

    printf("Enter your grade: ");
    scanf(" %c", &grade);

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

    printf("\n--- Student Details ---\n");
    printf("Age: %d\n", age);
    printf("Marks: %.2f\n", marks);
    printf("Salary: %.2f\n", salary);
    printf("Grade: %c\n", grade);
    printf("Name: %s\n", name);

    return 0;
}

Why do we use & in scanf()?

The & operator provides the address of a variable so that scanf() can store the input in that variable. For a character array such as name, use name without &.
The space before %c in scanf(" %c", &grade) skips leftover whitespace, such as a newline from the previous input.

Width and Precision in printf()

Format specifiers can control how values are displayed.

Format

Example statement

Output

%d

printf("%d", 25);

25

%5d

printf("%5d", 25);

25

%05d

printf("%05d", 25);

00025

%f

printf("%f", 3.14159);

3.141590

%.2f

printf("%.2f", 3.14159);

3.14

%.3f

printf("%.3f", 3.14159);

3.142

%-5d

printf("%-5d", 25);

25


Usage hint:
  • Width: Minimum character spaces for displaying a value.
  • Precision: Number of digits after the decimal point.
  • Minus (-): Left-aligns the value.
  • Zero (0): Pads the value with leading zeros.


Sunday, July 12, 2026

Use of Data Types in Programming Languages

Introduction to Data Types


If you are learning a new programming language, then understand "Data types" of that language first.

In programming, we use data types to inform the computer about the kind of data we want to handle.

Why do we use data types?

1. To store different kinds of data

Different variables hold different kinds of information.
int → Whole numbers (e.g., 100)
float → Decimal numbers (e.g., 99.5)
char → Single character (e.g., 'A')
string → Text (e.g., "Alice")
bool → true or false

2. To allocate memory efficiently

Each data type requires a specific amount of memory. Using the correct type helps save memory.

By choosing the appropriate type, programs use memory more efficiently.
  • char typically uses less memory than a string.
  • int usually uses less memory than a double.

3. To perform correct operations

Data types determine which operations are allowed. For example,
  • Numbers can be added or multiplied:
    • 10 + 20 = 30
  • Strings can often be concatenated:
    • "Hello" + " World" = "Hello World"
In C++ language, Numbers can be added like:

int a = 10, b = 20;
cout << a + b;   // 30

Strings can be joined like:

string s1 = "Hello";
string s2 = "World";
cout << s1 + " " + s2;   // Hello World

4. To detect errors: Data types help the compiler or interpreter catch mistakes.

int age = "Twenty";   // Error

This is an error because a string "Twenty" cannot be assigned to an integer variable "age".

Real-Life Analogy

Think of data types like labeled containers:
  • A water bottle is for water.
  • A file folder is for documents.
  • A coin box is for coins.
Similarly, in programming:
  • int stores whole numbers.
  • float stores decimal numbers.
  • string stores text.
  • bool stores true or false.

Using the right "container" for each kind of data keeps the program organized and working correctly.


Sunday, February 15, 2026

Tricky C++ Loops: Predict Output Questions

 C++ Loops - Tricky Predict Output Questions

1. Post-Increment in Condition
#include <iostream>
using namespace std;

int main() {
    int i = 0;
    while(i++ < 3)
        cout << i << " ";
    return 0;
}

Output:
1 2 3

Explanation:
  • i++ uses old value for comparison, then increments.
  • Values checked: 0,1,2 ; hence prints incremented values 1,2,3.

2. Pre-Increment in Condition
int main() {
    int i = 0;
    while(++i < 3)
        cout << i << " ";
}

Output:
1 2

Explanation:
  • ++i increments first.
  • Loop runs for i = 1, 2 only.

3. Missing Braces Trap
int main() {
    int i = 1;
    while(i <= 3)
        cout << i++ << " ";
        cout << "Done";
}

Output:
1 2 3 Done

Explanation:
  • Only first statement belongs to while.
  • cout << "Done" executes once after loop.

4. Decrement in Condition
int main() {
    int i = 3;
    while(i--)
        cout << i << " ";
}

Output:
2 1 0

Explanation:
  • Condition checks old value.
  • After decrement, printed values: 2,1,0.

5. Continue Before Print
int main() {
    for(int i = 1; i <= 5; i++) {
        if(i % 2 == 0)
            continue;
        cout << i << " ";
    }
}

Output:
1 3 5

Explanation: Even numbers skipped using continue.

6. Break Before Increment
int main() {
    int i = 1;
    while(i <= 5) {
        if(i == 4)
            break;
        cout << i << " ";
        i++;
    }
}

Output:
1 2 3

Explanation: Loop stops when i == 4.

7. Infinite Loop Trap
int main() {
    for(int i = 1; i < 3; )
        cout << i << " ";
}

Output:
1 1 1 1 ... (Infinite)

Explanation: i never increments.

8. Printing in For Update Section
int main() {
    for(int i = 0; i < 3; cout << i++ << " ");
}

Output:
0 1 2

Explanation: Printing happens in update section of for loop.

9. Double Increment Trap
int main() {
    int i = 1;
    do {
        cout << i++ << " ";
    } while(i++ < 4);
}

Output:
1 3

Explanation: i increments twice each iteration.

10. Continue in Do-While
int main() {
    int i = 0;
    do {
        i++;
        if(i == 2)
            continue;
        cout << i << " ";
    } while(i < 3);
}

Output:
1 3

Explanation: When i=2, continue skips printing.

11. Nested Loop with Break
int main() {
    for(int i = 1; i <= 3; i++) {
        for(int j = 1; j <= 3; j++) {
            if(j == 2)
                break;
            cout << i << j << " ";
        }
    }
}

Output:
11 21 31

Explanation: Inner loop stops when j==2.

12. Logical Trap with Semicolon
int main() {
    int i = 0;
    while(i < 3);
    {
        cout << i;
    }
}

Output:

Infinite Loop

Explanation: Semicolon ends while loop → empty infinite loop.

13. Scope of Variable
int main() {
    int i;
    for(i = 0; i < 3; i++)
        cout << i;
    cout << i;
}

Output:
0123

Explanation: After loop, i becomes 3.

14. Decrement by 2
int main() {
    for(int i = 5; i > 0; i -= 2)
        cout << i << " ";
}

Output:
5 3 1

Explanation: i decreases by 2 each iteration.

15. Nested Continue Trap
int main() {
    for(int i = 1; i <= 2; i++) {
        for(int j = 1; j <= 3; j++) {
            if(j == 2)
                continue;
            cout << i << j << " ";
        }
    }
}

Output:
11 13 21 23

Explanation: j=2 skipped for each i.

16. Logical Trap
#include <iostream>
using namespace std;

int main() {
    int i = 1;
    while(i < 4)
        cout << i++;
        cout << "Done";
    return 0;
}

Output:
123Done

Explanation: Only first cout is inside loop (no braces!).

17. For loop body
#include <iostream>
using namespace std;

int main() {
    for(int i = 0; i < 3; cout << i++ << " ");
    return 0;
}

Output:
0 1 2

Explanation: Printing happens in update section!

18. No Braces Trap
#include <iostream>
using namespace std;

int main() {
    for(int i = 1; i <= 3; i++)
        cout << i << " ";
        cout << "End";
    return 0;
}

Output:
1 2 3 End

Explanation: Only first statement is inside loop.

19. Infinite Loop Trap
#include <iostream>
using namespace std;

int main() {
    for(int i = 1; i < 5; ) {
        cout << i << " ";
    }
    return 0;
}

Output:
1 1 1 1 1 ... (Infinite Loop)

Explanation: i never changes!

< Back to C++ Home Page >

* * * * *