0% found this document useful (0 votes)
40 views31 pages

Unit 2 Basics of C

The document provides an overview of the history and features of the C programming language. It discusses that C was developed in 1972 by Dennis Ritchie at Bell Labs to overcome limitations of previous languages like B and BCPL. Some key features of C include it being a simple, portable, mid-level structured programming language with a rich library, support for pointers, recursion and memory management. The document also provides examples of basic input/output functions like printf and scanf and demonstrates their use. It describes different variable types in C and common types of errors that can occur like syntax, runtime, logical and linker errors.

Uploaded by

amritasharma5319
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)
40 views31 pages

Unit 2 Basics of C

The document provides an overview of the history and features of the C programming language. It discusses that C was developed in 1972 by Dennis Ritchie at Bell Labs to overcome limitations of previous languages like B and BCPL. Some key features of C include it being a simple, portable, mid-level structured programming language with a rich library, support for pointers, recursion and memory management. The document also provides examples of basic input/output functions like printf and scanf and demonstrates their use. It describes different variable types in C and common types of errors that can occur like syntax, runtime, logical and linker errors.

Uploaded by

amritasharma5319
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/ 31

History of C Language

C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of


AT&T (American Telephone & Telegraph), located in the U.S.A.

Dennis Ritchie is known as the founder of the c language.

It was developed to overcome the problems of previous languages such as B, BCPL, etc.

Initially, C language was developed to be used in UNIX operating system. It inherits many
features of previous languages such as B and BCPL.

Let's see the programming languages that were developed before C language.

Language Year Developed By

Algol 1960 International Group

BCPL 1967 Martin Richard

B 1970 Ken Thompson

Traditional C 1972 Dennis Ritchie

K&RC 1978 Kernighan & Dennis Ritchie

ANSI C 1989 ANSI Committee

ANSI/ISO C 1990 ISO Committee

C99 1999 Standardization Committee


Features of C Language

C is the widely used language. It provides many features that are given below.
1. Simple
2. Machine Independent or Portable
3. Mid-level programming language
4. structured programming language
5. Rich Library
6. Memory Management
7. Fast Speed
8. Pointers
9. Recursion
10. Extensible

1) Simple
C is a simple language in the sense that it provides a structured approach (to break the problem
into parts), the rich set of library functions, data types, etc.
2) Machine Independent or Portable
Unlike assembly language, c programs can be executed on different machines with some
machine specific changes. Therefore, C is a machine independent language.
3) Mid-level programming language
Although, C is intended to do low-level programming. It is used to develop system
applications such as kernel, driver, etc. It also supports the features of a high-level language.
That is why it is known as mid-level language.
4) Structured programming language
C is a structured programming language in the sense that we can break the program into parts
using functions. So, it is easy to understand and modify. Functions also provide code reusability.
5) Rich Library
C provides a lot of inbuilt functions that make the development fast.
6) Memory Management
It supports the feature of dynamic memory allocation. In C language, we can free the allocated
memory at any time by calling the free() function.
7) Speed
The compilation and execution time of C language is fast since there are lesser inbuilt functions
and hence the lesser overhead.
8) Pointer
C provides the feature of pointers. We can directly interact with the memory by using the
pointers. We can use pointers for memory, structures, functions, array, etc.
9) Recursion
In C, we can call the function within the function. It provides code reusability for every
function. Recursion enables us to use the approach of backtracking.
10) Extensible
C language is extensible because it can easily adopt new features.

printf() and scanf() in C

The printf() and scanf() functions are used for input and output in C language. Both functions are
inbuilt library functions, defined in stdio.h (header file).
printf() function
The printf() function is used for output. It prints the given statement to the console.
The syntax of printf() function is given below:
1. printf("format string",argument_list);
The format string can be %d (integer), %c (character), %s (string), %f (float) etc.
scanf() function
The scanf() function is used for input. It reads the input data from the console.
1. scanf("format string",argument_list);

