7 CPS393 IntroC
7 CPS393 IntroC
Topic 7: Intro to C
Notice!
Intro to C:
Variables
Functions
Control flow
Meaning:
Why teach C?
C is efficient at runtime
C is the basis for other languages (e.g. C++)
C is (relatively) easy to learn.
%:
"
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return (0);
}
out .
to
stand art
writes
→
/
a
Same as Linux!
Pre-processor directive ?
#include <stdio.h> function prototype
int main(void) or signature
{
main
printf("Hello, world!\n");
function
return (0);
Semi-colon terminates C statements
}
1. 3. Compiler
2.
4. Executable
Preprocessor (machine code)
© Alex Ufkes, 2020, 2021 15
Preprocessor Directives
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return (0);
}
What does stdio get us?
Many things! one of which
is the printf function
#include <stdio.h>
#define TEXT "Hello, world!\n"
int main(void)
{
printf(TEXT);
return (0);
}
help
caps
#include <stdio.h>
#
All
to
read your
better
#define HELLO main code
see
anywhere
if you
#define SAY printf else ,
you
can guess
a
define
it is
{
SAY("Hello, world!\n");
return (0); This makes perfect sense when you
understand how #define works.
}
It just obfuscates your code.
© Alex Ufkes, 2020, 2021 20
Preprocessor Directives: #define
#include <stdio.h>
/* define PI */ Comments can be used to
#define PI 3.14159265359 explain your code to other
programmers
int main(void) /* main function */
They can also serve as
{
reminders for the original
/* Say hello */
programmer.
printf("Hello, world!\n");
They are NOT compiled.
return (0);
}
processor ,
?
a statement .
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n"); /* printf
statement */
return (0); /* return
} statement */
However
#include <stdio.h>
#include <stdio.h>
int main(void)
int main(void) {
{
printf("Hello, world!\n");
printf("Hello, world!\n");
return (0) ;}
return (0);
}
#include <stdio.h>
int main(void) ?
{
int wholeNum; // Reserve memory for int
double realNum; // Reserve memory for double
char charValue; // Reserve memory for char
return (0);
}
Computer memory:
A collection of consecutively
numbered cells
Each cell is typically one byte
One byte == 8 bits
c wholeNum1 realNum2
→←É:¥Ét*÷t¥É*÷µ
Name Description Size* Range*
☆→
25
A
char Character 1 byte
signed: -128 to 127
unsigned: 0 to 255
short int signed: -32768 to 32767
Integer (short)
Short Integer. 2 bytes
unsigned: 0 to 65535
types
signed: -2147483648 to 2147483647
int Integer. 4 bytes
unsigned: 0 to 4294967295
long long signed: -2^63+1 to +2^63-1
Floating- Long integer. 8 bytes
int unsigned: 2^64 - 1
point float Floating point number. 4 bytes +/- 3.4e +/- 38 (~7 digits)
types Double precision
double 8 bytes +/- 1.7e +/- 308 (~15 digits)
floating point number.
(*depends on OS)
© Alex Ufkes, 2020, 2021 32
short int signed: -32768 to 32767
Short Integer. 2 bytes
(short) unsigned: 0 to 65535
signed: -2147483648 to 2147483647
int Integer. 4 bytes
unsigned: 0 to 4294967295
signed: -2^63+1 to +2^63-1
long long int Long integer. 8 bytes
unsigned: 2^64 - 1
The not-so-obvious:
Multi-
Numeric literals:
5, -7, 42 (signed integer, whole numbers)
3.1415, -99.9 (floating point, real numbers)
print-formatted
#include <stdio.h>
int main(void)
{
int num1 = 84;
char
printf( \n , c1);
printf( \n , num1);
return (0);
}
#include <stdio.h>
int main(void)
{
int num1 = 84;
char
printf( \n , c1);
printf( \n , num1);
return (0);
}
When we print a char as an int, we get the ASCII value
When we print an int as a char, we get the
corresponding character.
© Alex Ufkes, 2020, 2021 38
Formatting Placeholders
→recent
~É*r←*ax
int temp = 9;
printf( \n , temp);
%5d Reserves 5 places (right-justified)
Output
It is _ _ _ _ 9 degrees
4 spaces (not underscores), one spot for the number
© Alex Ufkes, 2020, 2021 39
Formatting Placeholders
Literal!
printf( -8d.\n , 1234);
%-8d reserves 8 places (left-justified)
Output
Number is 1 2 3 4 _ _ _ _.
Output
___-38
Common pitfall:
d is for decimal, not double.
Use %d for int, %lf for double.
Output
1.219970
int number;
scanf( , &number);
Syntactically: Very similar to printf
scanf( , &intVar);
scanf( \ , &intVar);
scanf( , &doubleVar); BAD! Use placeholder only
BAD!
scanf( , intVar);
scanf( , doubleVar);
scanf( , &intVar);
scanf( \ , &intVar); Not a syntax error!
scanf( , &doubleVar);
The content inside the quotes tells scanf what it is going to read in.
If you write , scanf will expect to read the
printf(
scanf( , &intVar);
);
GOOD!
© Alex Ufkes, 2020, 2021 53
Bad, But Why?
scanf( , intVar);
scanf( , doubleVar); Not a syntax error!
scanf needs an address to know where in memory it will write the value. This
is what the & gets us.
If you do not provide the address of the desired variable, but rather just the
value of the variable, your program will attempt to write the scanned value at
the address equal to the value of the provided variable (almost never correct).
Remember:
Memory is a sequence of
consecutively numbered cells.
scanf needs the cell number,
or the address. 01010101
Using & gets us the address 01001001
11100111 Scanf
NOT 00000000 needs
this! this!
#include <stdio.h>
int main(void)
{
int age;
double height;
#include <stdio.h>
int main(void)
{
int age;
double height;
Addition (+)
3 + 4 or 55.1 + 43.58
Subtraction (-)
Binary as in two operands.
50 - 20 or 45.3 - 0.78
Not binary code.
Multiplication (*)
5 * 10 or 0.6 * 3.4
Division (/)
Only works on integers! 50.0 / 2.0 or 45 / 2
Remainder (%)
30 % 7 or 45 % 3 or 23 % 77
© Alex Ufkes, 2020, 2021 60
Integer Expressions
int x = 5;
double y = 2.0, r1, r2; Output?
r1 = x*y;
r2 = x/y; x*y = 10.000000
printf( lf\ , r1); x/y = 2.500000
printf( lf\ , r2);
i += 1; /* i = i + 1; */
a /= 2; /* a = a / 2; */
x *= 5; /* x = x * 5; */
i++; /* i = i + 1; */
++i; /* i = i + 1; */
i--; /* i = i - 1; */
--i; /* i = i - 1; */
#include <math.h>
Recall:
Including stdio.h lets us use various I/O functions
printf, scanf
!
x !x
1 0
0 1
© Alex Ufkes, 2020, 2021 71
The AND Operator
x y x && y
&& 1
1
0
0
1
0
1
0
1
0
0
0
© Alex Ufkes, 2020, 2021 72
The OR Operator
x y x || y
|| 1
1
0
0
1
0
1
0
1
1
1
0
© Alex Ufkes, 2020, 2021 73
Branching
if (condition)
condition is true, execute statement;
Input: Output:
107 The water is boiling!
24
if (condition)
condition is true, execute statement;
else
condition is false, execute statement;
int value;
do {
printf(
printf( ); );
scanf(
scanf( , &value);
, &value);
}
while ( value >= 0 );
int value;
do {
printf( );
scanf( , &value);
} while (value >=0);
int n = 1;
while (n <= 2) {
printf( \ , n);
n = n + 1;
}