2. C FUNDAMENTALS - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

2. C FUNDAMENTALS

Description:

2' C FUNDAMENTALS – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 18
Provided by: Ritwik3
Learn more at: https://www.cise.ufl.edu
Category:

less

Transcript and Presenter's Notes

Title: 2. C FUNDAMENTALS


1
2. C FUNDAMENTALS
2
Example Printing a Message
/ Illustrates comments, strings, and the printf
function / include ltstdio.hgt int
main(void) printf("To C, or not to C ")
printf("that is the question.\n") return 0
3
Comments
  • Begin with / and end with /.
  • / This is a comment /
  • May extend over more than one line.
  • / This comment starts on one line,
  • but ends on another. /
  • Warning Failing to close a comment will cause
    the compiler to ignore part of your program.
  • printf("Hello") / forgot to close this
    comment...
  • printf("Goodbye") / so it ends here /
  • In C99, comments can also be written this way
  • // This is a comment.

4
Example Computing the Volume
/ Illustrates the use of variables, expressions,
and assignment statements / include
ltstdio.hgt int main(void) int height, length,
width, volume height 5 length 20 width
10 volume height length width printf("The
height is d\n", height) printf("The length is
d\n", length) printf("The width is d\n",
width) printf("The volume is d\n",
volume) return 0
5
Variables
Variables must be declared before they are
used. Variables can be declared one at a
time int height int length int width int
volume or several can be declared at the same
time int height, length, width, volume
6
Variables
Always initialize variables before using
them. Wrong int height printf("The height
is d\n", height) Right int height
height 5 printf("The height is d\n",
height) An variable can initialized in its
declaration int height 5 Variable
names must be legal identifiers.
7
Identifiers
Identifiers may contain letters, digits, and
underscores. An identifier must begin with a
letter or underscore. Examples of legal
identifiers times10 get_next_char _done Its
usually best to avoid identifiers that begin with
an underscore.
8
Identifiers
Examples of illegal identifiers 10times get-n
ext-char C is case-sensitive it
distinguishes between upper- and lower-case
letters in identifiers. For example, the
following identifiers are all different eof eoF
eOf eOF Eof EoF EOf EOF
9
Keywords
The following keywords may not be used as
identifiers
  • auto
  • break
  • case
  • char
  • const
  • continue
  • default
  • do
  • double
  • else
  • enum
  • extern
  • float
  • for
  • goto
  • if
  • inline
  • int
  • long
  • return
  • short
  • signed
  • sizeof
  • static
  • struct
  • switch
  • typedef
  • union
  • unsigned
  • void
  • volatile
  • while
  • _Bool
  • _Complex
  • _Imaginary
  • C99 only

10
Keywords
Keywords (with the exception of _Bool,
_Complex, and _Imaginary) and names of library
functions (e.g., printf) must be written using
only lowercase letters.
11
The General Form of a Simple Program
The simplest C programs have the following
form int main(void) declarations
statements The word int may be omitted
(although its good practice to include it). The
use of void is also optional. Each declaration
and each statement (except for compound
statements) must end with a semicolon.
12
The General Form of a Simple Program
C99 does not require that declarations precede
statements. However, each variable must be
declared before it is used for the first time
13
Program Layout
C programs arefor the most partfree-format.
Spaces and newline characters can be deleted,
except where this would cause two symbols to
merge into one include ltstdio.hgt int
main(void)int height,length,width,volumeheight5
length20width10volumeheightlengthwidth p
rintf("The height is d\n",height) printf("The
length is d\n",length) printf("The width is
d\n",width) printf("The volume is
d\n",volume)return 0
14
Program Layout
Extra spaces and newline characters can be
inserted, except where this would divide a
symbol include ltstdio.hgt int main ( void )
int height, length, width, volume height
5 length 20 width 10 volume height
length width printf ( "The height is
d\n" , height ) printf ( "The length is
d\n" , length ) printf ( "The width is
d\n" , width ) printf ( "The volume is
d\n" , volume ) return 0 Note Each
command that begins with must be on a separate
line.
15
Example Converting Fahrenheit to Celsius
/ Illustrates macro definition, floating-point
numbers, and scanf / include
ltstdio.hgt define FREEZING_PT 32.0 define
SCALE_FACTOR (5.0 / 9.0) int main(void) float
fahrenheit, celsius printf("Enter Fahrenheit
temperature ") scanf("f", fahrenheit) celsius
SCALE_FACTOR (fahrenheit -
FREEZING_PT) printf("\nCelsius equivalent is
.1f\n", celsius) return 0
16
Defining Constants
A macro definition has the form define
identifier replacement-list Macros often
represent constants values that wont change
during program execution define N 100 Macro
definitions are handled by the C preprocessor.
Wherever the identifier appears later in the
source file, the preprocessor replaces it by
the replacement list.
17
Defining Constants
If the replacement list contains operators, it is
best to enclose it in parentheses. define EOF
(-1) define TWO_PI (23.14159) By convention,
names of C constants contain only upper-case
letters. Advantages of defining constants
Programs are easier to read. Programs are
easier to modify. Helps reduce errors of
inconsistent use, as well as
typographical errors.
Write a Comment
User Comments (0)
About PowerShow.com