Beginner-Level Questions and Answers in C Programming (Variables, Operators, Arrays)
1. What is a variable in C?
A variable in C is a named memory location used to store a value. The value can be changed during
program execution.
Example:
int age = 20; // 'age' is a variable of type int storing value 20
2. What are the types of variables in C?
- int: for integers (e.g., 5, -3)
- float: for real numbers (e.g., 3.14)
- char: for single characters (e.g., 'A')
- double: for large real numbers
- void: for functions that return nothing
3. What is an operator?
An operator is a symbol that performs an operation on variables or values.
Types of Operators:
- Arithmetic: +, -, *, /, %
- Relational: ==, !=, <, >, <=, >=
- Logical: &&, ||, !
- Assignment: =, +=, -=, etc.
4. Write a program using arithmetic operators:
#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a / b);
printf("Modulus: %d\n", a % b);
return 0;
5. What is a data table or array?
An array is a collection of variables of the same type stored in a contiguous memory location.
Example:
int numbers[5] = {10, 20, 30, 40, 50};
6. How to print all elements of an array?
#include <stdio.h>
int main() {
int i;
int arr[5] = {1, 2, 3, 4, 5};
for(i = 0; i < 5; i++) {
printf("Element %d = %d\n", i, arr[i]);
return 0;
7. Program to find sum of all array elements:
#include <stdio.h>
int main() {
int i, sum = 0;
int arr[5] = {10, 20, 30, 40, 50};
for(i = 0; i < 5; i++) {
sum += arr[i];
printf("Sum = %d\n", sum);
return 0;
8. What is the difference between int and float?
| Data Type | Use Case | Example |
|-----------|-----------------|------------|
| int | Whole numbers | 10, -5 |
| float | Decimal numbers | 3.14, -0.9 |