Program to print cube of given number


Let's see a simple example of c language that gets input from the user and prints the cube of the
given number.
#include<stdio.h>
int main(){
int number;
printf("enter a number:");
scanf("%d",&number);
printf("cube of number is:%d ",number*number*number);
return 0;
}
Output
enter a number:5
cube of number is:125
The scanf("%d",&number) statement reads integer number from the console and stores the
given value in number variable.
The printf("cube of number is:%d ",number*number*number) statement prints the cube of
number on the console.
Program to print sum of 2 numbers
Let's see a simple example of input and output in C language that prints addition of 2 numbers.
#include<stdio.h>
int main(){
int x=0,y=0,result=0;

printf("enter first number:");


scanf("%d",&x);
printf("enter second number:");
scanf("%d",&y);
result=x+y;
printf("sum of 2 numbers:%d ",result);
return 0;
}
Output
enter first number:9
enter second number:9
sum of 2 numbers:18
Variables in C

A variable is a name of the memory location. It is used to store data. Its value can be changed,
and it can be reused many times.

It is a way to represent memory location through symbol so that it can be easily identified.

1. type variable_list;

The example of declaring the variable is given below:

1. int a;
2. float b;
3. char c;
Here, a, b, c are variables. The int, float, char are the data types.
We can also provide values while declaring the variables as given below:
1. int a=10,b=20;//declaring 2 variable of integer type
2. float f=20.8;
3. char c='A';

Rules for defining variables


o A variable can have alphabets, digits, and underscore.
o A variable name can start with the alphabet, and underscore only. It can't start with a
digit.
o No whitespace is allowed within the variable name.
o A variable name must not be any reserved word or keyword, e.g. int, float, etc.

Valid variable names:


1. int a;
2. int _ab;
3. int a30;
Invalid variable names:
1. int 2;
2. int a b;
3. int long;

Programming Errors in C
Errors are the problems or the faults that occur in the program, which makes the behavior of the
program abnormal.Programming errors are also known as the bugs or faults, and the process of
removing these bugs is known as debugging.
These errors are detected either during the time of compilation or execution. Thus, the errors
must be removed from the program for the successful execution of the program.
Types of error in C:-
There are 5 types of error in C:
1. Syntax Errors
2. Runtime Errors
3. Logical Errors
4. Linked Errors
5. Semantic Errors

1. Syntax Errors
These are also referred to as compile-time errors. These errors have occurred when the rule of C
writing techniques or syntaxes has been broken. These types of errors are typically flagged by
the compiler prior to compilation.
Example 1: In the below program we are getting an error because of a missing semicolon at the
end of the output statement (printf()) called syntax error.
// C program to demonstrate

// a syntax error due to

// missing semi colon

#include <stdio.h>

// Driver code

int main()

// missing semicolon

printf("Welcome")

return 0;

Example 2: In this case, we are getting errors because of missing parenthesis before the output
statement and below the main(). This type of error is also called syntax error.
// C program to demonstrate

// a syntax error due to

// missing parenthesis

#include <stdio.h>

// Driver code

int main()

printf("Welcome");

return 0;

2. Runtime Errors
This type of error occurs while the program is running. Because this is not a compilation error,
the compilation will be completed successfully. These errors occur due to segmentation fault
when a number is divided by division operator or modulo division operator.
Example: Let us consider an array of length 5 i.e. array[5], but during runtime, if we try to
access 10 elements i.e array[10] then we get segmentation fault errors called runtime errors.
Giving only an array length of 5
// C program to demonstrate

// a runtime error

#include <stdio.h>

// Driver code

int main()

int array[5];

printf("%d", array[10]);

return 0;

Output

