Constants, Variables, & Data Types: Prepared By:vipul Vekariya
Constants, Variables, & Data Types: Prepared By:vipul Vekariya
Character Set
Letters Digits Special Characters White Spaces Note: for c character set follow the page no 24.
Trigraph sequence
If any keyboard not support the special character then apply follow method. Trigraph sequence Translation ??= # ??( [ ??) ] ??~
C Tokens
Constants
Constants
Constants are data values that cannot be changed during the execution of your program. Like variables, constants also have a type. There are four kinds of constants in C :
Integer Constant
Although integers are always stored in their binary form, they are simply coded as you would use them in everyday life. (e.g. value of fifteen is coded as 15). If you code the number as a string of digits, its type is signed integer, or long integer if the number is large. You can override the default type by specifying unsigned (u or U) and long (l or L) after the number.
Float Constants
Float constants are numbers with decimal parts. They are stored in memory as two parts : the significant and the exponent. The default form for float constants is double. If you want the resulting data type to be float or long double, you must specify the desired data type (e.g. f and F are used for float and l and L are used for long double).
Character Constants
Constants are enclosed between two single quotes e.g. A (apostrophes). In addition to character, there can be a backslash (\) known as escape character between the quote marks. It is used when the character you need to represent does not have any graphic associated with it, i.e. it cannot be printed or when it cannot be entered from the keyboard. Most computers use the ASCII alphabet, or ASCII character set.
String Constants
A string constant is a sequence of zero or more characters enclosed in double quotes .
Examples of Strings:
/* A null string */ h Hello World\n HOW ARE YOU GOOD MORNING! Department of Information & Communications Technology
Coding Constants
Three ways to code constants in C :
Literal Constants
A literal is an unnamed constant used to
specify data. It is by far the most common form of constants. Literal Constant Examples
A 5 a+5 3.1416 Hello /* /* /* /* /* a character literal */ numeric literal 5 */ another numeric literal (5) */ a float literal */ a string literal */
Symbolic Constants
Use the preprocessor command, define to designate. It is prefaced with the pound sign (#), e.g. : #define SALES_TAX_RATE .0826
Usually placed at the beginning of the program. The expression that follows the name replaces the name wherever it is found in the source program. This action is just like the search and replace command found in the text editor. The preprocessor does not evaluate the code in any way, it just blindly makes the substitution.
Memory constants
Memory constants uses a C type qualifier to fix the content of certain memory location. The type qualifier is const e.g. : const float pi = 3.1416; Once assigned, value of the constant can not be changed.
Variables
In C, variables is data name that store the data value. Variables take different value at different times. A variable can be declared and defined at the same time. When we create variables, the declaration gives them a symbolic name and the definition reserves memory for them. Once defined, variables are used to hold the data that are required by the program for its operation.
Variables in Memory
Data in memory is accessed through their symbolic names, or identifiers by the programmer, not by their physical addresses in memory.
Data Types
A type defines a set of values and a set of operations that can be applied on those values. The set of values for type is known as the domain for the type. C contains four standard types : void char (short for character) int (short for integer) float (short for floating point)
Standard type cannot be broken down further into sub-types. Rather, they can be used to build more complex data types call Derived Types (e.g. pointers, array, union etc.).
void
The void type has no values and no operations. Both the set of values and the set of operations are empty. A number without a fraction part : integral number. C supports three different sizes of the integer data type : short int int long int
integer
1. If the integer is signed, then one bit must be used for the sign (0 is plus, 1 is minus). 2. The unsigned integer can therefore store a positive number that is twice as large as the signed integer of the same size.
Floating Point
A floating-point type is a number with a fractional part, e.g. 56.78 The C language supports three different sizes of floating-point data type : float, double, long double. size of (float) <= size of (double) <= size of (long double). Floating-point is always signed.
Floating-Point Types
Type Summary
void character integer float void char unsigned short int; unsigned int; unsigned long int short int; int; long int Float; double; long double
Variables Declaration
A variables type can be any of the data types such as char, int, and float except void. To create a variable, you must specify the type and then its identifier :
Data-type v1,v2.vn; float price; int count; char code;
SIZE
Examples: %hd %ld /* short integer */ /* long integer */
%Lf
/* long double */
Example 1
printf (%d\t%c\t%5.1f\n, 23, Z, 14.2 ); printf (%d\t%c\t%5.1f\n, 107, A, 53.6); printf (%d\t%c\t%5.1f\n, 1754, F, 122.0); printf (%d\t%c\t%5.1f\n, 3, P, 0.1); NOTE : Results : \t tab character 23 Z 14.2 to separate by tab 107 A 53.6 \n newline character 1754 F 122.0 to print on new 3 P 0.1 line
Code c d f
Example %c %d %f %f
Example 1
214 156 14Z
Note that a space between the 14 and the Z would create an error because % does not skip whitespace ! To prevent this program, put a space before the %c code as shown below : scanf (%d%d%d %c, &a, &b, &c, &d);
Example 2
2314 15 2.14 scanf (%d %d %f, &a, &b, &c);
Note the white-space between the field specifi-cation These spaces are not necessary, but it is a good practice to include them
Example 3
14/26 25/66
Note the slashes (/) in the format string. They are not part of field specification. They must be entered exactly as shown. Or scanf will stop reading.
Example 4
11-25-56
scanf (%d-%d-%d, &a, &b, &c);
Again, we see some required user input between the month, day, and year.