Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basics of “C” Programming

Similar presentations

Presentation on theme: "Basics of “C” Programming"— Presentation transcript:

1 Basics of “C” Programming

2 Terminologies of ‘C’ Keywords Identifiers Variables Constants
Special Symbols Operators Character & String

3 1. Keywords Keywords are the reserved words whose meaning has already been explained to the C compiler. C has 32 keywords. These keywords combined with a formal syntax form a C programming language. Rules to be followed for all programs written in C: All keywords are lower-cased. C is case sensitive, do-while is different from DO WHILE. Keywords cannot be used as a variable or function name.

4 2. Identifiers Identifiers refer to the name of variables, functions and arrays. These are user-defined names and consist of sequence of letters and digits, with a letter as a first character. Both uppercase and lowercase letters are permitted, although lowercase letters are commonly used . The underscore character is also permitted in identifiers. It is usually used as a link between two words in long identifiers.

5  X Identifier Names Some correct identifier names are -
arena, s_count marks40 class_one Some erroneous identifier names are - X 1stsst oh!god start….end The number of characters in the variable that are recognized differs from compiler to compiler An identifier cannot be the same as a C keyword

6 3. Variables Variables are named locations in memory that are used to hold a value that may be modified by the program. Unlike constants that remain unchanged during the execution of a program. A variable may take different values at different times during execution. The syntax for declaring a variable is – DataType IdentifierName ; Example- int num; long int sum , a;

7 4. Constants Constants are the fixed values that do not change during the execution of a program. C supports several types of constants. Numeric Constants Integer constants Real constants Character Constants Single character constant String Constants

8 5. Special Symbols ! #, $ , & , * , ….. These all symbols that can be find on Keyboard, are called Special Symbols. Every symbol has its special meaning in different respect at different place that’s why it is called Special Symbols.

9 6. Operators Operator is a symbol that operates on one or more operands and produces output. Example c = a + b ; In the above Example the symbols + and = are operators that operate on operands a, b , and c .

10 7. Character & String The Characters that can be used to form words, numbers and expressions depend upon the computer on which the program is running. The characters in C are grouped into the following categories : Letters Digits Special characters White Spaces Remember that a character ‘a’ is not equivalent to the string “a”.

11 Why use C? Mainly because it produces code that runs nearly as fast as code written in assembly language. Some examples of the use of C might be: Operating Systems Language Compilers Assemblers Text Editors Print Spoolers Network Drivers Modern Programs Data Bases Language Interpreters Utilities

12 Why C Still Useful? C provides:
Efficiency, high performance and high quality flexibility and power many high-level and low-level operations  middle level Stability and small size code Provide functionality through rich set of function libraries Gateway for other professional languages like C  C++  Java C is used: System software Compilers, Editors, embedded systems data compression, graphics and computational geometry, utility programs databases, operating systems, device drivers, system level routines there are zillions of lines of C legacy code Also used in application programs

13 Software Development Method
Requirement Specification Problem Definition Analysis Refine, Generalize, Decompose the problem definition Design Develop Algorithm Implementation Write Code Verification and Testing Test and Debug the code

14 Development with C Four stages
Editing: Writing the source code by using some IDE or editor Preprocessing or libraries: Already available routines compiling: translates or converts source to object code for a specific platform source code -> object code linking: resolves external references and produces the executable module Portable programs will run on any machine but….. Program correctness and robustness are most important than program efficiency

15 History In 1972 Dennis Ritchie at Bell Labs writes C and in 1978 the publication of The C Programming Language by Kernighan & Ritchie caused a revolution in the computing world In 1983, the American National Standards Institute (ANSI) established a committee to provide a modern, comprehensive definition of C. The resulting definition, the ANSI standard, or "ANSI C", was completed late 1988.

16 The Beginning of C BCPL - Martin Richards B - Ken Thompson C -
Dennis Ritchie

17 History of C Evolved from two previous languages BCPL , B
BCPL (Basic Combined Programming Language) used for writing OS & compilers B used for creating early versions of UNIX OS Both were “typeless” languages C language evolved from B (Dennis Ritchie – Bell labs) C was developed by Dennis Ritchie at AT & T bell laboratory of USA in 1972. Typeless – no datatypes. Every data item occupied 1 word in memory.

18 History of C Hardware independent Programs portable to most computers
Dialects of C Common C ANSI C ANSI/ ISO 9899: 1990 Called American National Standards Institute ANSI C Case-sensitive

