4. Introduction to _C_ Language.pdf

Full Transcript

1 2 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972. C is a successor of B language which was intr...

1 2 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972. C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute (ANSI). The UNIX OS was totally written in C. Today's most popular Linux OS and RDBMS MySQL have been written in C. 3 Features of ‘C’ Portable Procedural / Modular Structured Language Statically typed Middle level language 4 5 6 7 Character Set Every language has its own character set. ‘C’ language has its own character set. In the C programming language, a character set is a predefined collection of characters that the language recognizes and can manipulate. These characters are used to form words, expressions, and numbers in the source program. The character set typically includes: Alphabet: Both uppercase and lowercase letters (A – Z and a – z), Digits: 10 digits from 0 to 9 Special symbols: For mathematical operations, condition checking (~ ! @ # $ % ^ & * ( ) [ ] { } ; : ‘ “ ,. < > / ? \ |) , Whitespace (Space, Tab, New Line, Form feed etc). 8 9 10 Tokens in C Tokens are the smallest units in a C program and are used to build the program's structure and specify its actions. The C compiler breaks down the source code into tokens, which it uses to understand the program's functionality 11 Tokens in C 12 Tokens in C 13 Tokens in C Keywords Whose meaning is fixed and is known by the compiler, cannot change the meaning of keyword. These are reserved words of the C language. For example int, float, if, else, for, while etc. In C, we have 32 keywords, which have their predefined meaning and cannot be used as a variable name. 14 Tokens in C Constants Whose value does not change throughout the program. Numeric Constants Integer constants like 13, -15, 0, +15 etc. Real constants like 0.004, -0.34, 0.4e-2 etc. Non-numeric constants / Character constants Single Character constants : ‘B’, ‘a’, ‘5’, ‘+’ etc. Back slash constants are special type of character constants which actually consists of two characters. These are known as escape sequences. Escape sequences start with backslash ‘\’ character. E.g. ‘\t’ – horizontal tab, ‘\n’ - newline etc. String constants: “Computer”, “-345”, “B” etc. 15 16 Tokens in C Identifiers In C, identifiers are strings of characters that identify a specific item in a program, such as a variable, function, array, pointer, or structure. They can be short names like "x" and "y", or more descriptive names like "age", "sum", or "totalVolume". Identifiers are case sensitive. I. Characters Allowed : Underscore(_) Capital Letters ( A –Z ) Small Letters ( a –z ) Digits ( 0 –9 ) II. Blanks & Commas are not allowed. III. No Special Symbols other than underscore(_) are allowed. IV. First Character should be alphabet or Underscore. V. Variable name Should not be Reserved Word (keyword). Valid: Root, _getchar, __sin, x1, x2, x3, x_1, If 17 Invalid: 324, short, price$, My Name Tokens in C String Literals A sequence of characters enclosed in double quotes as “…”. For example “13” is a string literal and not number 13. ‘a’ and “a” are different. Operators Arithmetic operators like +, -, *, / ,% etc. Logical operators like ||, &&, ! etc. and so on. Special symboles Spaces, new lines, tabs, comments ( A sequence of characters enclosed in ) etc. These are used to separate the adjacent identifiers, kewords and constants. 18 19 Escape Sequences Escape Sequences Character \b Backspace \f Form feed \n Newline \r Return \t Horizontal tab \v Vertical tab \\ Backslash \’ Single quotation mark \” Double quotation mark \? Question mark \0 Null character 20 21 Structure of ‘C’ program 22 23 A Simple C Program: Printing a Line of Text Comments Text surrounded by is ignored by computer Used to describe program #include Preprocessor directive Tells computer to load contents of a certain file allows standard input/output operations 24 A Simple C Program: Printing a Line of Text int main() C programs contain one or more functions, exactly one of which must be main Parenthesis used to indicate a function int means that main "returns" an integer value Requires a return statement at the end of the function Braces ({ and }) indicate a block The bodies of all functions must be contained in braces void main ( ) Indicates no return type of main ( ) function. Does not require the return statement at the end of the function. 25 A Simple C Program: Printing a Line of Text printf( "Welcome to C!\n" ); Instructs computer to perform an action Specifically, prints the string of characters within quotes (“ ”) Entire line called is a statement. All statements must end with a semicolon (;) Escape character (\) Indicates that printf should do something out of the ordinary \n is the newline character 26 A Simple C Program: Printing a Line of Text return 0; A way to exit a function return 0, in this case, means that the program terminated normally Right brace } Indicates end of main has been reached Linker When a function is called, linker locates it in the library Inserts it into program If function name is misspelled, the linker will produce an error because it will not be able to find function in the library 27 Defining symbolic constants When a constant is used at many places in a program, due to some reason if the value of that constant needs to be changed, then we need to change at every statement where that constant occurs in the program – so modification becomes difficult. The symbolic constant helps in solving these problems. Here, the constant is given a symbolic name and instead of constant value, symbolic name is used in the program. It is defined as below: #define symbolic_name value #define PI 3.1415 28 Procedure of execution of ‘C’ program 29 30

Use Quizgecko on...
Browser
Browser