Presentation is loading. Please wait.

Presentation is loading. Please wait.

PART 1 : PROGRAMMING WITH C

Similar presentations


Presentation on theme: "PART 1 : PROGRAMMING WITH C"— Presentation transcript:

1 PART 1 : PROGRAMMING WITH C
WEEK 1 PART 1 : PROGRAMMING WITH C

2 INTRODUCTION TO COMPUTERS AND PROGRAMMING

3 Computers A computer is a tool to assist engineers to solve his/her problems. They comes in many types.

4 PC Block Diagram

5

6 Computer to control robots

7 Automation

8 Simulation Automotive Aviation Industry Process

9 Strong in Mathematics because behind it is MATHEMATICS and lots of it

10 Programming In order for us to use the computer, we need to convert the problems into a mathematical problem. The mathematical problem is converted into a form that a computer would understand. To be able to do that, we need to learn programming.

11 Programming Languages
Wikipedia ( ges_by_type) has listed a good classification of types of programming languages. C is considerend a compiled language. MATLAB is scripting language. Others SQL, Perl, Python, PHP

12 Compiler A compiler functions to convert the programming language from human readable to an executable form so the computer would know how to execute and run. C needs a compiler before we can run it. There are many C compilers in the market, free versions or commercial versions. The free version for Windows – cygwin.org From Microsoft website Many others. Before we start with C, you need to have a C compiler installed.

13 C Programming Language

14 Why C? C is a flexible, high-level, structured programming language.
C includes certain low-level features that are normally available only in assembly or machine language. Programs written in C compile into small programs that execute efficiently. C is widely available. C is largely machine-independent.

15 Computer Characteristics
Electronic devices that can transmit, store and manipulate information (i.e. data). Several different types of data can be processed by a computer. These include numerical data, character data, graphic data, sound and others. Scientific deal largely with numerical data. To process a particular data set, a computer must be given a set of instruction called a program. These instruction is loaded into the computer and stored in the memory.

16 Memory Every piece of information within the computer’s memory is encoded at some unique combination of zeroes and ones ( ). These zeros and ones are called bits (binary digits). Usually, memories are grouped into 8-bit multiples called bytes.

17 Bytes multiple 8 bits = 1 byte 1 kilobytes = 210 bytes = 1024 bytes
1 megabytes = 210 x 210 bytes = 1024 kbytes and so on.... Large computers have memories that are organised into words rather than bytes. Each word consists of a relatively large number of bits – typically 32 or 64 bit.

18 Introduction to C C is a general purpose, structured programming language. Uses English keywords to resemble algebraic expressions. It can be use to write very concise source programs, due in part to the large number of operators included within the language. It has extensive library functions. C compilers are commonly available for computers of all sizes. C interpreters are becoming common too.

19 History of C It was developed in the 1970’s by Dennis Ritchie at Bell Telephone Laboratories. In 1978, Ritche and Brian Kernighan published a definitive description of the language commonly known as “K&R C”. American National Standards Institude (ANSI) standardized the definition of C language. Most C compilers are adhering to this standard.

20 Structure of C Every C program consists of one or more funtion. The first function must be called main. Each function must contain A function heading, which consists of the function name, followed by an optional list of arguments enclosed in parentheses. A list of argument declarations, if arguments are included in the heading. A compound statement, which comprises the remainder of the function.

21 Example of a C program save as test.c and to compile,
/* calculation of area of a circle */ /*Comment*/ #include <stdio.h> /*Library file access */ main() /*Function heading*/ { float radius, area; /* Variable declarations*/ printf(“Radius = ? “); /* Output statement*/ scanf(“%f”, &radius); /*Input statement*/ area = *radius*radius; /*Assignment statement*/ printf(“Area = %f \n”, area); /*Output statement*/ system(“pause”); /*“Hold output screen”*/ } save as test.c and to compile, gcc test.c –o test to run, ./test

22 Example of C #include <stdio.h> int main() {
printf(“Braces come in pairs!”); printf(“Comments come in pairs!”); printf(“All statements end with a semicolon!”); printf(“Spaces are optional!”); printf(“Must have a main function!”); printf(“C is done mostly in lowercase.\ It’s a case-sensitive language.”); return(0); }

23 C Fundamentals Valid characters are: • Lowercase a to z, uppercase A to Z, digits 0 to 9, • special characters + - * / = % & # ! ? ^ “ ‘ ~ \ | < > ( ) [ ] { } : ; . , - (blank space) • Most compilers and $ • Escape sequences (e.g. \b, \n, \t) represent a single character \b – backspace \n – newline \t – horizontal tab

24 Identifiers and Keywords
Identifiers are name given to various program elements, such as variables, functions and arrays. Keywords – have standard predefined meaning in C. They cannot be used as programmer-defined identifiers. They are used for their intended purpose. 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

