Home
Username:
Password:
Advanced C Programming Tutorials

Types of Data / Literals

Subtitles of the Movie

A constant is a value that the program is not able to alter. To define a constant, you simply enter the number. Such a number in a program is called a literal and there are several different formats of literals. The default data type is an int. Any time you write a number that is in the range of an int and don't otherwise specify its type, it is taken by the compiler to be an int. All of these numbers are ints because their type is not specified and they fall within the range that can be held inside an int. To write a long literal, you need to use the letter L to specify that it is an long. These numbers are long values because they have an L at their end. Now, it doesn't matter whether the L is an upper or lowercase but there is a problem. This is the number 42 with a lowercase L. It works. It is a long but because the letter L looks so much like the digit one, you could easily mistake this for the int literal 421. Do yourself a favor and stick with uppercase letter L. I'm going to use uppercase letters in the rest of these examples but lowercase works just as well. The letter L is the only one that's that confusing. Here's another one of those things that I'll bet you didn't know. If you use two Ls, you get a long long. This is a new type of literal which came about from the C99 standard. You can declare an unsigned with the letter U. This is an int by default but you have to declare it as unsigned. Any time that you include a decimal point, the literal becomes a real number. The default size of a real number is double. You can use an upper or lowercase E to specify a positive or a negative exponent but whether you use the E or not, the decimal point must be present. To make a number a float instead of a double, you have to use the letter F. The format is the same as it is for a double. It just has the letter F appended to the end. If you use both the decimal point and the letter L, you've got a long double. All of the examples so far used base-ten digits. But you're not limited to that. Any number that begins with zero and does not contain a decimal point is taken to be base eight; octal. It can only contain the digits zero through seven because those are the only ones that are valid for base eight. Each digit represents three bits in the final binary number. Octal numbers are not used as much as they once were but you will still come across the need for them in some places. More common today is base 16. Any number beginning with a zero X, upper or lowercase, is taken to be a hexadecimal constant. It's made up of the digits zero through nine and the letters A through F. You can have upper or lowercase for those digits. Each digit represents four bits. Any of these numbers, base eight or 16 can be tagged with L for long or U for unsigned but they are normally int values. Character constants may be the most common type of literal. Placing a character between a pair of single quote marks creates a character constant. It is a constant with a type char with a value of the ASCII character that you specified. But there are some ASCII characters that you can not enter from the keyboard. These have special two-character escape sequences that define them. They're all backslash characters followed by a character that assumes a special meaning. The backslash character itself must be escape, so two backslashes in a row results in one backslash character. Any one of these can be used between a couple of single quotes to generate a character constant. You can also use any one of these in a character string to represent one character value. You must not confuse character strings and character constants. A character string is an Array of characters, whereas a character constant is the value of one char. These two items are both literal constants but they are quite different. The first one is an eight-bit integer with a value of ASCII A. The second one is an Array of eight-bit integers with the last one in the Array being set to zero. The Array in this example is only two members in length. The first member holds the ASCII value of A. You can declare string literals as long as you want them to be. If you declare two string literals back to back, the compiler will join them together into one long string. Even they're split from one line to the next. This makes it convenient for you to declare strings as long as you would like. I've covered just about all of them. The only things left are some numerical rules and wide characters. In the midst of a string or in a character constant, you can declare the numeric value that you want for that character. If you follow a backslash with three digits, it's taken to be an octal constant and that value is assigned to your character. The first digit represents the value of the first two bits. The other two digits represent three bits each. If the backslash is followed by an X, upper or lowercase, the next two digits are taken to be the hexadecimal value of the character. C even makes provision for wide characters. By using an L in front of a character literal, you declare it to be a wide character literal; sixteen bits instead of eight. You can also use a numeric literal to declare a wide character. This lets you define the value of all 16 bits in a wide character. There is information about using wide characters later in this course.

Tutorial Information

Course: Advanced C Programming
Author: Arthur Griffith
SKU: 33965
ISBN: 1-935320-24-6
Release Date: 2009-01-30
Duration: 5.5 hrs / 82 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available