IntroC DataTypesCtrlFlow
IntroC DataTypesCtrlFlow
I/O
example literals
'\0' null character 0
'\n' newline (or linefeed) 10
'\r' return 13
'\t' tab 9
' ' space 32
'0' 48
'A' 65
'a' 97
Variables
Symbolic Constants
Simple Operators
Numeric Operators
+, -, *, /, %, unary +, Assignment Operators (modifies state of object)
=, +=, -=, *=, /=, %=, ++, -velocity = ( acceleration * time * time ) / 2.0;
Statements
Declaration Statements
introduces a new variable
variable is in scope to the end of enclosing block
int main()
{
double d = 2 * PI;
printf("%f\n", d);
return 0;
}
Expression Statements
any expression may be used as a statement
the value is discarded
int main()
{
double PI = 3.14159;
double d = 2.0 * PI;
printf("f\n", d);
d = d / 2.0;
square( 2.0 ); // be careful of this mistake
printf("%f\n", d);
return 0;
}
Output
output is done via printf function
EG
int main()
{
printf("Hello");
printf("%d", 10 * 10);
printf("%c", 'A');
printf("%f", 3.14159);
...
}
Input
input is done via scanf function
uses address of parameter to modify its value
it waits for a value to be entered (may require a return/enter)
EG
int main()
{
int i;
double d;
char c;
scanf(%d, &i); // reads string of digits as integer
scanf(%f, &d); // reads digits, decimal as a real number
scanf(%c, &c); // reads a single character
...
}
Other Statements
if, switch, while, for, return, break
you can declare local variables in loops
int main()
{
for ( int i=0; i<10; ++i )
printf(%d\n, i);
for ( int i=10; i>=0; --i )
printf("%d\n", i);
return 0;
}
The if Statement
Nesting if Statements
if Statement Caveats
return a < b;
real examples
// print out numbers 0 through 9
for (int i = 0;i < 10; ++i)
printf("%d\n", i);
// read 10 integers from the input and print the sum
int main()
{
int valueRead = 0;
int sumTotal = 0;
for ( int i = 0; i < 10; i++ )
{
scanf("%d", &valueRead);
sumTotal += valueRead;
}
printf("The total is: %d\n", sumTotal);
}
Natural for doing an action then testing for completion before repetition
EG
do
turnIgnition( car );
while ( ! started( car ) );
do
pressANumber( phone );
while ( ! haveAConnection( phone ) );
do
{
readTheHomeworkHandout( student );
askSomeQuestions( student, TA );
} while ( ! understands( student, materialForWeek( w ) ) );
do
eat( person, pintOfIceCream );
while ( !sick( person ) );
Nested loops
EG // print out a calendar
#define JAN 1
#define DEC 12
int days_per_month[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int main()
{
for ( int y = 2015; y <= 2020; y++ )
for ( int m = JAN; m <= DEC; m++ )
{
for ( int d = 1; d <= days_per_month[m]; d++ )
printf( "%d / %d / %d ", m, d, y );
printf("\n");
}
}
Loop Caveats
some errors may cause wrong values for i or incorrect number of loops
for (int i = 0; i <= 10; i++ ) /// wrong < operator
printf("%d ", i);
...
for (int i = 1; i < 10; i++ ) /// wrong initial value
printf("%d ", i);
default:
printf("%d isn't in range 0 to 9\n", i);
break;
}
printf("Some more stuff here\n");
}
default:
printf("Invalid Grade %d\n", grade);
}
}