Introduction to C - Yola · Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr....

Preview:

Citation preview

Introduction to Computing

Lecture 01: Introduction to C

Assist.Prof.Dr. Nükhet ÖZBEK

Ege University

Department of Electrical&Electronics Engineering

ozbek.nukhet@gmail.com

Topics

• Introduction to C language

• Basic Components of C

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

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

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

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

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...

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

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)

C++?

• Derived from C

• Object-oriented functionality with C-like syntax

• Nearly a superset of C

– -> C++ compilers can compile C code

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”

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

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

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

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

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

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

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

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

C Language Elements in Miles-to-Kilometers Conversion Program

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

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

Syntax for #include directive

• #include <standard header file>

– #include <stdio.h>

– #include <math.h>

Syntax for #define directive

• #define NAME value

– #define MILES_PER_KM 0.62137

– #define PI 3.141493

– #define MAX_LENGTH 100

Comments

• Start with /* and end with */

• Provide supplementary information making it easier to understand the program

• Comments are ignored by the compiler

Function main

• Every C program must have a main function

• Valid forms are

– int

main(void)

– int main(void)

– int main()

Function main

• Code betwen { and } is called function body

– int

main(void)

{

function body

}

Reserved Words

• All appear in lowercase

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

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

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

User-Defined Identifiers

• Variable names, function names, etc

– MILES_PER_KM

– miles, kms

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

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

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

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

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

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

Recommended