37
Introduction to Computing Introduction to Computing Lecture 0 Lecture 0 1 1 : : Introduction to Introduction to C C Assist.Prof. Assist.Prof. Dr. Dr. Nükhet ÖZBEK Nükhet ÖZBEK Ege Ege University University Department of Department of Electrical&Electronics Electrical&Electronics Engineering Engineering ozbek.nukhet ozbek.nukhet @ @ gmail.com gmail.com

Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Embed Size (px)

Citation preview

Page 1: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Introduction to Computing Introduction to Computing Lecture 0Lecture 011::

Introduction to Introduction to CC

Assist.Prof.Assist.Prof.Dr. Dr. Nükhet ÖZBEKNükhet ÖZBEKEgeEge University University

Department of Department of Electrical&ElectronicsElectrical&Electronics Engineering Engineeringozbek.nukhetozbek.nukhet@@gmail.comgmail.com

Page 2: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

TopicsTopics

• Introduction to C languageIntroduction to C language

• Basic Components of CBasic Components of C

Page 3: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

From Algorithms to From Algorithms to ProgramsPrograms

• Both are sets of instructions on how to Both are sets of instructions on how to do a taskdo a task– Algorithm: Algorithm:

•talking to humans, easy to understandtalking to humans, easy to understand

• in plain (English) languagein plain (English) language

– Program:Program:•talking to computer (compiler)talking to computer (compiler)

•can be regarded as a “formal expression” of can be regarded as a “formal expression” of an algorithman algorithm

Page 4: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

What is a program?What is a program?

• A program is a way of solving some A program is a way of solving some problem-a set directives (or problem-a set directives (or instructions) stating how to go about instructions) stating how to go about achieving to some desired resultsachieving to some desired results

• Thus program must provide a precise Thus program must provide a precise list of instructions stating what you want list of instructions stating what you want done, which someone or something done, which someone or something could follow in order to achieve the taskcould follow in order to achieve the task

Page 5: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Programming Language Programming Language TermsTerms• A computer program is a precise list of A computer program is a precise list of

instructions, or statements, to solve a instructions, or statements, to solve a particular problem, specified in a particular problem, specified in a programming languageprogramming language

• Effectively, the computer obey or executes Effectively, the computer obey or executes the statements in the program, creating the statements in the program, creating and manipulating objects, following the and manipulating objects, following the algorithm expressed in the programalgorithm expressed in the program

• It is useful to have some idea of what is It is useful to have some idea of what is going on “behind the scenes” in getting a going on “behind the scenes” in getting a program to executeprogram to execute

Page 6: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

How does a program get How does a program get executed?executed?• The programs you write are expressed in a The programs you write are expressed in a

programming language and stored in the computer programming language and stored in the computer as a sequence of printable characters in a file-called as a sequence of printable characters in a file-called the the source source filefile

• In this source form, the program cannot be directly In this source form, the program cannot be directly executed by the computer executed by the computer

• The “language” and instructions that the computer The “language” and instructions that the computer understands and obeys are very different: understands and obeys are very different:

* expressed as sequences of binary digits/bits (0’s * expressed as sequences of binary digits/bits (0’s and 1’s)and 1’s)

* very low-level and general purpose* very low-level and general purpose

• Source file has to be translated into a form that the Source file has to be translated into a form that the computer can executecomputer can execute

Page 7: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

High-Level LanguageHigh-Level Language

• CompilersCompilers and and linkers linkers translate a high level translate a high level program into executable machine code.program into executable machine code.

#include <stdio.h>

int main(){ printf(“Hello”);

return 0;}

Source code Executable code

10100110 0111011000100110 0000000011111010 1111101001001110 1010011011100110 1001011011001110 0010111010100110 0100111011111010 0110011001001110 10000110

etc...

Page 8: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Why C?Why C?

• Structured languageStructured language• Standard library exists, allowing Standard library exists, allowing

portabilityportability• Wide availability on a variety of Wide availability on a variety of

computerscomputers• Low level activities possibleLow level activities possible• It can produce lean and efficient codeIt can produce lean and efficient code• Widely usedWidely used• Has great influence on many other Has great influence on many other

popular languagespopular languages

Page 9: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

History of CHistory of C

• CPL CPL Combined Programming Language (Barron et al., Combined Programming Language (Barron et al., 1963)1963)

• BCPL BCPL Basic CPL (Richards, 1969)Basic CPL (Richards, 1969)

• B B (Thompson, 1970)(Thompson, 1970)

• C C K&R C (Ritchie, 1972) (for use in UNIX operating system)K&R C (Ritchie, 1972) (for use in UNIX operating system)

• ANSI C ANSI C American National Standards Institute C (X3J11, American National Standards Institute C (X3J11, 1989)1989)

• C9X C9X (JTC1/SC22/WG14, ISO/IEC 9899)(JTC1/SC22/WG14, ISO/IEC 9899)

Page 10: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

C++?C++?

• Derived from CDerived from C

• Object-oriented functionality with C-Object-oriented functionality with C-like syntaxlike syntax

