chapter1a-introductiontocprogramming
chapter1a-introductiontocprogramming
Language
❖ Structure of C Program
❖ First C Program
❖ Write comments
❖ The Greeting Program
C Programs
{ /* ignore this */
/* start here */
return 0; /*end here */
}
2
C Libraries
Computer Science: A
Structured Programming
Approach Using C
Structure of a C Program
The Greeting Program
PROGRAM 1 The Greeting Program
Examples of Block Comments
Examples of Line Comments
Variable
• The most common types are: char, int, float, and double.
Declaring a Variable
Computer Science: A
Structured Programming
Approach Using C
Global and Local Variable
• Global Variables
• These variables are declared
outside all functions.
• Life time of a global variable is
the entire execution period of the
program.
• Can be accessed by any function
defined below the declaration, in
a file.
Global and Local Variable
• Local Variables
• These variables are declared
inside some functions.
• Life time of a local variable is the
entire execution period of the
function in which it is defined.
• Cannot be accessed by any other
function.
• In general variables declared
inside a block are accessible only
in that block.
Variable Types
Computer Science: A
Structured Programming 25
Approach Using C
Examples of Valid and Invalid Names
Character Types
Integer Types
Note
sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long)
Typical Integer Sizes and Values for Signed Integers
Floating-point Types
Note
sizeof (float) ≤ sizeof (double) ≤ sizeof (long double)
Type Summary
Variables
Variable Initialization
Note
When a variable is defined, it is not initialized.
We must initialize any variable requiring
prescribed data when the function starts.
PROGRAM 2 Print Sum of Three Numbers
PROGRAM 2 Print Sum of Three Numbers (continued)
PROGRAM 2 Print Sum of Three Numbers (continued)
Constants
if (myVal < 3)
printf(“myVal is less than 3.\n”);
else
printf(“myVal is greater than or equal to 3.\n”);
The for Loop
Counter = 0;
Counter = 1;
Counter = 2
Thank You