Beginner · C PROGRAMMING

Variables and types

What you will learn

Store whole numbers, decimal values and characters.

Understand the concept

A variable has a type and a value. int stores integers, double stores floating-point values and char stores a character. printf uses format specifiers that must match the supplied argument types: %d for int, %f for double and %c for a character.

Read the example

The complete program appears below. Read it first, predict the output, then run it.

Expected output

Quantity: 3, price: 125.50, grade: A

Practise

Add a variable for total cost and print quantity multiplied by price.

Common mistake

Do not read an uninitialized variable. Single quotes represent a character; double quotes represent a string.

Lesson discussion

Log in to participate.

#include <stdio.h>

int main(void) {
    int quantity = 3;
    double price = 125.50;
    char grade = 'A';
    printf("Quantity: %d, price: %.2f, grade: %c\n", quantity, price, grade);
    return 0;
}
PRACTICE LAB

Read. Edit. Run.

C source · .c

Experiment with the program below. Your code and input are sent to the selected compiler only when you run it.

Tab indents · Shift+Tab unindents · Esc then Tab leaves the editor · Ctrl/Cmd+Enter runs · Ctrl/Cmd+S saves a draft

Changes save automatically to your account.

Input / output example

Example input
No input required.
Expected output for the original program
Quantity: 3, price: 125.50, grade: A
Output will appear here.

Log in to save your progress

Ready when you are

Need help with your laptop or computer?

Send us the model and fault details. Start your repair request online or contact TCS Pune directly.

WA