Note: ANSI C refers to ISO/IEC C. The source code for this Module is: C & C++ source codes and the practice worksheets: C & C++ basic data types and the C & C++ standard input, scanf()/scanf_s().
The C/C++ programming skills that supposed to be acquired:
2.6 Escape Sequence
| |||||||||||||||||||||||||
| Sequence | Value (hex) | Char | What it does |
| \a | 0x07 | BEL | Audible bell |
| \b | 0x08 | BS | Backspace |
| \f | 0x0C | FF | Formfeed |
| \n | 0x0A | LF | Newline (linefeed) |
| \r | 0x0D | CR | Carriage return |
| \t | 0x09 | HT | Tab (horizontal) |
| \v | 0x0B | VT | Vertical tab |
| \\ | 0x5c | \ | Backslash |
| \' | 0x27 | ' | Single quote (apostrophe) |
| \" | 0x22 | " | Double quote |
| \? | 0x3F | ? | Question mark |
| \o | - | any | o=a string of up to three octal digits |
| \xH | - | any | H=a string of hex digits |
| \XH | - | any | H=a string of hex digits |
|
Table 2.7: Example of Borland C++ escape sequence | |||
Values that do not change during program execution.
Can be integer, character or floating point type.
To declare a constant, use keyword const as shown in the following variable declaration example:
const int day_in_week = 7;
const float total_loan = 1100000.35;
Consider the following example. Compiler will generate error when we try to change the PI.
#include <stdio.h>
int main()
{
const double PI = 3.14;
double rad = 3.5;
printf("Circle area = %.2f\n", PI*rad*rad);
printf("Circle circumference = %.2f\n", 2*PI*rad);
printf("Circle diameter = %.2f\n", 2*rad);
// error C3892: 'PI' : you cannot assign to a variable that is const
// PI = 3.14 + 5.0;
return 0;
}
A sample output:

A character constant is any character enclosed between two single quotation marks (' and ').
When several characters are enclosed between two double quotation marks (" and "), it is called a string.
Character constants examples:
'$' '*' ' ' 'z' 'P'
String constants examples, note that the blank space(s) considered as string:
"Name: "
"Type of Fruit"
"Day: "
" "
You will learn other aggregate or derived data type specifiers such as struct, union, enum and typedef in other Modules or in the program examples.
During the program development, you may encounter the situations where you need to convert to the different data type from previously declared variables, as well as having mixed data type in one expression.
For example, let say you have declared the following variables:
int total, number;
float average;
But in the middle of your program you encountered the following expression:
average = total / number;
This expression has mixed data type, int and float. The value of the average will be truncated, and it is not accurate anymore. Many compilers will generate warning and some do not, but the output will be inaccurate.
C provides the unary (take one operand only) typecast operator to accomplish this task. The previous expression can be re written as
average = (float) total / number;
This (float) is called type cast operator, which create temporary floating-point copy of the total operand. The construct for this typecast operator is formed by placing parentheses around a data type name as:
(type) such as (int), (float) and (char).
|
Basically, the C data types fall into general categories. The "integral types" include char, int, short, long, signed, unsigned, and enum. The "floating types" include float, double, and long double. The "arithmetic types" include all floating and integral types.
| Type | Storage |
| char, unsigned char, signed char | 1 byte |
| short, unsigned short | 2 bytes |
| int, unsigned int | 4 bytes |
| long, unsigned long | 4 bytes |
| float | 4 bytes |
| double | 8 bytes |
| long double | 8 bytes |
Conversion of an operand value to a compatible type causes no change to the value or the representation. For example, from int to int and float to float. However for different type in same category such as the arithmetic types, the narrow conversion will lose the precision. For example from float to int and from double to float.

Explicit and implicit type conversion example:
#include <stdio.h>
int main()
{
short a = 30;
int b = 200;
// use f or F to force to float
float c = 300.34F;
double d = 10000.99;
// narrow conversion, compiler warning
// warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
c = d + d;
// warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
b = c + c;
printf("c = %.2f\n", c);
printf("b = %d\n", b);
// explicit conversion, may lose precision, no compiler warning
c = (float)(d + d);
d = (double)(c + c);
printf("\nc = %.2f\n", c);
printf("b = %d\n", b);
// wide conversion, it is OK
b = a + a;
d = c + c;
printf("\nb = %d\n", b);
printf("d = %.2f\n", d);
return 0;
}
A sample output:

