Full Transcript

CEUC101: COMPUTER CONCEPTS & PROGRAMMING UNIT – 1 Basics of programming terminologies Chapter – 1 : Basics of programming terminologies Objectives To be able to create and use variables and constants. To be able to distinguish the constants of different data...

CEUC101: COMPUTER CONCEPTS & PROGRAMMING UNIT – 1 Basics of programming terminologies Chapter – 1 : Basics of programming terminologies Objectives To be able to create and use variables and constants. To be able to distinguish the constants of different data type. To be able to name, declare, initialize and assign values to variables. To become familiar with fundamental data types. To be able to list, describe, and use the C basic data types. Chapter – 1 : Basics of programming terminologies Objectives To learn arithmetic, string, relational, logical, bit wise, unary, binary, ternary operators To learn about implicit/explicit conversions between values of different types To learn how to build complicated expressions from operators and functions To learn how operator precedence/associativity control order of evaluation Chapter Chapter – 5 : –Constants, 3 : Operators Variables and Expression & Data Types in ‘C’in ‘C’ programming terminologies In this chapter, we will discuss Software & Hardware Compiler & Interpreter Algorithm & Flowchart Character set C Tokens Constants (integer, real, character, string, enum), symbolic constants Variables (name, declare, assign) Fundamental data type ( int, float, double, char, void) Chapter – 1 : Basics of programming terminologies Software A set of instruction in a logical order to perform a meaningful task is called program and a set of program is called software. It tell the hardware how to perform a task. Types of software ▪ System software ▪ It is designed to operate the computer hardware efficiently. ▪ Provides and maintains a platform for running application software. Examples: Windows, Linux, Unix etc. Chapter – 1 : Basics of programming terminologies Software Application software It is designed to help the user to perform general task such as word processing, web browser etc. Examples: Microsoft Word, Excel, PowerPoint etc. Chapter – 1 : Basics of programming terminologies Categories of System Software Operating system ▪ It controls hardware as well as interacts with users, and provides different services to user. ▪ It is a bridge between computer hardware and user. ▪ Examples: Windows XP, Linux, UNIX, etc… System support software ▪ It makes working of hardware more efficiently. ▪ For example drivers of the I/O devices or routine for socket programming, etc… System development software ▪ It provides programming development environment to programmers. ▪ Example: Editor, pre-processor, compiler, interpreter, loader, etc… Chapter – 1 : Basics of programming terminologies Categories of Application Software General purpose software ▪ It is used widely by many people for some common task, like word processing, web browser, excel, etc… ▪ It is designed on vast concept so many people can use it. Special purpose software ▪ It is used by limited people for some specific task like accounting software, tax calculation ▪ software, ticket booking software, banking software etc… ▪ It is designed as per user’s special requirement. Chapter – 1 : Basics of programming terminologies Compiler, Interpreter and Assembler Compiler translates program of higher level language to machine language. It converts whole program at a time. Interpreter translates program of higher level language to machine language. It converts program line by line. Assembler translates program of assembly language to machine language. Chapter – 1 : Basics of programming terminologies Types of Computer Languages Machine level language OR Low level language ▪ It is language of 0’s and 1’s. ▪ Computer directly understand this language. Assembly language ▪ It uses short descriptive words (MNEMONIC) to represent each of the machine language instructions. ▪ It requires a translator knows as assembler to convert assembly language into machine language ▪ so that it can be understood by the computer. ▪ Examples: 8085 Instruction set. Chapter – 1 : Basics of programming terminologies Types of Computer Languages Higher level language ▪ It is a machine independent language. ▪ We can write programs in English like manner and therefore easier to learn and use. ▪ Examples: C, C++, JAVA etc… Assembly language Chapter – 1 : Basics of programming terminologies Problem solving methods.. Flowchart Algorithm Flowchart is a pictorial or Algorithm is a finite sequence graphical representation of a of well defined steps for program. solving a problem. It is drawn using various It is written in the natural symbols. language like English. Easy to understand. Difficult to understand. Easy to show branching and Difficult to show branching and looping. looping. Flowchart for big problem is Algorithm can be written for impractical. any problem. Chapter – 1 : Basics of programming terminologies Symbols used in Flowchart Chapter – 1 : Basics of programming terminologies Problem solving methods.. Flowchart Algorithm Algorithm Step-1 Start Step-2 Input first numbers say A Step-3 Input second number say B Step-4 SUM = A + B Step-5 Display SUM Step-6 Stop Chapter – 1 : Basics of programming terminologies Character Set Every language contains a set of characters used to construct words, statements etc. C language character set contains the following set of characters... Alphabets Digits Special Symbols White spaces Compiler ignores white spaces unless they are part of a string constant. White spaces may be used to separate words but prohibited between characters of keywords and identifiers. Chapter – 1 : Basics of programming terminologies Character Set LETTERS: Uppercase A….Z, lower case a..z DIGITS: All decimal digits 0..9 SPECIAL CHARACTERS: comma(,), period(.), semicolon(;), colon(:), question mark(?), quotation(“), dollar sign($), slash(/),back slash(\), percent sign(%), underscore(_), ampersand(&), asterisk(*), number sign(#). WHITE SPACES: Blank space, Horizontal tab, Carriage return, Newline, Form feed. Chapter – 1 : Basics of programming terminologies Trigraph characters C introduces the concept of trigraph sequences to provide a way to enter certain characters that are not available on some keywords. Each Trigraph sequence consists of three characters, 2 question marks followed by another character. Trigraph sequence Equal character ??= # ??( [ ??) ] ??/ \ ??< { ??> } ??! | ??’ ^ ??- ~ Chapter – 1 : Basics of programming terminologies C Tokens Every C program is a collection of instructions and every instruction is a collection of some individual units. Every smallest individual unit of a c program is called token. Every instruction in a c program is a collection of tokens. Tokens are used to construct c programs and they are said to the basic building blocks of a c program. Chapter – 1 : Basics of programming terminologies C Tokens Chapter – 1 : Basics of programming terminologies Keywords Keywords are the reserved words with predefined meaning which already known to the compiler. Properties of Keywords All the keywords are defined as lowercase letters so they must be use only in lowercase letters Every keyword has a specific meaning, users can not change that meaning. Keywords can not be used as user defined names like variable, functions, arrays, pointers etc... Every keyword represents something or specifies some kind of action to be performed by the compiler. Chapter – 1 : Basics of programming terminologies Keywords Chapter – 1 : Basics of programming terminologies Identifiers Identifier is a user defined name of an entity to identify it uniquely during the program execution. Example: int marks; char studentName; Here, marks and studentName are identifiers. Chapter – 1 : Basics of programming terminologies Identifiers Rules for creating Identifiers An identifier can contain letters (UPPERCASE and lowercase), numerics & underscore symbol only. An identifier should not start with numerical value. It can start with a letter or an underscore. We should not use any special symbols in between the identifier even whitespace. However, the only underscore symbol is allowed. Keywords should not be used as identifiers. There is no limit for length of an identifier. However, compiler consider first 31 characters only. An identifier must be unique in its scope. Chapter – 1 : Basics of programming terminologies Examples of Valid and Invalid Names Chapter – 1 : Basics of programming terminologies Constants A constant is a named memory location which holds only one value throughout the program execution. Chapter – 1 : Basics of programming terminologies Creating constants in C In c programming language, constants can be created using two concepts... Using 'const' keyword Using '#define' preprocessor Chapter – 1 : Basics of programming terminologies Creating constants in C Using 'const' keyword To create a constant, we prefix the variable declaration with 'const' keyword. The general syntax for creating constant using 'const' keyword is as follows... const datatype constantName ; OR const datatype constantName = value ; Example const int x = 10 ; Here, 'x' is a integer constant with fixed value 10. Chapter – 1 : Basics of programming terminologies Creating constants in C Using '#define' preprocessor To create constant using this preprocessor directive it must be defined at the beginning of the program (because all the preprocessor directives must be written before the gloabal declaration). We use the following syntax to create constant using '#define' preprocessor directive… #define CONSTANTNAME value Example: #define PI 3.14 Here, PI is a constant with value 3.14 Chapter – 1 : Basics of programming terminologies Constants in C- Example Program //Using 'const' keyword //Using '#define' preprocessor #include #include void main() #defien PI 3.14 { void main() int i = 9 ; { const int x = 10 ; int r, area ; printf("Please enter the radius of circle : ") ; i = 15 ; scanf("%d", &r) ; x = 100 ; // creates an error printf("i = %d\n x = %d", i, x ) ; area = PI * (r * r) ; } printf("Area of the circle = %d", area) ; } The above program gives an error because we are trying to change the constant variable value (x = 100). Chapter – 1 : Basics of programming terminologies Backslash character constants C supports special backslash character constants that are used in output functions. These character combinations are known as escape sequences. Chapter – 1 : Basics of programming terminologies Variables Variable is a name given to a memory location where we can store different values of same datatype during the program execution. It must be declared in the declaration section before it is used. It must have a datatype that determines the range and type of values to be stored. A variable name may contain letters, digits and underscore symbol. Chapter – 1 : Basics of programming terminologies Variables The following are the rules to specify a variable name... Variable name should not start with digit. Keywords should not be used as variable names. Variable name should not contain any special symbols except underscore(_). Variable name can be of any length but compiler considers only the first 31 characters of the variable name. Valid variables are: Invalid variables are: john 123 x1 (area) T_raise 25th first_tag price$ % Chapter – 1 : Basics of programming terminologies Declaration of Variables Declaration allocate required amount of memory with specified variable name and datatype values into that memory location. The declaration can be performed either before the function as global variables or inside any block or function. It must be at the beginning of block or function. Syntax: Datatype variableName; Example int number; The above declaration allocate 2 bytes(32 bit Compiler)/4 bytes(64 bit Compiler) of memory with the name number and allows only integer values into that memory location. Chapter – 1 : Basics of programming terminologies Datatypes Datatype is a set of value with predefined characteristics. Datatypes are used to declare variable, constants, arrays, pointers and functions. The memory size and type of value of a variable are determined by varible datatype. User-defined Data Types The user defined data types enable a program to invent his own data types and define what values it can taken on Chapter – 1 : Basics of programming terminologies Datatypes- Primary Datatypes All the primary datatypes are already defined in the system. Primary datatypes are also called as Built-In datatypes. The following are the primary datatypes in c programming lanuage... Chapter – 1 : Basics of programming terminologies Datatypes- Integer Datatype The keyword "int" is used to represent integer datatype in c. The integer datatype is used with different type modifiers like short, long, signed and unsigned. The following table provides complete details about integer datatype. Chapter – 1 : Basics of programming terminologies Datatypes- Floating Point Datatypes Floating point datatypes are set of numbers with decimal value. The floating point datatype has two variants... float double Both float and double are similar but differ in number of decimal places. float value contains 6 decimal places. double value contains 15 or 19 decimal places. The following table provides complete details about floating point datatypes. Chapter – 1 : Basics of programming terminologies Datatypes- Character Datatype Character datatype is a set of characters enclosed in single quotations. The following table provides complete details about character datatype. Chapter – 1 : Basics of programming terminologies Datatypes- void Datatype The void datatype means nothing or no value. Generally, void is used to specify a function which does not return any value. We also use the void datatype to specify empty parameters of a function. Chapter – 1 : Basics of programming terminologies Assigning values to variables The general form of a variable declaration that uses a storage class is shown here: The syntax is Variable_name=constant Example: int a=20; bal=75.84; yes=’x’; C permits multiple assignments in one line. Example: initial_value=0;final_value=100; Chapter – 1 : Basics of programming terminologies Reading data from keyword Another way of giving values to variables is to input data through keyboard using the scanf function. The general format of scanf is as follows. scanf(“control string”,&variable1,&variable2,….); The ampersand symbol & before each variable name is an operator that specifies the variable name’s address. Example: scanf(“%d”,&number); Chapter – 1 : Basics of programming terminologies #define Directive The #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. The syntax for creating a constant using #define in the C language is: #define CNAME value value: The value of the constant. OR #define CNAME (expression) CNAME: expression: The name of the constant. Expression whose value is Most C programmers define assigned to the constant. their constant names in The expression must be uppercase, but it is not a enclosed in parentheses if it requirement of the C contains operators. Language. Chapter – 1 : Basics of programming terminologies #define Directive - Example #include #define NAME "Charusat" #define YEAR 2000 int main() { printf("%s is established in year %d\n", NAME, YEAR); return 0; } Chapter – 1 : Basics of programming terminologies Operators Operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators are used in programs to manipulate the data and variables. They usually form a part of the mathematical or logical expressions. Some operator required 2 operands (Binary Operator) to perform operation or Some required single operand (Unary) to perform operation. Chapter Chapter––51: :Constants, Basics of programming Variables & Data terminologies Types in ‘C’ C operators are classified into no. of categories 1. Arithmetic Operators 2. Relational Operators 3. Logical Operators 4. Assignment Operators 5. Increment/Decrement Operators 6. Bitwise Operators 7. Conditional Operators 8. Special Operators Chapter – 1 : Basics of programming terminologies Classification of Operator Continued… Arithmetic Operator This operator used for numeric calculation. There are 2 types of arithmetic operators in C: 1. unary operators : operators that require only one operand. 2. binary operators : operators that require two operands. Chapter Chapter –– 51 :: Constants, Variables & Data Basics of programming Types in ‘C’ terminologies Classification of Operator Continued… Unary Operator C Operation Operator Example Positive + a = +3 Negative - b = -a Increment ++ i++ Decrement -- i-- The first assigns positive 3 to a The second assigns the negative value of a to b. i++ is equivalent to i= i+ 1 i--is equivalent to i= i-1 Chapter Chapter––51: :Constants, Basics of programming Variables & Data terminologies Types in ‘C’ Classification of Operator Continued… Binary Operator C Operation Operator Example Addition + a+b Subtraction - a-b Division / a/b Multiplication * a*b Modulus % a%b The division of variables of type int will always produce a variable of type int as the result. You could only use modulus (%) operation on int variables. Chapter Chapter––51: :Constants, Basics of programming Variables & Data terminologies Types in ‘C’ PROGRAM Binary Expressions Chapter – 1 : Basics of programming terminologies PROGRAM Binary Expressions (continued) Chapter – 1 : Basics of programming terminologies PROGRAM Binary Expressions (continued) Chapter – 1 : Basics of programming terminologies Classification of Operator Continued… Relational Operator It is use to compared value of two expressions depending on their relation. Associativity = Left to right Operator Example Meaning > x>y x is greater than y < x= x>=y x is greater than or equal to y

Use Quizgecko on...
Browser
Browser