-621007737
But in output trying to access more than 5 i.e if we try to access array[10] during runtime then
the program will throw an error or will show an abnormal behavior and print any garbage value.
3. Logical Errors
Even if the syntax and other factors are correct, we may not get the desired results due to logical
issues. These are referred to as logical errors. We sometimes put a semicolon after a loop, which
is syntactically correct but results in one blank loop. In that case, it will display the desired
output.
Example: In the below example, the for loop iterates 5 times but the output will be displayed
only one time due to the semicolon at the end of for loop. This kind of error is called a logical
error.
// C program to demonstrate

// a logical error

#include <stdio.h>

// Driver code

int main()

int i;

for(i = 0; i <= 5; i++);

printf("Welcome");

return 0;

Output

Welcome

4. Linker Errors
When the program is successfully compiled and attempting to link the different object files with
the main object file, errors will occur. When this error occurs, the executable is not generated.
This could be due to incorrect function prototyping, an incorrect header file, or other factors. If
main() is written as Main(), a linked error will be generated.
Example: Below is the C program to show the linker error.
// C program to demonstrate

// a linker error

#include <stdio.h>

// Driver code

int Main()

printf("Welcome ");

return 0;

5. Semantic Errors
When a sentence is syntactically correct but has no meaning, semantic errors occur. This is
similar to grammatical errors. If an expression is entered on the left side of the assignment
operator, a semantic error may occur.
Example: Below is the C program to show semantic error.
// C program to demonstrate

// a semantic error

#include <stdio.h>

// Driver code

int main()

int x = 10;

b = 20, c;

x + y = c;

printf("%d", c);

return 0;

}
Data Types in C

A data type specifies the type of data that a variable can store such as integer, floating, character,
etc.

There are the following data types in C language.

Types Data Types

Basic Data Type int, char, float, double

Derived Data Type array, pointer, structure, union

Enumeration Data Type Enum

Void Data Type Void

Basic Data Types


The basic data types are integer-based and floating-point based. C language supports both signed
and unsigned literals.
The memory size of the basic data types may change according to 32 or 64-bit operating system.
Basic data types & their size is given according to 64-bit architecture.

Data Types Memory Size

Char 1 byte

Int 4 byte

Float 4 byte

Double 8 byte

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

Keywords in C

A keyword is a reserved word. You cannot use it as a variable name, constant name, etc. There
are only 32 reserved words (keywords) in the C language.

A list of 32 keywords in the c language is given below:

Auto break case Char const continue default do

Double else enum Extern float For goto if

Int long register Return short signed sizeof static

Struct switch typedef union unsigned void volatile while


C Identifiers

C identifiers represent the name in the C program, for example, variables, functions, arrays,
structures, unions, labels, etc. An identifier can be composed of letters such as uppercase,
lowercase letters, underscore, digits, but the starting letter should be either an alphabet or an
underscore. If the identifier is not used in the external linkage, then it is called as an internal
identifier. If the identifier is used in the external linkage, then it is called as an external identifier.
We can say that an identifier is a collection of alphanumeric characters that begins either with an
alphabetical character or an underscore, which are used to represent various programming
elements such as variables, functions, arrays, structures, unions, labels, etc. There are 52
alphabetical characters (uppercase and lowercase), underscore character, and ten numerical digits
(0-9) that represent the identifiers. There is a total of 63 alphanumerical characters that represent
the identifiers.
Rules for constructing C identifiers
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.

Example of valid identifiers


1. total, sum, average, _m _, sum_1, etc.
Example of invalid identifiers
1. 2sum (starts with a numerical digit)
2. int (reserved word)
3. char (reserved word)
4. m+n (special character, i.e., '+')

Types of identifiers
o Internal identifier
o External identifier

Internal Identifier
If the identifier is not used in the external linkage, then it is known as an internal identifier. The
internal identifiers can be local variables.
External Identifier
If the identifier is used in the external linkage, then it is known as an external identifier. The
external identifiers can be function names, global variables.
Differences between Keyword and Identifier

Keyword Identifier

Keyword is a pre-defined word. The identifier is a user-defined word

It must be written in a lowercase letter. It can be written in both lowercase and uppercase
letters.

