The C Language: Intro.

Slides:



Advertisements
Similar presentations
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Advertisements

11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Structure of a C program
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
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?
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
C programming Language and Data Structure For DIT Students.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
Basics of “C” Programming
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Programming With C.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Introduction to Programming
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
MAHENDRAN. Session Objectives Session Objectives  Discuss the Origin of C  Features of C  Characteristics of C  Current Uses of C  “C” Programming.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Introduction to C Programming I Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language.
Bill Tucker Austin Community College COSC 1315
Introduction to ‘c’ language
Chapter 2: Basic Elements of C++
Computer Organization and Design Pointers, Arrays and Strings in C
CSCE 206 Structured Programming in C
The Machine Model Memory
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Data types Data types Basic types
Chapter 12 Variables and Operators
BASIC ELEMENTS OF A COMPUTER PROGRAM
Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update.
C Language VIVA Questions with Answers
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Data types and variables
Introduction to C++.
Getting Started with C.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
C Basics.
Introduction to C Programming Language
' C ' PROGRAMMING SRM-MCA.
The C Language Prof. Stephen A. Edwards.
Introduction to C Programming
INTRODUCTION c is a general purpose language which is very closely associated with UNIX for which it was developed in Bell Laboratories. Most of the programs.
Chapter 12 Variables and Operators
Chapter 10 Programming Fundamentals with JavaScript
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Basics of ‘C’.
Introduction to C Programming
Govt. Polytechnic,Dhangar
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.
Chapter # 2 Part 2 Programs And data
C programming Language
C – Programming Language
Module 2 Variables, Data Types and Arithmetic
Session 1 – Introduction to Computer and Algorithm (Part 2)‏
C Language B. DHIVYA 17PCA140 II MCA.
INTRODUCTION TO C.
PRINCIPLES OF PROGRAMMING ADAM.S. ALGORITHM Algorithm is the first step in problem solving. Every problem has some given information and some result has.
Presentation transcript:

The C Language: Intro

The C Language Currently, the most commonly-used language for general purpose as well as embedded systems “High-level assembly” Very portable: compilers exist for virtually every processor Easy-to-understand compilation Produces efficient code Fairly concise

C History Developed between 1969 and 1973 along with Unix Due mostly to Dennis Ritchie Designed for systems programming Operating systems Utility programs Compilers Evolved from B, which evolved from BCPL

BCPL Designed by Martin Richards (Cambridge) in 1967 Typeless Everything an n-bit integer (a machine word) Pointers (addresses) and integers identical Memory is an undifferentiated array of words Natural model for word-addressed machines Local variables depend on frame-pointer-relative addressing: dynamically-sized automatic objects not permitted Strings awkward Routines expand and pack bytes to/from word arrays

C History Original machine (DEC PDP-11) was very small 24K bytes of memory, 12K used for operating system Written when computers were big, capital equipment Group would get one, develop new language, OS

C Strengths Efficiency Portability Power Flexibility Standard library Limited amount of memory Fast Portability Compilers are small and easily written C: UNIX and ANSI/ISO standard Power Flexibility Standard library Input/output, string handling, storage allocation, etc. Integration with UNIX

C Weakness Can be error-prone Flexibility C compiler doesn’t detect many programming mistakes Pitfalls Can be difficult to understand Some features are easier to be misused Can be difficult to modify No modules

Structure of C program

Hello World in C #include <stdio.h> void main() { Preprocessor used to share information among source files Clumsy + Cheaply implemented + Very flexible #include <stdio.h> void main() { printf(“Hello, world!\n”); }

Hello World in C #include <stdio.h> void main() { Program mostly a collection of functions “main” function special: the entry point “void” qualifier indicates function does not return anything #include <stdio.h> void main() { printf(“Hello, world!\n”); } I/O performed by a library function: not included in the language