The mixed integer and float for division issue. Study the source code and the output.
#include <stdio.h>
int main()
{
float p = 0.0;
int r = 4, s = 5;
// integer division gives an integer result. Mixed integer and floating
// point expression give a floating point result.
//
// make sure the format specifier is matched
//
// implicit conversion
printf("3/2 = %d\n", 3/2);
printf("\n3.0/2.0 = %.2f\n", 3.0/2.0);
printf("\n3/2.0 = %.2f\n", 3/2.0);
printf("\n3.0/2 = %.2f\n", 3.0/2);
printf("\n1/2 = %d\n", 1/2);
// explicit conversion
printf("\n(float)3/2 = %.2f\n", (float)3/2);
p = r/s;
// warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
// fraction part has been discarded
printf("\np = r/s = %.2f\n", p);
p = (float)r/s;
printf("\np = (float)r/s = %.2f\n", p);
return 0;
}
A sample output:
The following Table list format specifiers used in standard input and output C functions for VC++.
| Data type | printf conversion specification | scanf conversion specification |
| long double | %Lf | %Lf |
| double | %f | %lf |
| float | %f | %f |
| unsigned long int | %lu | %lu |
| long int | %ld | %ld |
| unsigned int | %u | %u |
| int | %d | %d |
| short | %hd | %hd |
| char | %c | %c |
|
Table 2.8: type promotion precedence, top = highest | ||
Sometime you may find a length modifier as listed in the following table.
| Modifier | Description |
| l (letter ell) | Indicates that the argument is a long or unsigned long. |
| L | Indicates that the argument is a long double. |
| h | Indicates that the corresponding argument is to be printed as a short or unsigned short. |
|
Table 2.9: Length modifier | |
The following table is a list of the ANSI C formatted output conversion of the printf()/printf_s() function, used with %. The program examples are presented in C Formatted Input/Output.
| Character | Argument type | Converted to |
| c | int | single character, after conversion to unsigned char. |
| d, i | int | Signed decimal notation. |
| e, E | double | Decimal notation of the form [-]m.de±xx or [-]m.dE±xx, where the number of d is specified by the precision. 6 is the default precision, 0 suppresses the decimal point. Example: -123.434E-256. |
| f | double | Decimal notation of the form [-]m.d, where the d is specified by the precision. 6 is the default precision, 0 suppresses the decimal point. Example: 234.123456. |
| g, G | double | %e or %E is used if the exponent is less than -4 or greater than or equal to the precision; otherwise %f is used. Trailing zeros or a trailing decimal point is not printed. |
| n | int * | The number of characters written so far by this call to printf() is written into the argument. No argument is converted. |
| o | int | Unsigned octal notation (without a leading zero). |
| p | void | Print as a pointer (implementation dependent). |
| s | char * | Characters from the string are printed until ‘\0’ is reached or until the number of characters indicated by the precision has been printed. |
| u | int | Unsigned decimal notation. |
| x, X | int | Unsigned hexadecimal notation (without a leading 0x or 0X), use abcd for 0x or ABCD for 0X. |
| % | - | No argument is converted; just print a %. |
|
Table 2.10: printf() formatted output conversion | ||
The following table is a list of ANSI C formatted input conversion of the scanf()/scanf_s() function. We will learn more detail about this in another Module.
| Character | Input Data | Argument Type |
| c | Characters. | char *. The next input characters are placed in the indicated array, up to the number given by the width field; 1 is the default. No ‘\0’ is added. The normal skip over white space characters is suppressed in this case; use %1s to read the next non-white space character. |
| d | Decimal integer. | int * |
| i | Integer. | int *. The integer may be in octal (with leading 0) or hexadecimal (with leading 0x or 0X). |
| n | Writes into the argument the number of characters read so far by this call. | int *. No input is read. The converted item count is not incremented. |
| o | Octal integer, with or without leading zero. | int *. |
| p | Pointer value as printed by printf("%p"). | void *. |
| s | String of non-white space characters, not quoted. | char *. Pointing to an array of characters large enough to hold the string and a terminating ‘\0’ that will be appended. |
| u | Unsigned decimal integer. | unsigned int * |
| x | Hexadecimal integer, with or without leading 0x or 0X. | int *. |
| e, f, g | Floating-point number. | float *. The input format for float’s is an optional sign, a string of numbers possibly containing a decimal point, and an optional exponent field containing an E or e followed by a possibly signed integer. |
| […] | Matches the longest non-empty string of input characters from the set between brackets. | char *. A ‘\0’ is appended. [ ]…] will include ] in the set. |
| [^…] | Matches the longest non-empty string of input characters not from the set between brackets. | char *. A ‘\0’ is appended. [^]…] will include ] in the set. |
| % | Literal %. | No assignment is made. |
|
Table 2.11: scanf() formatted input conversion | ||