Chapter 2: Basic Elements of C Programming
In the previous chapter, you learned about the introduction and basic structure of the C language. In this chapter, you will learn about the basic elements used to write C programs, including variables, constants, data types, input, output, format specifiers, and escape sequences.
2.1 Character Set in C
A character set is a collection of characters that can be used while writing a C program.
The C character set includes:
1. Alphabets
A to Z a to z
2. Digits
0 1 2 3 4 5 6 7 8 9
3. Special Characters
Some commonly used special characters are:
+ - * / % = < > ( ) { } [ ] ; , .
2.2 Variables in C
A variable is a named location used to store data in memory. The value stored in a variable can change during program execution.
Syntax
data_type variable_name;
Example
int age;
Here:
int → Data type
age → Variable name
We can also assign a value to a variable:
int age = 18;
2.3 Rules for Naming Variables
A variable name must follow certain rules:
It can contain letters, digits, and underscore _.
It should not start with a digit.
Spaces are not allowed.
Special characters are generally not allowed.
C keywords cannot be used as variable names.
Variable names are case-sensitive.
Valid Variable Names
int age; int student_marks; int number1; float percentage;
Invalid Variable Names
1number student marks int total-marks
2.4 Constants in C
A constant is a fixed value that does not change during program execution.
Examples
10 25 3.14 'A' "Hello"
Types of Constants
1. Integer Constants
Integer constants contain whole numbers.
10 100 -25 0
2. Floating-Point Constants
These constants contain decimal values.
3.14 25.5 -10.75
3. Character Constants
A single character written inside single quotation marks.
'A' 'B' '7'
4. String Constants
A group of characters written inside double quotation marks.
"Hello" "GWALNET" "C Programming"
2.5 Data Types in C
A data type specifies the type of data that a variable can store.
Some common data types are:
| Data Type | Description | Example |
|---|---|---|
| int | Stores whole numbers | 25 |
| float | Stores decimal values | 25.5 |
| double | Stores more precise decimal values | 25.12345 |
| char | Stores a single character | 'A' |
| void | Represents no value | void function() |
Example Program
#include <stdio.h> int main() { int age = 18; float marks = 85.5; char grade = 'A'; printf("Age = %d\n", age); printf("Marks = %.1f\n", marks); printf("Grade = %c\n", grade); return 0; }
Output
Age = 18 Marks = 85.5 Grade = A
2.6 Input and Output in C
C programs communicate with users using input and output functions.
Input → Data entered into the program.
Output → Information displayed by the program.
The most commonly used functions are:
| Function | Purpose |
|---|---|
| printf() | Displays output |
| scanf() | Accepts input |
2.7 The printf() Function
The printf() function is used to display text, numbers, and other values on the screen.
Syntax
printf("message");
Example
#include <stdio.h> int main() { printf("Welcome to C Programming"); return 0; }
Output
Welcome to C Programming
2.8 Displaying Variables
Variables can also be displayed using printf().
#include <stdio.h> int main() { int age = 20; printf("Age = %d", age); return 0; }
Output
Age = 20
2.9 Format Specifiers
A format specifier tells C which type of data is being displayed or accepted.
| Data Type | Format Specifier |
|---|---|
| int | %d |
| float | %f |
| double | %lf |
| char | %c |
| String | %s |
Example
#include <stdio.h> int main() { int number = 10; float marks = 95.5; char grade = 'A'; printf("Number = %d\n", number); printf("Marks = %f\n", marks); printf("Grade = %c\n", grade); return 0; }
2.10 The scanf() Function
The scanf() function is used to accept input from the user.
Syntax
scanf("format_specifier", &variable_name);
Example
#include <stdio.h> int main() { int age; printf("Enter your age: "); scanf("%d", &age); printf("Your age is %d", age); return 0; }
If the user enters:
20
The output will be:
Your age is 20
2.11 Why Do We Use & in scanf()?
In many basic cases, the & symbol is placed before a variable name in scanf().
Example:
scanf("%d", &age);
Here, &age provides the location where the entered value should be stored.
2.12 Escape Sequences
An escape sequence is a combination of characters that performs a special task.
Some common escape sequences are:
| Escape Sequence | Meaning |
|---|---|
| \n | New line |
| \t | Horizontal tab |
| \\ | Backslash |
| \" | Double quotation mark |
| \' | Single quotation mark |
Example
#include <stdio.h> int main() { printf("Name: Rahul\n"); printf("Class:\t10\n"); printf("Subject: C Programming"); return 0; }
Output
Name: Rahul Class: 10 Subject: C Programming
2.13 Assignment and Initialization
Declaration
Creating a variable with a data type:
int marks;
Initialization
Giving an initial value to a variable:
int marks = 90;
Assignment
Changing or assigning a value:
marks = 95;
Complete Example
#include <stdio.h> int main() { int marks; marks = 90; printf("Marks = %d", marks); return 0; }
2.14 Program to Take Student Information
#include <stdio.h> int main() { int age; float marks; char grade; printf("Enter age: "); scanf("%d", &age); printf("Enter marks: "); scanf("%f", &marks); printf("Enter grade: "); scanf(" %c", &grade); printf("\n--- Student Information ---\n"); printf("Age = %d\n", age); printf("Marks = %.2f\n", marks); printf("Grade = %c\n", grade); return 0; }
2.15 Important Points to Remember
A variable stores data in memory.
Every variable must have a data type.
Constants represent fixed values.
printf() is used to display output.
scanf() is used to accept input.
%d is used with integers.
%f is commonly used with float.
%c is used with characters.
%s is used with strings.
\n moves the cursor to a new line.
C is case-sensitive.
Chapter Summary
In this chapter, you learned about the basic elements of C programming. You studied variables, constants, data types, input and output functions, format specifiers, and escape sequences.
These concepts are essential because they help a program store, process, accept, and display data.
The next chapter can cover Operators and Expressions in C Language.
Practice Questions
Very Short Answer Questions
What is a variable?
What is a constant?
Which function is used to display output?
Which function is used to take input?
What is the format specifier for an integer?
Which escape sequence is used for a new line?
What does %c represent?
Is C a case-sensitive language?
Short Answer Questions
Explain variables and constants with examples.
What are data types? Explain the basic data types in C.
Differentiate between printf() and scanf().
What are format specifiers? Give examples.
Explain any four escape sequences.
Write the rules for naming variables in C.
Programming Questions
1. Write a program to display your name and class.
#include <stdio.h> int main() { printf("Name: Rahul\n"); printf("Class: 10"); return 0; }
2. Write a program to accept and display an integer.
#include <stdio.h> int main() { int number; printf("Enter a number: "); scanf("%d", &number); printf("You entered: %d", number); return 0; }
3. Write a program to accept two numbers and display them.
#include <stdio.h> int main() { int a, b; printf("Enter first number: "); scanf("%d", &a); printf("Enter second number: "); scanf("%d", &b); printf("First Number = %d\n", a); printf("Second Number = %d", b); return 0; }