C Programming 1 Semester 2017-2018
C Programming 1 Semester 2017-2018
1. Machine language
• Strings of numbers representing instructions
2. Assembly language
• Human-like abbreviations representing elementary
computer operations
3. High-Level languages
• Code similar to spoken language
• Use mathematical notations
High-level Languages
• “high-level” is a relative term
• C can be seen as a relatively “low-level” high-
level language
• Pascal, Fortran, COBOL are typical high-level
languages
• Java, Python, Perl, VB are examples of “high-
level” high-level languages
• Application specific languages (Matlab,
Javascript, VBScript) are even higher-level.
Programming Languages
Programming Paradigms
• Imperative programming
• statements specified as commands
• the order of statements execution is important
• Declarative programming
• describe what to do, not how to do
• functional programming
• logical programming
• Object-oriented programming
• organized around classes and objects
Programming Language
Implementation
• Compilers
• Scans the entire program and translates it as a
whole into machine code
• Generates intermediate object code which further
requires linking
• Interpreters
• Translates program one statement at a time
• No intermediate object code is generated
• Continues translating the program until the first
error is met, in which case it stops
C Programming Language
● C is a structured, portable programming
language
● Many software applications are written in C
● Useful to make user understand fundamentals
of programming
● Simple/short (32 reserved words, 4 basic
datatypes)
C Programming Language
High-level programming
● Source (text) file
– Instructions that form the program
– “the look” depends on the programming language
– File is valid if syntax rules are followed
– File is called “source code”
High-level programming
● Compile
– The source code is converted (compiled) into
machine language – this is stored in a new file
● Run
– Execute the machine code file
Compilation Process
C - Introduction
● You need (for this course):
– Computer with OS (Linux)
– Text editor (joe, nano/pico, vi)
– Compiler (gcc)
● Useful to become acquainted with C in
conjunction with Linux and text editors for next
semesters
● If too complicated, you can use Code::blocks
or Bloodshed Dev-C++
First Program
main()
{
}
First Program
int main()
int main()
{
return 0;
}
Better First Program
• First we should specify all the preprocessor
directives
• Comments are enclosed between /* and */ , no
nesting allowed
• int means that main "returns" an integer value
• Return 0;
• When exiting a function you should return
something
• Return 0, in this case, means that the program
terminated successfully
Better First Program
● Compile and execute
– gcc -Wall first.c -o first
– ./first
● -Wall turns on all warnings
● -o gives a name to the output binary file
instead of default “a.out”
Basic C Data Types
● 4 basic data types:
– Char: holds 1 character in local char set in 1 byte
– Int: holds an integer number (size is platform
dependent)
– Float: single precision floating point value
– Double: double precision floating point value
• In addition:
- Void: placeholder o represent incomplete type, or
“no data”
Char Type
● 1 byte of storage
● Signed or unsigned
● Stored internally as a number (using ASCII
char set)
● Remember: Analyze difference when printing
with format %d, %c or computing ++
Int Type
● Signed integer usually stored on 4/8 bytes
(platform 32/64 bits)
● C allows to use also octal and hexadecimal
values as int
– Octal begins with a leading zero (0)
– Hexadecimal begins with 0x or 0X
decimal 59, octal 073, hexa 0x3b
Float and Double Types
● Store 32/64 bit floating point (real) numbers
● Differences are represented by the number of
digits for precision (twice number of digits for
double than for float)
● Float constants can have an f/F suffix
● Can be written using usual notation, scientific
notation, exponential notation (e-notation)
● Double: 322.56 = 3.2256x102 = 3.2256e2
● Float: 322.56f = 3.2256x102f = 3.2256e2F
Additional types
● In addition to these 4 type there can be
constructed using modifiers:
- unsigned, signed, short (int), long (int, double)
• C99
- long long
- int8_t, int16_t, int32_t, int64_t, intmax_t
- _Bool, _Complex, _Imaginary
Numerical Types Lengths
Numerical Type Lengths
• Can be found in library files limits.h and float.h
- CHAR_MAX, INT_MAX, INT_MIN, UINT_MAX
• Can be used a special operator sizeof
/* name: types_size.c
* purpose: find number of bytes ocupied by certain types*/
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main(void) {
printf("An int occupies: %d bytes \n", sizeof(int));
printf("Smallest int is: %d\n", INT_MIN);
printf("Greatest int is: %d\n\n", INT_MAX);
printf("A double occupies: %d bytes \n", sizeof(double));
printf("Smallest double is: %e\n", DBL_MIN);
printf("Greatest double is: %e\n", DBL_MAX);
return 0;}
Variables in C
● A program (with some logic) has at least:
– Variable declarations
– Variables initialization
– Main body
● Variable
– Storage unit in the program
– Type of variable determines storage size
Variables in C
● To place a value in a variable use assignment
● Variables must be declared before used
(assigned)
● A variable is declared when given a name
● C is Case Sensitive; names must begin with a
letter and can be followed by any non space
character (max length 31 chars to be portable)
Variables in C
● In ANSI C all variables must be declared
before any executable statement
● Even if we might initialize variables when
declared, in ANSI C they must be declared first
and then be given values
int var=0;
vs
int var;
var=0;
Variables Declaration
● Variables must be declared before first
executable instruction
● Declarations are constructions like:
– var_type var_name
● Char c;
● Int value;
● Float first_number, second_number;
● When declared, memory is reserved but they
have no meaning until they are assigned a
value
Variables Assignment
● Values are assigned to variables using “=”
operator
● Values assigned should have proper types, as
the variable is declared
● Examples:
char c;
int value;
c='c';
value=10;
Constant Values
● Fixed values that can’t be modified during
program execution
● The type of such a constant is given by the
way it is written
● Integer constants
- can be prefixed by a sign (+ or -)
- modifiers can be used: U - unsigned, l - long
- by default are represented as decimal (base 10)
- to specify a different base, a prefix is needed: 0
(zero) for octal, 0x or 0X for hexadecimal
Constant Values
● Floating point constants
- by default are of type double
- can be restricted to float using suffix f, or
- can be extended to long double using suffix Lf
● Character constants
- a single character between single quotes or
- ASCII code
● Character array constants
- character array specified between double quotes
Structure of a program
/* description of program */
#include <stdio.h>
/* any other includes , macro definitions, other
preprocessor directives go here */
int main(){
/* program body */
return 0;
}
Program Body