C Programming Session 5
C Programming Session 5
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.
List of Constants in C
Constant Example
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;
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:
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:
2) C #define preprocessor
The #define preprocessor is also used to define constant. We will learn about #define
preprocessor directive later.
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 is defined by representing the digits between 0 to 9. For example, 45, 67, etc.
L or l: It is a size qualifier that specifies the size of the integer type as long.
Note: The order of the qualifier is not considered, i.e., both lu and ul are the same.
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.
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:
o We can also use the ASCII in integer to represent a character literal. For
example, the ascii value of 65 is 'A'.
String literal
For example,
String1= "javatpoint";
String2= "family";
To concatenate the above two strings, we use '+' operator, as shown in the below
statement:
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
o Identifiers in C
o Strings in C
o Operators in C
o Constant in C
o Special Characters in C
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:
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 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.
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.
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.
Types of constants in C
Constant Example
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 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 Asterisk (*): This symbol is used to represent pointers and also used as an
operator for multiplication.
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.
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
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.
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.
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
&&(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.
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