C Data Types and Format Specifiers Practice Questions with Solutions

Introduction

Data types tell C what kind of value a variable is expected to store, while format specifiers tell functions such as printf() and scanf() how that value should be formatted or interpreted. In this chapter, you will practice common C data types such as int, float, double, and char, along with format specifiers such as %d, %f, %lf, %c, and %s. The examples gradually move from simple values to practical programs. C Data Types and Format Specifiers Practice questions with solutions to help you understand the concepts.

Q1. Store and Display an Integer

Problem Statement

Create an integer variable to store a student’s age and display it using the correct format specifier.

C Program

#include <stdio.h>

int main()
{
    int age = 15;

    printf("Age = %d", age);

    return 0;
}

Sample Output

Age = 15

Concepts Covered

  • int data type
  • Integer variable
  • %d format specifier
  • printf()

Q2. Store and Display a Float Value

Problem Statement

Create a float variable to store the price of a product and display it with two decimal places.

C Program

#include <stdio.h>

int main()
{
    float price = 499.99;

    printf("Price = %.2f", price);

    return 0;
}

Sample Output

Price = 499.99

Concepts Covered

  • float
  • %f
  • Precision
  • %.2f
  • Decimal values

Q3. Store a Double-Precision Value

Problem Statement

Create a double variable to store a large decimal value and display it.

C Program

#include <stdio.h>

int main()
{
    double distance = 123456.789;

    printf("Distance = %.3f", distance);

    return 0;
}

Sample Output

Distance = 123456.789

Concepts Covered

  • double
  • Floating-point values
  • Decimal precision
  • %f

Q4. Store and Display a Character

Problem Statement

Create a char variable to store a student’s grade and display it using the correct format specifier.

C Program

#include <stdio.h>

int main()
{
    char grade = 'A';

    printf("Grade = %c", grade);

    return 0;
}

Sample Output

Grade = A

Concepts Covered

  • char
  • Character variable
  • %c
  • Character literals

Q5. Store and Display a String

Problem Statement

Create a character array containing a student’s name and display it using the %s format specifier.

C Program

#include <stdio.h>

int main()
{
    char name[] = "Rahul";

    printf("Name = %s", name);

    return 0;
}

Sample Output

Name = Rahul

Concepts Covered

  • Character array
  • String
  • %s
  • printf()

Q6. Display Multiple Data Types

Problem Statement

Create variables for a student’s age, marks, grade, and name, then display all values using the appropriate format specifiers.

C Program

#include <stdio.h>

int main()
{
    char name[] = "Aman";
    int age = 16;
    float marks = 88.5;
    char grade = 'A';

    printf("Name = %s\n", name);
    printf("Age = %d\n", age);
    printf("Marks = %.1f\n", marks);
    printf("Grade = %c", grade);

    return 0;
}

Sample Output

Name = Aman
Age = 16
Marks = 88.5
Grade = A

Concepts Covered

  • int
  • float
  • char
  • Character array
  • %s
  • %d
  • %f
  • %c

Q7. Use sizeof() With Different Data Types

Problem Statement

Use the sizeof() operator to display the amount of memory occupied by common C data types.

C Program

#include <stdio.h>

int main()
{
    printf("Size of int = %zu bytes\n", sizeof(int));
    printf("Size of float = %zu bytes\n", sizeof(float));
    printf("Size of double = %zu bytes\n", sizeof(double));
    printf("Size of char = %zu byte\n", sizeof(char));

    return 0;
}

Sample Output

The exact sizes are implementation-dependent. A common output is:

Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte

Concepts Covered

  • sizeof()
  • Data type size
  • size_t
  • %zu
  • Memory representation basics

Q8. Take Integer and Float Input

Problem Statement

Create a program that accepts an integer and a floating-point number from the user and displays both values.

C Program

#include <stdio.h>

int main()
{
    int age;
    float height;

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

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

    printf("Age = %d\n", age);
    printf("Height = %.2f", height);

    return 0;
}

