1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C program. ❏ To introduce the include preprocessor command. ❏ To be able to create good identifiers for objects in a program. ❏ To be able to list, describe, and use the C basic data types. ❏ To be able to create and use variables and constants. ❏ To understand input and output concepts. ❏ To be able to use simple input and output statements. Introduction to the C Language
2 2-1 Background C is a structured programming language. It is considered a high-level language because it allows the programmer to concentrate on the problem at hand and not worry about the machine that the program will be using. That is another reason why it is used by software developers whose applications have to run on many different hardware platforms.
Computer Science: A Structured Programming Approach Using C 3 2-2 C Programs It's time to write your first C program. Structure of a C Program Your First C Program Comments The Greeting Program Topics discussed in this section:
4 FIGURE 2-2 Structure of a C Program
5 FIGURE 2-3 The Greeting Program
6 PROGRAM 2-1 The Greeting Program
7 FIGURE 2-4 Examples of Block Comments
8 FIGURE 2-5 Examples of Line Comments
9 2-3 Identifiers One feature present in all computer languages is the identifier. Identifiers allow us to name data and other objects in the program. Each identified object in the computer is stored at a unique address.
10 Table 2-1 Rules for Identifiers
11 An identifier must start with a letter or underscore: it may not have a space or a hyphen. Note
12 C is a case-sensitive language. Note
13 Table 2-2 Examples of Valid and Invalid Names
14 2-4 Types A type defines a set of values and a set of operations that can be applied on those values. Void Type Integral Type Floating-Point Types Topics discussed in this section:
15 FIGURE 2-7 Data Types
16 FIGURE 2-8 Character Types
17 FIGURE 2-9 Integer Types
18 sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long) Note
19 Table 2-3 Typical Integer Sizes and Values for Signed Integers
20 FIGURE 2-10 Floating-point Types
21 sizeof (float) ≤ sizeof (double) ≤ sizeof (long double) Note
22 Table 2-4 Type Summary
23 2-5 Variables Variables are named memory locations that have a type, such as integer or character, which is inherited from their type. The type determines the values that a variable may contain and the operations that may be used with its values. Variable Declaration Variable Initialization Topics discussed in this section:
24 FIGURE 2-11 Variables
25 Table 2-5 Examples of Variable Declarations and Definitions
26 FIGURE 2-12 Variable Initialization ‘B’
27 When a variable is defined, it is not initialized. We must initialize any variable requiring prescribed data when the function starts. Note
28 PROGRAM 2-2 Print Sum of Three Numbers
29 PROGRAM 2-2 Print Sum of Three Numbers (continued)
30 PROGRAM 2-2 Print Sum of Three Numbers (continued)
31 2-6 Constants Constants are data values that cannot be changed during the execution of a program. Like variables, constants have a type. In this section, we discuss Boolean, character, integer, real, complex, and string constants. Constant Representation Coding Constants Topics discussed in this section:
32 A character constant is enclosed in single quotes. Note
33 Table 2-6 Symbolic Names for Control Characters
34 Table 2-7 Examples of Integer Constants
35 Table 2-8 Examples of Real Constants
36 FIGURE 2-13 Some Strings
37 FIGURE 2-14 Null Characters and Null Strings
38 Use single quotes for character constants. Use double quotes for string constants. Note
39 PROGRAM 2-3 Memory Constants
40 PROGRAM 2-3 Memory Constants (continued)
41 PROGRAM 2-4 Enumeration Data Type enum week{sunday, monday, tuesday, wednesday, thursday, friday, saturday}; enum week day; LCD Example
42 // An example program to demonstrate working // of enum in C #include<stdio.h> enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun}; int main() { enum week day; day = Wed; printf("%d",day); return 0; }
43 #include <stdio.h> #include <float.h> int main(){ printf("Storage size for float : %d n", sizeof(float)); printf("Minimum float positive value: %En", FLT_MIN ); printf("Maximum float positive value: %En", FLT_MAX ); printf("Precision value: %dn", FLT_DIG ); return 0; }
44 USER DEFINED (DATA) TYPES Keyword Size Note struct ≥ sum of size of each member An aggregate type which can contain more than one different types. tag or label is optional struct theEmployee { int age; double salary; char department; char name[15]; char address[5][25]; }; struct theEmployee workerRec; typedef struct { int x; int SomeArray[100]; } MyFoo; int main() { MyFoo strctVar; return 0; } struct newPoint { short xPoint; short yPoint; } justPoint; justPoint thePoint;
45 USER DEFINED (DATA) TYPES union ≥ size of the largest member An aggregate type which can contain more than one other types. union uses shared memory space compared to struct, so only one member can be accessed at one time. union someData { int pNum; float qNum; double rNum; }; union someData simpleData; union OtherData{ char aNum; int xNum; float fNum; } simpleData; simpleData saveData;
46 USER DEFINED (DATA) TYPES typedef same as the type; being given a new name typedef used to give new identifier names or alias (to simplify the long identifier names), normally used for aggregate defined types. typedef unsigned char BYTE; /* Declares BYTE to be a synonym for unsigned char */ typedef float FLOAT; /* Declares FLOAT (uppercase letter) to be a synonym for unsigned float (lowercase) */ tag or label is optional typedef struct simpleData { int nData; char cData; } newNameType; Or typedef struct { int nData; char cData;} newNameType; newNameType strctType; typedef struct TOKEN_SOURCE { CHAR SourceName[8]; LUID SourceIdentifier; } TOKEN_SOURCE, *PTOKEN_SOURCE; TOKEN_SOURCE newToken; typedef union unData{ double lngSalary; int nDay; }newUntype; newUnType lntotalSalary; typedef enum DayNames { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } Weekdays; Weekdays dayOfWeek;
47 Type Size Note type* (a pointer) ≥ size of char  Hold the memory address which point to the actual data/value.  0 address always represents the null pointer (an address where no data can be placed), irrespective of what bit sequence represents the value of a null pointer.  Pointers to different types will have different sizes. So they are not convertible to one another.  Even in an implementation which guarantees all data pointers to be of the same size, function pointers and data pointers are in general incompatible with each other.  For functions taking a variable number of arguments, the arguments passed must be of appropriate type. char *ptoChar; char csimpleChr = 'T'; char *chptr; // assignment chptr = &csimpleChr; int iNumber = 20; int *imyPtr = &iNumber; DERIVED (DATA) TYPES
48 DERIVED (DATA) TYPES type [integer] (an array) ≥ integer × size of type  Use to declare a variable with collection of identical properties or types.  Simplify variable declaration.  In a declaration which also initializes the array (including a function parameter declaration), the size of the array (the integer) can be omitted, which is called unsized.  type [ ] is not the same as type*. Only under some circumstances one can be converted to the other. int fstudentNumber[3] = {4,7,1}; int nrowandColumn[1][2] = {34, 21}; int nlongHeightWidth[3][4][5] = 0; char cName1[ ] = {'a','r','r','a','y'}; char cName2[ ] = {"array"}; char cName3[6] = "array"; int nrowCol[2][3] = {4,2,3,7,2,8};

