C Fundamentals
C Fundamentals
Program Structure
Constants
Variables
Assignment Statements
Standard Output
Standard Input
Math Functions
Character Functions
System Limitations
Conditional Expressions
Selection Statements
Loop Structures
Simple Program Structure
/* ANSI-compliant comments */
// Comment to end of line - common but not ANSI-compliant prior to C99
Left Associativity:
x = y * z * w; => x = ( (y * z) * w);
Right Associativity:
x = y += z += w; => x = (y += (z += w));
{-, +, , #, 0}
[width] - sets the minimum width of the field - may be longer.
[.precision] - sets the number of digits following the decimal point.
Usually used for floating points, but also affects character and integer types as
well.
[modifier] - combines with type_character to determine argument type.
type_character - the basic type of that argument.
{c, d, e, E, f, g, G, i, o, p, s, u, x, X, %}
In general, look up what you need.
You will tend to remember the forms you use most often.
\ - printf() escape sequences
\n - newline
\r - return w/o line feed.
\” - print double quote
\’ - print single quote (aka apostrophe)
\a - sound bell
\b - backspace
\\ - print backslash
\? - question mark
If a number follows the \, the ASCII character of the octal number is printed.
Standard Input
#include <stdio.h>
scanf(“format string”, arg1, arg2, .... argN); // from the keyboard
scanf(file_ptr, “format string”, arg1, arg2, .... argN); // from a file
sscanf(str_ptr, “format string”, arg1, arg2, .... argN); // from a string
each returns the number of successful conversions.
Format String
Literal characters in format string can be used to skip characters.
% conversion specifiers similar (but not identical) to printf().
Arguments need to be memory locations where values will be stored.
Big Time Caveat
If input does not adequately match format, results can be VERY unpredictable.
Many programmers avoid the use of scanf() at nearly any cost.
Use of fscanf() for file I/O is generally safe provided the file format is adequately
constrained.
Common mistakes with scanf()
Passing values instead of memory locations.
The scanf() function read values and store them at the memory locations you
supply.
k = 10;
scanf(“%i”, k);
Tells scanf() to format the value as an integer and store it at location 10.
But the memory location for variable k is almost certainly not 10.
The address operator, &, returns the memory location of the specified variable.
scanf(“%i”, &k);
Tells scanf() to format the value as an integer and store it at the memory address
used for variable k.
tolower(k) returns k+32 if isupper(k) is TRUE otherwise returns k.
System Limitations
#include <limits.h>
Symbolic constants that define the limits of integer representations.
Examples:
INT_MAX Maximum value of an int
INT_MIN Minimum value of an int
ULONG_MAX Maximum value of an unsigned long int
#include <float.h>
Symbolic constants that define the limits of floating point representations.
Examples:
DBL_MAX Largest representable value for a double.
DBL_MIN Smallest positive representable value for a double.
DBL_EPSILON Smallest value x such that 1+x is not equal to 1.
Selection Statements
Selectively choose one path of execution.
Based on the evaluation of a test.
Test outcome is either TRUE or FALSE.
FALSE if expression evaluates to ZERO.
TRUE if expression evaluates to ANYTHING else.
Three varieties of selection statements.
if()/else
switch()
()? (conditional or ternary operator)
Conditional Expressions
(test)
May be ANY expression that evaluates to a value.
logical operator (!, ||, &&)
relational operator (==,!=),(>, <=), (<,>=)
assignment operator (equal to value assigned)
single variable (or constant)
function return value
Test outcome is either TRUE or FALSE.
FALSE if expression evaluates to ZERO - exactly.
TRUE if expression evaluates to ANYTHING else.
if() and if()/else statements
Syntax if() if()...else
if(test)
{
if_code;
}
else test? test?
{ F F
else_code;
}
T T
if_code if_code else_code
()? - conditional or ternary operator
Syntax x = ()?:
x = (test)? if_expr:else_expr;
T
x = if_expr x = else_expr
switch() statement
Syntax (int_expr)
must evaluate to an integer value
switch(int_expr)
at runtime.
{
case int_const1: code1;
(int_constN)
break;
must evaluate to a unique integer
case int_const2: code2; constant at compile time.
case int_const3: code3;
break; execution jumps to case where
default: code4; (int_constN == int_expr) is TRUE.
}
Compact way of writing certain break;
terminates switch() execution.
types of complex but common
if()/else blocks. default case
Optional. Executes only if NO
other case executes.
Testing for floating point
equality
if(x == y) // BAD!!!
Only true if EXACTLY equal. Because a great deal of care went into how floating
point values are represented, you can frequently get away with it - but unless you
REALLY know what is going on and what is preventing disaster from visiting you,
you are just being lucky!
Syntax
ini_code // not part of loop
while(test_expr) test?
{ F
loop_code;
increment_code; T
}
loop_code
next_code; // not part of loop
Features
loop does not execute at all if test
inc_code
fails the first time.
next_code
do/while() loop ini_code
Syntax loop_code
ini_code // not part of loop
do
{ inc_code
loop_code;
increment_code;
} while(test_expr);
next_code; // not part of loop T
test?
Features
loop will always execute at least
once, even if test fails the first F
time.
next_code
for() loop ini_code
Syntax
for(ini_code; test_expr; inc_code)
{ test?
loop_code; F
}
next_code; // not part of loop T
Features loop_code
Just a while() loop with the initialization
and increment code formally
inc_code
incorporated into the syntax.
Can makes a cleaner divide between the
loop logic and the housekeeping logic.
next_code