0% found this document useful (0 votes)
385 views

C Constants 3

C constants include fixed values like integers, characters, strings, and floating point numbers that do not change during program execution. Constants can be declared with the const keyword followed by the data type, and attempting to modify a constant will result in a compiler error. Format specifiers starting with % are used with printf() to output the values of different data types to the console.

Uploaded by

Carab
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
385 views

C Constants 3

C constants include fixed values like integers, characters, strings, and floating point numbers that do not change during program execution. Constants can be declared with the const keyword followed by the data type, and attempting to modify a constant will result in a compiler error. Format specifiers starting with % are used with printf() to output the values of different data types to the console.

Uploaded by

Carab
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

C constants

Lecture three
C constants
C Constants is a most fundamental and essential part
of C programming language. Constants in C are the
fixed values that are used in a program, and its value
remains the same during the entire execution of the
program.
Constants are also called literals.
Constants can be any of the data types.
A constant is a value or variable that can't be changed
in the program, for example: 10, 20, 'a', 3.4,
"c programming" etc.
C constants
Constants can be of any of the basic data types like an
integer constant, a floating constant, a character
constant, or a string literal. There are enumeration
constants as well.
Syntax
const type constant_name;
const keyword defines a constant in C.
Example of constant
#include<stdio.h>
int main()
{
 const float PI=3.14;
 printf("The value of PI is: %f",PI);
 return 0;
}
Cont…
If you try to change the value of PI, it will render
compile time error.
#include<stdio.h>    
int main(){    
const float PI=3.14;     
PI=4.5;    
printf("The value of PI is: %f",PI);    
    return 0;  
}     
Integer constant
It's referring to a sequence of digits. Integers are of three
types:
Decimal Integer
Octal Integer
Hexadecimal Integer
Example:
15, -265, 0, 99818, +25, 045, 0X6
Real constant
Real constant
The numbers containing fractional parts like 99.25 are called
real or floating points constant.
Single character constant
It simply contains a single character enclosed within ' and ' (a
pair of single quote). It is to be noted that the character '8' is not
the same as 8. Character constants have a specific set of integer
values known as ASCII values (American Standard Code for
Information Interchange).

Example:
'X', '5', ';'
String constant
These are a sequence of characters enclosed in double
quotes, and they may include letters, digits, special
characters, and blank spaces. It is again to be noted
that "G" and 'G' are different - because "G" represents a
string as it is enclosed within a pair of double quotes
whereas 'G' represents a single character.
Example:
"Hello!", "2015", "2+1"
Backslash character constant
C supports some character constants having a
backslash in front of it. The lists of backslash
characters have a specific meaning which is known to
the compiler. They are also termed as "Escape
Sequence".
For Example:
\t is used to give a tab
\n is used to give a new line
Format specifiers
Format specifiers can be defined as the operators which
are used in association with printf() function for printing
the data that is referred by any object or any variable.
When a value is stored in a particular variable, then you
cannot print the value stored in the variable
straightforwardly without using the format specifiers. You
can retrieve the data that are stored in the variables and
can print them onto the console screen by implementing
these format specifiers in a printf() function.
Format specifiers
Format specifiers start with a percentage % operator
and followed by a special character for identifying the
type of the data.
There are mostly six types of format specifiers that are
available in C.
List of format specifiers
Format specifier Description
%d Integer Format Specifier
%f Float Format Specifier
%c Character Format Specifier
%s String Format Specifier
%u Unsigned Integer Format Specifier
%ld Long Int Format Specifier
Integer format specifiers
The %d format specifier is implemented for
representing integer values. This is used with printf()
function for printing the integer value stored in the
variable.

Syntax:
printf("%d",<variable name>);
Float format specifiers
The %f format specifier is implemented for representing
fractional values. This is implemented within printf()
function for printing the fractional or floating value stored
in the variable. Whenever you need to print any fractional
or floating data, you have to use %f format specifier.
Syntax:
printf("%f", <variable name>);
Character format specifiers
The %c format specifier is implemented for
representing characters. This is used with printf()
function for printing the character stored in a variable.
When you want to print a character data, you should
incorporate the %c format specifier.

Syntax:
printf("%c",<variable name>);
String format specifiers
The %s format specifier is implemented for
representing strings. This is used in printf() function
for printing a string stored in the character array
variable. When you have to print a string, you should
implement the %sformat specifier.
Syntax:
printf("%s",<variable name>);
Unsigned integer format specifiers
The %u format specifier is implemented for fetching
values from the address of a variable having unsigned
decimal integer stored in the memory. This is used
within printf() function for printing the unsigned
integer variable.
Syntax:
printf("%u",<variable name>);
Long integer format specifiers
The %ld format specifier is implemented for
representing long integer values. This is implemented
with printf() function for printing the long integer
value stored in the variable.

Syntax:
printf("%ld",<variable name>);

You might also like