C Programming Day 2 (Part 1)
After learning the basics of C programming and writing your first "Hello, World!" program in Day 1, it's time to explore one of the most important concepts in programming—Variables and Data Types.
Every C program works with data. Whether you're creating a calculator, a banking application, or a game, you'll need a way to store and manipulate information. That's where variables and data types come in.
By the end of this lesson, you'll understand:
- What variables are
- How to declare and initialize variables
- Rules for naming variables
- Types of variables in C
- Data types and their sizes
- Choosing the correct data type
- Practical coding examples
- Common beginner mistakes
What is a Variable?
A variable is a named memory location used to store data that can change while a program is running.
Think of a variable as a container.
Just as a bottle stores water, a variable stores information such as numbers, characters, or decimal values.
Example:
int age = 20;Here:
int→ Data typeage→ Variable name20→ Stored value
Why Do We Need Variables?
- Store user information
- Perform calculations
- Save temporary results
- Update values during execution
- Process input and output
Declaring Variables
data_type variable_name;int age;
float salary;
char grade;
Initializing Variables
int age = 21;
float price = 150.75;
char grade = 'A';Initialization helps prevent unexpected values in your program.
Declaring Multiple Variables
int x, y, z;
float length, width;
char firstLetter, lastLetter;
Updating Variable Values
#include <stdio.h>
int main()
{
int marks = 60;
printf("Marks = %d\n", marks);
marks = 90;
printf("Updated Marks = %d", marks);
return 0;
}Marks = 60
Updated Marks = 90
Variable Naming Rules
Rule 1: Start with a Letter or Underscore
age
_name
studentMarks2age
5marksRule 2: Do Not Use Spaces
studentName
student_namestudent nameRule 3: Do Not Use Special Characters
price
totalAmountprice#
salary@Rule 4: Keywords Cannot Be Variable Names
int if;int total;Rule 5: Variable Names Are Case Sensitive
age
Age
AGERule 6: Use Meaningful Names
studentAge
totalPrice
employeeSalarya
x
abcBest Practices for Variable Names
Use descriptive names such as:
studentNamestudentIDemployeeSalarymonthlyIncometotalMarksaverageScore
Good naming improves readability and simplifies debugging.
Types of Variables in C
1. Local Variables
int main()
{
int age = 20;
return 0;
}2. Global Variables
#include <stdio.h>
int marks = 100;
int main()
{
printf("%d", marks);
return 0;
}3. Static Variables
static int count = 0;4. Register Variables
register int i;5. Extern Variables
extern int total;What is a Data Type?
A data type tells the compiler:
- What type of data will be stored.
- How much memory to allocate.
- What operations are allowed on the data.
Every variable in C must have a data type.
Basic Data Types in C
| Data Type | Description | Example |
|---|---|---|
| int | Integer numbers | 10 |
| char | Single character | 'A' |
| float | Decimal number | 3.14 |
| double | Large decimal number | 3.141592653 |
| void | No value | Functions |
Integer Data Type (int)
int age = 22;
int marks = 95;22
95Character Data Type (char)
char grade = 'A';
char gender = 'M';'A'"A"Float Data Type
float temperature = 35.7;
float price = 120.50;Double Data Type
double pi = 3.141592653589793;Double is preferred when higher precision is required.
Void Data Type
void displayMessage()
{
printf("Welcome");
}Size of Basic Data Types
| Data Type | Typical Size |
| char | 1 Byte |
| int | 4 Bytes |
| float | 4 Bytes |
| double | 8 Bytes |
Using sizeof Operator
sizeof operator returns the memory occupied by a data type or variable.#include <stdio.h>
int main()
{
printf("%zu\n", sizeof(int));
printf("%zu\n", sizeof(float));
printf("%zu\n", sizeof(double));
printf("%zu\n", sizeof(char));
return 0;
}Choosing the Right Data Type
- int → Whole numbers
- char → Single characters
- float → Decimal values with moderate precision
- double → Decimal values requiring higher precision
Complete Example
#include <stdio.h>
int main()
{
int age = 20;
float height = 5.8;
char grade = 'A';
double salary = 35000.75;
printf("Age = %d\n", age);
printf("Height = %.1f\n", height);
printf("Grade = %c\n", grade);
printf("Salary = %.2lf\n", salary);
return 0;
}Age = 20
Height = 5.8
Grade = A
Salary = 35000.75Common Beginner Mistakes
Forgetting to Initialize Variables
int age;Using Double Quotes for Characters
char grade = "A";char grade = 'A';Using Reserved Keywords
int while;int totalMarks;Storing Decimal Values in an Integer
Wrong
int price = 25.75;Correct
float price = 25.75;Practice Exercises
- Declare variables to store your name's first letter, age, and height.
- Create three integer variables and print their values.
- Store your exam marks using an
intvariable. - Store the value of π using a
double. - Use the
sizeofoperator to display the size ofchar,int,float, anddouble.
What's Next?
In Part 2 (Day 2), you'll learn:
- Constants in C
- Types of Constants
- The
constKeyword #defineConstants- C Keywords (32 Reserved Keywords)
- Input and Output Functions (
printf(),scanf(),getchar(),putchar(),puts()) - Reading User Input
- Writing Interactive Programs
Keep practicing these concepts before moving on, as variables and data types form the foundation of every C program you'll write.
C Programming – Day 2 (Part 2)
Learn C Constants, Keywords & Input Output Functions
Welcome to Part 2 of Day 2 in our C Programming Full Course. In Part 1, you learned about variables and data types. Now it's time to understand constants, keywords, and input/output (I/O) functions, which are essential for writing interactive C programs.
By the end of this lesson, you will learn:
- What constants are
- Types of constants in C
- Using the
constkeyword - Using
#definefor symbolic constants - C reserved keywords
- Input and Output functions
printf()scanf()getchar()putchar()puts()- Safe alternatives to deprecated functions
- Practical coding examples
- Common beginner mistakes
- Best practices
What is a Constant?
A constant is a value that cannot be changed during program execution.
Unlike variables, constants remain fixed throughout the program.
Example:
const float PI = 3.14159;Here, the value of PI cannot be modified after it has been declared.
Why Use Constants?
Constants make programs:
- Easier to read
- Easier to maintain
- More secure
- Less error-prone
- More reusable
Instead of writing the same value repeatedly, define it once as a constant.
Types of Constants in C
1. Integer Constants
100
-25
5000
02. Floating-Point Constants
3.14
25.75
0.001
100.503. Character Constants
'A'
'Z'
'5'
'#'4. String Constants
"Hello"
"C Programming"
"Welcome"5. Enumeration Constants
enum keyword.enum Days
{
MONDAY,
TUESDAY,
WEDNESDAY
};The const Keyword
The const keyword creates variables whose values cannot be changed.
Example
#include <stdio.h>
int main()
{
const int MAX_USERS = 100;
printf("%d", MAX_USERS);
return 0;
}Attempting to modify a const variable causes a compilation error.
Wrong
MAX_USERS = 200;Symbolic Constants Using #define
The preprocessor directive #define creates symbolic constants before compilation.
Syntax
#define NAME valueExample
#include <stdio.h>
#define PI 3.14159
int main()
{
printf("%.5f", PI);
return 0;
}const vs #define
| Feature | const | #define |
|---|---|---|
| Data Type | Yes | No |
| Memory Allocation | Yes | No |
| Type Checking | Yes | No |
| Compiler Support | Better | Limited |
| Debugging | Easier | Harder |
For most modern C programs, const is generally preferred when an actual typed constant is needed.
What are Keywords?
Keywords are reserved words that have predefined meanings in the C language.
You cannot use them as variable names, function names, or identifiers.
Example
Wrong
int if = 10;Correct
int marks = 10;Common C Keywords
The classic C language includes these reserved keywords:
auto
break
case
char
const
continue
default
do
double
else
enum
extern
float
for
goto
if
int
long
register
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
whileCategories of Keywords
Data Types
- int
- char
- float
- double
- void
Decision Making
- if
- else
- switch
- case
Loops
- for
- while
- do
Storage Classes
- auto
- register
- static
- extern
Others
- return
- break
- continue
- sizeof
- typedef
- const
- volatile
Input and Output Functions
Programs become useful when they can communicate with users.
The Standard Input/Output library (stdio.h) provides functions for reading input and displaying output.
printf()
The printf() function displays output on the screen.
Syntax
printf("message");Example
#include <stdio.h>
int main()
{
printf("Welcome to C Programming!");
return 0;
}Output
Welcome to C Programming!Printing Multiple Values
#include <stdio.h>
int main()
{
int age = 20;
printf("Age = %d", age);
return 0;
}Output
Age = 20scanf()
The scanf() function reads user input.
Syntax
scanf("format", &variable);Example
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("Your age is %d", age);
return 0;
}Sample Output
Enter your age: 22
Your age is 22Why is '&' Used in scanf()?
The & (address-of operator) passes the memory address of a variable to scanf() so it can store the user's input in that variable.
Example
scanf("%d", &age);Without &, the program will not correctly store the value for most basic variable types.
Reading Multiple Inputs
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Sum = %d", a + b);
return 0;
}getchar()
Reads a single character from the keyboard.
Example
#include <stdio.h>
int main()
{
char letter;
printf("Enter a character: ");
letter = getchar();
printf("You entered: ");
putchar(letter);
return 0;
}putchar()
Displays a single character.
Example
putchar('A');Output
Aputs()
Displays a string followed by a newline.
Example
#include <stdio.h>
int main()
{
puts("Welcome to ZAREEN TECH WAVE");
return 0;
}Output
Welcome to ZAREEN TECH WAVEA Note About gets()
Older books often show gets() for reading strings. Do not use it in new programs.
gets() was removed from the C standard because it cannot prevent buffer overflow, making it unsafe.
Use fgets() instead.
Example
char name[50];
fgets(name, sizeof(name), stdin);Complete Input & Output Program
#include <stdio.h>
int main()
{
int age;
float salary;
char grade;
printf("Enter Age: ");
scanf("%d", &age);
printf("Enter Salary: ");
scanf("%f", &salary);
printf("Enter Grade: ");
scanf(" %c", &grade);
printf("\n----- Student Information -----\n");
printf("Age : %d\n", age);
printf("Salary : %.2f\n", salary);
printf("Grade : %c\n", grade);
return 0;
}Sample Output
Enter Age: 22
Enter Salary: 25000.50
Enter Grade: A
----- Student Information -----
Age : 22
Salary : 25000.50
Grade : ACommon Beginner Mistakes
Forgetting '&' with scanf()
Wrong
scanf("%d", age);Correct
scanf("%d", &age);Using gets()
Avoid
gets(name);Use
fgets(name, sizeof(name), stdin);Using Keywords as Variable Names
Wrong
int return;Correct
int totalMarks;Trying to Change a Constant
Wrong
const int MAX = 100;
MAX = 200;Constants declared with const cannot be reassigned.
Best Practices
- Use
constfor values that should never change. - Prefer meaningful constant names such as
MAX_STUDENTSorPI. - Use
fgets()instead of deprecatedgets(). - Always validate user input where practical.
- Include
<stdio.h>when using standard input/output functions. - Use clear prompts before calling
scanf().
Practice Exercises
Exercise 1
Create a constant named PI and print its value.
Exercise 2
Write a program to input your age and display it.
Exercise 3
Read two integers from the user and print their sum.
Exercise 4
Read a character using getchar() and display it using putchar().
Exercise 5
Create a program that uses both const and #define in the same source file.
Quick Quiz
1. What is a constant in C?
Answer: A value that cannot be changed during program execution.
2. Which keyword creates a constant variable?
Answer: const
3. Which function displays output?
Answer: printf()
4. Which function reads formatted input?
Answer: scanf()
5. Which function is recommended instead of gets()?
Answer: fgets()
Summary
Congratulations! You have completed Part 2 of Day 2.
In this lesson, you learned:
- What constants are
- Types of constants
constkeyword#definesymbolic constants- C reserved keywords
- Standard input and output functions
printf()scanf()getchar()putchar()puts()- Why
fgets()is safer thangets() - Common mistakes and coding best practices
In Part 3, you'll learn Format Specifiers, Escape Sequences, practical coding examples, exercises, To complete Day 2 of your C Programming course.
C Programming – Day 2 (Part 3)
Learn Format Specifiers & Escape Sequences in C
Welcome to the final part of Day 2 in our C Programming Full Course 2026. In the previous lessons, you learned about variables, data types, constants, keywords, and input/output functions. Now, you'll explore Format Specifiers and Escape Sequences, which are essential for displaying and formatting data correctly in C programs.
By the end of this lesson, you will learn:
- What Format Specifiers are
- Common Format Specifiers in C
- Escape Sequences
- Practical Programming Examples
- Common Beginner Mistakes
- Practice Exercises
- Quiz
- Conclusion
- Frequently Asked Questions (FAQs)
What are Format Specifiers?
Format Specifiers are special symbols used with functions like printf() and scanf() to tell the compiler what type of data should be displayed or read.
Every data type has its own format specifier.
Example:
int age = 20;
printf("%d", age);Output
20Here, %d tells printf() to display an integer value.
Why are Format Specifiers Important?
Format specifiers help the compiler:
- Display the correct data type
- Read user input correctly
- Format output professionally
- Prevent unexpected results
Common Format Specifiers in C
| Specifier | Data Type | Example |
|---|---|---|
%d | int | 25 |
%i | int | 25 |
%c | char | A |
%f | float | 12.50 |
%lf | double | 3.141592 |
%s | String | Hello |
%u | unsigned int | 250 |
%x | Hexadecimal | FF |
%o | Octal | 75 |
%p | Memory Address | 0x7ffc... |
Integer Format Specifier
#include <stdio.h>
int main()
{
int number = 100;
printf("%d", number);
return 0;
}Output
100Character Format Specifier
char grade = 'A';
printf("%c", grade);Output
AFloat Format Specifier
float price = 250.75;
printf("%f", price);Output
250.750000Controlling Decimal Places
float pi = 3.14159265;
printf("%.2f", pi);Output
3.14More examples:
printf("%.1f", pi);
printf("%.3f", pi);
printf("%.4f", pi);Output
3.1
3.142
3.1416Double Format Specifier
double salary = 25000.56789;
printf("%.2lf", salary);Output
25000.57String Format Specifier
char name[] = "ZAREEN TECH WAVE";
printf("%s", name);Output
ZAREEN TECH WAVEReading Data with scanf()
int age;
scanf("%d", &age);Other examples
scanf("%f", &price);
scanf("%lf", &salary);
scanf("%c", &grade);
scanf("%s", name);What are Escape Sequences?
Escape sequences are special character combinations beginning with a backslash (\). They perform special formatting tasks.
Common Escape Sequences
| Escape Sequence | Meaning |
\n | New Line |
\t | Horizontal Tab |
\\ | Backslash |
\" | Double Quote |
\' | Single Quote |
\r | Carriage Return |
\b | Backspace |
\a | Alert Sound |
New Line (\n)
printf("Hello\nWorld");Output
Hello
WorldTab (\t)
printf("Name\tAge");Output
Name AgePrinting Double Quotes
printf("\"C Programming\"");Output
"C Programming"Printing a Backslash
printf("C:\\Program Files");Output
C:\Program FilesPractical Example 1
#include <stdio.h>
int main()
{
char name[30];
int age;
printf("Enter your name: ");
scanf("%29s", name);
printf("Enter your age: ");
scanf("%d", &age);
printf("\nName : %s", name);
printf("\nAge : %d", age);
return 0;
}Practical Example 2
Student Information
#include <stdio.h>
int main()
{
char name[30];
int roll;
float marks;
printf("Enter Name : ");
scanf("%29s", name);
printf("Enter Roll : ");
scanf("%d", &roll);
printf("Enter Marks : ");
scanf("%f", &marks);
printf("\n------ Student Information ------\n");
printf("Name : %s\n", name);
printf("Roll : %d\n", roll);
printf("Marks : %.2f\n", marks);
return 0;
}Practical Example 3
Area of a Rectangle
#include <stdio.h>
int main()
{
float length, width, area;
printf("Enter Length : ");
scanf("%f", &length);
printf("Enter Width : ");
scanf("%f", &width);
area = length * width;
printf("Area = %.2f", area);
return 0;
}Common Beginner Mistakes
Using the Wrong Format Specifier
Wrong
float price = 25.5;
printf("%d", price);Correct
printf("%f", price);Forgetting '&' in scanf()
Wrong
scanf("%d", age);Correct
scanf("%d", &age);Forgetting \n
Without a new line, output can appear on the same line and become harder to read.
Using %s for Multiple Words
scanf("%s", name);This reads only the first word.
To read an entire line, use fgets().
fgets(name, sizeof(name), stdin);Best Practices
- Match each variable with the correct format specifier.
- Limit
%sinput width to help avoid buffer overflows (for example,%29sfor a 30-character array). - Use
fgets()when reading sentences or names with spaces. - Use
%.2ffor currency values. - Keep your output neatly formatted using
\nand\t.
Practice Exercises
Exercise 1
Create a program that displays your name, age, and city using format specifiers.
Exercise 2
Take two numbers as input and display their sum.
Exercise 3
Display the multiplication table of a number entered by the user.
Exercise 4
Input a student's name, roll number, and marks, then print them in a formatted report.
Exercise 5
Create a simple calculator using +, -, *, and /.
Mini Project
Student Result Card
Create a program that asks the user for:
- Student Name
- Roll Number
- Marks in Three Subjects
Then display:
- Total Marks
- Average Marks
- Percentage
Use proper formatting with:
%d%f%s\n\t
Quick Quiz
1. Which format specifier is used for integers?
Answer: %d
2. Which format specifier is used for characters?
Answer: %c
3. Which format specifier is used for strings?
Answer: %s
4. What does \n do?
Answer: Moves the cursor to a new line.
5. Which escape sequence inserts a horizontal tab?
Answer: \t
6. Which function is used to display output?
Answer: printf()
7. Which function is used to read formatted input?
Answer: scanf()
8. Which function is recommended for reading a full line of text?
Answer: fgets()
Day 2 Summary
Congratulations! 🎉
You have successfully completed Day 2 of the C Programming Full Course.
Today you learned:
- Variables
- Data Types
- Variable Naming Rules
- Constants
constand#define- C Keywords
- Input and Output Functions
printf()scanf()getchar()putchar()puts()- Format Specifiers
- Escape Sequences
- Practical Coding Examples
- Common Mistakes
- Best Practices
These concepts form the foundation for writing almost every C program.
Conclusion
Excellent work on completing Day 2 of your C Programming journey!
You now understand how to store data using variables, choose the correct data types, define constants, use C keywords appropriately, accept user input, display formatted output, and apply format specifiers and escape sequences to create clear and professional programs.
Take time to practice each example and modify the programs on your own. The more you experiment with code, the faster you'll build confidence and improve your programming skills.
In Day 3, we'll explore Operators in C, including Arithmetic, Relational, Logical, Assignment, Increment/Decrement, Bitwise, and Conditional Operators. These operators are the building blocks for performing calculations, making decisions, and controlling program behavior.
Keep coding, keep practicing, and continue your learning journey with the C Programming Full Course!
Frequently Asked Questions (FAQ)
What is a variable in C?
What are the rules for naming variables in C?
What are data types in C?
What is the difference between int and float?
int stores whole numbers, while float stores decimal numbers with single precision.What is the sizeof operator in C?
sizeof operator returns the amount of memory occupied by a data type or variable in bytes.What is a constant in C?
What is the difference between const and #define?
const creates a typed constant managed by the compiler, while #define creates a symbolic constant using the preprocessor.What is a variable in C?
What are the rules for naming variables in C?
What are keywords in C?
What is printf() used for?
printf() displays formatted output to the screen.Why is '&' used with scanf()?
&) passes the memory address of a variable so that scanf() can store the user's input.What is a format specifier in C?
printf() and scanf() to display or read specific data types.What does %d mean in C?
%d is the format specifier used for integer (int) values.What are escape sequences?
\n for a new line and \t for a horizontal tab.Which format specifier is used for float?
%f is used for float values, while %lf is used with scanf() for reading double values.

.png)
Post a Comment
Thank You For Visit My Website.
I Will Contact As Soon As Possible.