Euclid’s algorithm in C “New Style” function declaration lists number and type of arguments Originally only listed return type. Generated code did not care how many arguments were actually passed. Arguments are call-by-value int gcd(int m, int n) { int r; while ( (r = m % n) != 0) { m = n; n = r; } return n;

Euclid’s algorithm in C int gcd(int m, int n) { int r; while ( (r = m % n) != 0) { m = n; n = r; } return n; Automatic variable Storage allocated on stack when function entered, released when it returns. All parameters, automatic variables accessed w.r.t. frame pointer. Extra storage needed while evaluating large expressions also placed on the stack Excess arguments simply ignored n m Frame pointer ret. addr. Stack pointer r

Euclid’s algorithm in C int gcd(int m, int n) { int r; while ( (r = m % n) != 0) { m = n; n = r; } return n; Expression: C’s basic type of statement. Arithmetic and logical Assignment (=) returns a value, so can be used in expressions % is remainder != is not equal

Euclid’s algorithm in C int gcd(int m, int n) { int r; while ( (r = m % n) != 0) { m = n; n = r; } return n; High-level control-flow statement. Ultimately becomes a conditional branch. Supports “structured programming” Each function returns a single value, usually an integer. Returned through a specific register by convention.

C tokens Basic Building Blocks Constructed together to write C program Types Keywords               (eg: int, while), Identifiers               (eg: main, total), Constants              (eg: 10, 20), Variable (eg: int x) Operators              (eg: +, /,-,*) Strings                    (eg: “total”, “hello”), Special symbols  (eg: (), {}),

C Token example void main() { int x, y, total; x = 10, y = 20; total = x + y; printf ("Total = %d \n", total); } where, main – identifier {,}, (,) – delimiter int – keyword x, y, total – identifier + = - Operator main, {, }, (, ), int, x, y, total – tokens

Keywords Keywords are pre-defined words in a C compiler. Each keyword is meant to perform a specific function in a C program. Since keywords are referred names for compiler, they can’t be used as variable name. Supports 32 keywords Eg: auto, int, char, float, if, else, for, do, while, break, continue etc

Identifier Each program elements in a C program are given a name called identifiers. Names given to identify Variables, functions and arrays are examples for identifiers. eg. x is a name given to integer variable in above program. RULES First character should be an alphabet or underscore. Succeeding characters might be digits or letter. Punctuation and special characters aren’t allowed except underscore. Identifiers should not be keywords.

Constant C Constants are also like normal variables. But, only difference is, their values can not be modified by the program once they are defined. Constants refer to fixed values. They are also called as literals Constants may be belonging to any of the data type. Syntax: const data_type variable_name; (or) const data_type *variable_name; OR #define pi 3.14

Types of C constant Integer constants Real or Floating point constants Octal & Hexadecimal constants Character constants String constants Backslash character constants

C variables C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable. The value of the C variable may get change in the program. C variable might be belonging to any of the data type like int, float, char etc.

Rules For Naming C Variable Variable name must begin with letter or underscore. Variables are case sensitive They can be constructed with digits, letters. No special symbols are allowed other than underscore. sum, height, _value are some examples for variable name Three types: Local Global Environment

Example

Operator and Expressions The symbols which are used to perform logical and mathematical operations in a C program are called C operators. These C operators join individual constants and variables to form expressions. Operators, functions, constants and variables are combined together to form expressions. Consider the expression A + B * 5. where, +, * are operators, A, B  are variables, 5 is constant and A + B * 5 is an expression.

Types of C operator Arithmetic operators Assignment operators Relational operators Logical operators Bit wise operators Conditional operators (ternary operators) Increment/decrement operators Special operators

Special Symbol The following special symbols are used in C having some special meaning and thus, cannot be used for some other purpose. [] () {} , ; : * … = # Braces{}: These opening and ending curly braces marks the start and end of a block of code containing more than one executable statement. Parentheses(): These special symbols are used to indicate function calls and function parameters. Brackets[]: Opening and closing brackets are used as array element reference. These indicate single and multidimensional subscripts.