Its meaning is pre-defined in the c compiler. Its meaning is not defined in the c compiler.

It is a combination of alphabetical It is a combination of alphanumeric characters.


characters.

It does not contain the underscore character. It can contain the underscore character.

Let's understand through an example.


1. int main()
2. {
3. int a=10;
4. int A=20;
5. printf("Value of a is : %d",a);
6. printf("\nValue of A is :%d",A);
7. return 0;
8. }
Output
Value of a is : 10
Value of A is :20
The above output shows that the values of both the variables, 'a' and 'A' are different. Therefore,
we conclude that the identifiers are case sensitive.
C Operators

An operator is simply a symbol that is used to perform operations. There can be many types of
operations like arithmetic, logical, bitwise, etc.
There are following types of operators to perform different types of operations in C language.
o Arithmetic Operators
o Relational Operators
o Shift Operators
o Logical Operators
o Bitwise Operators
o Ternary or Conditional Operators
o Assignment Operator
o Misc Operator

Precedence of Operators in C
The precedence of operator species that which operator will be evaluated first and next. The
associativity specifies the operator direction to be evaluated; it may be left to right or right to left.
Let's understand the precedence by the example given below:
1. int value=10+20*10;

The value variable will contain 210 because * (multiplicative operator) is evaluated before +
(additive operator).

The precedence and associativity of C operators is given below:

Category Operator Associativity

Postfix () [] -> . ++ - - Left to right

Unary + - ! ~ ++ - - (type)* & sizeof Right to left

Multiplicative */% Left to right

Additive +- Left to right

Shift << >> Left to right

Relational < <= > >= Left to right

Equality == != Left to right


Bitwise AND & Left to right

Bitwise XOR ^ Left to right

Bitwise OR | Left to right

Logical AND && Left to right

Logical OR || Left to right

Conditional ?: Right to left

Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left

Comma , Left to right

Conditional Operator in C

The conditional operator is also known as a ternary operator. The conditional statements are
the decision-making statements which depends upon the output of the expression. It is
represented by two symbols, i.e., '?' and ':'.
As conditional operator works on three operands, so it is also known as the ternary operator.
The behavior of the conditional operator is similar to the 'if-else' statement as 'if-else' statement
is also a decision-making statement.
Syntax of a conditional operator
Expression1? expression2: expression3;

o In the above syntax, the expression1 is a Boolean condition that can be either true or false
value.
o If the expression1 results into a true value, then the expression2 will execute.
o The expression2 is said to be true only when it returns a non-zero value.
o If the expression1 returns false value then the expression3 will execute.
o The expression3 is said to be false only when it returns zero value.

Example:-

1. #include <stdio.h>
2. int main()
3. {
4. int age; // variable declaration
5. printf("Enter your age");
6. scanf("%d",&age); // taking user input for age variable
7. (age>=18)? (printf("eligible for voting")) : (printf("not eligible for voting")); // conditional op
erator
8. return 0;
9. }
In the above code, we are taking input as the 'age' of the user. After taking input, we have
applied the condition by using a conditional operator. In this condition, we are checking the
age of the user. If the age of the user is greater than or equal to 18, then the statement1 will
execute, i.e., (printf("eligible for voting")) otherwise, statement2 will execute, i.e.,
(printf("not eligible for voting")).

1. #include <stdio.h>
2. int main()
3. {
4. int a=5,b; // variable declaration
5. b=((a==5)?(3):(2)); // conditional operator
6. printf("The value of 'b' variable is : %d",b);
7. return 0;
8. }
In the above code, we have declared two variables, i.e., 'a' and 'b', and assign 5 value to the
'a' variable. After the declaration, we are assigning value to the 'b' variable by using the
conditional operator. If the value of 'a' is equal to 5 then 'b' is assigned with a 3 value
otherwise 2.

o A conditional operator is a single programming statement, while the 'if-else' statement is


