C is one of the most popular programming languages and is often called the foundation of modern programming. Developed by Dennis Ritchie in 1972, C is widely used for building operating systems, embedded systems, databases, compilers, and high-performance applications.
Learning C helps you understand programming fundamentals such as variables, data types, operators, memory management, and functions. Mastering these concepts makes it easier to learn advanced programming languages like C++, Java, Python, and C#. C Basics, Variables & Data Types practice questions with solutions help to understand the concepts.
In this chapter, you’ll practice beginner-friendly C programming questions focused on variables and data types. Every question includes a complete C solution, sample output, explanation, and key concepts, making it ideal for beginners, students, coding interviews, and programming practice.
1. C Program to Print “Hello, World!”
Problem Statement
Write a C program to print “Hello, World!” on the screen.
C Solution
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
Sample Output
Hello, World!
Explanation
#include <stdio.h>includes the Standard Input Output library.main()is the starting point of every C program.printf()displays text on the screen.return 0;indicates that the program executed successfully.
Concepts Covered
- First C Program
- Header Files
- printf()
- main() Function
2. C Program to Print Your Name
Problem Statement
Write a C program to print your name.
C Solution
#include <stdio.h>
int main()
{
printf("My name is Rahul.");
return 0;
}
Sample Output
My name is Rahul.
Explanation
The printf() function prints the text exactly as written inside double quotation marks.
Concepts Covered
- Output Statements
- printf()
- Basic Program Structure
3. C Program to Declare and Print an Integer Variable
Problem Statement
Write a C program to declare an integer variable and display its value.
C Solution
#include <stdio.h>
int main()
{
int age = 21;
printf("Age = %d", age);
return 0;
}
Sample Output
Age = 21
Explanation
intdeclares an integer variable.%dis the format specifier used to print integer values.- The variable
agestores the value 21.
Concepts Covered
- Variables
- Integer Data Type
- Format Specifier
%d
4. C Program to Declare and Print a Float Variable
Problem Statement
Write a C program to declare a floating-point variable and display its value.
C Solution
#include <stdio.h>
int main()
{
float percentage = 87.5;
printf("Percentage = %.1f", percentage);
return 0;
}
Sample Output
Percentage = 87.5
Explanation
floatis used to store decimal numbers.percentageis a floating-point variable.%.1fdisplays the value with one digit after the decimal point.
Concepts Covered
- Float Data Type
- Floating-Point Variables
- Format Specifier
%f
5. C Program to Declare and Print a Character Variable
Problem Statement
Write a C program to declare a character variable and display its value.
C Solution
#include <stdio.h>
int main()
{
char grade = 'A';
printf("Grade = %c", grade);
return 0;
}
Sample Output
Grade = A
Explanation
charis used to store a single character.- Character values are enclosed in single quotation marks (
' '). %cis the format specifier used to print character values.
Concepts Covered
- Character Data Type
- Character Variables
- Format Specifier
%c
6. C Program to Declare and Print Multiple Variables
Problem Statement
Write a C program to declare multiple variables of different data types and display their values.
C Solution
#include <stdio.h>
int main()
{
int age = 21;
float height = 5.9;
char grade = 'A';
printf("Age = %d\n", age);
printf("Height = %.1f\n", height);
printf("Grade = %c", grade);
return 0;
}
Sample Output
Age = 21
Height = 5.9
Grade = A
Explanation
The program demonstrates how different data types can be declared and printed in the same program using appropriate format specifiers.
Concepts Covered
- Multiple Variables
- Integer
- Float
- Character
- printf()
7. C Program to Find the Size of Basic Data Types Using sizeof()
Problem Statement
Write a C program to display the size of basic data types using the sizeof() operator.
C Solution
#include <stdio.h>
int main()
{
printf("Size of int = %lu bytes\n", sizeof(int));
printf("Size of float = %lu bytes\n", sizeof(float));
printf("Size of char = %lu bytes\n", sizeof(char));
printf("Size of double = %lu bytes", sizeof(double));
return 0;
}
Sample Output
Size of int = 4 bytes
Size of float = 4 bytes
Size of char = 1 bytes
Size of double = 8 bytes
Note: Output may vary depending on your compiler and operating system.
Explanation
The sizeof() operator returns the amount of memory (in bytes) occupied by a data type or variable.
Concepts Covered
- sizeof()
- Memory Allocation
- Data Types
8. C Program to Swap Two Numbers Using a Third Variable
Problem Statement
Write a C program to swap two numbers using a temporary variable.
C Solution
#include <stdio.h>
int main()
{
int first = 10;
int second = 20;
int temp;
temp = first;
first = second;
second = temp;
printf("First Number = %d\n", first);
printf("Second Number = %d", second);
return 0;
}
Sample Output
First Number = 20
Second Number = 10
Explanation
A temporary variable stores the value of the first variable before the values are exchanged.
Concepts Covered
- Variables
- Swapping
- Temporary Variable
9. C Program to Swap Two Numbers Without Using a Third Variable
Problem Statement
Write a C program to swap two numbers without using a temporary variable.
C Solution
#include <stdio.h>
int main()
{
int first = 10;
int second = 20;
first = first + second;
second = first - second;
first = first - second;
printf("First Number = %d\n", first);
printf("Second Number = %d", second);
return 0;
}
Sample Output
First Number = 20
Second Number = 10
Explanation
The program swaps values using arithmetic operations instead of a temporary variable.
Concepts Covered
- Swapping
- Arithmetic Operations
- Variables
10. C Program to Display the ASCII Value of a Character
Problem Statement
Write a C program to display the ASCII value of a character.
C Solution
#include <stdio.h>
int main()
{
char character = 'A';
printf("ASCII Value = %d", character);
return 0;
}
Sample Output
ASCII Value = 65
Explanation
Every character has a unique ASCII value. When a character is printed using the %d format specifier, C automatically displays its corresponding ASCII code.
Concepts Covered
- ASCII Values
- Character Data Type
- Type Conversion
11. C Program to Read and Display an Integer Value
Problem Statement
Write a C program to accept an integer from the user and display it.
C Solution
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
printf("You entered: %d", number);
return 0;
}
Sample Output
Enter an integer: 25
You entered: 25
Explanation
The scanf() function reads input from the keyboard, while printf() displays the entered value.
Concepts Covered
- scanf()
- User Input
- Integer Variables
12. C Program to Read and Display a Float Value
Problem Statement
Write a C program to accept a floating-point number from the user and display it.
C Solution
#include <stdio.h>
int main()
{
float salary;
printf("Enter your salary: ");
scanf("%f", &salary);
printf("Salary = %.2f", salary);
return 0;
}
Sample Output
Enter your salary: 35000.50
Salary = 35000.50
Explanation
The %f format specifier is used with both scanf() and printf() to work with floating-point values.
Concepts Covered
- Float Variables
- scanf()
- User Input
13. C Program to Read and Display a Character
Problem Statement
Write a C program to accept a character from the user and display it.
C Solution
#include <stdio.h>
int main()
{
char grade;
printf("Enter a grade: ");
scanf(" %c", &grade);
printf("Grade = %c", grade);
return 0;
}
Sample Output
Enter a grade: A
Grade = A
Explanation
A space before %c in scanf() ignores any leftover newline characters from previous input.
Concepts Covered
- Character Input
- scanf()
- Character Variables
14. C Program to Display Values of Different Data Types
Problem Statement
Write a C program to declare variables of different data types and display them together.
C Solution
#include <stdio.h>
int main()
{
int age = 22;
float percentage = 91.4;
char grade = 'A';
printf("Age = %d\n", age);
printf("Percentage = %.1f\n", percentage);
printf("Grade = %c", grade);
return 0;
}
Sample Output
Age = 22
Percentage = 91.4
Grade = A
Explanation
This program demonstrates how different data types can be used together in a single program.
Concepts Covered
- Integer
- Float
- Character
- Multiple Variables
15. C Program to Display Default Format Specifiers
Problem Statement
Write a C program to print different data types using their correct format specifiers.
C Solution
#include <stdio.h>
int main()
{
int number = 100;
float price = 49.99;
char letter = 'C';
printf("Integer : %d\n", number);
printf("Float : %.2f\n", price);
printf("Character : %c\n", letter);
return 0;
}
Sample Output
Integer : 100
Float : 49.99
Character : C
Explanation
Different data types require different format specifiers while displaying output.
Concepts Covered
- Format Specifiers
- printf()
- Data Types
Chapter Summary
In this chapter, you learned the fundamentals of C programming, including variables, data types, format specifiers, user input, and basic input/output functions. You practiced creating variables, printing values, accepting user input using scanf(), displaying ASCII values, swapping variables, and understanding how memory is allocated using sizeof(). These concepts form the foundation for writing efficient C programs and are essential before learning operators, conditional statements, loops, and functions.
Key Takeaways
- C is a procedural programming language widely used for system and application development.
- Variables are used to store data in memory.
- Common data types include
int,float,char, anddouble. printf()displays output, whilescanf()accepts user input.- Every data type has its own format specifier.
sizeof()helps determine the memory occupied by a data type.- Characters are internally stored as ASCII values.
- Swapping variables is a common programming exercise.
- Understanding variables and data types is essential before learning advanced C concepts.
- Strong fundamentals make learning C++, Java, and Python much easier.
Frequently Asked Questions (FAQs)
1. What is the C programming language?
C is a general-purpose, procedural programming language developed by Dennis Ritchie. It is widely used for system programming, embedded systems, and application development.
2. What is a variable in C?
A variable is a named memory location used to store data that can change during program execution.
3. What are the basic data types in C?
The most commonly used data types are:
intfloatchardouble
4. What is the difference between printf() and scanf()?
printf()is used to display output.scanf()is used to accept input from the user.
5. What is a format specifier?
A format specifier tells printf() and scanf() what type of data is being printed or read, such as %d, %f, and %c.
6. What does the sizeof() operator do?
The sizeof() operator returns the amount of memory (in bytes) occupied by a data type or variable.
7. What is an ASCII value?
An ASCII value is the numeric representation of a character used internally by computers.
8. Why should beginners learn C before other programming languages?
Learning C builds a strong understanding of programming fundamentals, memory management, variables, data types, and problem-solving skills, making it easier to learn advanced programming languages later.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
