Presentation is loading. Please wait.

Presentation is loading. Please wait.

Revision Lecture 1 - 5.

Similar presentations


Presentation on theme: "Revision Lecture 1 - 5."— Presentation transcript:

1 Revision Lecture 1 - 5

2 Terminology Term Example Uppercase letters A,B,……Z Lowercase letters
Digits 0,1,2,…...,9 Special characters , . : ; ! “ ^ # % ^ & * ( ) { } [ ] < > | \ / _ ~ etc. Space characters Keyword is a reserved word that has a particular meaning in the programming language. The meaning of a keyword is predefined.

3 Summary A computer program is just a collection of the instructions necessary to solve a specific problem. The approach or method that is used to solve the problem is known as an algorithm. Computing machine (Computer): “a machine that stores and manipulates information under the control of a changeable program that is stored in its memory.” C is a general-purpose, block-structured, procedural, case-sensitive, free-flow, portable.

4 Summary Translation of assembly language into machine language: in the beginning done manually, later done by a special computer program – the assembler Translation of high level language into machine instructions: done by special computer programs – compilers or interpreters Operating system: a program that controls the entire operation of a computer system. Syntax vs. Semantics errors.

5 Variables & Data Types

6 Rules for valid Variable names
Name must begin with a letter (Uppercase or lowercase) or underscore ( _ ) The first character of an identifier name cannot be a digit. Student student _student STUDENT Student_45 student8 12student

7 Rules for valid Variable names
Keywords or reserved words cannot form a valid variable name. Examples: int float char for return if

8 Rules for valid Variable names
No special character (except underscore) can be used in variable name. Student&id A#

9 Rules for valid Variable names
The C programming language is case sensitive, meaning that it distinguishes between uppercase and lowercase letters. For example: each one of them represent different variable. VAR Var var VAr vAr vaR

10 How to write (declare) a variable in the program?

11 Variables of the same type can be declared in the same line
Declaring variables Type Variable name Data_Type Variable_Name Examples: int x ; int a , b , c; int a; int b; int c; Variables of the same type can be declared in the same line

12 Initialize Variables (Assign values to Variables)
int a; a = 10; a variable can be initialized together with its declaration int a = 10; initialize more than one variable of the same type together with its declaration. int a , b; a = 10; b = 20; int a = 10, b = 20;

13 Initialize Variables (Assign values to Variables)
You can also write: What is the value of a? a = 12 int a = ; What is the value of b? b = 15 int b = a + 3;

14 What is Data Type? Data type specifies the range of values that may be stored in a variable of that type. Basic Data Types: int used to store integer numbers float used for storing floating-point numbers double the same as type float, only with roughly twice the precision. char used to store a single character bool used to store just the values 0 or 1 (used for indicating a true/false situation)

15 Remember to use ‘ ‘ Single Qutation.
Data Types Examples int X = 2; float X = 2.5; double X = 2.5; Remember to use ‘ ‘ Single Qutation. char X = ‘2’; or X = ‘G’ or X = ‘&’

16 Important ! The value assigned to a variable should match the variable type. For example, the statement int a = 10.9; actually sets the value of a to 10 since a has been declared as int and not float. (The decimal part is completely ignored, and the assigned value is not rounded to 11.) However, the value of a float variable can be an integer. For example, you could write float a = 50; since that’s equivalent to float a = 50.0;

17 Arithmetic Operators

18 Operator Name of operator Precedence Associativity * / % Multiplication Division Modulus Level I L > R (Left to Right) + _ Addition Subtraction Level II use parentheses ( ) in an expression to force the terms to be evaluated in any desired order.

19 Examples

20 The assignment operators
Example: count += 10; Equivalent with: count=count+10; Example: precedence of op=: a /= b + c a = a / (b + c) addition is performed first because the addition operator has higher precedence than the assignment operator

21 Structure of C Program Display variables , multiple

22 Comment Statement is a preprocessor directive statement, which includes standard input/output function e.g. printf() and scanf() Function main()

23 Comment Statements Comments Multiple Lines Single Line
Comment statements are used in a program to document it and to enhance its readability. Comments Single Line Multiple Lines

24 Output & Input Display variables , multiple

25 Output – printf() Use printf() function to display messages and value of variables. Examples: C:\Windows\System32\cmd.exe A B C

26 Output - printf() You can write the example using only one printf(); A
C:\Windows\System32\cmd.exe A B C New line is represented by \n (Escape sequence)

27 Output- printf() Display value of variables Type Format Specifier int
%i or %d float %f double %f or %g char %c Display value of variables C:\Windows\System32\cmd.exe A = 2 B = C = D = ?

28 Output-putchar() The putchar function is for writing data to the terminal by writing a single character at a time. C:\Windows\System32\cmd.exe 1M

29 How to input (read) values?

30 Input- scanf()  scanf() function used to get input from a user, store that input in variables, and use it in your programs we’ll mainly use scanf() to read numeric data and store that data in numeric variables. In the following example, we use scanf() to read an integer and store it in variable x: NOTE: In Visual Studio use function scanf_s(); The & character that usually precedes the name of a variable is called the address operator, and it indicates the memory location in which the input number will be stored.

31 Input- scanf() In the following example, we use scanf() to read an integer and a float number from the keyboard and store them in variables x and y, respectively.

32 Input- getchar() The getchar function used when you wanted to read data from a single character at a time Example:

33 Simple input&output Program
Write a program to read two numbers from the user and find their Sum. Alternatively, you can use variable sum See next slide

34 Simple input&output Program


Download ppt "Revision Lecture 1 - 5."

Similar presentations


Ads by Google