a programming block in which statements come under the parenthesis.
o A conditional operator can also be used for assigning a value to the variable, whereas the
'if-else' statement cannot be used for the assignment purpose.
o It is not useful for executing the statements when the statements are multiple, whereas the
'if-else' statement proves more suitable when executing multiple statements.
o The nested ternary operator is more complex and cannot be easily debugged, while the
nested 'if-else' statement is easy to read and maintain.

Comments in C

Comments in C language are used to provide information about lines of code. It is widely used
for documenting code. There are 2 types of comments in the C language.
1. Single Line Comments
2. Multi-Line Comments

Single Line Comments


Single line comments are represented by double slash \\. Let's see an example of a single line
comment in C.
#include<stdio.h>
int main(){
//printing information
printf("Hello C");
return 0;
}
Output:
Hello C
Even you can place the comment after the statement. For example:
printf("Hello C");//printing information

Mult Line Comments


Multi-Line comments are represented by slash asterisk \* ... *\. It can occupy many lines of code,
but it can't be nested. Syntax:
/*
code
to be commented
*/
Let's see an example of a multi-Line comment in C.
#include<stdio.h>
int main(){
/*printing information
Multi-Line Comment*/
printf("Hello C");
return 0;
}
Output:
Hello C

C Format Specifier

The Format specifier is a string used in the formatted input and output functions. The format
string determines the format of the input and output. The format string always starts with a '%'
character.

The commonly used format specifiers in printf() function are:

Format Description
specifier

%d or %i It is used to print the signed integer value where signed integer means that the
variable can hold both positive and negative values.

%u It is used to print the unsigned integer value where the unsigned integer means
that the variable can hold only positive value.

%o It is used to print the octal unsigned integer where octal integer value always starts
with a 0 value.

%x It is used to print the hexadecimal unsigned integer where the hexadecimal integer
value always starts with a 0x value. In this, alphabetical characters are printed in
small letters such as a, b, c, etc.
%X It is used to print the hexadecimal unsigned integer, but %X prints the alphabetical
characters in uppercase such as A, B, C, etc.

%f It is used for printing the decimal floating-point values. By default, it prints the 6
values after '.'.

%e/%E It is used for scientific notation. It is also known as Mantissa or Exponent.

%g It is used to print the decimal floating-point values, and it uses the fixed precision,
i.e., the value after the decimal in input would be exactly the same as the value in
the output.

%p It is used to print the address in a hexadecimal form.

%c It is used to print the unsigned character.

%s It is used to print the strings.

%ld It is used to print the long-signed integer value.

Let's understand the format specifiers in detail through an example.

o %d

int main()
{
int b=6;
int c=8;
printf("Value of b is:%d", b);
printf("\nValue of c is:%d",c);

return 0;
}

In the above code, we are printing the integer value of b and c by using the %d specifier.

Output
o %u

1. int main()
2. {
3. int b=10;
4. int c= -10;
5. printf("Value of b is:%u", b);
6. printf("\nValue of c is:%u",c);

7. return 0;
8. }

In the above program, we are displaying the value of b and c by using an unsigned format
specifier, i.e., %u. The value of b is positive, so %u specifier prints the exact value of b, but it
does not print the value of c as c contains the negative value.

Output
o %o

1. int main()
2. {
3. int a=0100;
4. printf("Octal value of a is: %o", a);
5. printf("\nInteger value of a is: %d",a);
6. return 0;
7. }

In the above code, we are displaying the octal value and integer value of a.

Output

o %x and %X

1. int main()
2. {
3. int y=0xA;
4. printf("Hexadecimal value of y is: %x", y);
5. printf("\nHexadecimal value of y is: %X",y);
6. printf("\nInteger value of y is: %d",y);
7. return 0;
8. }

In the above code, y contains the hexadecimal value 'A'. We display the hexadecimal value of y
in two formats. We use %x and %X to print the hexadecimal value where %x displays the value
in small letters, i.e., 'a' and %X displays the value in a capital letter, i.e., 'A'.

