SlideShare a Scribd company logo
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};
Ad

Recommended

structured Programming Unit-2-Basic-Elements-of-C.pptx
structured Programming Unit-2-Basic-Elements-of-C.pptx
SuryaBasnet1
 
Theory1&amp;2
Theory1&amp;2
Dr.M.Karthika parthasarathy
 
Intro
Intro
CGC Technical campus,Mohali
 
6276830.ppt
6276830.ppt
Shrinivas54
 
CODING-DAY-2-INTRODUCTION TO C PROGRAMMING.ppt
CODING-DAY-2-INTRODUCTION TO C PROGRAMMING.ppt
adamjackson818417
 
C language by Dr. Gholkar D. R.
C language by Dr. Gholkar D. R.
drgholkar
 
C programming notes.pdf
C programming notes.pdf
AdiseshaK
 
unit 1 (1).pptx
unit 1 (1).pptx
PriyadarshiniS28
 
C programming notes
C programming notes
Prof. Dr. K. Adisesha
 
Data Type in C Programming
Data Type in C Programming
Qazi Shahzad Ali
 
introductory concepts
introductory concepts
Walepak Ubi
 
Programming construction tools
Programming construction tools
sunilchute1
 
introduction to programming using ANSI C
introduction to programming using ANSI C
JNTUK KAKINADA
 
Lesson 4 Basic Programming Constructs.pptx
Lesson 4 Basic Programming Constructs.pptx
John Burca
 
Features and Fundamentals of C Language for Beginners
Features and Fundamentals of C Language for Beginners
Dr. Chandrakant Divate
 
Semester-1-22-23-Introduction_to_c_programming.ppt
Semester-1-22-23-Introduction_to_c_programming.ppt
ChetanChauhan203001
 
Introduction_to_c_programming.ppt for software developing students
Introduction_to_c_programming.ppt for software developing students
Asfiya14
 
C.ppt
C.ppt
ShajiAvaroth1
 
C Programming language basic for students
C Programming language basic for students
MohammedRizwanSharie
 
Ch2 introduction to c
Ch2 introduction to c
Hattori Sidek
 
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
KathanPatel49
 
C++ programming
C++ programming
Anshul Mahale
 
Introduction To C
Introduction To C
gscprasad1111
 
Savitch ch 02
Savitch ch 02
Terry Yoast
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
Basic Information About C language PDF
Basic Information About C language PDF
Suraj Das
 
C Programming Unit-1
C Programming Unit-1
Vikram Nandini
 
C prog ppt
C prog ppt
xinoe
 
Embedded Systems.pptx
Embedded Systems.pptx
LECO9
 
Basic Electronics.pptx
Basic Electronics.pptx
LECO9
 

More Related Content

Similar to C Programming Intro.ppt (20)

C programming notes
C programming notes
Prof. Dr. K. Adisesha
 
Data Type in C Programming
Data Type in C Programming
Qazi Shahzad Ali
 
introductory concepts
introductory concepts
Walepak Ubi
 
Programming construction tools
Programming construction tools
sunilchute1
 
introduction to programming using ANSI C
introduction to programming using ANSI C
JNTUK KAKINADA
 
Lesson 4 Basic Programming Constructs.pptx
Lesson 4 Basic Programming Constructs.pptx
John Burca
 
Features and Fundamentals of C Language for Beginners
Features and Fundamentals of C Language for Beginners
Dr. Chandrakant Divate
 
Semester-1-22-23-Introduction_to_c_programming.ppt
Semester-1-22-23-Introduction_to_c_programming.ppt
ChetanChauhan203001
 
Introduction_to_c_programming.ppt for software developing students
Introduction_to_c_programming.ppt for software developing students
Asfiya14
 
C.ppt
C.ppt
ShajiAvaroth1
 
C Programming language basic for students
C Programming language basic for students
MohammedRizwanSharie
 
Ch2 introduction to c
Ch2 introduction to c
Hattori Sidek
 
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
KathanPatel49
 
