Download presentation
Presentation is loading. Please wait.
Published byJoel Tyler Modified over 8 years ago
1
Choice of Programming Language 1 All the information is stored in the memory must be in the form of binary. As such any program to be executed by the CPU must be in the form of 0 and 1.
2
High Level Languages 2 High-Level Programming Languages C Fortran Pascal Turing Example: TO ADD 5 TO 6 … sum = 5 + 6; … These programming languages are easy to read/write since the instructions are English like sentences. we still need to compile & to link our program. Note:
3
Solving Problems 3 Here are the steps required to solve a problem with a computer program: 1. Define the problem 2. Analyse the problem. 3. Design a solution. 4. Implement the solution. 5. Test the program. 6. Update and maintain the program.
4
Why teach C? 4 C is small (only 32 keywords). C is common (lots of C code about). C is stable (the language doesn’t change much). C is quick running. C is the basis for many other languages (Java, C++, awk, Perl). It may not feel like it but C is one of the easiest languages to learn.
5
C : Introduction 5 The language is called “C” because its direct ancestor was called “B”. C was created around 1972 by Kernighan and Ritchie.
6
Programming Facilities 6 Generally there needs to be a number software applications that facilitate production of a program including: Editors, Complier, Library, Linker Programming Environment or Integrated Development Environment (IDE) An environment that has all the different programming tools needed for a particular programming language
7
‘ C’ Environment 7 Collection of ‘C’ programming tools o Borland ‘C++’, o Quincy All environments are operating under some OS. Above mentioned IDEs are available under the WINDOWS.
8
Some programmer jargon 8 Some words that will be used a lot: Source code: The stuff you type into the computer. The program you are writing. Compile (build): Taking source code and making a program that the computer can understand. Executable: The compiled program that the computer can run. Library: Added functions for C programming which are bolted on to do certain tasks. Header file: Files ending in.h which are included at the start of source code.
9
Data Types in ‘C’ Major data types o Numbers:any numerical value. o Characters:any item from set of characters used by C. o Strings:a combination of characters. o Void:any expression that does not have any value 9
10
Numbers in ‘C’ Two general categories: Integers Unsigned – all positive integers Signed – positive and negative integers Floats 10
11
Constants in C There are 3 basic types of constants in C. An integer constant is an integer-valued number. We will concern here solely with decimal constants like 0, 1, 743, 5280, 32767 or -764. A floating point constant is a base-10 number than contains either a decimal point or an exponent or both like 0., 1., 0.2, 50.0, 12.3,-12.667, 2E-8 or 0.006e-3. A character constant is a single character enclosed in apostrophes like 'a', 'x', '9', or '?'.
12
Preprocessor directives Preprocessor: A system program that modifies the C program prior to compilation. Preprocessor directive: An instruction to the preprocessor. Begins with #. Library: A collection of functions, symbols and values. 2 kinds of preprocessor directives: includes and defines.
13
Preprocessor directives #include : stdio.h, which stands for "standard input/output header", is the header in the C standard library that contains macro definitions, constants, and declarations of functions and types used for various standard input and output operations. The #include directive must be included on top of every C program. #define PI 3.1416 : this is a constant macro definition. It associates a name to a value for the duration of the program In this case it associates the symbol PI to the value 3.1416. It is an optional directive.
14
Comments Comments are lines of code that are ignored by the compiler. They are placed for the programmer's benefit. /* this is a comment */ /* this is another comment, it can be spread over multiple lines */
15
Instructions Instructions in C are terminated by a semi-colon (;) Line changes and tabs are not important to the C compiler. a=3; b=4; c=5; are the same as a=3; b=4; c=5;
16
Skeleton of a program #include /* optional additional includes */ /* optional constant macros */ int main (void) { /* optional declarative statements */ /* one or more executable statements */ return (0); }
17
/* PROGRAM # 1 */ #include /* This is my first C program. */ /* It’ll print a message to the display. */ int main (void ){ printf(“Welcome to the C programming language course!\n”); printf("This is our very first C program.\n"); printf(“We wish you a very pleasant experience with C.”); return (0); } 17
18
1st line:Welcome to the C programming language course! 2nd line:This is our very first C program. 3rd line:We wish you a very pleasant experience with C. 18
19
Skeleton of a program #include /* optional additional includes */ /* optional constant macros */ int main (void) { /* optional declarative statements */ /* one or more executable statements */ return (0); }
20
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change values. All variables must be declared at the top of the program. There are three basic types of variables in C: int: for integer (whole numbers). double (or float): for real (floating point numbers). char: for characters.
21
Declaring Variables in ‘C’ 21 All variables in ‘C’ must be declared. Compiler should know 1. The variable name 2. Type of the variable Why? In order to allocate enough memory to it, before the variable can be used. What are data types? Char int, long float, double, long double void
22
How to declare? 22 /*declare variable of type character.*/ This Denotes a comment in C chara_character; charletter; /*declare variable of type integer.*/ intan_integer; intnumber; /*declare variable of type float.*/ floatfloating_point_number; floataverage; The following is also valid: intage, number, mark;
23
Where to Declare? Declaration MUST happen at the top of the program, that is, the very first thing that we do, MUST be declaring the variables. 23
24
Identifiers All variables must have names. There are strict rules for variable names. These rules will apply to function names later so we will call these names identifiers. A declaration is done with the type followed by the identifier ;. Ex: int lifespan; double mass; char letter;
25
Hard rules for identifiers Rule #1: An identifier must not be a reserved word. Reserved words are used by C exclusively. Here are a few: double, char, int, do, float, if, return, sizeof, void,while, typedef, struct, switch, for, else. See the complete list in the Documents section of the course website. Rule #2: An identifier must contain only letters, digits or underscores. Abc8 is valid, Abc-8 is not. _xyz is valid, atom number is not.
26
Hard rules for identifiers Rule #3: An identifier must never begin with a digit. U238 and _765 are valid, 7abc and 67_q are not.
27
Soft rules for identifiers Rule #4: An identifier should not be a standard identifier. A standard identifier is a name used by C but is not a reserved word. printf, scanf are examples of standard identifiers. Rule #5: All-capital names should be used only for constant macros. Variables and function should never use capital letters. Never mix upper- case and lower-case letters in a name. 27
28
How to select names for variables, functions, etc. in ‘C’? o Use the set of rules for picking up VALID names in C o Use meaningful and descriptive names so we need less comments. Rules For Picking up Names 1. Names in ‘C’ are Case Sensitive, o i.e., ‘C’ makes distinction between upper and lower-case letters. o e.g., int number; int Number; /* number and Number are two different integer variables.*/ 2. User defined names cannot be the same as C keywords. o i.e., names such as for, while, if are NOT valid user defined names. o e.g., int for; /* invalid variable name. for is C keyword. */ 28
29
Rules For Picking up Names 3. Every name must start with o A letter of alphabet (upper, lower) o Underscore ( _ ) o e.g., int age; float Salary; double _code; 4. The remainder of the name may be o Upper or lower case letters of alphabets o Decimal digits (0-9) o Underscore 29
30
A collection of keywords, variables, operators, expressions, statements, different data types, etc. that together performs one or more than one task. Therefore, we need to know the followings: o C Keywords o if o for o while o C Different data types o integer int o floating Point float o character char o void void 30
31
o Valid C Variables o int month; o float average; o double PI; o char name; o C Operators o Arithmetic o Assignment and Compound Assignment o Sizeof o Relational o Logical o Conditional o Increment/Decrement o C Statements o C Expressions, A valid sequence of operands and operators to calculate a value, usually expressing a math, logical calculation, … 31
32
‘C’ Keywords Terms with pre-defined meaning in ‘C’. We must use the keywords within their intended meanings. Keyword definition cannot be changed. 32
33
caseused as part of switch sizeofgives the size of the variable in byte forused for looping purposes doused as part of do-while chara data type double a data type long a data type return used to return a value from a function staticused in combination with data types voida data type inta data type float a data type ifused for conditional purposes elseused a part of if switchused for selection purposes structused to define a data structure enumused to define user defined data type whileused for looping purposes 33 Example:
34
Numbers in C in a table Data TypePurposeBytesRange intinteger2-32,768 to +32,767 longlong integer4-2,147,483,648 to +2,147,483,647 unsignedunsigned integer20 to 65,535 unsigned long unsigned long integer 40 to 4,294,967,295 floatfloating point43.4E+/-38 doubledouble float81.7E+/-308 long doublelong double float161.7E+/-4932 34
35
Escape Sequences SequencePurpose \nNew line \tTab \"To print a double quote \\To print a backslash Are used to format the print out. (\) symbol is referred to as the escape character AND is used to signify a escape sequence.
36
‘C’ Operators o Can be used to perform specific types of operation on the variable(s) o Can be arithmetic, logic, relational, etc. Arithmetic Operators Indicate arithmetic operations in C Such as, addition, subtraction, multiplication, division, etc. 36
37
Arithmetic Operators in C OperatorDenoteExampleWhat it does +Addition2+8Add numbers & return the result 2+8 returns 10 -Subtraction12-8Subtract numbers & return the result 12-8 returns 4 *Multiplication5*8Multiply numbers & return the result 5*8 returns 40 /Division40/5Divide numbers & return the result 40/5 returns 8 %Remainder (modulo) 7%3Divide numbers & return the remainder 7%3 Returns 1 37 NOTE: Unary operation is an operation that ONLY requires one operand, for example, -2.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.