Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
42 views

C Programming Language

The document discusses the basics of C programming language files and code structure. It explains that C source code files use the .c extension and contain preprocessor directives, comments, and C language statements. Header files use the .h extension and contain guard directives to prevent multiple inclusions. The code is organized with descriptive comments and sections for preprocessing, main function, and statements. Identifiers in C have scope at the file, block, function, and function prototype level. Objects represent stored values and have declaration, definition, and lifetimes in various storage types like automatic, static, and allocated memory. Fundamental data types include characters and integers.

Uploaded by

Kirk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

C Programming Language

The document discusses the basics of C programming language files and code structure. It explains that C source code files use the .c extension and contain preprocessor directives, comments, and C language statements. Header files use the .h extension and contain guard directives to prevent multiple inclusions. The code is organized with descriptive comments and sections for preprocessing, main function, and statements. Identifiers in C have scope at the file, block, function, and function prototype level. Objects represent stored values and have declaration, definition, and lifetimes in various storage types like automatic, static, and allocated memory. Fundamental data types include characters and integers.

Uploaded by

Kirk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

C Programming Language

Slides by Russ hersberger based on N2454 draft of ISO/IEC


1
9899:202x
Basic C Program Files
• One or more text files
• Do not use a word processor.
• Use a basic text editor such as Notepad, Notepad++, Sublime Text, Visual Studio
Code, or many others that are available.
• File names:
• prog_name.c , the .c extension identifies the file as c language source code.
• header_name.h , the .h extension identifies the file a c language header.
• File names are generally lowercase with no spaces or other whitespace
characters.
• The underscore, considered a letter, is used to separate words in place of a
space.

Slides by Russ hersberger based on N2454 draft of ISO/IEC


2
9899:202x
C Header File Layout (*.h)
• Guard directives
• A set of directives:
• For including other files (usually header
files).
• Called text replacement macros.
• For conditionally controlling
preprocessing flow.
• Guard terminal directive

Slides by Russ hersberger based on N2454 draft of ISO/IEC


3
9899:202x
Basic C Source Code Files (*.c)
• Contain:
• A descriptive set of comments
• A sequence of preprocessing
directives for preparing the file for
compilation
• The sequence of C language
statements to be compiled
• One and only one file contains a
function named main that will be
called by the operating system.

Slides by Russ hersberger based on N2454 draft of ISO/IEC


4
9899:202x
Sample C Source File (demo.c)
Descriptive header section

Preprocessor Header section

The function main

C language statements Descriptive comment

Slides by Russ hersberger based on N2454 draft of ISO/IEC


5
9899:202x
Sample Header File (demo.h)

Descriptive header

Guard statements
to prevent multiple
inclusion
Macro to define a text
replacement for E_CHARGE
Guard terminator

Slides by Russ hersberger based on N2454 draft of ISO/IEC


6
9899:202x
Sample C program including local header
Descriptive header
Preprocessor Header section
that also includes local
header file demo.h

The identifier E_CHARGE


will be replaced by the
text 1.602176634e-16 by
the preprocessor before
compilation occurs.

Slides by Russ hersberger based on N2454 draft of ISO/IEC


7
9899:202x
Identifiers Clause 6.2.1
• Object
• Function
• Tag or a member of a structure, union, or enumeration
• Typedef name
• Label name
• Macro name
• Macro parameter.

Slides by Russ hersberger based on N2454 draft of ISO/IEC


8
9899:202x
Identifier Scope (Visibility) Clause 6.2.1
• Scope
• A region in a program where the identifier is visible.
• The meaning associated with an identifier can be used only within its scope.
• This means the same identifier can have different meanings for different
scopes.
• An out of scope identifier is invisible and cannot be used unless declared in
the local scope.

Slides by Russ hersberger based on N2454 draft of ISO/IEC


9
9899:202x
Scope regions Clause 6.2.1
• Four scope regions:
• File
• Identifier is not visible within other files in the same project
• Identifier is visible within the entire file even inside blocks
• Block
• A block is often identified by a pair of braces {}
• Identifiers declared within a block are invisible out side the block.
• Identifiers declared within a block override the file scope identifiers.
• Identifiers declared in a function definition’s parameter list have block scope of the associated
definition block.
• Function
• Only labels have function scope.
• They are implicitly declared when used.
• Function prototype (These have no associated definition block.)
• Identifiers declared in a function prototype’s parameter list become invisible at the end of the
prototype.

Slides by Russ hersberger based on N2454 draft of ISO/IEC