Output

o %f

1. int main()
2. {
3. float y=3.4;
4. printf("Floating point value of y is: %f", y);
5. return 0;
6. }

The above code prints the floating value of y.

Output
o %c

1. int main()
2. {
3. char a='c';
4. printf("Value of a is: %c", a);
5. return 0;
6. }

Output

o %s

1. int main()
2. {
3. printf("%s", "javaTpoint");
4. return 0;
5. }
Output

Minimum Field Width Specifier


Suppose we want to display an output that occupies a minimum number of spaces on the screen.
You can achieve this by displaying an integer number after the percent sign of the format
specifier.
1. int main()
2. {
3. int x=900;
4. printf("%8d", x);
5. printf("\n%-8d",x);
6. return 0;
7. }
In the above program, %8d specifier displays the value after 8 spaces while %-8d specifier will
make a value left-aligned.
Output
Now we will see how to fill the empty spaces. It is shown in the below code:
1. int main()
2. {
3. int x=12;
4. printf("%08d", x);
5. return 0;
6. }
In the above program, %08d means that the empty space is filled with zeroes.
Output

Specifying Precision
We can specify the precision by using '.' (Dot) operator which is followed by integer and format
specifier.
1. int main()
2. {
3. float x=12.2;
4. printf("%.2f", x);
5. return 0;
6. }

Output

Escape Sequence in C

An escape sequence in C language is a sequence of characters that doesn't represent itself when
used inside string literal or character.
It is composed of two or more characters starting with backslash \. For example: \n represents
new line.
List of Escape Sequences in C

Escape Sequence Meaning

\a Alarm or Beep

\b Backspace

\f Form Feed
\n New Line

\r Carriage Return

\t Tab (Horizontal)

\v Vertical Tab

\\ Backslash

\' Single Quote

\" Double Quote

\? Question Mark

Escape Sequence Example


1. #include<stdio.h>
2. int main(){
3. int number=50;
4. printf("You\nare\nlearning\n\'c\' language\n\"Do you know C language\"");
5. return 0;
6. }
Output:
You
are
learning
'c' language
"Do you know C language"

ASCII value in C

What is ASCII code?


The full form of ASCII is the American Standard Code for information interchange. It is a
character encoding scheme used for electronics communication. Each character or a special
character is represented by some ASCII code, and each ascii code occupies 7 bits in memory.
In C programming language, a character variable does not contain a character value itself rather
the ascii value of the character variable. The ascii value represents the character variable in
numbers, and each character variable is assigned with some number range from 0 to 127. For
example, the ascii value of 'A' is 65.
In the above example, we assign 'A' to the character variable whose ascii value is 65, so 65 will
be stored in the character variable rather than 'A'.

sWe will create a program which will display the ascii value of the character variable.

1. #include <stdio.h>
2. int main()
3. {
4. char ch; // variable declaration
5. printf("Enter a character");
6. scanf("%c",&ch); // user input
7. printf("\n The ascii value of the ch variable is : %d", ch);
8. return 0;
9. }

In the above code, the first user will give the character input, and the input will get stored in the
'ch' variable. If we print the value of the 'ch' variable by using %c format specifier, then it will
display 'A' because we have given the character input as 'A', and if we use the %d format
specifier then its ascii value will be displayed, i.e., 65.

Output

The above output shows that the user gave the input as 'A', and after giving input, the ascii value
of 'A' will get printed, i.e., 65.

Now, we will create a program which will display the ascii value of all the characters.

1. #include <stdio.h>
2. int main()
3. {
4. int k; // variable declaration
5. for(int k=0;k<=255;k++) // for loop from 0-255
6. {
7. printf("\nThe ascii value of %c is %d", k,k);
8. }
9. return 0;
10. }

The above program will display the ascii value of all the characters. As we know that ascii value
of all the characters starts from 0 and ends at 255, so we iterate the for loop from 0 to 255.

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.u

You might also like