Unit 3
The C Programming Language: Basic Syntax
Alberto Casagrande
Email: [email protected]
A.Y. 2021/2022
The C Programming Language
The C programming language is an imperative language.
Programs are sequences of instructions.
Trust Your Master
At the beginning, all the programs will have this structure
#i n c l u d e < s t d i o . h>
i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
{
...
return 0;
}
By the end of the course, you’ll have all the details.
Instructions
Are basic “commands” for the computer.
They are syntactically closed by the symbol “;” (semi-column)
More than one instruction can lay on the same line
Comments
Even if you are an expert, unraveling the meaning of a sequence of
instructions may be difficult.
Comments
Even if you are an expert, unraveling the meaning of a sequence of
instructions may be difficult.
It is really important to comment code
Comments
In ANSI C everything is preceded by “/*” and followed by “*/” is
a comment.
It can be longer that one line.
If you come from either C++ or Java, pay attention: sigle line
comment (i.e., //) is not standard.
Commento con ”/*” e ”*/”
...
i n t y ; /∗ T h i s i s a comment
and , a s you can s e e ,
i t can i n c l u d e more t h a t
one l i n e ∗/
...
Variables
Are “containers” for values.
Each of them is equipped of a name and a type.
The name is used to identify the variable when we want to either
“read” or change its content.
The type specifies:
the set of values that can be store into the variable
the functions that can be applied on them
Declaring a Variable
Any variable must be declared before its use.
...
< v a r i a b l e t y p e > < v a r i a b l e name >;
...
From that point on, <variable name> will denote the variable
having the specified type.
Variable Names
Variables can have as names any word in
[a-zA-Z ][a-zA-Z 0-9]*
excluding reserved words (e.g.,“int”, “return”)
C is case sensitive, e.g., “ciao”̸= “Ciao”
Basic Data Type
Type Values (at least) Domain
char [−27 , 27 − 1] and Integer values
ASCII Characters and Characters
short [−27 , 27 − 1] Integer values
int [−215 , 215 − 1] Integer values
long int [−231 , 231 − 1] Integer values
long long int [−263 , 263 − 1] Integer values
float represented by 32 bits Real values
double represented by 64 bits Real values
long double represented by 128bits Real values
Basic Data Type
unsigned can be used to signal the interest on non-negative
values only.
E.g, unsigned int variables can assume any value in [0, 216 − 1].
Declaring a Variable
/∗ I n t e g e r t y p e s ∗/
s h o r t s , S ; /∗ a t l e a s t 16 b i t s ( −2ˆ15 ,2ˆ15 −1) ∗/
i n t i , I ; /∗ a t l e a s t 16 b i t s ( −2ˆ15 ,2ˆ15 −1) ∗/
l o n g i n t l i ; /∗ a t l e a s t 32 b i t s ( −2ˆ31 ,2ˆ31 −1) ∗/
l o n g l o n g i n t l l i ; /∗ a t l e a s t 64 b i t s ( −2ˆ63 ,2ˆ63 −1) ∗
/∗ F l o a t i n g p o i n t t y p e s ∗/
f l o a t f ; /∗ 32 b i t s ∗/
double d ,D; /∗ 64 b i t s ∗/
long double l d ; /∗ 128 b i t s ∗/
/∗ C h a r a c t e r and i n t e g e r t y p e ∗/
c h a r c ; /∗ ASCII o r i n t e g e r v a l u e s i n ( −2ˆ7 , 2ˆ7 −1) ∗/
Assignments
To store a value in a variable:
...
< v a r i a b l e name> = <e x p r o f t h e same t y p e >;
...
Assignment Examples
i n t i ; char c ; short s ; f l o a t f ;
s =4; i=s ;
c =’a ’ ; /∗ c h a r a c t e r s a r e s p e c i f i e d between
apostrophes ’ , while s t r i n g s
between q u o t a t i o n marks ” ∗/
i =2∗( i +1); /∗ a l g e b r i c e x p r e s s i o n s a r e
s u p p o r t e d ( no power ) ∗/
f =4.1;
i =4.1;
The last instruction may have weird effects. Why?
Implicit Type Casting
When we assign a value to a variable of different type there is an
Implitic type casting.
If the the variable’s type is more “general”, the value is preserved.
char → short → int → long int → long long int →
float → double → long double
Implicit Type Casting
When we assign a value to a variable of different type there is an
Implitic type casting.
If the the variable’s type is more “general”, the value is preserved.
char → short → int → long int → long long int →
float → double → long double
Otherwise, the value may be approximated.
Explicit Type Casting
Any value can be approximated and forced to a given type by type
casting it.
(<new t y p e >) <e x p r e s s i o n >
E.g.,
i =( i n t ) 4 . 1 ;
c=( c h a r ) ( ( i n t ) c +1);
i= ( ( i n t ) ( 4 . 1 / 2 ) ) / 2 ;
Two Kinds of Division
C implements two different division functions:
integer division
floating point division
Their selection depend on the types of operators
i n t i =3, I ; f l o a t d=3, D ;
I=i / 2 ; /∗ b o t h i and 2 a r e i n t e g e r s
=> i n t e g e r d i v i s i o n ∗/
D=d / 2 ; /∗ d i s a f p v a r i a b l e
=> f l o a t i n g p o i n t d i v i s i o n ∗/
Boolean Expressions
The supported algebric relations are:
equality (==)
majority relations (> and <) and their derivatives (>= and <=)
diversity relation (!=)
The supported Boolean operators are:
logic conjuction (&&)
non-exclusive disjunction (||)
logic negation (!)
Boolean Expressions (Cont’d)
Their evaluations are natural numbers:
0 represents the Boolean value False
any other value is interpreted as True
char b ;
b = ( ! ( i >=3) && ( d−2==−1) && b ) | | ( s ! = 3 ) ;
From 1999, C has a Boolean type which is not really used.
Blocks of Instructions
Are either a single instruction or a sequence of instruction between
braces “{” “}”
Any variable exists only inside the block in which it is declared.
...
{ /∗ T h i s i s a b l o c k ∗/
i n t h =1;
h=h +1; /∗ Here , h d o e s e x i s t ∗
}
/∗ Here , i t d o e s n o t ∗/
...
Printing on the Standard Output
Use the “instruction” printf
...
i n t i =2; l o n g i n t j =3;
p r i n t f ( ” To p r i n t numbers : %d %d %d ” , i , i , j ) ;
f l o a t f =2.2 , d o u b l e d = 2 . 2 ;
p r i n t f ( ” To p r i n t f l o a t s : %f %f ” , f , d ) ;
c h a r c =’Y ’ ;
p r i n t f ( ” To p r i n t c h a r a c t e r s : %c ” , c ) ;
Non-Alphabetic Characters
There exist non-alphabetic characters like end line or new line.
How to represent them?
Non-Alphabetic Characters
There exist non-alphabetic characters like end line or new line.
How to represent them? by using escape sequences
\n newline
\b backspace
\t horizontal tabulation
\\ backslash character
\" double quotation character
\a alert
\0 string terminator
Non-Alphabetic Characters (Examples)
...
p r i n t f ( ” T h i s d o e s n o t end w i t h a \” new l i n e \ ” ” ) ;
p r i n t f ( ” T h i s d o e s \ nbut t h i s n o t ” ) ;
p r i n t f ( ” T h i s p r o c u d e s an a l e r t sound \ a ” ) ;
p r i n t f ( ” T h i s i s m i s s i n g t h e l a s t l e t t e r \b ” ) ;
p r i n t f ( ” T h i s l i n e e \0 nds b e f o r e i t s r e a l end ” ) ;
...
Loop Statements
Blocks of instructions can be repeated many times by using the
loop statements:
while-do
for
The While-do Statement
Repeats a block while a Boolean condition holds.
i n t i =0;
w h i l e ( ! ( i ==4)) {
p r i n t f ( ” Here i=%d\n ” , i ) ;
i ++; // t h i s i s e q u i v a l e n t t o i=i +1
}
The For Statement
Has the syntax:
f o r (< i n i t i a l i z a t i o n code >;
<l o o p c o n d i t i o n >;
<u p d a t i n g code >) <b l o c k >
E.g.,
f o r ( i n t j =0; j <i ; j ++) {
p r i n t f ( ” Here j=%d\n ” , j ) ;
}
The Condition Statement If-Then
Executes a block if and only if the Boolean condition holds.
i f ( i ==2) {
p r i n t l f (” i e q u a l s 2 ” ) ;
}
The Condition Statement If-Then
Decides the execution of one among two blocks.
i f ( i ==2) {
p r i n t f (” i e q u a l s 2 ” ) ;
} else {
p r i n t f ( ” i d i f f e r s from 2 ” ) ;
}
Coming next. . .
modular programming
C functions
libraries