Anúncio

SPC Unit 2

Assistant Professor em Kongunadu College of Engineering and Technology
24 de May de 2021
Anúncio

Mais conteúdo relacionado

Anúncio
Anúncio

SPC Unit 2

  1. 20GE101- STRUCTURED PROGRAMMING USING C Unit II Basics of C Programming Overview of C – C Character Set – Identifiers and Keywords – Declaration – Data Types – Type Qualifiers and Type Modifiers – Variables and Constants – Structure of a C Program – Executing a C Program – Operators and Expressions – Decision-Making and Looping Statements.
  2. Introduction to C programming Types of Language: Low level Language (or) Machine Language. • A machine language or an assembly language. Low-level languages are closer to the hardware. • Low-level languages can be converted to machine code without using a compiler or interpreter, and the resulting code runs directly on the processor. High Level Language • Normal English, human understandable language, machine independent, C++, java, BASIC, COBOL, FORTRAN, etc…
  3. Middle Level Language • C Language is also called as Middle level Language because ‘C’ stands in between Low level Language (nor) high level language. • It performs task of low level languages as well as high level language. • We can write program for operating, application programs, Assembly language programs in ‘C’ Languages. • UNIX operating system is written in ‘C’ Language.
  4. History of C-Language: • C is a structured, procedural programming language, and most powerful Language. • C was developed by Dennis Ritchie at AT & T Bell laboratory in 1972. • It is an upgraded version of two earlier languages, called BCPL and B, which were also developed at Bell laboratories.
  5. History of C-Language: Sl. No Language Year Founders 1. ALGOL 1960 International Committee 2. BCPL 1967 Martin Richards 3. B 1970 Ken Thompson 4. C 1972 Dennis Ritchie 5. K & R C 1978 Kernighan and Ritchie 6. ANSI C 1989 ANSI Committee 7. ANSI / ISO C 1990 ISO Committee
  6. History of C-Language: • All modern computer languages are started through the ALGOL (ALGOrithmic Language) language in 1960. • After the COBOL was being used for commercial applications. • FORTAN was developed for scientific applications. • A committee was formed to develop a new language called Combined Programming Language (CPL) at Cambridge University. • Basic CPL was derived by BCPL. • BCPL was developed by Martin Richards at Cambridge University by 1967. • At the same time a language called ‘B’ was developed by Ken Thomson at AT & T’s Bell labs in 1970.
  7. History of C-Language: • B language does not have Data type and it does not provide the structure to overcome this Ritchie introduced C language. • Then C language is developed by Dennis Ritchie in 1972 with some additional features of BCPL and B which is very simple. Using C language UNIX operating system is made. • ANSI C American National Standard Institute has began to work on a standardized definition of the ‘C’ language that makes it still powerful.
  8. Features and Applications of ‘C’ Languages: 1. C is a general purpose, structured programming language. 2. C is a powerful, efficient, compact & flexible. 3. C is highly portable. It can run in different operating systems environment. 4. C is robust language. It has built in functions and operators can be used to write any complex program. 5. C is well suited for writing system software as well as application software. 6. C is middle level language. It supports low level & high level language. 7. C language allows reference to memory allocation with the help of the pointers. 8. C language allows dynamic memory allocation. C programs are fast and efficient.
  9. C Character Set • A character set defines the valid characters that can be used in a source program or interpreted when a program is running. • The set of characters that can be used to write a source program is called a source character set. • The set of characters available when the program is being executed is called an execution character set. • The basic source character set of C language includes: 1. Letters:  Uppercase letters: A, B, C,….., Z  Lowercase letters: a, b, c,……, z 2. Digits: 0, 1, 2, 3……, 9 3. Special characters: ,.;;!”^#$%@&*()[]{}<>|/_~ etc
  10. C Character Set 4. White space characters: a. Blank space character b. Horizontal tab space character c. Carriage return d. New line character e. Form feed character
  11. Identifiers and Keywords Identifier • An Identifier refers to an object. • It can be a variable name, a label name, a function name, a typedef name, a macro name or a macro parameter, a tag or a member of a structure, a union or an enumeration.
  12. Identifier • The syntactic rules to write an identifier name in C are as follows; • Identifier name in C can have letters, digits or underscores. • The first character of an identifier name must be a letter (Either Uppercase or lowercase) or an underscore. The first character of an identifier name cannot be a digit. • No special character (except an underscore), blank space and comma can be used in an identifier name. • Keywords or reserved words cannot form a valid identifier name. • The maximum number of characters allowed in an identifier name is compiler dependent, but the limit imposed by all the compilers provides enough flexibility to create meaningful identifier name.
  13. Identifier • The following identifier names are valid in C:  Student_Name  StudentName  Student_name  Student1  _Student • The following identifier names are not valid in C;  Student Name (due to blank space)  Name&Rollno (due to special character &)  1st_student (First character begin a digit)  for (for being a keyword)
  14. Keywords • Keywords are the reserved words that has a particular meaning in the programming language. • The meaning of a keyword is predefined. Each keywords has fixed meaning and that cannot be changed by user. • A keyword cannot be used as an identifier name in C language. C has 32 keywords. They always written in lowercase. The C keywords are, Auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while
  15. Variables and Constant • A variable is a identifier that is used to represent some specified type of information. • A variable is a area of storage that can hold a single value (numeric or character). • A variable specifies a data type and contains a list of one or more variables of that type. Variables:
  16. Variables Rules for naming the variable: 1. The variable name can be any combination of 1 to 8 alphabets, digits or underscore. 2. The first character must be an alphabet letters, some system permits underscore ( _ ) as first character . 3. The length of the variable cannot exceed up to 8 characters long, and ANSI C compilers recognize up to 31 character long. 4. No commas or blank spaces are allowed within a variable name. 5. Variable name should not be a C- keyword. 6. No special symbol, an underscore can be used in a variable name.
  17. Variables Declaration of Variables: • After designing suitable variable name, we must declare them in to a program. • This declaration tells the compiler what the variable and type of the data that the variable will hold. • Here, a,b,c,d is list of variable names it is a integer type variable, i.e., it can hold integer value. • Add, result is a floating point variable. Which holds an decimal numbers. Syntax: data type variable name 1, …variable name n; Example: int a,b,c,d; float add, result; char name;
  18. Variables Initializing variables: • Initializing the variable can be done using the assignment operator(=). • The variable can be initialized when the value is declared itself. Syntax: Data_type variable_name=constant value; Example: int i=45; float var1=28.67; char c=’t’;
  19. Variables Global variable: Global variables are declared above the main() function in the following way. Example: #include<stdio.h> short number, sum; int bignumber, bigsum; // Global variable declaration; void main() { //methods and execution statement declaration; }
  20. Variables Variables has spitted into two categories. • Local variable. • Global variable. • Local Variable: The variable declared within the function. (i.e., with in the open and closed curly brace ‘{}’) Example: #include<stdio.h> int main() { int a=10, b=20,c; // a, b and c are variables with constant values 10,20. //here, a,b,c is a local variable. c=a+b; printf(“the result is %d”,c); } OUTPUT: 30.
  21. Constants • The constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. • A constant is an entity whose value remains the same throughout the execution of a program. • Literal Constants • Qualified Constants • Symbolic Constants
  22. Constants Literal Constants: • Literal constants or just literal denotes a fixed values, which may be an integer, floating point number, character or a string. • Literal constants are of the following types, • Integer literal constant • Floating point literal constant • Character literal constant • String literal constant
  23. Constants Integer Literal Constants: • Integer Literal Constants are integer values like -1, 2,8 etc. RULES:  An Integer Literal Constants must have at least one digit.  It should not have any decimal point.  Can be positive or negative. If no sign precedes an integer literal constants, then it is assumed to be positive.  No special characters (Even Underscore) and blank spaces are allowed.
  24. Constants Floating point Literal Constants: • Floating point Literal Constants are values like -23.1, 12.8 etc. RULES:  A fractional Floating point Literal Constants must have at least one digit.  It should have any decimal point.  Can be positive or negative. If no sign precedes an integer literal constants, then it is assumed to be positive.  No special characters (Even Underscore) and blank spaces are allowed.
  25. Constants Character Literal Constants: • A character literal constants can have one or at most two characters enclosed with single quotes. eg) ‘A’, ‘a’, ‘n’ etc. • Character literal constants are classified as,  Printable character literal constants  Non-printable character literal constants Printable character literal constants: • All characters of source character set except quotation mark, backslash and new line character when enclosed within single quotes form a printable character literal constant. • Example: ‘A’, ‘#’, ‘6’
  26. Constants Non-Printable character literal constants: • Non-Printable character literal constants are represented with the help of escape sequences. • An escape sequence consist of backward slash (i.e. ) followed by a character and both enclosed within single quotes.
  27. Constants Non-Printable character literal constants:
  28. Constants String literal constant: • A string literal constant consists of a sequence of character enclosed with in double quotes. • Each string literal constant is implicitly terminated by a null character (i.e. ‘0’)
  29. Constants Qualified Constants: • Qualified constants are created by using const qualifier. • The following statement creates a qualifier character constant named a; const char a=‘A’;
  30. Constants Symbolic constants: • Symbolic constants are created with the help of the define preprocessor directive. • For example, #define pi 3.14 Here pi is a symbolic constant with values 3.14
  31. Structure Of C-Programming: • C language is a programming language and it is also a structured programming language was developed by Dennis Ritchie in 1972. • Every C program contains a number of building blocks. These blocks are written in a correct order and procedure.
  32. Structure Of C-Programming:
  33. Structure Of C-Programming: 1.Documentation Block: • Documentation block contains the information about the program. Like program name, author name, date of development and program details. • This block should be written with in Multi lines command. Because the compiler does not compile the lines present in the command lines. • It is optional one in C structure. • Command Lines has two types. • Single line command- It should be written with in “//”(Double slash) • Multi line command- multi line command consist of two or more lines. Should start with “/*” and end with “*/”.
  34. Structure Of C-Programming: 2.Preprocessor directive block: • It provides preprocessor statement which direct the compiler to link functions from the system library. • Header files is necessary. include in this block. With out this header files a program does not link with the system library. Example: #include<stdio.h> Here, #  Preprocessor directive. <stdio.h>  header files end with .h extension.
  35. Structure Of C-Programming: 3.Definition Block: • Definition block is used to declare the constant values as globally. Syntax: #define constant_name constant_value Example: #define π=3.14 4.Global Variable Declaration Block: • It contains variable declarations which can be accessed anywhere within the program.
  36. Structure Of C-Programming: 5.Main Function: • C program execution is starts from main function. Every c program must contain C program. • Main section is divided into two portions.  Declaration part  Execution part • Declaration part is used to declare any variable in main block of the program. • Execution part contains set of statement which is used to take the input from the user process it, and displaying the result. These statements are pre defined type. • Execution of the program begins and ends with in the open and close ‘{}’ curly brace.
  37. Structure Of C-Programming: 6. Sub program block: • Sub program section or user defined section contains user defined function which are called by main functions. • Each user defined functions contains the function name, argument and return values.
  38. Executing a C Program Compilation and Linking Processes Executing a C program involves a series of steps, there are, • Creating a program • Compiling the program • Linking the program with functions that are needed from the C library. • Executing the program
  39. Executing a C Program
  40. Executing a C Program Preprocessing: • The preprocessor changes the program according to the directives mentioned (that starts with # sign). The C preprocessor takes the program and deals with the # include directives and the resulting program. Compilation: • It translates the program into a low level assembly level code. The compiler takes the preprocessed file and generates an object file containing Assembly level code. • Assembly - Using a Assembler program to convert assembly source code to object code. Linking: • The creation of a single executable file from multiple object files. The file created, after linking is ready to be loaded into memory and executed by the system. • Using a Linker program to convert object code to executable code. Multiple units of object codes are linked to together in this step.
  41. Executing a C Program Process of Compilation and linking in C program:
  42. DATA TYPES IN C • Data type is the type of data, that are going to access within the program. • C data types are defined as the data storage format that a variable can store a data to perform a specific operation. • Data types are used to define a variable before to use in a program. • Size of variable, constant and array are determined by data types. • A data type is used to  Identify the type of a variable when the variable is declared  Identify the type of the return value of a function  Identify the type of a parameter expected by a function.
  43. DATA TYPES IN C Void is valueless 
  44. DATA TYPES IN C 1.Primitive Data Type:  The primitive data types in c language are the inbuilt data types provided by the C language itself. Thus, all c compilers provide support for these data types. Integer (int):  Integer data type is used to declare a variable that can store numbers without a decimal.  The keyword used to declare a variable of integer type is “int”.  Syntax: int variable_name;
  45. DATA TYPES IN C Float point (float): • Float data type declares a variable that can store numbers containing a decimal number. • Syntax: float variable_name; Character (char): • Character data type declares a variable that can store a character constant. Thus, the variables declared as char data type can only store one single character. • Syntax: char variable_name;
  46. DATA TYPES IN C Double Data Type (double): • Double data type also declares variable that can store floating point numbers but gives precision double than that provided by float data type. • Thus, double data type are also referred to as double precision data type. • Syntax: double variable_name; Valueless (void): • void data type does not create any variable but returns an empty set of values. Thus, we can say that it stores null. • Syntax: void variable_name;
  47. DATA TYPES IN C
  48. DATA TYPES IN C
  49. DATA TYPES IN C 2.Derived Data Types • Derived Data Type are a derivative of primitive data types known as arrays, pointer and function. • Array – A finite sequence (or table) of variables of the same data type • String – An array of character variables • Structure – A collection of related variables of the same and/or different data types. The structure is called a record and the variables in the record are called members or fields. 3.User Defined Data Types: • User defined data types are those data types which are defined by the user/programmer himself. • Structure, union and enum are user defined data types. • These are also referred to as composite data types.
Anúncio