0% found this document useful (0 votes)
8 views

CMP1401: Introduction To Programming With C: Ece Gelal Soyak Bahc Es Ehir Universitesi

This document provides an overview of data types in the C programming language. It discusses the different data types that can be used to store data in C programs and how to take user input and display output using stream input/output functions. The document also briefly explains operators that can be used to perform operations on data types in C.

Uploaded by

hicape5709
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

CMP1401: Introduction To Programming With C: Ece Gelal Soyak Bahc Es Ehir Universitesi

This document provides an overview of data types in the C programming language. It discusses the different data types that can be used to store data in C programs and how to take user input and display output using stream input/output functions. The document also briefly explains operators that can be used to perform operations on data types in C.

Uploaded by

hicape5709
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

CMP1401: Introduction to Programming with C


0 1
14
Ece Gelal Soyak

P
Bahçeşehir Üniversitesi

C M
Week 2: Data Types and Variables
Oct. 20, 2020

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 1 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

In this lecture...

0 1
4
An overview of the C programming language

In which format do we maintain data in C?

How do we take user input in C?


How do we show the user a data using C?
P 1
M
Which operations can we perform on data in C?

C
Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 2 / 37
Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Programming – Some Definitions

Computer program
set of instructions that tell a computer exactly what to do

0
e.g. add up a set of numbers, compare two numbers and make a 1
decision based on the result, etc.

Programming language
14
P
for a computer to recognize the instructions they need to be written in a
programming language
e.g. C, C++, Java, Python, Javascript, PHP, Perl, etc.

Compiler

C M
Translates a program written in computer language into a form that a
computer can execute
e.g. .exe files

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 3 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Types of Programming Languages


High-level programming languages
Examples are: C++, Fortran, Java, Python

0
e.g. Python code at an ATM machine for a withdrawal of $100:
x = 100 1
4
if balance x:
print ’Insufficient balance’

1
else:
print ’Please take your money’
Assembly

P
Makes machine language more readable by humans
A translator (“assembler”) is needed to translate to machine code

e.g.

C M
Each processor family has its own assembly language
MOVE, ADD, SUB, END
Machine language (machine code)
Directly understood by the computer, doesn’t need to be translated
Lowest level represented by an assembled or compiled program
e.g. 10010101100101001111101010011011100101

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 4 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Compilers

It is a computer program

0 1
4
Translates a program (“source code”) written in high-level language
to machine language

executed

P 1
Entire source code needs to be compiled before the program can be

Compiled code is “executable’ -- suitable for running on a computer

M
A compiled code can be executed over and over again

C
Compilation does not process input or produce output
Input/output used when the compiled code is executed (“i.e., the
program is running”)

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 5 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Programming Jargon

Doctors do not discuss diagnoses with each other in simple terms

0 1
“transverse compound fracture of the tibia” instead of “broken leg”

14
In programming, too, we use terminology to communicate accurately

P
I’ll explain programming terminology and highlight them in slide

M
Please interrupt and ask if I am using a term you haven’t heard before

C
Visit this blog for the a fun video and for some programming terminology

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 6 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

C
The most widely used programming language of all time
Due to its speed, stability, and near-universal availability

Developed by Dennis Ritchie 1969-1973


0 1
Is a compiled language

14
Statically- and weak- typed (all data has a type, but implicit conversions may

P
be performed)

Enables programmers efficiently implement algorithms and data

M
structures -- useful for computationally intense programs
GNU Scientific Library, Mathematica, MATLAB are written in C

C
Compilers, libraries and interpreters of other programming languages
are often implemented in C ( e.g. Python, Perl, PHP)
Portable and thus widely used for system programming (operating
systems, embedded system applications)
Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 7 / 37
Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

In C Programs...

0 1
1
A C program consists of functions and variables
4
A function contains statements that specify the computing operations
to be done

P
Variables store values used during the computation

C M
Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 8 / 37
Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

A Simple Program

0 1
int main() // function main begins program execution
14
#include <stdio.h> // allows program to output data to the screen

P
{
printf("Welcome to C!\n"); // display message

M
return 0; // indicate that program ended successfully
} // end function main

C
Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 9 / 37
Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

We’ll break down the simple program:

1
#include <stdio.h>

0
int main()
{

4
printf ( ”Welcome to C!\n”);
return 0;

1
}

#include
int main()
P
“syntax” of I/O in C

C M
the form, not the meaning
set of rules that define the correctly structured code in a programming
language

“Commenting out”/commenting in

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 10 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

int main()

Designated start of a program


Exactly one function in every program must be main
0 1
14
Main will usually call other functions to help perform its job

int main() → main function “returns” an integer value

Syntax:
P
int main ()
{

}
C M
/* body of your code */

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 11 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Preprocessor Directives

Lines that begin with # are processed by the preprocessor before the
program is compiled

0 1
#include <stdio.h> is a preprocessor directive

14
Notifies the preprocessor to include the input/output header file in the
program

