Basics of “C” Programming

Slides:



Advertisements
Similar presentations
CSE 105 Structured Programming Language (C)
Advertisements

Module 6: Introduction to C Language ITEI102 Introduction to Programming Structure of C++ Program - Programming Terminologies - Microsoft Visual Studio.
EC-111 Algorithms & Computing Lecture #1 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Chapter 3: Beginning Problem Solving Concepts for the Computer Programming Computer Programming Skills /1436 Department of Computer Science.
Lecture 2 Introduction to C Programming
Three types of computer languages
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
Programming C/C++ on Eclipe C Training Trình bày : Ths HungNM.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Introduction to C Programming
C programming Language and Data Structure For DIT Students.
Introduction to C language
CHAPTER 1: INTORDUCTION TO C LANGUAGE
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Using C Programming Language.  The programs that run on a computer are referred to as software.  You’ll learn key programming methodology that are enhancing.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Computer Science 210 Computer Organization Introduction to C.
Programming In C++ Spring Semester 2013 Programming In C++, Lecture 1.
COMPUTER SCIENCE I C++ INTRODUCTION
Welcome In The World Of ‘C’.  TEXT BOOK: Programming in ANCI ‘C By: E Balagurusamy. TMH  Reference Books: 1) The ‘C Programming Language By: Kernighan.
“C” Programming Language What is language ? Language is medium of communication. If two persons want to communicate with each other, they have to use.
Programming Design Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
 2008 Pearson Education, Inc. All rights reserved Introduction to Computers, the Internet and World Wide Web.
CSC141 Introduction to Computer Programming
History of C and C++ C++ evolved from C ANSI C C++ “spruces up” C
C++ Programming Language Lecture 1 Introduction By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Computer Organization Six logical units in every.
Programming With C.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
Chapter 1 Introduction to Computers, the Internet and the Web.
Control Flow Statements
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 1 – Introduction to C.
Introduction to C Programming Language. History of C  C was evolved by Dennis Ritchie at AT&T Bell Laboratories in early of 1970s  Successor of: ALGOL.
An overview of C Language. Overview of C C language is a general purpose and structured programming language developed by 'Dennis Ritchie' at AT &T's.
MAHENDRAN. Session Objectives Session Objectives  Discuss the Origin of C  Features of C  Characteristics of C  Current Uses of C  “C” Programming.
Introduction to C Programming
Evolution of C and C++ n C was developed by Dennis Ritchie at Bell Labs (early 1970s) as a systems programming language n C later evolved into a general-purpose.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
INTRODUCTION TO PROGRAMING System Development Mansoura October 2015.
Introduction to C Programming I Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 1.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Computer Science 210 Computer Organization
CSC201: Computer Programming
C Programming Hardik H. Maheta.
C Language VIVA Questions with Answers
Revision Lecture
Intro to Programming Week # 1 Hardware / Software Lecture # 2
Overview of C.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Introduction to C Programming Language
' C ' PROGRAMMING SRM-MCA.
Welcome In The World Of ‘C’.  TEXT BOOK: Programming in ANSI ‘C By: E Balagurusamy. TMH  Reference Books: 1) The ‘C Programming Language By: Kernighan.
Computer Science 210 Computer Organization
Introduction to C Programming
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Programming Fundamentals Lecture #3 Overview of Computer Programming
Introduction to Computer Programming
C programming Language
Introduction to Computer Programming
The C Language: Intro.
Computer Programming-1 CSC 111
Introduction to C Programming
Presentation transcript:

Basics of “C” Programming 1

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

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.

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.

 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

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;

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

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.

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 .

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”.

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

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

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

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

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.

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

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.

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

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

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.

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.

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

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.

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

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

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

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

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

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

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

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.

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

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;

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.

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.

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;

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:-

Jumping Statement Break Continue Goto

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; ------- }

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.

#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); } } }

Output 1 2 2 1

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 ;