SlideShare a Scribd company logo
CSE240 – Introduction to
Programming Languages
Lecture 07:
Programming with C
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2
Imperative Paradigm
Fully specified and fully controlled
manipulation of named data in a stepwise fashion.
which means that:
Programs are algorithmic in nature:
do this, then that, then repeat this ten times
Focuses on how rather than what.
variables
functions (methods in Java)
Loop statements
Conditional statements
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3
Paradigms
variables
functions (methods in Java)
Loop statements
Conditional statements
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4
Paradigms
variables
functions (methods in Java)
Loop statements
Conditional statements
// Different from
// Java
Arrays
Structs
Pointers
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5
Paradigms
variables
functions (methods in Java)
Loop statements
Conditional statements
/*There are NOT
classes
Classes and
Objects will
appear in C++
*/
// Different from
// Java
Array
Structures
Pointers
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 6
Outline
1. Getting started with Language C
2. Primitive data types, arrays (and strings)
3. Pointers
4. typedef, enum and struct type
5. Functions calls and parameter passing
1. Getting Started
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8
We Need a Compiler
• Microsoft Visual Studio for C/C++
View instructions on Blackboard
• Online Compiler for C/C++
https://www.onlinegdb.com/online_c_compiler
• Dev-C++ 5
http://www.bloodshed.net/devcpp.html
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9
Reference
• Textbook
Section 2.1 to 2.5 and 2.7
Optional 2.6 (files), 2.7 (recursion). – not included in the exam
• The Programming Language C by Kernighan and Ritchie
https://archive.org/details/TheCProgrammingLanguageFirstEdition
• Learning C programming
https://www.tutorialspoint.com/cprogramming/cprogramming_tutorial.pdf
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 10
Anatomy of a Program in C
• The parts (components) of a C program are called functions.
• There are built-in functions that exist in libraries and user defined
functions that are written by the programmers.
• You can declare variables inside and outside functions. They are
called local variables and global variables, respectively.
• And, a program in C must contain exactly one main( ) function.
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 11
Getting Started
1. Comments are identical in Java
2. #include is equivalent to import
3. Libraries are *.h files
4. There are not classes
5. Methods are called functions.
6. Global variables, local
variables, and parameters.
7. Most of the lexical, syntactical,
and semantical rules are as you
know from your Java courses
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 12
main()
8. Like Java, main( ) is the entry point
for program execution.
But, (1) C allows void or int as
type for main; (2) C allows empty
parameters or two parameters for
main. This are correct:
• void main () {
}
• void main (int argc, char *argv [ ]) { }
• int main() {return 0;}
• main() {return 0;} // if there is not a
type, C apply as default int
• int main (int argc, char *argv [ ]) { }
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 13
main()
8. Like Java, main( ) is the entry point
for program execution.
But, (1) C allows void or int as
type for main; (2) C allows empty
parameters or two parameters for
main. This are correct:
• void main () {
}
• void main (int argc, char *argv [ ]) { }
• int main() {return 0;}
• main() {return 0;} // if there is not a
type, C apply as default int
• int main (int argc, char *argv [ ]) { }
public static void main(String [] args) {
}
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 14
Output
printf (control sequence, expressions);
• The control sequence includes a
constant string to be printed, e.g.,
"Result: ", and control symbols to be
used to convert variables from their
numeric values that are stored in the
computer to printing format.
• The expressions is the list of
expressions whose values are to be
printed out. Each expression is
separated by a comma. This is
optional.
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 15
Output
printf (control sequence, expressions);
• The control sequence includes a
constant string to be printed, e.g.,
"Result: ", and control symbols to be
used to convert variables from their
numeric values that are stored in the
computer to printing format.
• The expressions is the list of
expressions whose values are to be
printed out. Each expression is
separated by a comma.
%d for integer
%f for floating point
%c for character
%s for string of characters
%p address
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 16
Output
printf (control sequence, expressions);
• The control sequence includes a
constant string to be printed, e.g.,
"Result: ", and control symbols to be
used to convert variables from their
numeric values that are stored in the
computer to printing format.
• The expressions is the list of
expressions whose values are to be
printed out. Each expression is
separated by a comma. This is
optional.
// Java
int x = 5;
float y = 10.3f;
System.out.println("hello " + x + " bye " + y);
// C
int x = 5;
float y = 10.3;
printf("hello %d bye %f", x, y);
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 17
#include <stdio.h>
void main () {
int i, n = 5;
printf("Hi, please enter an integer: ");
// input: scanf (control sequence, &variable1, ... &variablen);
// &variable: address of the variable.
scanf("%d", &i); // input function
if (i > n)
n = n + i;
else
n = n - i;
printf("i = %d, n = %dn", i, n); //output function
}
Input
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 18
Control Statements
These that you know, work in the same way
• for, while, do/while
• if/else, switch
But, there are not boolean values. Zero is false and any other number is true.
The following program is correct and print “Hello”
Primitive Data Types
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 20
Primitive Data Types and Modifiers
C defines five basic types
• int - integer, a whole number.
• float - floating point value i.e., a number with a fractional part.
• double - a double-precision floating point value.
• char - a single character.
• void - valueless special purpose type which we will examine closely in later sections.
C++ will add
• bool – boolean values
Primitive types can be modified using one or more of these modifiers
• signed,
• unsigned,
• short,
• long
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 21
Type Guaranteed minimum range ³ bits
char -127 to 127 or 0 to 255 8
signed char -127 to 127 8
unsigned char 0 to 255 8
int -32,768 to 32,767 16
signed int same as int 16
unsigned int 0 to 65,535 16
short int -32,768 to 32,767 16
signed short int same as short int 16
unsigned short int unsigned int 16
long int ±2,147,483,647 32
signed long int same as long int 32
unsigned long int 0 to 4,294,967,295 32
float 6 decimal digits of precision 32
double 10 decimal digits of precision 64
long double 10 decimal digits of precision 64
bool (C++ only) true/false 1 (8)
Basic Data Types and Ranges in C
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 22
Type Guaranteed minimum range ³ bits
char -127 to 127 or 0 to 255 8
signed char -127 to 127 8
unsigned char 0 to 255 8
int -32,768 to 32,767 16
signed int same as int 16
unsigned int 0 to 65,535 16
short int -32,768 to 32,767 16
signed short int same as short int 16
unsigned short int unsigned int 16
long int ±2,147,483,647 32
signed long int same as long int 32
unsigned long int 0 to 4,294,967,295 32
float 6 decimal digits of precision 32
double 10 decimal digits of precision 64
long double 10 decimal digits of precision 64
bool (C++ only) true/false 1 (8)
Basic Data Types and Ranges in C
unsigned int x = 65000;
int x = 32767;
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 23
Type Guaranteed minimum range ³ bits
char -127 to 127 or 0 to 255 8
signed char -127 to 127 8
unsigned char 0 to 255 8
int -32,768 to 32,767 16
signed int same as int 16
unsigned int 0 to 65,535 16
short int -32,768 to 32,767 16
signed short int same as short int 16
unsigned short int unsigned int 16
long int ±2,147,483,647 32
signed long int same as long int 32
unsigned long int 0 to 4,294,967,295 32
float 6 decimal digits of precision 32
double 10 decimal digits of precision 64
long double 10 decimal digits of precision 64
bool (C++ only) true/false 1 (8)
Basic Data Types and Ranges in C
Arrays
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 25
Arrays
Homogeneous collection of data elements (all have the same type). Individual
elements are accessed by an index.
int a[3], i, j, k; // a is not initialized
int ma[2][3] = {{4, 2, 3}, {7, 8, 9}};
int b[4] = {2, 3, 9, 4}; // b is initialized
a[0] = 2; // index starts from 0
a[1] = 3;
a[2] = 9;
i = sizeof a; // # of bytes used by a is 12
j = sizeof b; // # of bytes used by b is 16
k = sizeof ma; // # of bytes used by ma is 24
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 26
Arrays and Strings
A string is an array of characters.
There is not a class String (like in Java) or a primitive data type for strings
In other words, an array of characters can be seen as:
• An array of characters
• An string
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 27
• There are two ways of initializing an array of characters in
declaration:
char s1[] ={'a', 'l', 'p', 'h', 'a'}; // as an array of char
char s2[] ="alpha"; // as a string
• These two initializations have different results in memory:
• where '0' is the null terminator (null character), marking the end of a
string. It is automatically added to the end of a string.
a l p h as1 size = 5 a l p h a 0s2 size = 6
Array and String
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 28
• To have the same result, we could do:
// char s1[] = "alpha";
char s1[]={'a', 'l', 'p', 'h', 'a', '0'};
• If the size of the array is specified and
there is not enough space, the null
character will not be attached to the
string.
char s2[5] = "alpha";
char s3[4] = "alpha";
a l p h a 0s1
a l p h as2 size = 5
a l p hs3 size = 4
Array and String
size = 5
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 29
#include <stdio.h>
main() {
char s1[ ] = "hello";
printf("%s n", s1);
for (int i = 0; i < 5; i++)
printf("%c", s1[ i ]);
printf("n");
return 0;
}
Arrays and Strings
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 30
Array and Strings
#include <stdio.h>
#include <string.h>
main() {
int i;
char s1[ ] = "hello", s2[] = "world";
for (i = 0; i < 5; i++)
printf("%c", s1[ i ]);
printf("n");
printf("s1 = %s, size = %dn", s1, sizeof s1);
for (i = 0; i < 5; i++)
printf("%c", s2[ i ]);
printf("n");
}
h e l l o 0
w o r l d 0
Constants
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 32
Constants
C allows two ways to define a constant:
• const It is equivalent to final in Java. This way is slower since it have
to read memory.
const int i = 5;
i = i + 2; // this line will cause a compilation error
• #define It substitute values for constant definitions in compilation time.
Provide a faster execution.
#define PI 3.14
int x = PI * 5;
CSE240 – Introduction to Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
Fall 2017
Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.

