0% found this document useful (0 votes)
0 views14 pages

C Programming Session 5

The document provides an overview of constants in C programming, detailing their types such as decimal, octal, hexadecimal, character, and string constants, as well as methods to define them using the 'const' keyword and '#define' preprocessor. It also explains literals, tokens, keywords, identifiers, strings, operators, and the Boolean data type in C, including examples for better understanding. Additionally, it discusses the significance of special characters and the use of typedef for creating new data types.

Uploaded by

abhinavdaksha2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views14 pages

C Programming Session 5

The document provides an overview of constants in C programming, detailing their types such as decimal, octal, hexadecimal, character, and string constants, as well as methods to define them using the 'const' keyword and '#define' preprocessor. It also explains literals, tokens, keywords, identifiers, strings, operators, and the Boolean data type in C, including examples for better understanding. Additionally, it discusses the significance of special characters and the use of typedef for creating new data types.

Uploaded by

abhinavdaksha2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Constants in C

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.

There are different types of constants in C programming.

List of Constants in C

Constant Example

Decimal Constant 10, 20, 450 etc.

Real or Floating-point Constant 10.3, 20.2, 450.6 etc.

Octal Constant 021, 033, 046 etc.

Hexadecimal Constant 0x2a, 0x7b, 0xaa etc.

Character Constant 'a', 'b', 'x' etc.

String Constant "c", "c program", "c in javatpoint" etc.

2 ways to define constant


in C
There are two ways to define constant in C programming.

1. const keyword

2. #define preprocessor

1) C const keyword
The const keyword is used to define constant in C programming.
1. const float PI=3.14;

Now, the value of PI variable can't be changed.

1. #include<stdio.h>
2. int main(){
3. const float PI=3.14;
4. printf("The value of PI is: %f",PI);
5. return 0;
6. }

Output:

The value of PI is: 3.140000

If you try to change the the value of PI, it will render compile time error.

1. #include<stdio.h>
2. int main(){
3. const float PI=3.14;
4. PI=4.5;
5. printf("The value of PI is: %f",PI);
6. return 0;
7. }

Output:

Compile Time Error: Cannot modify a const object

2) C #define preprocessor
The #define preprocessor is also used to define constant. We will learn about #define
preprocessor directive later.

What are literals?


Literals are the constant values assigned to the constant variables. We can say that the
literals represent the fixed values that cannot be modified. It also contains memory but
does not have references as variables. For example, const int =10; is a constant integer
expression in which 10 is an integer literal.

Types of literals
There are four types of literals that exist in C programming:

o Integer literal

o Float literal

o Character literal

o String literal

Integer literal

It is a numeric literal that represents only integer type values. It represents the value
neither in fractional nor exponential part.

It can be specified in the following three ways:


Decimal number (base 10)

It is defined by representing the digits between 0 to 9. For example, 45, 67, etc.

Octal number (base 8)

It is defined as a number in which 0 is followed by digits such as 0,1,2,3,4,5,6,7. For


example, 012, 034, 055, etc.

Hexadecimal number (base 16)

It is defined as a number in which 0x or 0X is followed by the hexadecimal digits (i.e.,


digits from 0 to 9, alphabetical characters from (a-z) or (A-Z)).

An integer literal is suffixed by following two sign qualifiers:

L or l: It is a size qualifier that specifies the size of the integer type as long.

U or u: It is a sign qualifier that represents the type of the integer as unsigned. An


unsigned qualifier contains only positive values.

Note: The order of the qualifier is not considered, i.e., both lu and ul are the same.

Let's look at a simple example of integer literal.

1. #include <stdio.h>
2. int main()
3. {
4. const int a=23; // constant integer literal
5. printf("Integer literal : %d", a);
6. return 0;
7. }

Output

Integer literal : 23
Float literal

It is a literal that contains only floating-point values or real numbers. These real
numbers contain the number of parts such as integer part, real part, exponential part,
and fractional part. The floating-point literal must be specified either in decimal or in
exponential form. Let's understand these forms in brief.

Decimal form

The decimal form must contain either decimal point, exponential part, or both. If it does
not contain either of these, then the compiler will throw an error. The decimal notation
can be prefixed either by '+' or '-' symbol that specifies the positive and negative
numbers.

Examples of float literal in decimal form are:

1. 1.2, +9.0, -4.5

Let's see a simple example of float literal in decimal form.

1. #include <stdio.h>
2. int main()
3. {
4. const float a=4.5; // constant float literal
5. const float b=5.6; // constant float literal
6. float sum;
7. sum=a+b;
8. printf("%f", sum);
9. return 0;
10. }
Output

10.100000
Character literal

A character literal contains a single character enclosed within single quotes. If multiple
characters are assigned to the variable, then we need to create a character array. If we
try to store more than one character in a variable, then the warning of a multi-
character character constant will be generated. Let's observe this scenario through
an example.

