You are on page 1of 12
A. cminotes What is Programming Language A programming language is an artificial language for computers that allows a programmer to write commands in a format that can be easily understand or remembered by a person/programmer and can easily be translated into codes that computer can understand and executes. Types of Programming Language Programming languages are of two types given below: Programmi Language | |Low level prégramming High level programming | |language language | |e Assembly language Ex: C, C++, C#, JAVA etc. What is C C is a computer programming language and it was developed by DENNIS RITCHIE between 1969 and 1973 at Bell laboratory. Why It Named C It was named C because its features were derived from an earlier language called B which was a version of BCPL programming language. _Aouree Code _ Any program (or code) that is written using any programming language is known as source code. Machine Language A language that computer understand is known as machine language or machine code. > 1 | = EI A. cminotes Compiler Compiler is a translator that translates (or converts) the source code into machine code Compilation and Execution Process of C Program The compilation and execution of C program is given below: fe < h: ee | (c Source code) void main() orintf(“Hello, my name is khan getch() po C Compiler 101010101010 101010101010 | __ (Machine code) CPUor 0S _}—» oyp Platform dependency of C Program When program is compiled using C compiler then OS instruction set is added with machine code. Now, if we try to execute this code on other Operating system then it will not execute on that Operating system because instruction set of one Operating system cannot be understand by other operating system. i i Since we cannot run the compiled code of one Operating system directly on another operating system (hence Cis a platform dependent programming language. \ a aa A. cminotes The following figure shows how C is a platform dependent programming language: Hinclude #include void main() printf(“Hello, my name is khan”); | (C Source code) | getch(); | C Compiler of Windows 0101010101010 + OS instruction set 0101010101010 (Machine code) Cetereoreie Operating System other than windows [> It will not run System Software System software is computer software that is developed to operate the computer hardwi vide a platform for running application” Sepa a RAO software. Examples: Operating systems like Microsoft Windows, Mac OS X, UNIX, Linux etc. Application Software Application software is computer software that is developed to perform user’s specific tasks and run with the help of system software. Examples: Notepad, Media players, accounting software etc. AEP 3 ie Re A. cminotes Co Uses af € gor following Uhings most important use of C is developing system softwares. ‘ Maximun parts of different operating systems like Windows, Mac OSX UNIX, Linux are developed in ¢ Compilers. libraries and) interpreters of many programming ges are developed in ran y sed for application software development + important use of C is using C in Embedded System ying like using © in Washing machines, Refrigerators, piles ete Why Operating Systems are Developed in C ason for developing operating systems in C is that C has the great to oasily access hardware addresses and it is easy to do with hardware using C ANSLCE 83. the American National Standards Institute (ANSI) formed a vittee to establish a standard version of C standard was completed in 1989 and this version of C language is Known as ANSIC Why ANSI C Since first version of C was developed for UNIX Operating System Hence, programs can be compiled and run only on UNIX Operating System because there was only C compiler for UNIX operating system. or using C with different operating systems (or platforms) ANSI C was | developed. With this version of C different compilers for different operating system were developed. F A. cminotes Rules for Writing C Programs 1. Cis case sensitive language, hence main() and Main() are different things and you should be aware of that . . Line or statement must be terminated by semicolon (;) Header files must be used before the main() if you are using predefined functions of C standard library Like: #include, #include 4. You should have main() and all code should be inside the main() First Program in C Rinclude or #include void main() { #include“stdio.h” Output Hello incapp printf(“Hello Incapp”); getch(); Explanation of Above Program * #include is the pre-processor directive and stdio.h, conio. h are header files. Here stdio stand for strandard intput-output and conio stands for console input-output. Header-files are just containers for pre-defined (or built-in) functionalities of the programming language and pre-process directive includes these pre-defined functionalities to the given program at compile-time by using #include with header files. We can also use #includestdio.h” instead of #include. The only difference between <> and “” is that when using <> with header files, header-files are searched from the standard-library of C, whereas when using “” with header files, header files are searched first from the current directory and if not there, then searched from standard library. ~ ~ A. cminotes © main() function is the entry-point of C program and the code that you want to execute should be inside the main() function. © printf() is a built-in function defined in stdio.h header file to give output on computer screen. ¢ getch() is also a built-in function defined in conio.h header file to receive single character input from keyboard Tokens The smallest individual units of C program that can be recognized by C compiler are known as tokens. C has following tokens: Keywords Identifiers Constants Strings Operators Special Symbols QuUpwWNE Keywords Keywords are those words whose meaning has been already defined to C compiler. C contains 32 keywords. auto extern —_| sizeof break | float [static [case © [for [struct [char | goto switch | const if sizedef continue [int [union |default | long lunsigned do _| register | void | double return volatile | else short | while enum [signed | | These keywords cannot be used as variable names because if we do so, we are trying to change their meaning which is not allowed. C Language e een chs A. cminotes Identifiers In C, identifiers are names of variables, functions. These are user defined names and consist of any Combination of letters, underscore and digits, according to rules of defining a variable name For examples: = main, salary Rules for Identifiers The rules for identifiers are given below: 1. First character must be an alphabet or underscore 2. It must consist of only letters, digits or underscore 3. Identifiers, maximum of 31 Characters length are allowed. 4. Cannot use keyword for identifiers. 5. It must not contain any space. For examples: Valid a49,_1ab salary, _post ~~ Iday, ab, aSb Data Types in C, data type is a keyword that tells compiler what type of value can be stored by the identifier associated with it. Why Data Types Data types are used to tell about: ‘ ' 1. Possible values that can be store by the identifier. 2. Possible operations that can be done on that identifier. i 3. How much storage space is allocated to the identifier? eae 7 Ee ec A. cminotes Categorization of Data Types Categorization of data types in C 1s given below: | C Data Types | i I 7 | | Primary Data Types Derived Data Types User-defined Data Types | \| Integ Array Structure Tl | Floating Point Types string Enumeration | void Type — Formats of Data Types Following are formats of basic data types: Data Types | Format ] int | “float __ char | Variable In real world, variable is an entity that can change with time. In C programming, a variable is an identifier which is used to store a value and its value can be changed at any time in the program. Declaration for Variable In order to use a variable, we have to declare it first. The syntax for declaring a variable is given below: For example: You can also declare more than one variable of same data type in single statement by using comma operator. The syntax for declaring multiple variables of same type is given below: data_type identifier, identifier2, ------, identifier’; C Language 7: ee A. cminotes Forexample: int a, b, c; Initializing a Variable The process of assigning a value to a variable is known as variable initialization, The syntax for variable initialization is given below: | identifier = value; | Forexample: a= 2; The process of declaration and initialization of a variable can be done in a single step as given below data_type identifie Forexample: —inta [_identitie Forexample: a=7; Now previous value 2 of variable a is replaced by new value 7. Memory Representation of Variable The following figure illustrates how value of a variable changes in the memory: int a= 2; a 2 AT Memory representation of variable a Printing the Value of a Variable The following form of printf() function is used for printing the value of the variable: printf(“data_type format”, variable_name); For example: _ printf(“%d", / / We can also print some message with the value of the variable using following form of printf() function: fem Ur 9 Getting Started A. cminotes ; : printf("String Message data_type_format, variable_name); | For example: _ printf(“Value of a=%d", a); Sample Program for a Variable #include #include void main() int a — or inta=2; a=2; printf(“Value of a = %d \n”, a); a=7; printf(“Changed value of a = %d”, a); getch(); Output Value ofa=2 Changed value of a= 7 Escape Sequences Character | Escape Sequence [ASCII vatue —] \n _10 | Horizontal tab _\t 9 | Vertical tab | W 1 | “Backspace _ _Apostrophe | _Question mark Backslash ‘Null Carriage return Clan A. cminotes Constant In real world, a constant is an entity that cannot change with time. In C Programming, a constant is an identifier which is used to store a value and its value cannot be changed once assigned. We use const keyword to declare a constant. Declaring and Initializing a Constant Like variables, to use a constant, we have to declare it first The syntax for declaring a constant is given below: —_—_—___ | const data_type identifier; LS Forexample: const int a; Constant must be initialized at the time of declaration and syntax for initialization of a constant is given below: const data_type identifier = value; Forexample: const int a = 2; Note: Now the value of constant a cannot be changed, because we cannot modify the value of a constant. We cannot initialize a constant after declaration because by doing so we are trying to change the garbage value of a constant which is not allowed Forexample: const int a; 3 a=2; (illegal and will not work) . Sample Program for a Constant #include #ifeludesconio.h> — € void main() { const int a= 2; printf(“Value of a = %d\n", a): | (ec 11 Ce RoC) a A.aminotes a=2; printf(“Changed value of a = %d\n", a); getch(); } Note: Above program will not run because value of a constant cannot be modified once assigned, so correct program will be as given below #include #include Output void main() Value of a=2 { const int a = 2; printf(“Value of a = %d”, a); getch();

You might also like