Skip to main content

Variables and Constants in C

In programming, a variable is a container (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a memory location. For example:
int playerScore = 95;
Here, playerScore is a variable of integer type. The variable is assigned value: 95.
The value of a variable can be changed, hence the name 'variable'.
In C programming, you have to declare a variable before you can use it.
Rules for naming a variable in C
A variable name can have letters (both uppercase and lowercase letters), digits and underscore only.
The first letter of a variable should be either a letter or an underscore. However, it is discouraged to start variable name with an underscore. It is because variable name that starts with an underscore can conflict with system name and may cause error.
There is no rule on how long a variable can be. However, only the first 31 characters of a variable are checked by the compiler. So, the first 31 letters of two variables in a program should be different.
C is a strongly typed language. What this means it that, the type of a variable cannot be changed.
A constant is a value or an identifier whose value cannot be altered in a program. For example: 1, 2.5, "C programming is easy", etc.
As mentioned, an identifier also can be defined as a constant.
const double PI = 3.14
Here, PI is a constant. Basically what it means is that, PI and 3.14 is same for this program.
Below are the different types of constants you can use in C.
1. Integer constants
An integer constant is a numeric constant (associated with number) without any fractional or exponential part. There are three types of integer constants in C programming:
decimal constant(base 10)
octal constant(base 8)
hexadecimal constant(base 16)
For example:
Decimal constants: 0, -9, 22 etc
Octal constants: 021, 077, 033 etc
Hexadecimal constants: 0x7f, 0x2a, 0x521 etc
In C programming, octal constant starts with a 0 and hexadecimal constant starts with a 0x.
2. Floating-point constants
A floating point constant is a numeric constant that has either a fractional form or an exponent form. For example:
Note: E-5 = 10-5
3. Character constants
A character constant is a constant which uses single quotation around characters. For example: 'a', 'l', 'm', 'F'
4. Escape Sequences
Sometimes, it is necessary to use characters which cannot be typed or has special meaning in C programming. For example: newline(enter), tab, question mark etc. In order to use these characters, escape sequence is used.
For example: \n is used for newline. The backslash ( \ ) causes "escape" from the normal way the characters are interpreted by the compiler.

Escape Sequences
Form feed
Horizontal tab
Vertical tab
Single quotation mark
Double quotation mark
Question mark
Null character
5. String constants
String constants are the constants which are enclosed in a pair of double-quote marks. For example:
"good"                  //string constant
""                     //null string constant
"      "               //string constant of six white space
"x"                    //string constant having single character.
"Earth is round\n"         //prints string with newline
6. Enumeration constants
Keyword enum is used to define enumeration types. For example:
enum color {yellow, green, black, white};
Here, color is a variable and yellow, green, black and white are the enumeration constants having value 0, 1, 2 and 3 respectively.


Popular posts from this blog

Files in C

A file is a repository of data that is stored in a permanent storage media, mainly in secondary memory. In order to use the files we should learn how to read information from a file and how to write information into a file. A very important concept in C is the stream.The stream is a common logical interface to the various devices( files).A stream is linked to a file while using an open operation. A stream is disassociated from a file while using a close operation. The current location, also referred to as the current position, is the location in a file where the next fie access will occur.There are two types of streams text and binary. The following are some difference between text and binary files ·Text file is human readable because everything is stored in terms of text. In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable. ·A newline(\n) character is converted into the carriage return-linefeed combination before being written to the d…

KTU C programming question paper and evaluation scheme

PART A 1 An identifier is a sequence of characters invented by the programmer or to identify or name a specific object. The sequence of characters may be letters, digits, and special character ‘_’known as an underscore Rules: i)Identifiers should start with alphabets. ii)Identifiers are case sensitive iii)A numeric digit should not be the first character iv)Identifier name should not be a keyword v)Identifier may be of any reasonable length 1mark

2mark 2 Associativity defines the direction, left to right or right to left in which operator act upon its operands Unary operators have associativity is from right to left. For examplein the expression &--x, pre decrement works first and then address of operator works Direction + example<

Strings in C

Strings in C Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’. Declaration of strings: Declaring a string is as simple as declaring a one dimensional array. Below is the basic syntax for declaring a string. charstr_name[size]; In the above syntax str_name is any name given to the string variable and size is used define the length of the string, i.e the number of characters strings will store. Please keep in mind that there is an extra terminating character which is the Null character (‘\0’) used to indicate termination of string which differs strings from normal character arrays. Initializing a String: A string can be initialized in different ways. We will explain this with the help of an example. Below is an example to declare a string with name as str and initialize it with “cek”. 1. charstr[] = "cek"; 2. charstr[50] = "cek"; 3. charstr[] = {'c','…