Showing posts with label C Constants and Literals. Show all posts
Showing posts with label C Constants and Literals. Show all posts

Sunday, January 24, 2016

String Constants/Literals

String constants consist of any number of consecutive characters in enclosed quotation marks ("). String constants can have any valid character as part of it, including escape sequences. Example of string literal is given below
"Hello World!"
Some string constants can span multiple lines like the one given below
"First line \nSecond line \nThird line"
// Above statement when printed will look like as given below
First line 
Second line 
Third line
If you want to declare a string constant with quotation marks or backslash as part of the string, then you have to include proper escape character (\) for that. For example if you want a double quote (") as part of your string (like - Press the "Enter" button) you can do it as given below

Thursday, January 21, 2016

Character constants

Character constants hold a single character enclosed in single apostrophes. Characters are short integers. So, it can hold an integer value as well (maximum value character constants can hold is 127 and 255 for unsigned character constants). Also, different characters have associated integer values (like 'A' is 65, 'a' is 97 etc.). These values are standard and known as ASCII (American Standard Code for Information Interchange).
There are some characters which have special use in C language compared to other characters. They are preceeded by a backslash(\) (like \n, \b). These characters are called escape sequences. Below, you can find examples of escape sequences available in C.
CharactersEscape sequence
bell\a
backspace\b
horizontal tab\t
vertical tab\v
newline\n
form feed\f
carriage return\r
quotation mark\"
apostrophe\'
question mark\?
backslash\\
null\0
For example, using '\n' in printf statement will feed a line in the output. Character constants can hold escape sequences too. Examples of few of the character constants are given below.

Wednesday, January 20, 2016

Floating Point Constants/Literals

Floating point constants represents the floating point numbers, as the name suggests ie; numbers like 1.2, 3.6894 etc. In C programming language, we can represent a floating point constant in two ways,
  • Decimal point format or in
  • Exponent format.
In order to represent a floating point constant in decimal format, you need to include the decimal point (.) in the number. Floating point numbers can be expressed in exponent format like 1.23E-4 or 1.23e-4. It is another way to represent 1.23 x 10-4. The number after E can be signed, but should always be an integer. So, 3.56E1.2 is an invalid floating point constant. Some valid floating point constants in C are given below
3.968421
3968421E-6L
// Both of the above float literals represent the same value.

Tuesday, January 19, 2016

C Constants and Literals

Constants in C programming language, as the name suggests are the data that doesn't change. Constants are also known as literals. There are five types of constants or literals available in C programming language as shown below.
  • Integer Constants
  • Floating-point Constants
  • Character Constants
  • String Constants
  • Enumeration Constants

Integer Constants/Literals

Integer constants, as the name suggests, are integer values ie; numbers like 1, 20 etc. In C programming language, we can have integer constants of 3 different bases (or radix) namedDecimal (Base 10), Hexadecimal (Base 16) or Octal (base 8). During the declaration of an integer constant, a prefix is used along with the value to specify the base or radix.
  • For decimal literals, no prefix is used.
  • Prefix used for hexadecimal: 0x / 0X
  • Prefix used for octal: 0

Examples of different integer constants
123        /* decimal constant*/
0x9b       /* hexadecimal constant*/
0X9c       /* hexadecimal constant*/
0456       /* octal constant*/
In C programming, you can use upper case or lower case letters (ABCDEF or abcdef) as part of hexadecimal literals.