10
9899:202x
Object
• Clause 3.15: A region of data storage in the execution environment, the
contents of which can represent values.
• When referenced, an object may be interpreted as having a particular type
• Declaration of an object:
• Associates a type with a specified identifier.
• Definition of an object
• Allocates real memory (storage) to store value of the object.
• Assigns a sufficient number of bytes to accommodate the object’s type.
• Assigns a memory address to the object.
• Every byte in a computer’s memory has a unique address determined by
the memory system of the microprocessor when the computer is
constructed
Slides by Russ hersberger based on N2454 draft of ISO/IEC
11
9899:202x
Object Storage Lifetimes Clause 6.2.4
• Storage for objects may be returned to the memory pool once the
object is no longer needed.
• Some objects are stored in read-only memory and so they endure for
the life of the program.
• If an object is referred to outside of its lifetime, its behaviour is
undefined.

Slides by Russ hersberger based on N2454 draft of ISO/IEC


12
9899:202x
Four Kinds of Storage:
• Automatic (default)
• On demand. Volatile memory is allocated on demand for local variables, function parameters,
function return address etc.
• Memory is returned for re-allocation when no longer required. (Identifiers go out of scope).
• Usually allocated on a first-in-first-out memory structure called a stack.
• Static (use static keyword)
• Allocated for the duration of the program.
• Allocated from the same memory used for the program itself or from separate read only
memory. (Implementation defined)
• Its value is initialized only once prior to start-up.
• Thread (use _Thread-local keyword)
• Allocated for the duration of the thread execution.
• Its value is initialized prior to start up.
• Allocated
• Allocated on demand by using one of several allocation functions.
• The storage duration remains until de-allocated by the functions free() or realloc()

Slides by Russ hersberger based on N2454 draft of ISO/IEC


13
9899:202x
Types
• 6.2.5.1The meaning of a value stored in an object or returned by a
function is determined by the type of the expression used to access it.
• Function types
• Object types
• Can be complete (the storage size can be determined)
• Can be incomplete (the storage size can NOT be determined)

Slides by Russ hersberger based on N2454 draft of ISO/IEC


14
9899:202x
Object Character type
Variable declaration and definition

• The keyword without qualifiers implies a text based character


• The binary number is interpreted as an ASCII character
• Basic English (Latin) character set
• Character sets for other languages are available with Unicode
• Require a different type and more bytes
• Allocates 1 byte (8 bits) of storage.
• Using signed char or unsigned char qualifiers treat the variable as a 1-
byte signed or unsigned integer respectively not as a character.

Slides by Russ hersberger based on N2454 draft of ISO/IEC


15
9899:202x
Object Character Type
• Represents the Basic Execution Character set
• One byte of storage
• Use the reserved word char.
• Basic Execution Set
• ABCDEFGHIJKLMNOPQRSTUVWXYZ
• abc def g hIjk lmn o pqr s tu v wxy z
• 0123456789
• !”#% &‘()*+,-./:
• ;<=>?[\]^_{|}~
• The Unicode character set extends this concept to wide and multi-byte
characters
Slides by Russ hersberger based on N2454 draft of ISO/IEC
16
9899:202x
Character type example
• The first form declares, defines and assigns the character ‘H’ to c.
• The second form assigns a character ‘H’ to a previously defined c.
• A variable is defined when it is first assigned a value after its declaration.
• Assigning a value to an undeclared variable is a compiler error.

&c = 0x048abd64 1 bytes

• A variable that has a value assigned by the current program prior to first use is
called an initialized variable.
• It is best practice to always initialize variables.

Slides by Russ hersberger based on N2454 draft of ISO/IEC


17
9899:202x
Object Integer Types
• signed char – 8 bit signed integer
• unsigned char – 8 bit unsigned integer
• short, short int, signed, int, long int, long long, long long int
• Signed integer types
• Long long and long long int require the same storage space each and as much or
more than int
• signed and int require the same storage space each and as much or more than short
int
• The keywords signed and unsigned may also be applied to the types in the
second bullet as modifiers. Example signed short, or unsigned short, etc.
• As the name implies only integer (whole numbers) can be represented.s

Slides by Russ hersberger based on N2454 draft of ISO/IEC


18
9899:202x
Integer type example
Variable declaration and definition

• On a 32 bit processor:
• Allocates 4 bytes of storage at an address determined by the system.
• Assigns the symbol &a to represent
• The address 0x048abd65. The symbol, a, represents the contents at the address &a.
• The ampersand & is called the address-of operator in C
• Interprets the data contained within the 4 bytes as a signed integer.

&a = 0x048abd64 4 bytes


• Each rectangle represents 1 byte so can hold int numbers from -128 to +127. The data is
left over from previous assignments.
• The int value represented by a = -1711654075.
• This is called the uninitialized value of a. There is no guarantee of its value.

