Fundamentals of Computing
Chapter 2: Overview of C
Prepared by:
Eng. Malek Al-Lozi
Updated by:
Yazan Almasri
School of Computing and Informatics – Al Hussein Technical University
Outlines
Overview of C
Hello World Program
Program Outlines with Code Compilation & Execution
Examples
Variable Declaration in C
Data Types in C
scanf() function
Newline (\n)
Arithmetic Expressions with C rules of evaluation
Page 2
Page 3
Overview of C
One advantage of C is that it lets you write programs that resemble
everyday English.
The power and flexibility of C, with the availability of high-quality C
compilers for computers of all sizes, have made it a popular language
in industry for a wide variety of applications.
Even though you do not yet know how to write your own programs,
you can probably read and understand the next first C program.
Page 4
Features of C
Page 5
So let us start to learn
Page 6
Hello World Program (1/7)
Implementing on Ubuntu will be like this
#include <stdio.h>
int main ( )
{
printf (“Hello, World!”) ;
return 0 ;
}
Page 7
Hello World Program (2/7)
The program tells the computer to print "Hello, World!“
Recall from Chapter 1: the Compiler.
Page 8
Hello World Program (3/7)
Every C program has a main function.
The parentheses following the word “main” indicate that it is a
function.
Page 9
Hello World Program (4/7)
• The lines of the program between braces called the body of the
function.
• The beginning of the main function where program execution begins.
Page 10
Hello World Program (5/7)
printf() function is used to print anything is written between quotation
marks “ ” onto the screen.
This function is defined in the standard input/output file “stdio.h”, which
is a header file in C language.
Semicolon, the end of the statement.
Page 11
Hello World Program (6/7)
Tells the compiler where to find the meaning of printf() function.
It is called preprocessor directive.
Page 12
Hello World Program (7/7)
The return statement transfers control from a function back to the
activator of the function.
For function main , control is transferred back to the operating system.
Page 13
Reserved Words
Appear in lowercase
Have special meaning
Cannot be used for other purposes
Page 14
Standard Identifiers
Have special meaning.
Can be redefined and used by programmer (not recommended)
Examples : printf, scanf
Page 15
User-Defined Identifiers
Constants
Variables
Syntax Rules:
▪ An identifier must consist only of letters, digits, and underscores
▪ An identifier cannot begin with a digit
▪ A C reserved word cannot be used as an identifier
▪ An identifier defined in a C standard library should not be redefined
Page 16
Program outline
Preprocessor Dircetives
int main()
{
Function body
}
Page 17
Code Compilation
gcc sourceFile.c –o outputFile
gcc: is the name of the compiler.
sourceFile.c: is the name of your c file.
-o: the name after it will be the executable output file name.
outputFile: you can choose your output file name.
Page 18
Code Execution
Compiling this file “test.c”
./outputFile
You can execute the output file.
You will see the output on the terminal.
Page 19
Example: Sum of two numbers
The output of the
program is to print
the sum of a and b
on the terminal.
Page 20
Let us
compile
and
execute
it:
Page 21
Variable declaration
Page 22
Variable declaration
To declare a variable you need two things:
The name of the variable
The type of data the variable will hold
Syntax: Datatype VariableName;
In our example: a, b and sum are variables of type int.
Tells the compiler to allocate memory cells for the variables.
Recall from chapter 1: the Memory.
Page 23
Data types
The Data type will affect:
The size of memory cells allocated for the variable
The representation of the binary digits inside these memory cells
Some of predefined data types in C
int, float, double, char, short, unsigned short, long, …etc
Page 24
Data Type : int
In mathematics, integers are whole numbers.
The int data type is used to represent integers in C.
Some values that you can store in a type int variable are
-10500 435 +15 -25 32767
Page 25
Integer Types in C
Page 26
Data Type : double
A real number has an integral part and a fractional part that are separated
by a decimal point.
In C, the data type double is used to represent real numbers.
Examples, 3.14159 , 0.0005 , 150.0
Page 27
Floating-Point Types in C
Page 28
Numbers Representation in the Memory
All data are represented in memory as binary digits (0s and 1s).
But the binary string stored for the type int value 13 is not the same
as the binary string stored for the type double number 13.0.
Page 29
Data Type : char
Represents an individual character value.
a letter, a digit, or a special symbol.
Each type char value is enclosed in apostrophes (single quotes) as
shown
'A' 'z' '2' '9' '*' ':’
Character is represented in memory as an integer.
The value stored is determined by the code used by your C compiler.
The ASCII code (American Standard Code for Information
Interchange) is the most common.
Page 30
ASCII Codes for Characters
Page 31
Comments
Page 32
Comments
Comments are ignored by the C compiler.
Provide supplementary information making it easier for us to
understand the program.
Syntax:
// comment goes here
Or:
/*
comment goes here
*/
Page 33
Assignment Statements
Page 34
Assignment Statements
Stores a value or a computational result in a variable
Syntax: variable = expression;
The expression can be a variable, a constant, or a combination of these
connected by appropriate operators (for example, + , − , / , and * ).
Page 35
Assignment Statements
After variable declaration and Before the execution of assignment
statements.
Page 36
Assignment Statements
After the first assignment statement a = 5;
Page 37
Assignment Statements
After the second assignment statement b = 8;
Page 38
Assignment Statements
After the third assignment statement sum = a + b;
Page 39
How to use Printf() Function to print the output?
Page 40
More about Printf() Function
Used to print anything is written between quotation marks “” onto the
screen.
Syntax:
printf(“format string”);
If we want to print variables, we must have a way to specify what
variable values should be displayed.
Syntax:
printf( format string, print list );
Page 41
Placeholder
Page 42
Placeholder
A placeholder always begins with the symbol %
It is used to substitute the value of (sum) for its placeholder ( %d ) in
the format string.
Here the placeholder %d marks the display position for a type int
variable.
Page 43
Placeholder
Format strings can have multiple placeholders.
If the print list of a printf call has several variables, the format string
should contain the same number of placeholders.
C matches variables with placeholders in left-to-right order.
Example:
printf("I am %d years old, and my gpa is %f", age, gpa);
Page 44
The scanf() function
Page 45
The scanf() function
Get data from the keyboard into variables
Syntax:
scanf( “format string” , input list );
The format string “ %d” consists of a single placeholder that tells scanf
what kind of data to get into the variable.
Page 46
The scanf() function
Page 47
The scanf() Function
One placeholder for each variable in the input list.
Commas are used to separate variable names
The order of the placeholders must correspond to the order of the
variables in the input list.
Example :
scanf("%d%d", &Variable1, &Variable2);
Page 48
The scanf() Function
Notice that in a call to scanf(), the name of variable that is to be
given a value is preceded by the ampersand character ( &)
The & operator tells the scanf function where to find variable into
which it is to store a new value.
The & in C called the address-of operator.
When scanf executes, the program pauses until the required data
are entered and the <enter> key is pressed.
Page 49
Newline (\n)
Page 50
Newline (\n)
It’s equivalent to pressing Enter and starting a new writing
line in any word processor.
It is called newline escape sequence
Example:
printf(“Hello\nWorld”);
The output would be:
Hello
World!
Page 51
Arithmetic Expressions
To solve most programming problems, you will need to
write arithmetic expressions that manipulate type int and
double data.
Page 52
C rules for evaluating expressions
a = x + y / z;
Is + performed before / or is + performed after / ?
Page 53
C rules for evaluating expressions
1. Parentheses rule: All expressions in parentheses must be evaluated
separately. Nested parenthesized expressions must be evaluated from
the inside out, with the innermost expression evaluated first.
2. Operator precedence rule: Operators in the same expression are
evaluated in the following order:
unary +, - first
* , /, % next
binary +, - last
Page 54
C rules for evaluating expressions
3. Associativity rule: Unary operators in the same subexpression and
at the same precedence level (such as + and −) are evaluated right
to left ( right associativity) . Binary operators in the same
subexpression and at the same precedence level (such as + and −) are
evaluated left to right ( left associativity) .
Page 55
C rules for evaluating expressions
Example: the expression
value = x * y * z + a / b - c * d;
can be written in a more readable form using parentheses
value = (x * y * z ) + (a / b) - (c * d);
Page 56
Any Questions???…
Page 57