Grade 7 Exam  >  Grade 7 Notes  >  C Programming for Beginners  >  C Data Types & Constants

C Data Types & Constants - C Programming for Beginners - Grade 7 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 TypesData Types in C

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.

MULTIPLE CHOICE QUESTION
Try yourself: What do signed data types in C store?
A

Only negative values

B

Only positive values

C

Both positive and negative values

D

Non-numeric values

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

MULTIPLE CHOICE QUESTION
Try yourself: What type of constant is represented by the value 3.14 in C?
A

Integer Constants

B

Floating-point Constants

C

Character Constants

D

String Constants

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².

MULTIPLE CHOICE QUESTION

Try yourself: What is the recommended practice for declaring constant variables according to the text?

A

Lowercase

B

Uppercase

C

Mixed case

D

No specific case

The document C Data Types & Constants is a part of the Grade 7 Course C Programming for Beginners.
All you need of Grade 7 at this link: Grade 7

FAQs on C Data Types & Constants

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.
Explore Courses for Grade 7 exam
Get EduRev Notes directly in your Google search
Related Searches
video lectures, pdf , C Data Types & Constants, Important questions, study material, C Data Types & Constants, mock tests for examination, Extra Questions, MCQs, Objective type Questions, Sample Paper, ppt, Viva Questions, past year papers, Semester Notes, Free, practice quizzes, Previous Year Questions with Solutions, Exam, C Data Types & Constants, shortcuts and tricks, Summary;