Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University."— Presentation transcript:

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 Department of Electrical&Electronics Engineering ozbek.nukhet@gmail.com

2 Topics Introduction to C language Introduction to C language Basic Components of C Basic Components of C

3 From Algorithms to Programs Both are sets of instructions on how to do a task Both are sets of instructions on how to do a task –Algorithm: talking to humans, easy to understand talking to humans, easy to understand in plain (English) language in plain (English) language –Program: talking to computer (compiler) talking to computer (compiler) can be regarded as a “formal expression” of an algorithm can be regarded as a “formal expression” of an algorithm

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

5 Programming Language Terms A computer program is a precise list of instructions, or statements, to solve a particular problem, specified in a programming language 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 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 It is useful to have some idea of what is going on “behind the scenes” in getting a program to execute

6 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 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 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: 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 Source file has to be translated into a form that the computer can execute

7 High-Level Language Compilers and linkers translate a high level program into executable machine code. Compilers and linkers translate a high level program into executable machine code. #include int main() { printf(“Hello”); return 0; } Source code Executable code 10100110 01110110 00100110 00000000 11111010 01001110 10100110 11100110 10010110 11001110 00101110 10100110 01001110 11111010 01100110 01001110 10000110 etc...

8 Why C? Structured language Structured language Standard library exists, allowing portability Standard library exists, allowing portability Wide availability on a variety of computers Wide availability on a variety of computers Low level activities possible Low level activities possible It can produce lean and efficient code It can produce lean and efficient code Widely used Widely used Has great influence on many other popular languages Has great influence on many other popular languages

9 History of C CPL Combined Programming Language (Barron et al., 1963) CPL Combined Programming Language (Barron et al., 1963) BCPL Basic CPL (Richards, 1969) BCPL Basic CPL (Richards, 1969) B (Thompson, 1970) B (Thompson, 1970) C K&R C (Ritchie, 1972) (for use in UNIX operating system) C K&R C (Ritchie, 1972) (for use in UNIX operating system) ANSI C American National Standards Institute C (X3J11, 1989) ANSI C American National Standards Institute C (X3J11, 1989) C9X (JTC1/SC22/WG14, ISO/IEC 9899) C9X (JTC1/SC22/WG14, ISO/IEC 9899)

10 C++? Derived from C Derived from C Object-oriented functionality with C-like syntax Object-oriented functionality with C-like syntax Nearly a superset of C Nearly a superset of C –-> C++ compilers can compile C code

11 Basic Structure of a C Program output “it’s too easy to learn C” Algorithm: #include #include int main() { printf(“it’s too easy to learn C”); return 0; } C Program: Example: it’s too easy to learn C”

12 Basic Structure of a C Program #include #include int main() { printf(“it‘s too easy to learn C”); return 0; } C Program: “Skeleton” Example: it’s too easy to learn C

13 Basic Structure of a C Program #include #include 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

14 Basic Structure of a C Program #include #include int main() { printf(“it‘s too easy to learn C”); return 0; } C Program: main(){ return 0; return 0;} Also: Assumes int Example: it’s too easy to learn C

15 Basic Structure of a C Program C Program: main(){ return 0; return 0;} Also: Warning messages: “Return value expected” #include #include int main() { printf(“it‘s too easy to learn C”); return 0; } Example: it’s too easy to learn C

16 Basic Structure of a C Program #include #include 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

17 Basic Structure of a C Program #include #include int main() { printf(“it‘s too easy to learn C”); 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

18 Basic Structure of a C Program #include #include 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

19 Basic Structure of a C Program #include #include int main() { printf(“it’s too easy to learn C”); 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

20 C Language Elements in Miles-to-Kilometers Conversion Program

21 Preprocessor Directives Begin with # Begin with # C language defines only a small number of operations C language defines only a small number of operations C implementations contain collections of useful functions and symbols called libraries C implementations contain collections of useful functions and symbols called libraries C can be expanded with additional libraries, programmers can add their own libraries C can be expanded with additional libraries, programmers can add their own libraries

22 Preprocessor Directives #include directive gives a program access to a library (Read: hash-include) #include directive gives a program access to a library (Read: hash-include) –#include –#include #define directive instructs the preprocessor to replace each occurrence of statement used in #define with the supplied value #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 Use #define statements for data that changes rarely

23 Syntax for #include directive #include #include –#include –#include

24 Syntax for #define directive #define NAME value #define NAME value –#define MILES_PER_KM 0.62137 –#define PI 3.141493 –#define MAX_LENGTH 100

25 Comments Start with /* and end with */ Start with /* and end with */ Provide supplementary information making it easier to understand the program Provide supplementary information making it easier to understand the program Comments are ignored by the compiler Comments are ignored by the compiler

26 Function main Every C program must have a main function Every C program must have a main function Valid forms are Valid forms are –int main(void) –int main(void) –int main()

27 Function main Code betwen { and } is called function body Code betwen { and } is called function body –int main(void){ function body function body}

28 Reserved Words All appear in lowercase All appear in lowercase Have special meaning in C and cannot be used for other purposes Have special meaning in C and cannot be used for other purposes

29 ANSI C Reserved Words auto auto break break case case char char const const continue continue default default do do double double else else enum enum extern extern float float for for goto goto if if int int long long register register return return short short signed signed sizeof sizeof static static struct struct switch switch typedef typedef union union unsigned unsigned void void volatile volatile while while

30 Standard Identifiers Identifiers from standard library Identifiers from standard library –like printf, scanf Can be redefined and used by the programmer 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 If you redefine a standard identifier, you cannot use it for its original purpose

31 User-Defined Identifiers Variable names, function names, etc Variable names, function names, etc –MILES_PER_KM –miles, kms

32 Rules for User-Defined Identifiers 1. Must consist only of letters, digits, and underscores letter_1-> VALIDletter_1-> VALID inches-> VALIDinches-> VALID Hello-> VALIDHello-> VALID TWO*FOUR-> INVALIDTWO*FOUR-> INVALID joe’s-> INVALIDjoe’s-> INVALID

33 Rules for User-Defined Identifiers 2. Can not begin with a digit 1letter-> INVALID1letter-> INVALID 3. A C reserved word cannot be used as an identifier double -> INVALIDdouble -> INVALID 4. An identifier defined in a C standard library should not be redefined RECOMMENDATIONRECOMMENDATION

34 Uppercase and Lowercase Letters Identifiers in C are case-sensitive Identifiers in C are case-sensitive –Rate, rate and RATE are different identifiers Use your own style, but as a recommendation 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 Miles or miles

35 Choosing Identifier Names A program that “looks good” is easier to read and understand 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 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

36 Choosing Identifier Names It is a good practice to choose meaningful names for user-defined identifiers 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 If an identifier consists two or more words use an underscore between words –Use dollars_per_hour instead of dollarsperhour

37 Choosing Identifier Names Choose identifiers long enough to convey the meaning, but avoid excessively long names because of possible typing errors 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 Avoid using similar names for different identifiers that may cause confusion –For example, do not use LARGE and large, xcoord and x_coord together


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

Similar presentations


Ads by Google