C++ programming
C++ programming
Anshul Mahale
 
Introduction To C
Introduction To C
gscprasad1111
 
Savitch ch 02
Savitch ch 02
Terry Yoast
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
Basic Information About C language PDF
Basic Information About C language PDF
Suraj Das
 
C Programming Unit-1
C Programming Unit-1
Vikram Nandini
 
C prog ppt
C prog ppt
xinoe
 
Data Type in C Programming
Data Type in C Programming
Qazi Shahzad Ali
 
introductory concepts
introductory concepts
Walepak Ubi
 
Programming construction tools
Programming construction tools
sunilchute1
 
introduction to programming using ANSI C
introduction to programming using ANSI C
JNTUK KAKINADA
 
Lesson 4 Basic Programming Constructs.pptx
Lesson 4 Basic Programming Constructs.pptx
John Burca
 
Features and Fundamentals of C Language for Beginners
Features and Fundamentals of C Language for Beginners
Dr. Chandrakant Divate
 
Semester-1-22-23-Introduction_to_c_programming.ppt
Semester-1-22-23-Introduction_to_c_programming.ppt
ChetanChauhan203001
 
Introduction_to_c_programming.ppt for software developing students
Introduction_to_c_programming.ppt for software developing students
Asfiya14
 
C Programming language basic for students
C Programming language basic for students
MohammedRizwanSharie
 
Ch2 introduction to c
Ch2 introduction to c
Hattori Sidek
 
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
KathanPatel49
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
Basic Information About C language PDF
Basic Information About C language PDF
Suraj Das
 
C prog ppt
C prog ppt
xinoe
 

More from LECO9 (20)

Embedded Systems.pptx
Embedded Systems.pptx
LECO9
 
Basic Electronics.pptx
Basic Electronics.pptx
LECO9
 
Intro to Microcontroller.pptx
Intro to Microcontroller.pptx
LECO9
 
PIC_Intro.pptx
PIC_Intro.pptx
LECO9
 
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
LECO9
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
LECO9
 
UNIONS IN C.pptx
UNIONS IN C.pptx
LECO9
 
Processes, Threads.pptx
Processes, Threads.pptx
LECO9
 
OPERATORS IN C.pptx
OPERATORS IN C.pptx
LECO9
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
LECO9
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
LECO9
 
DESIGN PATTERN.pptx
DESIGN PATTERN.pptx
LECO9
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
LECO9
 
cprogramming Structures.pptx
cprogramming Structures.pptx
LECO9
 
POINTERS.pptx
POINTERS.pptx
LECO9
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
LECO9
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
LECO9
 
cprogramming strings.pptx
cprogramming strings.pptx
LECO9
 
C-Programming Control statements.pptx
C-Programming Control statements.pptx
LECO9
 
C-Programming Function pointers.pptx
C-Programming Function pointers.pptx
LECO9
 
Embedded Systems.pptx
Embedded Systems.pptx
LECO9
 
Basic Electronics.pptx
Basic Electronics.pptx
LECO9
 
Intro to Microcontroller.pptx
Intro to Microcontroller.pptx
LECO9
 
PIC_Intro.pptx
PIC_Intro.pptx
LECO9
 
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
LECO9
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
LECO9
 
UNIONS IN C.pptx
UNIONS IN C.pptx
LECO9
 
Processes, Threads.pptx
Processes, Threads.pptx
LECO9
 
OPERATORS IN C.pptx
OPERATORS IN C.pptx
LECO9
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
LECO9
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
LECO9
 
DESIGN PATTERN.pptx
DESIGN PATTERN.pptx
LECO9
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
LECO9
 
cprogramming Structures.pptx
cprogramming Structures.pptx
LECO9
 
POINTERS.pptx
POINTERS.pptx
LECO9
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
LECO9
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
LECO9
 
cprogramming strings.pptx
cprogramming strings.pptx
LECO9
 