Sample Output

Enter your age: 18
Enter your height: 5.8
Age = 18
Height = 5.80

Concepts Covered

  • int
  • float
  • %d
  • %f
  • scanf()
  • User input

Q9. Take Character and String Input

Problem Statement

Create a program that accepts a student’s name and grade from the user and displays them.

C Program

#include <stdio.h>

int main()
{
    char name[50];
    char grade;

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

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

    printf("Name = %s\n", name);
    printf("Grade = %c", grade);

    return 0;
}

Sample Output

Enter your name: Rahul
Enter your grade: A
Name = Rahul
Grade = A

Concepts Covered

  • Character array
  • char
  • String input
  • %s
  • %c
  • scanf()

Q10. Create a Student Result Program Using Multiple Data Types

Problem Statement

Create a program that stores a student’s name, roll number, marks, grade, and percentage. Display all information using suitable data types and format specifiers.

C Program

#include <stdio.h>

int main()
{
    char name[] = "Rahul";
    int rollNumber = 101;
    float marks = 87.5;
    char grade = 'A';
    double percentage = 87.50;

    printf("Student Result\n");
    printf("----------------\n");
    printf("Name       : %s\n", name);
    printf("Roll Number: %d\n", rollNumber);
    printf("Marks      : %.1f\n", marks);
    printf("Grade      : %c\n", grade);
    printf("Percentage : %.2f%%", percentage);

    return 0;
}

Sample Output

Student Result
----------------
Name       : Rahul
Roll Number: 101
Marks      : 87.5
Grade      : A
Percentage : 87.50%

Concepts Covered

  • Multiple data types
  • int
  • float
  • double
  • char
  • Character arrays
  • %d
  • %f
  • %c
  • %s
  • Formatted output

Common C Data Types & Format Specifiers

Data TypePurposeCommon printf() SpecifierExample
intWhole numbers%d25
floatDecimal numbers%f25.50
doubleHigher-precision decimal values%f25.500000
charSingle character%c'A'
Character arrayString%s"Rahul"
size_tSizes returned by sizeof%zu4

Format specifiers can differ between printf() and scanf() for some types. For example, a double is commonly printed with %f in printf(), but read with %lf in scanf().

Key Takeaways

  • A data type determines the kind of value a variable can store.
  • int is commonly used for whole numbers.
  • float is used for decimal values where single precision is sufficient.
  • double provides greater floating-point precision than float.
  • char stores a single character.
  • Character arrays are commonly used to store strings.
  • %d is commonly used with int in printf() and scanf().
  • %f is used for floating-point output with printf().
  • %lf is used to read a double with scanf().
  • %c is used for a character.
  • %s is used for a string.
  • %zu is used to display a size_t value, such as the result of sizeof().
  • sizeof() can be used to determine the size of a data type or object in bytes.
  • The exact size of some C data types depends on the compiler and platform.

FAQs

1. What are data types in C?

Data types specify what kind of value a variable is designed to store, such as an integer, decimal number, or character.

2. What is the difference between float and double?

Both store floating-point numbers, but double generally provides greater precision than float. The exact representation and precision are implementation-dependent.

3. What is %d used for in C?

%d is commonly used with int values in formatted input and output.

Example:

int age = 18;
printf("%d", age);

4. What is %f used for?

In printf(), %f is used to print a double argument after the usual default argument promotions; a float passed to printf() is also promoted to double.

Example:

float price = 99.50;
printf("%.2f", price);

5. What is the difference between %f and %lf?

In printf(), %f is used to print a floating-point value and %lf is also accepted with the same meaning. In scanf(), %f expects a pointer to float, while %lf expects a pointer to double.

6. What is %c used for?

%c is used to read or display a single character.

Example:

char grade = 'A';
printf("%c", grade);

7. What is %s used for?

%s is used to read or display a null-terminated character string.

Example:

char name[] = "Rahul";
printf("%s", name);

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top