C Programming Intro.ppt

  • 1.
    1 Objectives ❏ To understandthe structure of a C-language program. ❏ To write your first C program. ❏ To introduce the include preprocessor command. ❏ To be able to create good identifiers for objects in a program. ❏ To be able to list, describe, and use the C basic data types. ❏ To be able to create and use variables and constants. ❏ To understand input and output concepts. ❏ To be able to use simple input and output statements. Introduction to the C Language
  • 2.
    2 2-1 Background C isa structured programming language. It is considered a high-level language because it allows the programmer to concentrate on the problem at hand and not worry about the machine that the program will be using. That is another reason why it is used by software developers whose applications have to run on many different hardware platforms.
  • 3.
    Computer Science: AStructured Programming Approach Using C 3 2-2 C Programs It's time to write your first C program. Structure of a C Program Your First C Program Comments The Greeting Program Topics discussed in this section:
  • 4.
    4 FIGURE 2-2 Structureof a C Program
  • 5.
    5 FIGURE 2-3 TheGreeting Program
  • 6.
    6 PROGRAM 2-1 TheGreeting Program
  • 7.
    7 FIGURE 2-4 Examplesof Block Comments
  • 8.
    8 FIGURE 2-5 Examplesof Line Comments
  • 9.
    9 2-3 Identifiers One featurepresent in all computer languages is the identifier. Identifiers allow us to name data and other objects in the program. Each identified object in the computer is stored at a unique address.
  • 10.
    10 Table 2-1 Rulesfor Identifiers
  • 11.
    11 An identifier muststart with a letter or underscore: it may not have a space or a hyphen. Note
  • 12.
    12 C is acase-sensitive language. Note
  • 13.
    13 Table 2-2 Examplesof Valid and Invalid Names
  • 14.
    14 2-4 Types A typedefines a set of values and a set of operations that can be applied on those values. Void Type Integral Type Floating-Point Types Topics discussed in this section:
  • 15.
  • 16.
  • 17.
  • 18.
    18 sizeof (short) ≤sizeof (int) ≤ sizeof (long) ≤ sizeof (long long) Note
  • 19.
    19 Table 2-3 TypicalInteger Sizes and Values for Signed Integers
  • 20.
  • 21.
    21 sizeof (float) ≤sizeof (double) ≤ sizeof (long double) Note
  • 22.
  • 23.
    23 2-5 Variables Variables arenamed memory locations that have a type, such as integer or character, which is inherited from their type. The type determines the values that a variable may contain and the operations that may be used with its values. Variable Declaration Variable Initialization Topics discussed in this section:
  • 24.
  • 25.
    25 Table 2-5 Examplesof Variable Declarations and Definitions
  • 26.
    26 FIGURE 2-12 VariableInitialization ‘B’
  • 27.
    27 When a variableis defined, it is not initialized. We must initialize any variable requiring prescribed data when the function starts. Note
  • 28.
    28 PROGRAM 2-2 PrintSum of Three Numbers
  • 29.
    29 PROGRAM 2-2 PrintSum of Three Numbers (continued)
  • 30.
    30 PROGRAM 2-2 PrintSum of Three Numbers (continued)
  • 31.
    31 2-6 Constants Constants aredata values that cannot be changed during the execution of a program. Like variables, constants have a type. In this section, we discuss Boolean, character, integer, real, complex, and string constants. Constant Representation Coding Constants Topics discussed in this section:
  • 32.
    32 A character constantis enclosed in single quotes. Note
  • 33.
    33 Table 2-6 SymbolicNames for Control Characters
  • 34.
    34 Table 2-7 Examplesof Integer Constants
  • 35.
    35 Table 2-8 Examplesof Real Constants
  • 36.
  • 37.
    37 FIGURE 2-14 NullCharacters and Null Strings
  • 38.
    38 Use single quotesfor character constants. Use double quotes for string constants. Note
  • 39.
  • 40.
    40 PROGRAM 2-3 MemoryConstants (continued)
  • 41.
    41 PROGRAM 2-4 EnumerationData Type enum week{sunday, monday, tuesday, wednesday, thursday, friday, saturday}; enum week day; LCD Example
  • 42.
    42 // An exampleprogram to demonstrate working // of enum in C #include<stdio.h> enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun}; int main() { enum week day; day = Wed; printf("%d",day); return 0; }
  • 43.
    43 #include <stdio.h> #include <float.h> intmain(){ printf("Storage size for float : %d n", sizeof(float)); printf("Minimum float positive value: %En", FLT_MIN ); printf("Maximum float positive value: %En", FLT_MAX ); printf("Precision value: %dn", FLT_DIG ); return 0; }
  • 44.
    44 USER DEFINED (DATA)TYPES Keyword Size Note struct ≥ sum of size of each member An aggregate type which can contain more than one different types. tag or label is optional struct theEmployee { int age; double salary; char department; char name[15]; char address[5][25]; }; struct theEmployee workerRec; typedef struct { int x; int SomeArray[100]; } MyFoo; int main() { MyFoo strctVar; return 0; } struct newPoint { short xPoint; short yPoint; } justPoint; justPoint thePoint;
  • 45.
    45 USER DEFINED (DATA)TYPES union ≥ size of the largest member An aggregate type which can contain more than one other types. union uses shared memory space compared to struct, so only one member can be accessed at one time. union someData { int pNum; float qNum; double rNum; }; union someData simpleData; union OtherData{ char aNum; int xNum; float fNum; } simpleData; simpleData saveData;
  • 46.
    46 USER DEFINED (DATA)TYPES typedef same as the type; being given a new name typedef used to give new identifier names or alias (to simplify the long identifier names), normally used for aggregate defined types. typedef unsigned char BYTE; /* Declares BYTE to be a synonym for unsigned char */ typedef float FLOAT; /* Declares FLOAT (uppercase letter) to be a synonym for unsigned float (lowercase) */ tag or label is optional typedef struct simpleData { int nData; char cData; } newNameType; Or typedef struct { int nData; char cData;} newNameType; newNameType strctType; typedef struct TOKEN_SOURCE { CHAR SourceName[8]; LUID SourceIdentifier; } TOKEN_SOURCE, *PTOKEN_SOURCE; TOKEN_SOURCE newToken; typedef union unData{ double lngSalary; int nDay; }newUntype; newUnType lntotalSalary; typedef enum DayNames { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } Weekdays; Weekdays dayOfWeek;
  • 47.
    47 Type Size Note type* (apointer) ≥ size of char  Hold the memory address which point to the actual data/value.  0 address always represents the null pointer (an address where no data can be placed), irrespective of what bit sequence represents the value of a null pointer.  Pointers to different types will have different sizes. So they are not convertible to one another.  Even in an implementation which guarantees all data pointers to be of the same size, function pointers and data pointers are in general incompatible with each other.  For functions taking a variable number of arguments, the arguments passed must be of appropriate type. char *ptoChar; char csimpleChr = 'T'; char *chptr; // assignment chptr = &csimpleChr; int iNumber = 20; int *imyPtr = &iNumber; DERIVED (DATA) TYPES
  • 48.
    48 DERIVED (DATA) TYPES type[integer] (an array) ≥ integer × size of type  Use to declare a variable with collection of identical properties or types.  Simplify variable declaration.  In a declaration which also initializes the array (including a function parameter declaration), the size of the array (the integer) can be omitted, which is called unsized.  type [ ] is not the same as type*. Only under some circumstances one can be converted to the other. int fstudentNumber[3] = {4,7,1}; int nrowandColumn[1][2] = {34, 21}; int nlongHeightWidth[3][4][5] = 0; char cName1[ ] = {'a','r','r','a','y'}; char cName2[ ] = {"array"}; char cName3[6] = "array"; int nrowCol[2][3] = {4,2,3,7,2,8};

Editor's Notes

  • #3 Developed early 1970’s
  • #17 wchar_t is a wide character:  The increased datatype size allows for the use of larger coded character sets. Width is compiler specific (not portable).