Lecture 2 - C-Fundamentals
Lecture 2 - C-Fundamentals
C #include <stdio.h>
compiler
int main(void){
//declare variables
statement 1; body
statement 2;
return 0;
}
return keyword
need to be used
when an output is
specified
Compilation
Process
Editing Compilation Execution
Pre-processor
Compiler
Assembler
myFile.c myFile.out
Linker and/or
myFile.exe
Or install at home
https://
www.jetbrains.com/help/clion/quick-tutorial-on
-configuring-clion-on-windows.html
General form of a C-
Program
Pre-processor directives
#include <stdio.h> #include <stdio.h>
int main(void){
{ statement
return 0;
}
1;
statement
Statements
2; statement 1;
statement 2;
return 0;
}
Preprocessor
directives
• Begin with a # symbol, and are NOT terminated by a semicolon.
• Traditionally, preprocessor statements are listed at the beginning of
the source file.
• Preprocessor statements are handled by the compiler (or
preprocessor) before the program is actually compiled. All #
statements are processed first, and the symbols which occur in the C
program are replaced by their value.
#include <stdlib.h>
#define N 30
#define TRUE 1
int main(void)
{ printf(“Hello World.\
n”);
return 0;
}
• Only two kinds of statements
used:
1. A
function call
2. A return statement
• Function printf is used to
Variable
s•
A variable is named reference to a value stored in the
system’s memory.
• Each variable has to be declared before use.
• Each variable occupies one or more bytes in memory
based on their type.
• e.g.
Variable Memory
Allocated Definition
0x120 0x120
4 4
0x120 0x120
0 0
30 0x100 30
0x100 count
8 8
20 20 val
num
0x100 0x100
10 A
4 val 4 Ch
0x100 0x100
0 0
Data
Types
• ANSI C has five “atomic” data
types, and several modifications to
the atomic types
Modifiers
Basic Data Types
signed
int
unsigned
char
long
float
short
double
static
void
volatile
Data
Types
Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
#define N 3000
#define FALSE 0
#define PI 3.14159
#define FIGURE
"triangle"
• Pre-processor replaces the symbolic name with
defined value within the whole program
Defining Constants in C – an
Example
/* add.c
A simple C program to print the sum of integers from 1 to MAX
*/
#include <stdio.h>
#define MAX 10
int main()
{
int i; /* General purpose counter*/
int sum = 0; /* Cumulative sum */
return 0;
}
Note: we could easily change this program to add numbers from 1 to 100
by redefining MAX near the top of the program as #define MAX 100
Operators in
C
• C emphasizes on expression statements.
• Expressions are built from variables,
constants, and operators.
• Some common operators used in C:
– assignment operators
– arithmetic operators
– relational operators
– logical operators
– increment and decrement operators
Arithmetic
Operators
• C provides five binary arithmetic operators:
+ addition
- subtraction
* multiplication
/ division
% remainder
• Arithmetic operators are either binary
(uses two operands) or unary (only one
operand)
Unary Arithmetic Operators
• There are also two unary arithmetic
operators:
+ unary plus
- unary minus
• Examples
i = +1;
j = -i;
• The unary + operator does nothing.
It’s used primarily to emphasize that a
numeric constant is positive.
Behaviour of / and %
Operators
– The operator / “truncates” the
result when both operands are
integers.
• E.g. 1 / 2 is 0 (not 0.5).
– The % operator requires integer
operands; if either operand is not an
integer, the program won’t compile.
– Using zero as the right operand of
either
/ or % causes undefined behavior.
Operator precedence
• The arithmetic operators have the
following relative precedence:
Highest: + - (unary)
* / %
Lowest: + - (binary)
printf("Enter Fahrenheit
temperature: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit – 32.0) *
(5.0/9.0); printf("Celsius
equivalent: %.1f\n", celsius);
return 0;
}
Comment
s
• Comments: /∗ this is a simple comment ∗/
• Can span multiple lines (as in programme
banners)
/ ∗ This
comment spans
multiple lines ∗ /
• Completely
ignored by
compiler
• Can appear
almost
anywhere
Comment
s
• Warning: Forgetting to terminate a
comment may cause the compiler to
ignore part of your program:
printf("My "); /* forgot to close this comment...
printf("cat ");
printf("has "); /* so it ends here */
printf("fleas");
Summar
yIn this lesson you have learnt:
1. How to implement a simple C-
Program
2. What are derivatives,
functions and statements
3. How to define constants and
variables
4. A brief introduction to I/O
console
5. Commenting your program