More Related Content

PPT
Unit i intro-operators
PDF
C++ Tokens
PPTX
C Token’s
PPT
Basic concept of c++
PPTX
COM1407: Type Casting, Command Line Arguments and Defining Constants
PPTX
C++ PROGRAMMING BASICS
PDF
important C questions and_answers praveensomesh
PPTX
C programming tutorial for Beginner
Unit i intro-operators
C++ Tokens
C Token’s
Basic concept of c++
COM1407: Type Casting, Command Line Arguments and Defining Constants
C++ PROGRAMMING BASICS
important C questions and_answers praveensomesh
C programming tutorial for Beginner

What's hot (20)

PPTX
C++ Programming Language Training in Ambala ! Batra Computer Centre
PDF
VTU PCD Model Question Paper - Programming in C
PPT
Getting Started with C++
PDF
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
PPTX
Learn c++ Programming Language
PPT
T03 b basicioscanf
PPT
Basic of c &c++
ODP
(2) cpp imperative programming
PPT
Basics of c++
PPT
Basics of c++ Programming Language
DOC
C Programming
PPTX
What is c
PPTX
JavaScript – ECMAScript Basics By Satyen
PPTX
Cs1123 3 c++ overview
PPT
C++ for beginners
DOC
Data structures question paper anna university
PDF
PDF
C Programming
PPTX
POLITEKNIK MALAYSIA
C++ Programming Language Training in Ambala ! Batra Computer Centre
VTU PCD Model Question Paper - Programming in C
Getting Started with C++
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
Learn c++ Programming Language
T03 b basicioscanf
Basic of c &c++
(2) cpp imperative programming
Basics of c++
Basics of c++ Programming Language
C Programming
What is c
JavaScript – ECMAScript Basics By Satyen
Cs1123 3 c++ overview
C++ for beginners
Data structures question paper anna university
C Programming
POLITEKNIK MALAYSIA
Ad

Similar to 201801 CSE240 Lecture 07 (20)

PDF
201801 CSE240 Lecture 06
PPTX
C programming language
PPT
Introduction to C#
PDF
Lec-1c.pdf
PPTX
the refernce of programming C notes ppt.pptx
PDF
C programing Tutorial
PPTX
C programming
PDF
C and Data structure lab manual ECE (2).pdf
PDF
C-PPT.pdf
PPTX
introductory concepts
PDF
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
PPTX
C sharp part 001
PDF
Pc module1
PPTX
Semantic-Aware Code Model: Elevating the Future of Software Development
PPTX
INTRODUCTION TO C++.pptx
PPTX
Fundamentals of computers - C Programming
PDF
201801 CSE240 Lecture 08
PPSX
Esoft Metro Campus - Certificate in c / c++ programming
PDF
C# 7 development
PPTX
basics of c programming for naiver.pptx
201801 CSE240 Lecture 06
C programming language
Introduction to C#
Lec-1c.pdf
the refernce of programming C notes ppt.pptx
C programing Tutorial
C programming
C and Data structure lab manual ECE (2).pdf
C-PPT.pdf
introductory concepts
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
C sharp part 001
Pc module1
Semantic-Aware Code Model: Elevating the Future of Software Development
INTRODUCTION TO C++.pptx
Fundamentals of computers - C Programming
201801 CSE240 Lecture 08
Esoft Metro Campus - Certificate in c / c++ programming
C# 7 development
basics of c programming for naiver.pptx
Ad

More from Javier Gonzalez-Sanchez (20)

PDF
201804 SER332 Lecture 01
PDF
201801 SER332 Lecture 03
PDF
201801 SER332 Lecture 04
PDF
201801 SER332 Lecture 02
PDF
201801 CSE240 Lecture 26
PDF
201801 CSE240 Lecture 25
PDF
201801 CSE240 Lecture 24
PDF
201801 CSE240 Lecture 23
PDF
201801 CSE240 Lecture 22
PDF
201801 CSE240 Lecture 21
PDF
201801 CSE240 Lecture 20
PDF
201801 CSE240 Lecture 19
PDF
201801 CSE240 Lecture 18
PDF
201801 CSE240 Lecture 17
PDF
201801 CSE240 Lecture 16
PDF
201801 CSE240 Lecture 15
PDF
201801 CSE240 Lecture 14
PDF
201801 CSE240 Lecture 13
PDF
201801 CSE240 Lecture 12
PDF
201801 CSE240 Lecture 11
201804 SER332 Lecture 01
201801 SER332 Lecture 03
201801 SER332 Lecture 04
201801 SER332 Lecture 02
201801 CSE240 Lecture 26
201801 CSE240 Lecture 25
201801 CSE240 Lecture 24
201801 CSE240 Lecture 23
201801 CSE240 Lecture 22
201801 CSE240 Lecture 21
201801 CSE240 Lecture 20
201801 CSE240 Lecture 19
201801 CSE240 Lecture 18
201801 CSE240 Lecture 17
201801 CSE240 Lecture 16
201801 CSE240 Lecture 15
201801 CSE240 Lecture 14
201801 CSE240 Lecture 13
201801 CSE240 Lecture 12
201801 CSE240 Lecture 11

Recently uploaded (20)

PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Transform Your Business with a Software ERP System
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
top salesforce developer skills in 2025.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
System and Network Administraation Chapter 3
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
ai tools demonstartion for schools and inter college
Reimagine Home Health with the Power of Agentic AI​
Understanding Forklifts - TECH EHS Solution
Transform Your Business with a Software ERP System
Softaken Excel to vCard Converter Software.pdf
Design an Analysis of Algorithms II-SECS-1021-03
VVF-Customer-Presentation2025-Ver1.9.pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
2025 Textile ERP Trends: SAP, Odoo & Oracle
top salesforce developer skills in 2025.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Upgrade and Innovation Strategies for SAP ERP Customers
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
System and Network Administraation Chapter 3
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
How to Choose the Right IT Partner for Your Business in Malaysia
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Design an Analysis of Algorithms I-SECS-1021-03
ai tools demonstartion for schools and inter college