#define, #if, #endif are other examples P


C M
Preprocessor directives (like #include) do not end with a semicolon
(Whereas every C statement must end with a semicolon)

Here is a detailed definition with examples

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 12 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Variables

1
A variable is represented by:
Storage -- where is it stored, for example data, stack, heap
Scope -- who can see us, for example global, local...

4
Type -- what is our type, for example int, int*...
Address -- where are we located 0
Value -- what is our value

With a variable, you can:


Read the value in it
P 1
M
Write a value to it
Change the value of it

C
Variables are given names -- called an identifier
The name should indicate the purpose (THIS IS IMPORTANT.)
Name must begin with a letter
Name cannot be a reserved word in the C language

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 13 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Putting Data in Variables

*** ( All variables must be declared before use ) ***

0 1
We can put data into variables in one of three ways;
At initialization -- e.g. int y = 45;

14
Using an assignment (variable = expression;)
x = 1.2;
e.g.
P
e.g.
C M
area = (base * height) / 2;
Upon taking an input -- scanf(”%s”, &variable);
float height; scanf("%s", &height);

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 14 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

C Data Types

Built-in types (primitive data types)


Defined in the C compiler
0 1
14
Can be integral, floating point, or void:
Integral types are capable of handling whole numbers
Floating point types are capable of specifying values that may have
fractional parts

P
void type describes an empty set of values

different amounts of memory

C M
Different data types have different ranges of values and require

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 15 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Built-in Data Types - 1: Integer Types

1
bool (1 byte = 8 bits)
Has a boolean value (true / false)

char (1 byte)

4
contains members of the basic execution character set (ASCII) 0
short int (2 bytes = 16 bits)

P 1
Has an integer value between −(2 ) and +(215 ) − 1

unsigned short int (2 bytes)


15

int (4 bytes = 32 bits) M


Has an integer value between 0 and +(216 ) − 1

C
Has an integer value between −(231 ) and +(231 ) − 1
unsigned int has integer value between 0 and 232 − 1

long long (8 bytes)


Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 16 / 37
Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Built-in Data Types - 2: Floating Point Types

float (4 bytes = 64 bits)


Floating-point number
Can take positive or negative value
0 1
double (8 bytes = 64 bits)

14
Double-precision floating-point number

long double
80-bit extended precision
P
C M
If precision is more of a concern for you than storage, use double

If precision is not crucial, then use float

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 17 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

More on char

A variable of type char holds a single character


0 1
Represented within single quotation marks ‘’

14
“a” is a string of characters containing one character
‘a’ is a single char constant

P
M
Each character has a numeric value from the ASCII table
e.g. ‘A’: 65; ‘a’ : 97

C
ASCII code is the numerical representation of a character

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 18 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Type Casting

Changing an variable of one data type into another

0 1
4
Implicit
Occurs when operands are of different types
e.g. short a=2000;
int b;
b=a;
P 1
Explicit

C M
When the developer wants to change the variable type
e.g. int sum = 17, count = 5;
double mean;
mean = (double) sum / count;

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 19 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

<stdio.h>

Is a C library

0 1
Allows the program to:
output text to the screen
get input from the user
14
P
This file must be included for any C program that inputs/outputs data

C M
Input/Output in C are accomplished with streams of characters

#include<stdio.h> tells the compiler to include information about the


standard input/output library

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 20 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

The printf Function

printf is used to display information on screen (the standard output)


0 1
It returns the number of characters printed:

14
P
#include <stdio.h>

main()
{

}
printf("Welcome to C!\n");

C M
Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 21 / 37
Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Placeholders (Format Specifiers)

How do we display the sum of 20 and 15? printf() only prints strings!

We put a placeholder format code in the string:


0 1
printf("20+15 is ’%d’ ", 20+15);

14
%d literally “holds the place” for the actual number that is the result of

P
adding 20 to 15
Other format specifiers that work with printf():

%f – float
%lf – double
C M
%c – char (single character)
%d (same as %i) – int

%s – string (char sequence of characters)


%x – int – unsigned hex value

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 22 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Examples

#include <stdio.h>
0 1
main()
{

14
printf("Welcome to CMP %d, Introduction to Programming in C!\n",
1401);

P
M
int i = 60;
printf("There are %d students in this class \n", i );

C
}

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 23 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Escape Sequence (1/2)

In a program:
printf("Welcome to C!\n";
0 1
4
The characters \n are not printed on the screen

\n is an escape sequence

P 1
The backslash (\) is called an escape character
indicates that a special character is to be output

Some escape sequences:

C M
\n: newline → the cursor (current screen-position indicator) moves to
the beginning of the next line
\t: tab → a tab is printed
\”: double quotes → double quotes are printed

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 24 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Escape Sequence (2/2)

1
(Example from https://en.cppreference.com/w/cpp/language/escape)

#include <stdio.h>
int main()
{
4 0
}
return 0;

P 1
printf("This\nis\na\ntest\n\nShe said, \"Hello!\" ");

Output:
This

a
C
is

test
M
She said, “Hello!”

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 25 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

The scanf Function

scanf is used to read information from a standard input device


(keyboard)

0 1
4
& is used to indicate the memory address of the variable
We use it so the input stream is written in that memory address

#include <stdio.h>

main()
P 1
{
int i;
scanf("%d", &i);

C
printf("you entered: %d \n", i);
return 0;
M
}

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 26 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Assignment Operators

Assigns a value to a variable


When new value is assigned, old one is overwritten (LOST)

0 1
4
“=” is the assignment operator
Syntax: Variable = Expression ;
e.g.

P 1
total pairs = total count / 2;
The variable to be changed is always on the left of “=”
On the right of “=” must be an expression, examples:
Constants: age = 18;

C M
Variables: new budget = money earned;
Arithmetic expressions: circumference = diameter *
3.14159;
Data types on both sides of “=” must be the same

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 27 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Example: Assignment Operators

#include <stdio.h>

int main ()
0 1
{
int a, b; // a:?, b:?
a = 10; // a:10, b:?

14
P
b = 4; // a:10, b:4
a = b; // a:4, b:4
b = 7; // a:4, b:7
printf("a: ");
printf(a);
printf(" b: \%d", b);

return 0; C M
}

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 28 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Arithmetic Assignment Operators

Assume: int c = 3, d = 5, e = 4, f = 6, g = 12:

0 1
Assignment
Operator
Example
14
Explanation Assigns
+=
-=
c += 7
d -= 4
c=c+7
d=d-4
P c = 10
d=1
*=
/=
%=
e *= 5
f /= 3
g %= 9

C M e=e*5
f=f/3
g=g%9
e = 20
f=2
g=3

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 29 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Arithmetic Operators

1
Addition: The + operator -- adds its operands
E.g. 3+4 evaluates to 7

E.g. 12 − 3 evaluates to 9

4 0
Subtraction: The − operator -- subtracts the second operand from the first

E.g. 8 ∗ 4 evaluates to 32

P 1
Multiplication: The ∗ operator -- multiplies its operands

Division: The ∗ operator -- divides its first operand by the second

C M
E.g. 100 / 5 evaluates to 20
If both operands are integers, the result is the integer portion of the quotient.
For example, 17 / 3 is 5, with the fractional part discarded.

Modulus: The % operator -- finds the modulus of its first operand with respect
to the second (remainder of integer division)
E.g. 19 ∗ 6 evaluates to 1

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 30 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Example: Arithmetic Operators


#include <stdio.h>

int main()
{

0 1
4
int number1, number2;

1
printf("Enter a number: ");
scanf("%d", &number1);

P
printf("Enter another number: ");
scanf("%d", &number2);

printf("number1
printf("number1
printf("number1
printf("number1
printf("number1
=
+
-
*
/
M
%d; number2=%d\n", number1, number2);
number2 = %d\n", number1 + number2);

C
number2 = %d\n", number1 - number2);
number2 = %d\n", number1 * number2);
number2 = %f\n", number1 / number2);
printf("number1 % number2 = %d\n", number1 \% number2);
return 0;
}
Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 31 / 37
Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Relational Operators

Operator Description
0 1
and
or
not 4
true iff both operands are true
false iff both operands are false

1
true iff operand is false

P
= true iff operands are equal
< true iff left operand is less than right
>

M
true iff right operand is greater than right
<= true iff left is less than or equal to right

C
>= true iff left is greater than or equal to right

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 32 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Increment/Decrement Operators

a++ : Post-increment

0 1
4
Use the current value of a in the expression, then increment a by 1

++a : Pre-increment

1
Increment a by 1 then use the new value of a

a -- : Post-decrement
P
--a: Pre-decrement

C M
Use the current value of a in the expression, then decrement a by 1

Decrement a by 1 then use the new value of a

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 33 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Logical Operators

1
a true false
Negation ( ! )
!a false true

a b &&

4 0
1
true true true
AND ( && ) true false false
false
false
true
false
P
false
false

a
true
Inclusive OR ( || ) true
C M b
true
false
||
true
true
false true true
false false false

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 34 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Summary & Tips

Storing a variable (allocate memory)


0 1
Proper I/O design
Properly prompt for input

14
Don’t assume the user knows what you’re asking for

P
Echo the input by displaying what was read (gives user a chance to
verify the data)

C M
Use constants. Never hard code values.
Use parentheses to alter the order of arithmetic operations

Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 35 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Exercise - 1

0 1
14
Write a C program that inputs three integers from the keyboard and prints

P
the sum, average, and product.

C M
Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 36 / 37
Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Exercise - 2

0 1
14
Write a program that gets the radius of a circle and prints the diameter,

P
area and circumference. Use the constant value 3.14159 for pi value.

C M
Ece Gelal Soyak · Bahcesehir University Week 2 – Oct. 20, 2020 37 / 37

You might also like