1. #include <stdio.h>
2. int main()
3. {
4. const char c='ak';
5. printf("%c",c);
6. return 0;
7. }

In the above code, we have used two characters, i.e., 'ak', within single quotes. So, this
statement will generate a warning as shown below.

Warning generated:

1. main.c:6:18: warning: multi-character character constant


2. [-Wmultichar]
3. const char c='ak';
4. main.c:6:18: warning: implicit conversion from 'int' to 'char'
5. changes value from 24939 to 107 [-Wconstant-conversion]
6. const char c='ak';
7. ~ ^~~~
8. 2 warnings generated.
9. ? ./main

Representation of character literal

A character literal can be represented in the following ways:

o It can be represented by specifying a single character within single quotes. For


example, 'a', 'b', etc.
o We can specify the escape sequence character within single quotes to represent
a character literal. For example, '\n', '\a', '\b'.

o We can also use the ASCII in integer to represent a character literal. For
example, the ascii value of 65 is 'A'.

o The octal and hexadecimal notation can be used as an escape sequence to


represent a character literal. For example, '\023', '\0x12'.

String literal

A string literal represents multiple characters enclosed within double-quotes. It contains


an additional character, i.e., '\0' (null character), which gets automatically inserted.
This null character specifies the termination of the string. We can use the '+' symbol to
concatenate two strings.

For example,

String1= "javatpoint";

String2= "family";

To concatenate the above two strings, we use '+' operator, as shown in the below
statement:

"javatpoint " + "family"= javatpoint family

Note: If we represent a single character, i.e., 'b', then this character will occupy a
single byte as it is a character literal. And, if we represent the character within double
quotes "b" then it will occupy more bytes as it is a string literal.