• Nearly a superset of CNearly a superset of C– -> C++ compilers can compile C code-> C++ compilers can compile C code

Page 11: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Basic Structure of a C Basic Structure of a C ProgramProgram

output “it’s too easy to learn C”output “it’s too easy to learn C”

Algorithm: #include <stdio.h>#include <stdio.h> int main()int main(){{

printf(“it’s too easy to learn C”);printf(“it’s too easy to learn C”);

return 0;return 0;}}

C Program:

Example: it’s too easy to learn C”

Page 12: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Basic Structure of a C Basic Structure of a C ProgramProgram

#include <stdio.h>#include <stdio.h> int main()int main(){{

printf(“it‘s too easy to learn C”);printf(“it‘s too easy to learn C”);

return 0;return 0;}}

C Program:

“Skeleton”

Example: it’s too easy to learn C

Page 13: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Basic Structure of a C Basic Structure of a C ProgramProgram

#include <stdio.h>#include <stdio.h> int main()int main(){{

printf(“it‘s too easy to learn C”);printf(“it‘s too easy to learn C”);

return 0;return 0;}}

C Program:

void main()void main(){{

}}

Also:

Not recommended

Example: it’s too easy to learn C

Page 14: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Basic Structure of a C Basic Structure of a C ProgramProgram

#include <stdio.h>#include <stdio.h> int main()int main(){{

printf(“it‘s too easy to learn C”);printf(“it‘s too easy to learn C”);

return 0;return 0;}}

C Program:

main()main(){{

return 0;return 0;}}

Also:

Assumes int

Example: it’s too easy to learn C

Page 15: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Basic Structure of a C Basic Structure of a C ProgramProgram

C Program:

main()main(){{

return 0;return 0;}}

Also:

Warning messages: “Return value expected”

#include <stdio.h>#include <stdio.h> int main()int main(){{

printf(“it‘s too easy to learn C”);printf(“it‘s too easy to learn C”);

return 0;return 0;}}

Example: it’s too easy to learn C

Page 16: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Basic Structure of a C Basic Structure of a C ProgramProgram

#include <stdio.h>#include <stdio.h> int main()int main(){{ printf(“it‘s too easy to learn C”);printf(“it‘s too easy to learn C”);

return 0;return 0;}}

C Program:

Includes standard input / output library

of functions.

Read: “Hash-include”

Example: it’s too easy to learn C

Page 17: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Basic Structure of a C Basic Structure of a C ProgramProgram

#include <stdio.h>#include <stdio.h> int main()int main(){{ printf(“it‘s too easy to learn C”);printf(“it‘s too easy to learn C”);

return 0;return 0;}}

C Program:

Brackets mark the beginning and

end of a block of instructions.

Example: it’s too easy to learn C

Page 18: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Basic Structure of a C Basic Structure of a C ProgramProgram

#include <stdio.h>#include <stdio.h> int main()int main(){{

printf(“printf(“it’s too easy to learn Cit’s too easy to learn C”);”);

return 0;return 0;}}

C Program:

Instruction (function call) to output “it’s too easy to

learn C”

Example: it’s too easy to learn C

Page 19: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Basic Structure of a C Basic Structure of a C ProgramProgram

#include <stdio.h>#include <stdio.h> int main()int main(){{

printf(“it’s too easy to learprintf(“it’s too easy to learnn C C”)”);;

return 0return 0;;}}

C Program:

“Statements” (lines of instructions) end with a

semi-colon (;)

Example: it’s too easy to learn C

Page 20: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

C Language Elements in C Language Elements in Miles-to-Kilometers Conversion Miles-to-Kilometers Conversion ProgramProgram

Page 21: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Preprocessor DirectivesPreprocessor Directives

• Begin with #Begin with #• C language defines only a small C language defines only a small

number of operationsnumber of operations• C implementations contain C implementations contain

collections of useful functions and collections of useful functions and symbols called symbols called librarieslibraries

• C can be expanded with additional C can be expanded with additional libraries, programmers can add their libraries, programmers can add their own librariesown libraries

Page 22: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Preprocessor DirectivesPreprocessor Directives

• #include#include directive gives a program directive gives a program access to a library (Read: hash-access to a library (Read: hash-include)include)– #include <stdio.h>#include <stdio.h>

• #define#define directive instructs the directive instructs the preprocessor to replace each preprocessor to replace each occurrence of statement used in occurrence of statement used in #define with the supplied value#define with the supplied value

• Use #define statements for data that Use #define statements for data that changes rarelychanges rarely

Page 23: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Syntax for #include Syntax for #include directivedirective

• #include <standard header file>#include <standard header file>– #include <stdio.h>#include <stdio.h>– #include <math.h>#include <math.h>

Page 24: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Syntax for #Syntax for #definedefine directive directive

• ##definedefine NAME valueNAME value– ##define MILES_PER_KM 0.62137define MILES_PER_KM 0.62137– ##define PI 3.141493define PI 3.141493– #define MAX_LENGTH 100#define MAX_LENGTH 100

Page 25: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

CommentsComments

• Start with /* and end with */Start with /* and end with */

• Provide supplementary information Provide supplementary information making it easier to understand the making it easier to understand the programprogram

• Comments are ignored by the Comments are ignored by the compilercompiler

Page 26: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Function mainFunction main

• Every C program must have a main Every C program must have a main functionfunction

• Valid forms areValid forms are– intint

main(void)main(void)– int main(void)int main(void)– int main()int main()

Page 27: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Function mainFunction main

• Code betwen { and } is called Code betwen { and } is called function bodyfunction body– int int

main(void)main(void)

{{

function bodyfunction body

}}

Page 28: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Reserved WordsReserved Words

• All appear in lowercaseAll appear in lowercase

• Have special meaning in C and Have special meaning in C and cannot be used for other purposescannot be used for other purposes

Page 29: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

ANSI C Reserved WordsANSI C Reserved Words

• autoauto• breakbreak• casecase• charchar• constconst• continuecontinue• defaultdefault• dodo• doubledouble• elseelse• enumenum• externextern• floatfloat• forfor• gotogoto• ifif

• intint• longlong• registerregister• returnreturn• shortshort• signedsigned• sizeofsizeof• staticstatic• structstruct• switchswitch• typedeftypedef• unionunion• unsignedunsigned• voidvoid• volatilevolatile• whilewhile

Page 30: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Standard IdentifiersStandard Identifiers

• Identifiers from standard libraryIdentifiers from standard library– like printf, scanflike printf, scanf

• Can be redefined and used by the Can be redefined and used by the programmerprogrammer– BUT IT IS NOT RECOMMENDED!BUT IT IS NOT RECOMMENDED!

• If you redefine a standard identifier, If you redefine a standard identifier, you cannot use it for its original you cannot use it for its original purposepurpose

Page 31: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

User-Defined IdentifiersUser-Defined Identifiers

• Variable names, function names, etcVariable names, function names, etc– MILES_PER_KMMILES_PER_KM– miles,miles, kmskms

Page 32: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Rules for User-Defined Rules for User-Defined IdentifiersIdentifiers

1.1. Must consist only of letters, digits, Must consist only of letters, digits, and underscoresand underscores

• letter_1letter_1 -> VALID-> VALID• inchesinches -> VALID-> VALID• HelloHello -> VALID-> VALID• TWO*FOURTWO*FOUR -> INVALID-> INVALID• joe’sjoe’s -> INVALID-> INVALID

Page 33: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Rules for User-Defined Rules for User-Defined IdentifiersIdentifiers

2.2. Can not begin with a digitCan not begin with a digit• 1letter1letter -> INVALID-> INVALID

3.3. A C reserved word cannot be used A C reserved word cannot be used as an identifieras an identifier

• double double -> INVALID-> INVALID

4.4. An identifier defined in a C standard An identifier defined in a C standard library should not be redefinedlibrary should not be redefined

• RECOMMENDATIONRECOMMENDATION

Page 34: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Uppercase and Lowercase Uppercase and Lowercase LettersLetters

• Identifiers in C are case-sensitiveIdentifiers in C are case-sensitive– Rate, rate and RATE are Rate, rate and RATE are differentdifferent

identifiersidentifiers

• Use your own style, but as a Use your own style, but as a recommendationrecommendation– Use all uppercase letters in constant Use all uppercase letters in constant

definitions (#define statements)definitions (#define statements)– Use all lowercase or first letters uppercase, Use all lowercase or first letters uppercase,

other lower case for other identifiersother lower case for other identifiers•Miles or milesMiles or miles

Page 35: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Choosing Identifier NamesChoosing Identifier Names

• A program that “looks good” is easier to A program that “looks good” is easier to read and understandread and understand

• Most programs are examined by Most programs are examined by someone other than the original someone other than the original programmer and generally more time is programmer and generally more time is spent on the maintenance of the spent on the maintenance of the programs than time spent is writing the programs than time spent is writing the original programoriginal program– A neatly stated and clear program is easier A neatly stated and clear program is easier

to understandto understand

Page 36: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Choosing Identifier NamesChoosing Identifier Names

• It is a good practice to choose It is a good practice to choose meaningful names for user-defined meaningful names for user-defined identifiersidentifiers– Use salary instead of s or sal, etc.Use salary instead of s or sal, etc.

• If an identifier consists two or more If an identifier consists two or more words use awords use ann underscore between underscore between wordswords– Use dollars_per_hour instead of Use dollars_per_hour instead of

dollarsperhourdollarsperhour

Page 37: Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Choosing Identifier NamesChoosing Identifier Names

• Choose identifiers long enough to Choose identifiers long enough to convey the meaning, but avoid convey the meaning, but avoid excessively long nameexcessively long namess because of because of possible typing errorspossible typing errors

• Avoid using similar names for different Avoid using similar names for different identifiers that may cause confusionidentifiers that may cause confusion– For example, do not use LARGE and For example, do not use LARGE and

large, xcoord and x_coord togetherlarge, xcoord and x_coord together