Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: C Programming. No Comments on Constants in C Programming

This article provides a comprehensive overview of constants in C programming language along with relevant examples.

 

Constants are fixed values, which do not change during the execution of a program. C supports several types of constants, which are as shown below:

 

c-constants

 

Watch this video to learn about constants in C programming language:

Integer Constants

 

An integer constant refers to a sequence of digits. Generally in programs, the number systems used are: decimal, octal and hexadecimal. Decimal integers consists of umbers from 0 to 9, preceded by an optional + or – sign.

 

Some valid examples of decimal integer constants are: 133, +45, -15, 0, 342332 etc. Embedded spaces and non-digit characters are cannot be used between digits.

 

An octal integer consists of numbers from 0 to 7. Octal integer numbers are always preceded by a zero. For example if we have to represent 35 in octal system, we must write it as 035 in the program. Some valid examples of octal integer constants are: 035, 02, 0435, +025 etc.

 

An hexadecimal integer consists of numbers from 0 to 9 and 10, 11, 12, 13, 14 and 15 are represented as A, B, C, D, E and F. Hexadecimal numbers are always preceded by 0x or 0X. Some valid examples of hexadecimal integer constants are: 0x54, 0X23, +0x1F, -0X56 etc.

 

Real Constants

 

Integer numbers are not sufficient to represent quantities that vary continuously, such as distances, height, prices etc. These quantities are represented by numbers containing fractional parts like 25.234. Such numbers are called as real or floating point constants. Some valid examples of real constants are: 0.067, -12.5, +4.67, .87, 121. Etc.

 

A real number may also be expressed in exponential (scientific) notation. For example, 45.2344 can be written 0.452344e2. The exponential notation is: mantissa e exponent. The mantissa is either a real number expressed in decimal notation or an integer with an optional + or – sign. The exponent is an integer number with an optional + or – sign. Some valid examples of real constants in exponential notation are: 0.34e2, 13e-2, -1.23e-1 etc.

 

Single Character Constants

 

A single character constant or character constant contains a single character enclosed in between single quotes. Some valid examples of character constants are: ‘f’, ‘A’, ‘/’, ’;’, ‘ ‘, ‘4’. The character constant ‘4’ is not equal to the number 4.

 

Every character constant will have an associated integer value known as ASCII code. Since each character constant has an associated integer value, we can perform arithmetic operations on them.

 

String Literals or String Constants

 

A sequence of characters that are enclosed between double quotes is known as a string literal or string constant. The characters in a string literal can be either letters, digits or special symbols or white spaces.

 

The string literal does not have an associated integer value like the character constants. Some valid examples of string constants are: “hai”, “hEllO”, “hi5”, “Wel come” etc.

 

Escape Sequences

 

C supports some special backslash character constants that are used in output functions like printf(). For example to move the cursor from the current line to next line, there is no standard way to achieve it. So, for such special cases, C provides escape sequences.

 

In this case \n is used to move the cursor to next new line. Even though the escape sequences consist of a backslash (\) and a character or symbol, it is treated as a single character. C supports the following escape sequences:

 

Constant Meaning
\a Alert
\b Backspace
\f Form feed
\n New line
\r Carriage Return
\t Horizontal tab
\v Vertical tab
\’ Single quote
\” Double quote
\? Question mark
\\ Backslash
\0 Null

 

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Leave a Reply

Your email address will not be published. Required fields are marked *