19 C – A Middle Level Language
High-Level Language C + Low Level Language

20 C is Middle Level Language
C stands in between these two categories. Since it was designed to have both: A relatively good programming efficiency as compared to Machine Oriented Languages . A relatively good Machine efficiency as compared to Problem Oriented Languages . That’s WHY it is called a Middle Level Language.

21 Application Areas Of C C was initially used for systems programming.
A system program forms a portion of the operating system of the computer or its support utilities. Operating Systems, Interpreters, Editors, Assembly programs are usually called system programs. The UNIX operating system was developed using C. There are C compilers available for almost all types of PC’s.

22 A simple Program of C /* Write a program to print a message */ #include<stdio.h> #include<conio.h> void main() { printf(“ C Programming”); getch( ); }

23 Comment about the program should be enclosed within ‘/*’ & ‘*/’.
printf() is a function which is used to print messages on the screen. main() is a function from which execution of the program starts. Here stdio.h is a library file which contains standard input/output functions , keywords etc. #include<> is used to define the library file that is to be used in the program for compiler information.

24 Compilation & Execution of a Program
C Preprocessor Expanded C Source Code C Compiler Target Assembly Code Linker Executable Code Loader Output C Source Code

25 Basics of C Environment
C systems consist of 3 parts Environment Language C Standard Library Development environment has 6 phases Edit Pre-processor Compile Link Load Execute

26 Basics of C Environment
Editor Disk Phase 1 Program edited in Editor and stored on disk Preprocessor Disk Phase 2 program processes the code Compiler Disk Phase 3 Creates object code and stores on disk Linker Disk Phase 4 Links object code with libraries and stores on disk

27 Basics of C Environment
Loader Phase 5 Puts program in memory Primary memory CPU Phase 6 Takes each instruction and executes it storing new data values Primary memory

28 Execution Process of a Program
SHORT CUT FILE NAME Save F2 abc.c Compile Alt + F9 abc.obj Execute Ctrl + F9 abc.exe Back up Again F2 abc.bak

29 Escape Sequence \n new line \t tab \r carriage return \a alert
\\ backslash \” double quote

30 Control FLOW Statements
C language has decision making capabilities and supports controlling of statements. C supports following decision making statements: 1. IF 2.IF-ELSE and its various form 3. Switch 4. Conditional Operator

31 If statement The if statement is a powerful decision making statement.
The if statement is used to control the flow of execution of statements. It is a two way decision statement which is used in conjunction with an expression.

32 Different forms of If statement
Simple If statement. If…..else statement. Nested if…..else statement. Else if ladder.

33 Simple if statement If the if expression evaluates to true, the block following the if statement or statements are executed. The general form of a simple if statement is : if (test-expression) { statement-block; } statement-x;

34 If….Else statement The general form of if……else statements is :
IF the if expression evaluates to true, the block following the if statement or statements are executed. The else statement is optional. It is used only if a statement or a sequence of statements are to be executed in case the if expression evaluates to false.

35 Else if ladder The general form of else if ladder is:
The if – else – if statement is also known as the if-else-if ladder or the if-else-if staircase. The conditions are evaluated from the top downwards.

36 Nested if…else statement
The general form of nested if…else statement is: if (test-expression 1) { if (test-expression 2) statement 1; } else statement 2; statement 3; Statement x;

37 Switch – case statement
The switch-case control statement is a multi-way decision maker that tests the value of an expression against a list of integers or character constants. When a match is found, the statements associated with that constant are executed. The syntax of switch-case is:-

38 Jumping Statement Break Continue Goto

39 Break statement When the keyword break is encountered inside any c loop, control automatically passes to the first statement after the loop. A break is usually associated with an if. The general syntax of break statement is: For(; ;) { if (error) break; }

40 Continue statement In some programming situations we want to take the control to the beginning of the loop, by passing the statements inside the loop which have not yet been executed. The Keyword continue allows us to do this. When the keyword continue is encountered inside any C loop, control automatically passes to the beginning of the loop. A continue is usually associated with an if.

41 #include<stdio.h> void main() { int i,j; for(i=1;i<=2;i++) for(j=1;j<=2;j++) if(i==j) continue; printf(“%d%d”,i,j); } } }

42 Output

43 Go to statement C supports the go to statement to branch unconditionally from one point to another in the program. It requires a label in order to identify the place where branch is to be made. The general syntax of go to statement is: go to label; label: statement x ;

Download ppt "Basics of “C” Programming"

Similar presentations

Ads by Google