Tokens in C
Tokens in C is the most important element to be used in creating a program in C. We
can define the token as the smallest individual element in C. For `example, we cannot
create a sentence without using words; similarly, we cannot create a program in C
without using tokens in C. Therefore, we can say that tokens in C is the building block
or the basic component for creating a program in C language.

Classification of tokens in C

Tokens in C language can be divided into the following categories:


o Keywords in C

o Identifiers in C

o Strings in C

o Operators in C

o Constant in C

o Special Characters in C

Let's understand each token one by one.

Keywords in C

Keywords in C can be defined as the pre-defined or the reserved words having its
own importance, and each keyword has its own functionality. Since keywords are the
pre-defined words used by the compiler, so they cannot be used as the variable names.
If the keywords are used as the variable names, it means that we are assigning a
different meaning to the keyword, which is not allowed. C language supports 32
keywords given below:

Auto double int struct

Break else long switch

Case enum register typedef


Char extern return union

Const float short unsigne

continue for signed void

Default goto sizeof volatile

Do if static while

Identifiers in C

Identifiers in C are used for naming variables, functions, arrays, structures, etc.
Identifiers in C are the user-defined words. It can be composed of uppercase letters,
lowercase letters, underscore, or digits, but the starting letter should be either an
underscore or an alphabet. Identifiers cannot be used as keywords. Rules for
constructing identifiers in C are given below:

o The first character of an identifier should be either an alphabet or an underscore,


and then it can be followed by any of the character, digit, or underscore.

o It should not begin with any numerical digit.

o In identifiers, both uppercase and lowercase letters are distinct. Therefore, we


can say that identifiers are case sensitive.

o Commas or blank spaces cannot be specified within an identifier.

o Keywords cannot be represented as an identifier.

o The length of the identifiers should not be more than 31 characters.

o Identifiers should be written in such a way that it is meaningful, short, and easy
to read.

Strings in C

Strings in C are always represented as an array of characters having null character '\0'
at the end of the string. This null character denotes the end of the string. Strings in C
are enclosed within double quotes, while characters are enclosed within single
characters. The size of a string is a number of characters that the string contains.

Now, we describe the strings in different ways:

char a[10] = "javatpoint"; // The compiler allocates the 10 bytes to the 'a' array.
char a[] = "javatpoint"; // The compiler allocates the memory at the run time.

char a[10] = {'j','a','v','a','t','p','o','i','n','t','\0'}; // String is represented in the form of


characters.

Operators in C

Operators in C is a special symbol used to perform the functions. The data items on
which the operators are applied are known as operands. Operators are applied between
the operands. Depending on the number of operands, operators are classified as
follows:

Unary Operator

A unary operator is an operator applied to the single operand. For example: increment
operator (++), decrement operator (--), sizeof, (type)*.

Binary Operator

The binary operator is an operator applied between two operands. The following is the
list of the binary operators:

o Arithmetic Operators

o Relational Operators

o Shift Operators

o Logical Operators

o Bitwise Operators

o Conditional Operators

o Assignment Operator

o Misc Operator

Constants in C

A constant is a value assigned to the variable which will remain the same throughout
the program, i.e., the constant value cannot be changed.

There are two ways of declaring constant:

o Using const keyword

o Using #define pre-processor

Types of constants in C
Constant Example

Integer constant 10, 11, 34, etc.

Floating-point constant 45.6, 67.8, 11.2, etc.

Octal constant 011, 088, 022, etc.

Hexadecimal constant 0x1a, 0x4b, 0x6b, etc.

Character constant 'a', 'b', 'c', etc.

String constant "java", "c++", ".net", etc.

Special characters in C

Some special characters are used in C, and they have a special meaning which cannot
be used for another purpose.

o Square brackets [ ]: The opening and closing brackets represent the single and
multidimensional subscripts.

o Simple brackets ( ): It is used in function declaration and function calling. For


example, printf() is a pre-defined function.

o Curly braces { }: It is used in the opening and closing of the code. It is used in
the opening and closing of the loops.

o Comma (,): It is used for separating for more than one statement and for
example, separating function parameters in a function call, separating the
variable when printing the value of more than one variable using a single printf
statement.

o Hash/pre-processor (#): It is used for pre-processor directive. It basically


denotes that we are using the header file.

o Asterisk (*): This symbol is used to represent pointers and also used as an
operator for multiplication.

o Tilde (~): It is used as a destructor to free memory.

o Period (.): It is used to access a member of a structure or a union.


C Boolean
In C, Boolean is a data type that contains two types of values, i.e., 0 and 1. Basically,
the bool type value represents two types of behavior, either true or false. Here, '0'
represents false value, while '1' represents true value.

In C Boolean, '0' is stored as 0, and another integer is stored as 1. We do not require to


use any header file to use the Boolean data type in C++, but in C, we have to use the
header file, i.e., stdbool.h. If we do not use the header file, then the program will not
compile.

Syntax
1. bool variable_name;

In the above syntax, bool is the data type of the variable, and variable_name is the
name of the variable.

Let's understand through an example.

1. #include <stdio.h>
2. #include<stdbool.h>
3. int main()
4. {
5. bool x=false; // variable initialization.
6. if(x==true) // conditional statements
7. {
8. printf("The value of x is true");
9. }
10. else
11. printf("The value of x is FALSE");
12. return 0;
13. }

In the above code, we have used <stdbool.h> header file so that we can use the bool
type variable in our program. After the declaration of the header file, we create the bool
type variable 'x' and assigns a 'false' value to it. Then, we add the conditional
statements, i.e., if..else, to determine whether the value of 'x' is true or not.

Output

The value of x is FALSE

Boolean Array
Now, we create a bool type array. The Boolean array can contain either true or false
value, and the values of the array can be accessed with the help of indexing.

Let's understand this scenario through an example.

1. #include <stdio.h>
2. #include<stdbool.h>
3. int main()
4. {
5. bool b[2]={true,false}; // Boolean type array
6. for(int i=0;i<2;i++) // for loop
7. {
8. printf("%d,",b[i]); // printf statement
9. }
10. return 0;
11. }

In the above code, we have declared a Boolean type array containing two values, i.e.,
true and false.

Output

1,0,

typedef
There is another way of using Boolean value, i.e., typedef. Basically, typedef is
a keyword in C language, which is used to assign the name to the already existing
datatype.

Let's see a simple example of typedef.

1. #include <stdio.h>
2. typedef enum{false,true} b;
3. int main()
4. {
5. b x=false; // variable initialization
6. if(x==true) // conditional statements
7. {
8. printf("The value of x is true");
9. }
10. else
11. {
12. printf("The value of x is false");
13. }
14. return 0;
15. }

In the above code, we use the Boolean values, i.e., true and false, but we have not
used the bool type. We use the Boolean values by creating a new name of the 'bool'
type. In order to achieve this, the typedef keyword is used in the program.

1. typedef enum{false,true} b;

The above statement creates a new name for the 'bool' type, i.e., 'b' as 'b' can contain
either true or false value. We use the 'b' type in our program and create the 'x' variable
of type 'b'.

Output

The value of x is false

Boolean with Logical Operators


The Boolean type value is associated with logical operators. There are three types of
logical operators in the C language:

&&(AND Operator): It is a logical operator that takes two operands. If the value of
both the operands are true, then this operator returns true otherwise false

||(OR Operator): It is a logical operator that takes two operands. If the value of both
the operands is false, then it returns false otherwise true.
!(NOT Operator): It is a NOT operator that takes one operand. If the value of the
operand is false, then it returns true, and if the value of the operand is true, then it
returns false.

Let's understand through an example.

1. #include <stdio.h>
2. #include<stdbool.h>
3. int main()
4. {
5. bool x=false;
6. bool y=true;
7. printf("The value of x&&y is %d", x&&y);
8. printf("\nThe value of x||y is %d", x||y);
9. printf("\nThe value of !x is %d", !x);
10. }

Output

The value of x&&y is 0


The value of x||y is 1
The value of !x is 1

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy