37
Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering [email protected]

Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Introduction to Computing

Lecture 01: Introduction to C

Assist.Prof.Dr. Nükhet ÖZBEK

Ege University

Department of Electrical&Electronics Engineering

[email protected]

Page 2: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Topics

• Introduction to C language

• Basic Components of C

Page 3: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

From Algorithms to Programs

• Both are sets of instructions on how to do a task

– Algorithm:

• talking to humans, easy to understand

• in plain (English) language

– Program:

• talking to computer (compiler)

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

Page 4: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

What is a program?

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

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

Page 5: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Programming Language Terms

• A computer program is a precise list of instructions, or statements, to solve a particular problem, specified in a programming language

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

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

Page 6: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

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

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

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

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

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

* very low-level and general purpose

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

Page 7: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

High-Level Language

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

#include <stdio.h>

int main()

{

printf(“Hello”);

return 0;

}

Source code Executable code

10100110 01110110

00100110 00000000

11111010 11111010

01001110 10100110

11100110 10010110

11001110 00101110

10100110 01001110

11111010 01100110

01001110 10000110

etc...

Page 8: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Why C?

• Structured language

• Standard library exists, allowing portability

• Wide availability on a variety of computers

• Low level activities possible

• It can produce lean and efficient code

• Widely used

• Has great influence on many other popular languages

Page 9: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

History of C

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

• BCPL Basic CPL (Richards, 1969)

• B (Thompson, 1970)

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

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

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

Page 10: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

C++?

• Derived from C

• Object-oriented functionality with C-like syntax

• Nearly a superset of C

– -> C++ compilers can compile C code

Page 11: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Basic Structure of a C Program

output “it’s too easy to learn C”

Algorithm: #include <stdio.h>

int main()

{

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

return 0;

}

C Program:

Example: it’s too easy to learn C”

Page 12: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Basic Structure of a C Program

#include <stdio.h>

int main()

{

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

return 0;

}

C Program:

“Skeleton”

Example: it’s too easy to learn C

Page 13: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Basic Structure of a C Program

#include <stdio.h>

int main()

{

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

return 0;

}

C Program:

void main()

{

}

Also:

Not recommended

Example: it’s too easy to learn C

Page 14: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Basic Structure of a C Program

#include <stdio.h>

int main()

{

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

return 0;

}

C Program:

main()

{

return 0;

}

Also:

Assumes int

Example: it’s too easy to learn C

Page 15: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Basic Structure of a C Program

C Program:

main()

{

return 0;

}

Also:

Warning messages:

“Return value expected”

#include <stdio.h>

int main()

{

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

return 0;

}

Example: it’s too easy to learn C

Page 16: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Basic Structure of a C Program

#include <stdio.h>

int main()

{

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

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 C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Basic Structure of a C Program

#include <stdio.h>

int main()

{

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

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 C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Basic Structure of a C Program

#include <stdio.h>

int main()

{

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

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 C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Basic Structure of a C Program

#include <stdio.h>

int main()

{

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

return 0;

}

C Program:

“Statements” (lines of

instructions) end with a semi-colon (;)

Example: it’s too easy to learn C

Page 20: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

C Language Elements in Miles-to-Kilometers Conversion Program

Page 21: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Preprocessor Directives

• Begin with #

• C language defines only a small number of operations

• C implementations contain collections of useful functions and symbols called libraries

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

Page 22: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Preprocessor Directives

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

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

• Use #define statements for data that changes rarely

Page 23: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Syntax for #include directive

• #include <standard header file>

– #include <stdio.h>

– #include <math.h>

Page 24: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Syntax for #define directive

• #define NAME value

– #define MILES_PER_KM 0.62137

– #define PI 3.141493

– #define MAX_LENGTH 100

Page 25: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Comments

• Start with /* and end with */

• Provide supplementary information making it easier to understand the program

• Comments are ignored by the compiler

Page 26: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Function main

• Every C program must have a main function

• Valid forms are

– int

main(void)

– int main(void)

– int main()

Page 27: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Function main

• Code betwen { and } is called function body

– int

main(void)

{

function body

}

Page 28: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Reserved Words

• All appear in lowercase

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

Page 29: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

ANSI C Reserved Words

• auto • break • case • char • const • continue • default • do • double • else • enum • extern • float • for • goto • if

• int • long • register • return • short • signed • sizeof • static • struct • switch • typedef • union • unsigned • void • volatile • while

Page 30: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Standard Identifiers

• Identifiers from standard library

– like printf, scanf

• Can be redefined and used by the programmer

– BUT IT IS NOT RECOMMENDED!

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

Page 31: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

User-Defined Identifiers

• Variable names, function names, etc

– MILES_PER_KM

– miles, kms

Page 32: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Rules for User-Defined Identifiers

1. Must consist only of letters, digits, and underscores

• letter_1 -> VALID

• inches -> VALID

• Hello -> VALID

• TWO*FOUR -> INVALID

• joe’s -> INVALID

Page 33: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Rules for User-Defined Identifiers

2. Can not begin with a digit • 1letter -> INVALID

3. A C reserved word cannot be used as an identifier

• double -> INVALID

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

• RECOMMENDATION

Page 34: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Uppercase and Lowercase Letters

• Identifiers in C are case-sensitive – Rate, rate and RATE are different identifiers

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

definitions (#define statements)

– Use all lowercase or first letters uppercase, other lower case for other identifiers • Miles or miles

Page 35: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Choosing Identifier Names

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

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

understand

Page 36: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Choosing Identifier Names

• It is a good practice to choose meaningful names for user-defined identifiers

– Use salary instead of s or sal, etc.

• If an identifier consists two or more words use an underscore between words

– Use dollars_per_hour instead of dollarsperhour

Page 37: Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering

Choosing Identifier Names

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

• Avoid using similar names for different identifiers that may cause confusion

– For example, do not use LARGE and large, xcoord and x_coord together