Class 6 Exam  >  Class 6 Notes  >  C Programming for Beginners  >  C Data Types & Constants

C Data Types & Constants | C Programming for Beginners - Class 6 PDF Download

Data Types in C

Data types specify the type of data a variable can store. C supports several built-in data types categorized as basic, derived, and user-defined types.

a. Basic Data TypesC Data Types & Constants | C Programming for Beginners - Class 6

Signed vs Unsigned:

  • Signed types store both positive and negative values.

  • Unsigned types store only non-negative values, doubling the positive range.

Floating-Point Representation:

  • IEEE-754 Standard: Floating-point numbers (float, double, long double) are stored using sign, exponent, and mantissa.

b. Derived Data Types

Derived from basic types:

  1. Arrays – Collection of similar data types (int arr[5]).

  2. Pointers – Stores the memory address of another variable (int *ptr).

  3. Structures (struct) – Groups different types into one entity.

  4. Unions (union) – Similar to struct, but shares memory.

c. Type Modifiers

Modifiers change the storage size and range of data types:

  • Signed (signed) – Default for int, allows negative values.

  • Unsigned (unsigned) – Only non-negative values.

  • Short (short) – Uses less memory (short int).

  • Long (long) – Expands storage (long int, long double).

d. Type Qualifiers

Qualifiers modify variable behavior:

  • const – Declares a constant (const int x = 10;).

  • volatile – Prevents compiler optimizations (volatile int flag;).

  • restrict – Used for pointer optimization.

Question for C Data Types & Constants
Try yourself:
What do signed data types in C store?
View Solution

Constants in C

Constants are fixed values that cannot be modified during execution.

Types of Constants

  1. Integer Constants: Whole numbers (10, -5, 0xA in hex, 010 in octal).

  2. Floating-point Constants: Decimal numbers (3.14, 2.5e3).

  3. Character Constants: Single characters ('A', '1').

  4. String Constants: Sequence of characters ("Hello").

  5. Symbolic Constants: Defined using #define MAX 100.

When you don't want others (or yourself) to override existing variable values, use the const keyword (this will declare the variable as "constant", which means unchangeable and read-only):

Example

const int myNum = 15;  // myNum will always be 15

myNum = 10;  // error: assignment of read-only variable 'myNum'

You should always declare the variable as constant when you have values that are unlikely to change:
Example

const int minutesPerHour = 60;

const float PI = 3.14;

When you declare a constant variable, it must be assigned with a value:

Example
Like this:

const int minutesPerHour = 60;

This however, will not work:

const int minutesPerHour;

minutesPerHour = 60; // error

Question for C Data Types & Constants
Try yourself:
What type of constant is represented by the value 3.14 in C?
View Solution

Good Practice

Another thing about constant variables, is that it is considered good practice to declare them with uppercase. It is not required, but useful for code readability and common for C programmers:

Example

const int BIRTHYEAR = 1980;

Example Program Using Constants & Data Types

#include <stdio.h>

#define PI 3.14159 // Macro constant

int main() {
const int radius = 5; // Constant variable
float area = PI * radius * radius;

printf("Area of Circle: %.2f\n", area);
return 0;
}

Explanation

  • #define PI 3.14159 → Preprocessor macro (no memory usage).

  • const int radius = 5; → Read-only variable stored in memory.

  • Computes circle area using the formula πr².

Question for C Data Types & Constants
Try yourself:
What is the recommended practice for declaring constant variables according to the text?
View Solution

The document C Data Types & Constants | C Programming for Beginners - Class 6 is a part of the Class 6 Course C Programming for Beginners.
All you need of Class 6 at this link: Class 6
10 videos|13 docs|15 tests

FAQs on C Data Types & Constants - C Programming for Beginners - Class 6

1. What are constants in programming and why are they important?
Ans.Constants are fixed values that cannot be altered during the execution of a program. They are important because they provide meaningful names to values, making the code easier to read and maintain. Using constants helps prevent errors caused by accidentally changing values that should remain constant throughout the program.
2. How do data types differ from constants in programming?
Ans.Data types are classifications that specify which type of value a variable can hold (such as integers, floats, or strings), while constants are specific values of those data types that remain unchanged. Understanding data types is crucial for defining constants, as each constant must correspond to a specific data type to function correctly in a program.
3. Can you give examples of common data types used in programming?
Ans.Common data types include: - Integer: Whole numbers (e.g., -1, 0, 5) - Float: Numbers with decimal points (e.g., 3.14, -0.001) - String: A sequence of characters (e.g., "Hello, World!") - Boolean: Represents true or false values (e.g., true, false) These data types help define the kind of data that can be stored and manipulated in a program.
4. What is the difference between a constant and a variable?
Ans.A variable is a storage location in memory that can hold different values throughout a program's execution, while a constant is a fixed value that cannot be changed once it is assigned. Using constants enhances code clarity and reliability, as they prevent accidental modifications that could lead to bugs.
5. How can constants improve code readability and maintainability?
Ans.Constants improve code readability by allowing developers to use descriptive names instead of hard-coded values, making it clear what each value represents. This practice also enhances maintainability; if a constant value needs to change, the developer only updates it in one place, reducing the risk of errors and ensuring consistent updates throughout the codebase.
Related Searches

video lectures

,

practice quizzes

,

C Data Types & Constants | C Programming for Beginners - Class 6

,

Objective type Questions

,

Extra Questions

,

Previous Year Questions with Solutions

,

pdf

,

Summary

,

mock tests for examination

,

ppt

,

Viva Questions

,

C Data Types & Constants | C Programming for Beginners - Class 6

,

past year papers

,

C Data Types & Constants | C Programming for Beginners - Class 6

,

Exam

,

MCQs

,

Free

,

shortcuts and tricks

,

Important questions

,

Sample Paper

,

study material

,

Semester Notes

;