You are on page 1of 24

Constants

Constants refer to fixed values that the program may not alter during its execution.

These fixed values are also called literals.

Constants can be of any of the basic data types like an


 Integer constant,

 Floating constant,

 Character constant

 String literal.

Constants are treated just like regular variables except that their values cannot be

modified after their definition.


Types of Constants
Syntax for Declaring Constant
Single Character Constants

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).
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


Variables in C Programming
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.
syntax to declare a variable: type variable_list;
The example of declaring the variable is given below
Rules for defining variables
A variable can have alphabets, digits, and underscore.
A variable name can start with the alphabet, and underscore only. It can't start with
a digit.
No whitespace is allowed within the variable name.
A variable name must not be any reserved word or keyword, e.g. int, float, etc.
Types of Variables in C

There are many types of variables in c:


1. Local variable
2. Global variable
3. Static variable
4. External variable
Output
For First Function Call
X=11
Y=11
For Second Function Call
X=11
Y=12
For Third Function Call
X=11
Y=13
External Variable
We can share a variable in multiple C source files by using an external variable.

To declare an external variable, you need to use extern keyword.

extern int x=10;//external variable (also global)

Store under the file name myfile.h


1. Which of the following is true for variable names in C?
a) They can contain alphanumeric characters as well as special characters
b) It is not an error to declare a variable to be one of the keywords(like goto, static)
c) Variable names cannot start with a digit
d) Variable can be of any length

2.Which is valid C expression?


a) int my_num = 100,000;
b) int my_num = 100000;
c) int my num = 1000;
d) int $my_num = 10000;
3.Choose correct statements

A) A constant value does not change. A variable value can change according to needs.

B) A constant can change its values. A variable can have one constant value only.

C) There is no restriction on number of values for constants or variables.

D) Constants and Variables can not be used in a single main function.

4.Find a Floating Point constant.


A) 12.3E5
B) 12e34
C) 125.34857
D) All the above.
5.Find an integer constant.
A) 3.145
B) 34
C) "125"
D) None of the above
7.Choose a right statement.
A) int myage = 10; int my_age = 10;
B) int myage = 10; int my,age = 10;
C) int myage = 10; int my age = 10;
D) All are right
8.Choose a correct statement.

A) C Compiler converts your C program into machine readable language.

B) C Editor allows you to type C Programs. It is just like a Notepad with extra options.

C) Console shows the output of a C Program if it is text output.

D) All the above


9.Number of Keywords present in C Language are .?

A) 32

B) 34

C) 62

D) 64

You might also like