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

As explained in the Variables chapter, a variable in C must be a specified data type, and you must use a format specifier inside the printf() function to display it:

Example

// Create variables

int myNum = 5;             // Integer (whole number)

float myFloatNum = 5.99;   // Floating point number

char myLetter = 'D';       // Character


// Print variables

printf("%d\n", myNum);

printf("%f\n", myFloatNum);

printf("%c\n", myLetter);

Basic Data Types


The data type specifies the size and type of information the variable will store.
In this tutorial, we will focus on the most basic ones:

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

Constants

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;

Notes On Constants

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

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;

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
Related Searches

video lectures

,

Exam

,

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

,

Important questions

,

study material

,

Viva Questions

,

Objective type Questions

,

past year papers

,

Semester Notes

,

MCQs

,

mock tests for examination

,

Previous Year Questions with Solutions

,

Sample Paper

,

pdf

,

Summary

,

Free

,

ppt

,

shortcuts and tricks

,

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

,

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

,

practice quizzes

,

Extra Questions

;