C-Programming Control statements.pptx
C-Programming Control statements.pptx
LECO9
 
C-Programming Function pointers.pptx
C-Programming Function pointers.pptx
LECO9
 
Ad

Recently uploaded (20)

Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
hosseinihamid192023
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Mark Billinghurst
 
(Continuous Integration and Continuous Deployment/Delivery) is a fundamental ...
(Continuous Integration and Continuous Deployment/Delivery) is a fundamental ...
ketan09101
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Mark Billinghurst
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
20CE404-Soil Mechanics - Slide Share PPT
20CE404-Soil Mechanics - Slide Share PPT
saravananr808639
 
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
 
Fatality due to Falls at Working at Height
Fatality due to Falls at Working at Height
ssuserb8994f
 
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
 
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
machine learning is a advance technology
machine learning is a advance technology
ynancy893
 
How to Un-Obsolete Your Legacy Keypad Design
How to Un-Obsolete Your Legacy Keypad Design
Epec Engineered Technologies
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
hosseinihamid192023
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Mark Billinghurst
 
(Continuous Integration and Continuous Deployment/Delivery) is a fundamental ...
(Continuous Integration and Continuous Deployment/Delivery) is a fundamental ...
ketan09101
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Mark Billinghurst
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
20CE404-Soil Mechanics - Slide Share PPT
20CE404-Soil Mechanics - Slide Share PPT
saravananr808639
 
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
 
Fatality due to Falls at Working at Height
Fatality due to Falls at Working at Height
ssuserb8994f
 
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
 
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
machine learning is a advance technology
machine learning is a advance technology
ynancy893
 
Ad

C Programming Intro.ppt

  • 1. 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 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.
  • 3. 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. 4 FIGURE 2-2 Structure of a C Program
  • 5. 5 FIGURE 2-3 The Greeting Program
  • 6. 6 PROGRAM 2-1 The Greeting Program
  • 7. 7 FIGURE 2-4 Examples of Block Comments
  • 8. 8 FIGURE 2-5 Examples of Line Comments
  • 9. 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. 10 Table 2-1 Rules for Identifiers
  • 11. 11 An identifier must start with a letter or underscore: it may not have a space or a hyphen. Note
  • 12. 12 C is a case-sensitive language. Note
  • 13. 13 Table 2-2 Examples of Valid and Invalid Names
  • 14. 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:
  • 18. 18 sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long) Note
  • 19. 19 Table 2-3 Typical Integer Sizes and Values for Signed Integers
  • 21. 21 sizeof (float) ≤ sizeof (double) ≤ sizeof (long double) Note
  • 22. 22 Table 2-4 Type Summary
  • 23. 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:
  • 25. 25 Table 2-5 Examples of Variable Declarations and Definitions
  • 26. 26 FIGURE 2-12 Variable Initialization ‘B’
  • 27. 27 When a variable is defined, it is not initialized. We must initialize any variable requiring prescribed data when the function starts. Note
  • 28. 28 PROGRAM 2-2 Print Sum of Three Numbers
  • 29. 29 PROGRAM 2-2 Print Sum of Three Numbers (continued)
  • 30. 30 PROGRAM 2-2 Print Sum of Three Numbers (continued)
  • 31. 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. 32 A character constant is enclosed in single quotes. Note
  • 33. 33 Table 2-6 Symbolic Names for Control Characters
  • 34. 34 Table 2-7 Examples of Integer Constants
  • 35. 35 Table 2-8 Examples of Real Constants
  • 37. 37 FIGURE 2-14 Null Characters and Null Strings
  • 38. 38 Use single quotes for character constants. Use double quotes for string constants. Note
  • 40. 40 PROGRAM 2-3 Memory Constants (continued)
  • 41. 41 PROGRAM 2-4 Enumeration Data Type enum week{sunday, monday, tuesday, wednesday, thursday, friday, saturday}; enum week day; LCD Example
  • 42. 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. 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. 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* (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. 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).