Variables and Data Types in C 

Explained for Beginners

  

                 C programming is one of the most important foundational programming languages for beginners. Understanding variables and data types in C is essential to build strong programming logic. In this guide, we will clearly explain variables, data types, printf(), scanf(), and format specifiers with simple examples.

 I have already explained the basic concepts of C, variables, data types, operators, input and output, and simple C programs in my previous blog in short. If you haven’t read that, I have added the link below. Please read that blog first and then read this one. It will be easier for you to understand.

The Knowledge Space Hub

           A variable is like a container that stores data. Without variables, we cannot store or use data in a program.

Rules for Naming Variables in C

✔ Must start with a letter or underscore
✔ Cannot start with a number
✔ No spaces allowed

✔ Case-sensitive 

Data Types in C

data type tells the computer what kind of data we are storing.

1 int (Integer)

Used to store whole numbers.

Example:

int age = 20;

This stores a whole number (no decimal).

2 float (Decimal Number)

Used to store numbers with decimal points.

Example:

float price = 99.50;

This stores decimal value.

3 char (Character)

Used to store a single character.

Example:

char grade = 'A';

Important: Characters are written inside single quotes.

4 double

Used to store large decimal numbers with more precision than float.

Example:

double price = 1234.56789;

👉 double is more accurate than float.

 5 long

Used to store very large whole numbers.

Example:

long population = 1000000;

👉 When numbers are bigger than normal int, we use long.

 6 short

Used to store small whole numbers.

Example:

short marks = 90;

👉 Uses less memory than int.

7 unsigned

Used when we don’t need negative numbers.

Example:

unsigned int age = 25;

👉 Cannot store negative values.

Complete Example Program

#include <stdio.h>
int main() {
    int age = 21;
    float height = 5.6;
    char grade = 'A';

    printf("Age: %d\n", age);
    printf("Height: %f\n", height);
    printf("Grade: %c\n", grade);

    return 0;
}

Age: 21

Height: 5.600000

Grade: A

In C programming, printf() and scanf() are input and output
functions used to communicate with the user.They are defined
in the header file:
#include <stdio.h>

printf() is used to display output on the screen. It prints text,

numbers,and values to the console.

printf("text or format specifier", variables);

#include <stdio.h>

int main() {
int age = 20;
printf("My age is %d", age);
return 0;
}
Output:
My age is 20

scanf() is used to take input from the user. It reads data

from the keyboard and stores it in variables.

scanf("format specifier", &variable);

#include <stdio.h>

int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("Your age is %d", age);
return 0;
}

Placeholders are symbols used inside printf()

and scanf() to represent data types.They start with %.

📌 Common Placeholders in C

Data Type   Placeholder   Example
int%d   10
float%f   5.5
double%lf   10.25
char%c   A
string%s   Hello

Why Are Placeholders Important?

✔ They tell the compiler what type of data to expect

✔ They help format output properly

✔ They prevent errors

✔ They make programs dynamic

If you use the wrong placeholder,
the output may be incorrect.

Write a C program to declare an integer variable and print its value.

Program:

#include <stdio.h>

int main() {
int number = 10;
printf("The number is %d", number);
return 0;
}

Output:

The number is 10

Write a C program to store a character and print it.
Program:
#include <stdio.h>
int main() {
char grade = 'A';
printf("Your grade is %c", grade);
return 0;
}
Output:
Your grade is A
In C programming, variables and data types help us store and
manage information efficiently. Understanding int, float, char, and
other data types is the foundation of writing strong C
programs. Practice using printf() and scanf() with different format
specifiers to improve your programming skills.
📝 Mini Assignment

1. What will be the output of this program?

#include <stdio.h>

int main() {
int a = 5;
float b = 2.5;
printf("%d", a + b);
return 0;
}

👉 Explain why.

2. Write a program to:

  • Take two integer numbers from the user

  • Display their sum, difference, and product

3. Correct the Error:

int age;

scanf("%d", age);

👉 What is missing? Why?

Frequently Asked Questions (FAQ)

1. What is a variable in C?

A variable is a named container used to store data in a program.

2. What is the difference between float and double?

float stores decimal numbers with less precision, while double
stores decimal numbers with more

precision.

3. Why is & used in scanf()?

The & symbol gives the memory address of the variable so
the input value can be stored correctly.

4. What happens if we use the wrong format specifier?

The output may be incorrect or the program
may behave unexpectedly.

Related Posts:

Comments

Post a Comment

Popular posts from this blog