Download presentation
Presentation is loading. Please wait.
1
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004
2
Identifiers C source code is made up of various characters typed at the keyboard: The 52 uppercase and lowercase alphabetic characters. The 10 decimal digits (0,1,…,9). Various punctuation and graphic characters (+,-, {, &, !, etc.). White space (space character, tabulation). The name that a variable or function can take is called its identifier. Identifiers can consist of letters, digits, and underscore characters, but must not begin with a digit. The limit on the size of an identifier is set by the particular C implementation you are using.
3
Choosing Identifiers Here are examples of valid identifiers: Speed count1 first_name _result BankAccNumber Person133 Here are some invalid identifiers: 4sure percent% Tips Be consistent with the naming scheme you adopt. Two popular conventions are to SquashNamesTogether or to use_many_underscores. Choose descriptive variable names – make code self-documenting.
4
Reserved Words C has a set of reserved words that you cannot use as identifiers. The following table gives a list of the reserved words: auto break case char class const continue default do double else entry enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while
5
C Data Types C provides a comprehensive set of data types. A programmer can also define his/her own types. Type abstraction aids expressiveness, readability and correctness checking. Any number of variables can be created of a given type. The fundamental data types are: char, signed char, unsigned char, signed short int, signed int, signed long int, unsigned short int, unsigned int, unsigned long int, float, double, long double, bool Note: The keyword signed can be omitted and a type will default to the signed variant. Thus int is typically given instead of signed int.
6
Integral Types Numeric variables are an important part of any programming language. The type int is used for representing integers, and char for representing characters. Here is an example where we declare two variables count and answer, which are to have integer and character values respectively. int count; char answer; Variables must always be declared before they are used. These variables can be given initialisations in the declarations. Here are examples: int count = 0; char answer = ‘Y’;
7
Integral Types (continued) More than a single variable can be declared in a single line. For example: int current_size=0, max_size=100; These can be changed: current_size =50; max_size= current_size; Programming Tip It is always a good idea to initialise a variable in its declaration.
8
Binary Numbers Computers use binary (base 2) where each digit is either a 0 or 1. bit: binary digit byte: a group of 8 bits You are familiar with decimal (base 10): 38649 10 = 3 10 4 + 8 10 3 + 6 10 2 + 4 10 + 9 Binary works in a similar way: 110110 2 = 54 10 = 1 2 5 + 1 2 4 + 0 2 3 + 1 2 2 + 1 2 + 0
9
Integer Constants Integer constants can be given in a C program in decimal, octal or hexadecimal notation. If an integer begins with letters 0x or 0X it is in hexadecimal If an integer begins with digit 0 it is in octal Otherwise it is in decimal An integer followed by either the letter l or L indicates it is long. An integer followed by either the letter u or u indicates it is unsigned. Examples: 20 0x14 024 (these are all 20 10 ) 43 -1 0 -345 5665l 1u
10
Integral Types (continued) A char is 1 byte (0-255) A short is usually 2 bytes but can be anything between char and int. An int is usually 4 bytes but is guaranteed at least 2 bytes (-32768 to 32767) A long is usually 4 or 8 bytes but can be anything equal to or bigger than an int and guaranteed to be at least 4 bytes (-2,147,483,648 to 2,147,483,647) An unsigned int can store larger numbers than an int.
11
Size of an integer It is not dictated by the C standard what size an int will be. On some systems it will be two bytes whereas on other systems is could be four bytes. Therefore on a two byte machine, you can represent 2 16 states, the numbers –2 15, …,-1, 0, 1, …, 2 15 -1. You can determine the memory size allocated for a type by using the sizeof operator with single operand, which may be a type or a variable. int x; sizeof(int); sizeof x;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.