201801 CSE240 Lecture 07

  • 1. CSE240 – Introduction to Programming Languages Lecture 07: Programming with C Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2 Imperative Paradigm Fully specified and fully controlled manipulation of named data in a stepwise fashion. which means that: Programs are algorithmic in nature: do this, then that, then repeat this ten times Focuses on how rather than what. variables functions (methods in Java) Loop statements Conditional statements
  • 3. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3 Paradigms variables functions (methods in Java) Loop statements Conditional statements
  • 4. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4 Paradigms variables functions (methods in Java) Loop statements Conditional statements // Different from // Java Arrays Structs Pointers
  • 5. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5 Paradigms variables functions (methods in Java) Loop statements Conditional statements /*There are NOT classes Classes and Objects will appear in C++ */ // Different from // Java Array Structures Pointers
  • 6. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 6 Outline 1. Getting started with Language C 2. Primitive data types, arrays (and strings) 3. Pointers 4. typedef, enum and struct type 5. Functions calls and parameter passing
  • 8. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8 We Need a Compiler • Microsoft Visual Studio for C/C++ View instructions on Blackboard • Online Compiler for C/C++ https://www.onlinegdb.com/online_c_compiler • Dev-C++ 5 http://www.bloodshed.net/devcpp.html
  • 9. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9 Reference • Textbook Section 2.1 to 2.5 and 2.7 Optional 2.6 (files), 2.7 (recursion). – not included in the exam • The Programming Language C by Kernighan and Ritchie https://archive.org/details/TheCProgrammingLanguageFirstEdition • Learning C programming https://www.tutorialspoint.com/cprogramming/cprogramming_tutorial.pdf
  • 10. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 10 Anatomy of a Program in C • The parts (components) of a C program are called functions. • There are built-in functions that exist in libraries and user defined functions that are written by the programmers. • You can declare variables inside and outside functions. They are called local variables and global variables, respectively. • And, a program in C must contain exactly one main( ) function.
  • 11. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 11 Getting Started 1. Comments are identical in Java 2. #include is equivalent to import 3. Libraries are *.h files 4. There are not classes 5. Methods are called functions. 6. Global variables, local variables, and parameters. 7. Most of the lexical, syntactical, and semantical rules are as you know from your Java courses
  • 12. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 12 main() 8. Like Java, main( ) is the entry point for program execution. But, (1) C allows void or int as type for main; (2) C allows empty parameters or two parameters for main. This are correct: • void main () { } • void main (int argc, char *argv [ ]) { } • int main() {return 0;} • main() {return 0;} // if there is not a type, C apply as default int • int main (int argc, char *argv [ ]) { }
  • 13. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 13 main() 8. Like Java, main( ) is the entry point for program execution. But, (1) C allows void or int as type for main; (2) C allows empty parameters or two parameters for main. This are correct: • void main () { } • void main (int argc, char *argv [ ]) { } • int main() {return 0;} • main() {return 0;} // if there is not a type, C apply as default int • int main (int argc, char *argv [ ]) { } public static void main(String [] args) { }
  • 14. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 14 Output printf (control sequence, expressions); • The control sequence includes a constant string to be printed, e.g., "Result: ", and control symbols to be used to convert variables from their numeric values that are stored in the computer to printing format. • The expressions is the list of expressions whose values are to be printed out. Each expression is separated by a comma. This is optional.
  • 15. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 15 Output printf (control sequence, expressions); • The control sequence includes a constant string to be printed, e.g., "Result: ", and control symbols to be used to convert variables from their numeric values that are stored in the computer to printing format. • The expressions is the list of expressions whose values are to be printed out. Each expression is separated by a comma. %d for integer %f for floating point %c for character %s for string of characters %p address
  • 16. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 16 Output printf (control sequence, expressions); • The control sequence includes a constant string to be printed, e.g., "Result: ", and control symbols to be used to convert variables from their numeric values that are stored in the computer to printing format. • The expressions is the list of expressions whose values are to be printed out. Each expression is separated by a comma. This is optional. // Java int x = 5; float y = 10.3f; System.out.println("hello " + x + " bye " + y); // C int x = 5; float y = 10.3; printf("hello %d bye %f", x, y);
  • 17. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 17 #include <stdio.h> void main () { int i, n = 5; printf("Hi, please enter an integer: "); // input: scanf (control sequence, &variable1, ... &variablen); // &variable: address of the variable. scanf("%d", &i); // input function if (i > n) n = n + i; else n = n - i; printf("i = %d, n = %dn", i, n); //output function } Input
  • 18. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 18 Control Statements These that you know, work in the same way • for, while, do/while • if/else, switch But, there are not boolean values. Zero is false and any other number is true. The following program is correct and print “Hello”
  • 20. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 20 Primitive Data Types and Modifiers C defines five basic types • int - integer, a whole number. • float - floating point value i.e., a number with a fractional part. • double - a double-precision floating point value. • char - a single character. • void - valueless special purpose type which we will examine closely in later sections. C++ will add • bool – boolean values Primitive types can be modified using one or more of these modifiers • signed, • unsigned, • short, • long
  • 21. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 21 Type Guaranteed minimum range ³ bits char -127 to 127 or 0 to 255 8 signed char -127 to 127 8 unsigned char 0 to 255 8 int -32,768 to 32,767 16 signed int same as int 16 unsigned int 0 to 65,535 16 short int -32,768 to 32,767 16 signed short int same as short int 16 unsigned short int unsigned int 16 long int ±2,147,483,647 32 signed long int same as long int 32 unsigned long int 0 to 4,294,967,295 32 float 6 decimal digits of precision 32 double 10 decimal digits of precision 64 long double 10 decimal digits of precision 64 bool (C++ only) true/false 1 (8) Basic Data Types and Ranges in C
  • 22. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 22 Type Guaranteed minimum range ³ bits char -127 to 127 or 0 to 255 8 signed char -127 to 127 8 unsigned char 0 to 255 8 int -32,768 to 32,767 16 signed int same as int 16 unsigned int 0 to 65,535 16 short int -32,768 to 32,767 16 signed short int same as short int 16 unsigned short int unsigned int 16 long int ±2,147,483,647 32 signed long int same as long int 32 unsigned long int 0 to 4,294,967,295 32 float 6 decimal digits of precision 32 double 10 decimal digits of precision 64 long double 10 decimal digits of precision 64 bool (C++ only) true/false 1 (8) Basic Data Types and Ranges in C unsigned int x = 65000; int x = 32767;
  • 23. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 23 Type Guaranteed minimum range ³ bits char -127 to 127 or 0 to 255 8 signed char -127 to 127 8 unsigned char 0 to 255 8 int -32,768 to 32,767 16 signed int same as int 16 unsigned int 0 to 65,535 16 short int -32,768 to 32,767 16 signed short int same as short int 16 unsigned short int unsigned int 16 long int ±2,147,483,647 32 signed long int same as long int 32 unsigned long int 0 to 4,294,967,295 32 float 6 decimal digits of precision 32 double 10 decimal digits of precision 64 long double 10 decimal digits of precision 64 bool (C++ only) true/false 1 (8) Basic Data Types and Ranges in C
  • 25. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 25 Arrays Homogeneous collection of data elements (all have the same type). Individual elements are accessed by an index. int a[3], i, j, k; // a is not initialized int ma[2][3] = {{4, 2, 3}, {7, 8, 9}}; int b[4] = {2, 3, 9, 4}; // b is initialized a[0] = 2; // index starts from 0 a[1] = 3; a[2] = 9; i = sizeof a; // # of bytes used by a is 12 j = sizeof b; // # of bytes used by b is 16 k = sizeof ma; // # of bytes used by ma is 24
  • 26. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 26 Arrays and Strings A string is an array of characters. There is not a class String (like in Java) or a primitive data type for strings In other words, an array of characters can be seen as: • An array of characters • An string
  • 27. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 27 • There are two ways of initializing an array of characters in declaration: char s1[] ={'a', 'l', 'p', 'h', 'a'}; // as an array of char char s2[] ="alpha"; // as a string • These two initializations have different results in memory: • where '0' is the null terminator (null character), marking the end of a string. It is automatically added to the end of a string. a l p h as1 size = 5 a l p h a 0s2 size = 6 Array and String
  • 28. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 28 • To have the same result, we could do: // char s1[] = "alpha"; char s1[]={'a', 'l', 'p', 'h', 'a', '0'}; • If the size of the array is specified and there is not enough space, the null character will not be attached to the string. char s2[5] = "alpha"; char s3[4] = "alpha"; a l p h a 0s1 a l p h as2 size = 5 a l p hs3 size = 4 Array and String size = 5
  • 29. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 29 #include <stdio.h> main() { char s1[ ] = "hello"; printf("%s n", s1); for (int i = 0; i < 5; i++) printf("%c", s1[ i ]); printf("n"); return 0; } Arrays and Strings
  • 30. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 30 Array and Strings #include <stdio.h> #include <string.h> main() { int i; char s1[ ] = "hello", s2[] = "world"; for (i = 0; i < 5; i++) printf("%c", s1[ i ]); printf("n"); printf("s1 = %s, size = %dn", s1, sizeof s1); for (i = 0; i < 5; i++) printf("%c", s2[ i ]); printf("n"); } h e l l o 0 w o r l d 0
  • 32. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 32 Constants C allows two ways to define a constant: • const It is equivalent to final in Java. This way is slower since it have to read memory. const int i = 5; i = i + 2; // this line will cause a compilation error • #define It substitute values for constant definitions in compilation time. Provide a faster execution. #define PI 3.14 int x = PI * 5;
  • 33. CSE240 – Introduction to Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu Fall 2017 Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.