






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Variables and
3.0 Introduction 3.1 Objectives 3.2 Character Set 3.3 Identifiers and Keywords 3.3.1 Rules for Forming Identifiers 3.3.2 Keywords 3.4 Data Types and Storage 3.5 Data Type Qualifiers 3.6 Variables 3.7 Declaring Variables 3.8 Initialising Variables 3.9 Constants 3.9.1 Integer Constants 3.9.2 Floating Point Constants 3.9.3 Character Constants 3.9.4 String Constants 3.10 Symbolic Constants 3.11 Summary 3.12 Solutions / Answers 3.13 Further Readings
As every natural language has a basic character set, computer languages also have a character set, rules to define words. Words are used to form statements. These in turn are used to write the programs.
Computer programs usually work with different types of data and need a way to store the values being used. These values can be numbers or characters. C language has two ways of storing number values— variables and constants —with many options for each. Constants and variables are the fundamental elements of each program. Simply speaking, a program is nothing else than defining them and manipulating them. A variable is a data storage location that has a value that can change during program execution. In contrast, a constant has a fixed value that can’t change.
This unit is concerned with the basic elements used to construct simple C program statements. These elements include the C character set, identifiers and keywords, data types, constants, variables and arrays, declaration and naming conventions of variables.
After going through this unit, you will be able to:
When you write a program, you express C source files as text lines containing characters from the character set. When a program executes in the target environment,
An Introduction to C (^) it uses characters from the character set. These character sets are related, but need not
have the same encoding or all the same members.
Every character set contains a distinct code value for each character in the basic C character set. A character set can also contain additional characters with other code values. The C language character set has alphabets, numbers, and special characters as shown below:
3.3 IDENTIFIERS AND KEYWORDS
Identifiers are the names given to various program elements such as constants, variables, function names and arrays etc. Every element in the program has its own distinct name but one cannot select any name unless it conforms to valid name in C language. Let us study first the rules to define names or identifiers.
Identifiers are defined according to the following rules:
For example, some valid identifiers are shown below:
X X _XI temp tax_rate
For example, some invalid identifiers are shown below:
123 First character to be alphabet. “X.” Not allowed. order-no Hyphen allowed. error flag Blankspace allowed.
Keywords are reserved words which have standard, predefined meaning in C. They cannot be used as program-defined identifiers.
An Introduction to C
Signed int 2 −32768 to 32767
Unsigned int 2 0 to 65535
Signed char 1 −128 to 127
Unsigned char 1 0 to 255
3.6 VARIABLES
Variable is an identifier whose value changes from time to time during execution. It is a named data storage location in your computer’s memory. By using a variable’s name in your program, you are, in effect, referring to the data stored there. A variable represents a single data item i.e. a numeric quantity or a character constant or a string constant. Note that a value must be assigned to the variables at some point of time in the program which is termed as assignment statement. The variable can then be accessed later in the program. If the variable is accessed before it is assigned a value, it may give garbage value. The data type of a variable doesn’t change whereas the value assigned to can change. All variables have three essential attributes:
For example, in the following C program a, b, c, d are the variables but variable e is not declared and is used before declaration. After compiling the source code and look what gives?
main( ) { int a, b, c; char d; a = 3; b = 5; c = a + b; d = ‘a’; e=d; ………. ………. } After compiling the code, this will generate the message that variable e not defined.
3.7 DECLARING VARIABLES
Before any data can be stored in the memory, we must assign a name to these locations of memory. For this we make declarations. Declaration associates a group of identifiers with a specific data type. All of them need to be declared before they appear in program statements, else accessing the variables results in junk values or a diagnostic error. The syntax for declaring variables is as follows:
data- type variable-name(s);
For example,
int a; short int a, b;
Variables and int long c, d; c, f; Constants
float r1, r2;
3.8 INITIALISING VARIABLES
When variables are declared initial, values can be assigned to them in two ways:
a) Within a Type declaration
The value is assigned at the declaration time.
For example,
int a = 10; float b = 0.4 e –5; char c = ‘a’;
b) Using Assignment statement
The values are assigned just after the declarations are made.
For example,
a = 10; b = 0.4 e –5; c = ‘a’;
Check Your Progress 1
hello function day-of-the-week student_1 max_value “what” 1_student int union
……………………………………………………………………………………… ……………………………………………………………………………………… ………………………………………………………………………………………
a) Unsigned character b) Unsigned integer c) Double
……………………………………………………………………………………… ……………………………………………………………………………………… ………………………………………………………………………………………
Variables and These constants begintaken from hexadecimal digits 0 to 9, a to f or A to F. with 0x or OX and are followed by combination of digits Constants
Valid Hexadecimal integer constants are:
OX0 OX1 OXF77 Oxabcd.
Invalid Hexadecimal integer constants are:
OBEF x is not included Ox.4bff illegal char (.) OXGBC illegal char G
Maximum values these constants can have are as follows:
Integer constants Maximum value
Decimal integer 32767 Octal integer 77777 Hexadecimal integer 7FFF
Unsigned interger constants: Exceed the ordinary integer by magnitude of 2, they are not negative. A character U or u is prefixed to number to make it unsigned.
Long Integer constants: These are used to exceed the magnitude of ordinary integers and are appended by L.
For example,
50000U decimal unsigned. 1234567889L decimal long. 0123456L otal long. 0777777U otal unsigned.
What is a base 10 number containing decimal point or an exponent.
Examples of valid floating point numbers are:
000.2 5. 50000.1 0. 1.6667E+3 0.006e-
Examples of Invalid Floating Point numbers are:
1 decimal or exponent required. 1,00.0 comma not allowed. 2E+10.2 exponent is written after integer quantity. 3E 10 no blank space.
A Floating Point number taking the value of 5 x 10^4 can be represented as:
An Introduction to C (^) The magnitude of floating point numbers range from 3.4E –38 to a maximum of
3.4E+38, through 0.0. They are taken as double precision numbers. Floating Point constants occupy 2 words = 8 bytes.
This constant is a single character enclosed in apostrophes ‘ ’.
For example, some of the character constants are shown below:
‘A’, ‘x’, ‘3’, ‘$’
‘\0’ is a null character having value zero.
Character constants have integer values associated depending on the character set adopted for the computer. ASCII character set is in use which uses 7-bit code with 2^7 = 128 different characters. The digits 0-9 are having ASCII value of 48-56 and ‘A’ have ASCII value from 65 and ‘a’ having value 97 are sequentially ordered. For example,
‘A’ has 65, blank has 32
ESCAPE SEQUENCE There are some non-printable characters that can be printed by preceding them with ‘\’ backslash character. Within character constants and string literals, you can write a variety of escape sequences. Each escape sequence determines the code value for a single character. You can use escape sequences to represent character codes:
The following is the list of the escape sequences:
Character Escape Sequence " " ' ' ? ? \ \ BEL \a BS \b FF \f NL \n CR \r HT \t VT \v
It consists of sequence of characters enclosed within double quotes. For example,
“ red ” “ Blue Sea ” “ 41213*(I+3) ”.
3.10 SYMBOLIC CONSTANTS
Symbolic Constant is a name that substitutes for a sequence of characters or a numeric constant, a character constant or a string constant. When program is compiled each occurrence of a symbolic constant is replaced by its corresponding character sequence. The syntax is as follows:
An Introduction to C
The constants are the fixed values and may be either Integer or Floating point or Character or String type. Symbolic Constants are used to define names used for constant values. They help in using the name rather bothering with remembering and writing the values.
3.12 SOLUTIONS / ANSWERS
Check Your Progress 1
Check Your Progress 2
3.13 FURTHER READINGS