25 Data Types C supports several data types. Data type Description
Typical Memory Requirements int integer quantity 2 bytes or 1 word char single character 1 byte float floating-point number 1 word (4 bytes) double double-precision float-point number 2 words (8 bytes)

26 Integer Types To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The expressions sizeof(type) yields the storage size of the object or type in bytes. Following is an example to get the size of int type on any machine:

27 Floating-point types

28 Constant C has four basic types of constant. integer constant
unsigned long floating-point constant character constant string constant For integer and floating-point constants (numeric-type) these rules apply: No commas and blank spaces within the constants Constant can be preceded by a minus (-) sign The value cannot exceed specified minimum and maximum bounds

29 Integer Constant Made up of integer-valued numbers; any combination of digits from 0 to 9. Example of invalid decimal integer constants: 12,400 comma is not allowed 30.5 decimal point not allowed blank space not allowed hyphen is illegal 0123 first digit cannot be 0

30 Floating-point constants
Base-10 number with either a decimal point and/or exponent. Valid constants: E-5 0.2e-4 1.5e e10 Invalid constants: 5 requires decimal point or exponent 1,234.0 comma is illegal 5e+10.4 exponent must be integer 4E 12 blank space not allowed

31 Character Constants A character constant is a single character, enclosed in apostrophes (single quotation marks). Example: ‘B’ ‘y’ ‘5’ ‘?’ ‘ ‘ Character constants have integer values – refer to ASCII character set. ‘A’ 65 ‘x’ 120 ‘3’ 51 ‘?’ 63 ‘ ‘ 32

32 ASCII Character Set

33 Escape sequences begin with a backslash (\) followed by one or more special characters. Example: \ is the null character – used to indicate end of string. Escape sequences are viewed as single characters.

34 String Constants Consist of any number of consecutive characters (including none), enclosed in (double) quotation marks. Example “green” “ ” “$1.99” “2*i/j” “ “ “ “ To display this string on the computer screen : To continue, press the “RETURN” key The command is printf(“\tTo continue, press the \”RETURN\” key\n”); with special escape sequences \t \” and \n a null character \0 is automatically placed at the end of every string constant which is not visible when the string is displayed. a character constant is not equivalent to a single-character string constant. (e.g. ‘A’ is nor equal to “A”).

35 Variables A variable is an identifier.
Used to represent some specified type of information within the designated part of the program. The simplest form of a variable represents a single data item. A data item must be assigned to the variable at some point in the program. A variable can be assigned different data items at various places within the program but the data type associated with the variable cannot change.

36 Example of variable int a,b,c; char d; a=3; b=4; c=a+b; d=“t”; : a=b*2; c=b*a; d=“y”;

37 Arrays An array is an identifier that refers to a collection of data items which all have the same name. The individual data items are represented by their corresponding array elements. The first data item is represented by the first array element. Example char arr[] = {'c','o','d','e','\0'};; int arr[10]; int i = 0; for(i=0;i<sizeof(arr);i++) /*first element of array start with 0 */ { arr[i] = i; // Initializing each element separately } ;

38 String Array

39 Declarations A declaration associates a group of variables with a specific data type. All variables must be declared before then can appear in statements. A declaration consists of a data type, followed by one or more variable names, ending with a semicolon. Eq. int a,b,c; char x,y,z[10]; float area, value2;

40 Declaration (2)

41 Declaration (3)

42 Declaration (4)

43 Declaration (5)

44 Declaration (6)

45 Expressions An expression represent a single data item, such as a number or a character. The expression may consist of a single entity, such as a constant, a variable, an array element or a reference to a function, a logical condition. In C, true or false are represented by the integer values of One and Zero.

46 Statements A statement causes the computer to carry out some actions.
There are THREE different classes of statement in C. expression statement; compound statement; and control statement.

47 Statement (1)

48 Statement (2)

49 Statement (3)

50 Symbolic constant A symbolic constant is a name substitutes for a character sequence. The value may represent a numeric constant, a character constant or a string constant. Thus, a symbolic constant allows a name to appear in place of a numeric, character or string constant. When a program is compiled, each occurence of a symbolic constant is replaced by its corresponding character sequence. A symbolic constant is defined by writing #define name text where name is the symbolic name (typically uppercase) and text is the character sequence

51 Symbolic constant (2)

52 Symbolic constant (3)

53 Symbolic constant (4)

54 Problems (1)

55 Problem (2)

56 Operators and Expressions

57 Arithmetic Operators There are five arithmetic operators in C. They are function pow() is used for exponentiation.

58 Arithmetic (2)

59 Arithmetic (3)

60 Arithmetic (4)

61 Arithmetic (5)


Download ppt "PART 1 : PROGRAMMING WITH C"

Similar presentations


Ads by Google