C Overview
C Overview
int
main(void)
{
double miles, /* input - distance in miles. */
kms; /* output - distance in kilometers */
return (0);
}
8
Preprocessor Directives
• Preprocessor directives are commands that give
instructions to the C preprocessor.
• Preprocessor is a system program that modifies a C
program prior to its compilation.
• Preprocessor directives begins with a #
• Example: #include or #define
9
#include
• #include is used to include other source files into your
source file.
• The #include directive gives a program access to a
library.
• Libraries are useful functions and symbols that are
predefined by the C language (standard libraries).
• Example: You must include stdio.h if you want to use
the printf and scanf library functions.
• #include<stdio.h> insert their definitions to your
program before compilation.
10
#define
• The #define directive instructs the preprocessor
to replace each occurrence of a text by a particular
constant value before compilation.
• #define replaces all occurrences of the text you
specify with value you specify
• Example:
#define KMS_PER_MILES 1.60
#define PI 3.14159
11
The “main” Function
12
The “main” Function
• The heading int main(void) marks the beginning of
the main function where program execution begins.
• Every C program has a main function.
• Braces ({,}) mark the beginning and end of the body of
function main.
• A function body has two parts:
• declarations - tell the compiler what memory cells are
needed in the function
• executable statements - (derived from the algorithm)
are translated into machine language and later executed
by the compiler.
13
Reserved words
• A word that has special meaning to C and can not be
used for other purposes.
• These are words that C reserves for its own uses
(declaring variables, control flow, etc.)
• For example, you couldn’t have a variable named
return
• Always lower case
• Appendix H has a list of them all (ex: double,
int, if , else, ...)
14
Reserved words
• Appendix H has a list of them all (ex: double,
int, if , else, ...)
15
Standard Identifiers
• Identifier - A name given to a variable or an operation
• In other words, Function names and Variable names
• Standard Identifier - An identifier that is defined in the
standard C libraries and has special meaning in C.
• Example: printf, scanf
• Standard identifiers are not like reserved words; you
could redefine them if you want to. But it is not
recommended.
• For example, if you create your own function called
printf, then you may not be able to access the
library version of printf.
16
User Defined Identifiers
• We choose our own identifiers to name memory cells that
will hold data and program results and to name operations
that we define (more on this in Chapter 3).
• Rules for Naming Identifiers:
• An identifier must consist only of letters, digits, and
underscores.
• An identifier cannot begin with a digit.
• A reserved word cannot be used as an identifier.
• A standard identifier should not be redefined.
• Valid identifiers: letter1, inches, KM_PER_MILE
• Invalid identifiers: 1letter, Happy*trout, return
17
Few Guidelines for Naming Identifiers
• Uppercase and lowercase are different
• LETTER != Letter != letter
• Avoid names that only differ by case; they can lead to
problems to find bugs
• Choose meaningful identifiers that are easy to understand.
Example: distance = rate * time
• means a lot more than x = y * z
• All uppercase is usually used for constant macros (#define)
• KMS_PER_MILE is a defined constant
• As a variable, we would probably name it KmsPerMile
or Kms_Per_Mile
18
Variables Declarations
• Variable – The memory cell used for storing a program’s
data and its computational results
• Variable’s value can change.
• Example: miles, kms
• Variable declarations
• Example: double miles
• Tells the compiler to create space for a variable of
type double in memory with the name miles.
• C requires you to declare every variable used in the
program.
19
Data Types
• Data Types: a set of values and a set of operations that
can be performed on those values
• int: Stores integer values – whole numbers
• 65, -12345
• double: Stores real numbers – numbers that use a
decimal point.
• 3.14159 or 1.23e5 (which equals 123000.0)
• char: An individual character value.
• Each char value is enclosed in single quotes. e.g. 'A', '*'.
• Can be a letter, a digit, or a special symbol
• Arithmetic operations (+, -, *, /) and compare can be
performed in case of int and double. Compare can be
performed in char data.
20
Executable Statements
• Executable Statements: C statements used to write or code the
algorithm. C compiler translates the executable statements to
machine code.
• Assignment Statements
• Input/Output Operations and Functions
• printf Function
• scanf Function
• return Statement
21
Hello World! Example
#include <stdio.h>
int main() {
// printf() displays the string inside
quotation
printf("Hello, World!");
return 0;
}
22
Key Points for Python Programmers
• The #include works like import in Python (not really,
but the analogy works for now) so in this case the
input/output stream library is brought in.
• The int main() begins the declaration of the main
function, which in C indicates the first function to be
executed, i.e., "begin here".
• In Python, main was the start of the program by convention
only.
• The curly braces { and } delimit the beginning and end of a
block of code (compound statement), in this case the
beginning and end of the function main .
• Python identifies blocks through indentation.
23
Assignment Statements
• Assignment statement - Stores a value or a
computational result in a variable
scanf("%lf", &miles);
A !A
printf("%3d", -- printf("%3d",
n); n--);
printf("%3d", n); printf("%3d", n);
3 3 4 3
Nested for loops 96
/* Illustrates a pair of nested counting loops */ //output:
#include <stdio.h> I
int main(void) J
{
Outer 1
int i, j;
printf(" I J\n"); Inner
for (i = 1; i < 4; ++i) { 0
printf("Outer %6d\n", i); Outer 2
for (j = 0; j < i; ++j) { Inner
printf(" Inner%9d\n", j);0
} Inner
} 1
return (0); Outer 3
}
Inner
0
Inner
97
Do While statement
• Both the for statement and the while statement
evaluate the loop condition before the first
execution of the loop body.
• In most cases, this pretest is desirable and prevents
the loop from executing when there may be no data
items to process
• There are some situations, generally involving
interactive input, when we know that a loop must
execute at least one time.
98
Do-While Example do {
#include <stdio.h>
#define KMS_PER_MILE 1.609 ---
/* converts miles to kilometers - repeateadly */
int main(void) { ---
double kms, miles;
char res; //for user response [y/n]} while
do { (Condition);
printf("Enter the distance in miles> ");
scanf("%lf", &miles);
kms = KMS_PER_MILE * miles;
printf("That equals %f kilometers. \n", kms);
printf("\nDo you wish to try again [y/n]? ");