Fundamentals of C: COMP 1002/1402
Fundamentals of C: COMP 1002/1402
Fundamentals of C: COMP 1002/1402
COMP 1002/1402
Structure of a C Program
1
First Program
Comments
2
Comments
Pre-processor Directives
Preprocessor directives start with #
#include copies a file into the source code
#include <systemFilename>
#include “undefinedFilename”
3
#include <stdio.h>
4
First Program
Variables
A variable is a block of memory that stores data of a
particular type and is named with an appropriate
identifier.
5
Variables
• The name of the variable corresponds to the
address of the first byte!
6
Legal/Illegal Names
Legal Illegal
a $sum
student_name 2names
TRUE stdnt number
FALSE int
7
Primitive Data Types
Data Type C-Implementation
void void
character char (1 byte)
unsigned short int (1 byte)
unsigned int (2 or 4 bytes)
unsigned long int (4 or 8 bytes)
integer
short int (1 byte)
int (2 or 4 bytes)
long int (4 or 8 bytes)
float (4 bytes)
floating point double (8 bytes)
long double (10 bytes)
Logical Data
No Boolean data type!!
In C:
any nonzero number is true
zero is false
8
Variable Declaration
Variable Initialization
• No variable is initialized until you do so!
9
Special Characters
Special Characters
10
String Constants
#define
#define name token(s)
Example:
#define PI 3.1415926535
#define SIZE 1000
11
#define
• No equals sign
• No semicolon
Constants
To define a constant define a variable with:
const
Works:
const float pi = 3.1415926;
Doesn’t:
const float pi;
pi=3.1415926; /* not allowed to change it */
12
Standard Input and Output
Output
Requires: #include <stdio.h>
13
printf statements
Field Specifiers
%<flag><minimum width><precision><size>code
14
Width
Value %d %4d
12 12 12
Flag:
15
printf examples
printf(“%06d %c\n%6.3f“,23,’A’,4.23);
000023 A
4.230
Input
Requires: #include <stdio.h>
16
scanf Statements
Field Specifiers
%<flag><maximum width><size>code
(no precision!!)
17
Rules
Fields are converted to specific addresses
Rules
• Initial whitespace is ignored (not %c)
• The conversion operation process until:
• End of file is reached
• Maximum characters are processed
• A whitespace character is found after a digit
• An error is detected
18
Rules
• A field specifier for each variable
scanf examples
scanf(“%d%d%d%c”,&a,&b,&c,&d);
scanf(“%d%d%d %c”,&a,&b,&c,&d);
scanf(“%d-%d-%d”,&a,&b,&c);
19
Expressions
An expression is a sequence of operands and
operators that reduces to a single value.
2+5
Operators
An Operator is a token that requires action.
e.g., + - * / %
20
C Expressions
Primary Expressions
Identifiers, constants (already covered)
Parenthetical expressions:
(2 * 3 + 4) (a = 23 + b * 6)
21
Binary Expressions
Examples: + - * / %
Assignment
Assignment expressions evaluate to the
expression on the right of the assignment
operator.
22
Simple Assignment
Contents of Contents of Expression Value of Result of
Variable x Variable y Expression Expression
10 5 x=y+2 7 x = 7
10 5 x=x/y 2 x=2
10 5 x=y%4 1 x=1
Compound Assignment
Contents of Contents of Expression Value of Result of
Variable x Variable y Expression Expression
10 5 x *= y 50 x = 50
10 5 x /= y 2 x = 2
10 5 x %= y 0 x = 0
10 5 x += y 15 x = 15
10 5 x -= y 5 x = 5
23
Postfix Expressions
Contents of Expression Value of Contents of
x Before Expression x After
10 x++ 10 11
10 x-- 10 9
Unary Expressions
Contents of Expression Value of Contents of
x Before Expression x After
10 ++x 11 11
10 --x 9 9
24
Unary Operator sizeof
sizeof is an operator (not a function)
sizeof(int)
sizeof(x)
sizeof(3.256)
Unary operator + -
25