Slides by Russ hersberger based on N2454 draft of ISO/IEC


19
9899:202x
Integer type example
Variable declaration and definition
• The first form both declares, defines and assigns the value of 3 to a.
• The second form assigns a value of 3 to a previously defined a.
• A variable is defined when it is first assigned a value after its declaration.
• Defining an undeclared variable is a compiler error.

&a = 0x048abd64 4 bytes


• A variable that has a value assigned by the current program prior to first use is
called an initialized variable.
• It is best practice to always initialize variables.

Slides by Russ hersberger based on N2454 draft of ISO/IEC


20
9899:202x
Sample integer definitions with initialization
• The numeric literal 3 is assigned to each of
the defined variables at the right.
• The use of suffixes associates a data type
with each use of numeric literal 3.
• No suffix is used for char, short, and int
• U or U is used for unsigned literals
• L or l is used for long literals
• LL or ll is used for long long literals
• U and L may be combined in any order
• The suffixes may be uppercase or
lowercase.
• The objects are declared and defined at
the same time.
Slides by Russ hersberger based on N2454 draft of ISO/IEC
21
9899:202x
Object Floating Point Types <float.h>
• Floating point refers to a number representation similar to scientific
notation but adapted to restricted storage space and to allow efficient
arithmetic. IEEE-754.
• Standard floating types:
• float, double, long double.
• Decimal floating types:
• _Decimal32, _Decimal64, and _Decimal128.
• These are called the real floating types.
• All are signed types that allow fractional representation of quantites

Slides by Russ hersberger based on N2454 draft of ISO/IEC


22
9899:202x
Sample float definitions with initialization
• The numeric literal 3.0 is assigned to each
of the declared variables at the right.
• The use of suffixes associates a data type
with each use of numeric literal 3.0.
• F or f is used for float literals.
• No suffix is used for double float literals
• L or l is used for long double float literals
• The _Decimalx types are not supported in our
environment
• The objects are declared and defined at
the same time.
Slides by Russ hersberger based on N2454 draft of ISO/IEC
23
9899:202x
Object Complex Types <complex.h>
• Represent the real and imaginary parts of complex quantities.
• Storage as if a 2-element numeric array.
• All complex types are floating types.
• float _Complex have float storage size.
• _Complex have double storage size.
• double _Complex have long double storage size.
• Must include <complex.h>

Slides by Russ hersberger based on N2454 draft of ISO/IEC


24
9899:202x
Object type bool <stdbool.h>
• Two values:
• true
• false
• The header file defines:
• false = 0
• true = 1
• Allocates storage of at least 1 bit.

Slides by Russ hersberger based on N2454 draft of ISO/IEC


25
9899:202x
Object type Pointer
• The identifier (letter) a represents the address of a storage location.
• The addressing of a variable is also called referring to or referencing a
variable
• The asterisk (in this context) is called the dereferencing operator.
• It means the contents of the storage location that the address is referring to.
• The act of referencing is called pointing.
• The identifier that does the pointing is called a pointer.
• The letter sequence *a:
• Declares the letter “a” to be a pointer.
• Means the contents of the location referred to by a.

Slides by Russ hersberger based on N2454 draft of ISO/IEC


26
9899:202x
Object type Pointer examples
• The complete declaration also specifies
the data type of the contents stored at the
address
• The contents at address x is of type bool.
• The contents at address y is of type char.
• And so on.
• The following phrases are equivalent.
• The identifier x points to a value of type bool.
• “y points to int” is a shortened form.
• And so on

Slides by Russ hersberger based on N2454 draft of ISO/IEC


27
9899:202x
Selection Statement: IF
Single line

• The expression evaluates to


either: Block style
•0
• Not 0
• The statement is executed if the
expression is unequal to 0.
• The statement is not executed for
expression equal to 0.
• Expression type is any arithmetic
type, including but not necessarily
bool.
Slides by Russ hersberger based on N2454 draft of ISO/IEC
28
9899:202x
Selection Statement: If-Else Single line

• If expression is unequal to 0
• Statement 1 is executed Block style
• Statement 2 is not executed
• If expression is equal to 0
• Statement 2 is executed
• Statement 1 is not executed
• The block containing statement2 is
associated with the else keyword

Slides by Russ hersberger based on N2454 draft of ISO/IEC


29
9899:202x
Selection Statement: If-Else-if
• There is no elseif keyword in C
• It is a way of selecting one of several choices.
• The second if statement is executed if
expression1 equals 0
• The statement2 is executed only if
expression2 is unequal to zero.
• The block containing statement2 is
associated with the second if keyword, not
the else keyword

Slides by Russ hersberger based on N2454 draft of ISO/IEC


30
9899:202x

You might also like