Programming Languages and Paradigms

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 7: User-Defined Functions II
Structure of a C program
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
Guide To UNIX Using Linux Third Edition
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
Computer Science 210 Computer Organization Introduction to C.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Programming Languages and Paradigms Imperative Programming.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Numbers in ‘C’ Two general categories: Integers Floats
Information and Computer Sciences University of Hawaii, Manoa
Definition of the Programming Language CPRL
Test 2 Review Outline.
‘C’ Programming Structures and Commands
The Machine Model Memory
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Computer Science 210 Computer Organization
ECE Application Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 - Introduction to C Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Functions, Part 2 of 2 Topics Functions That Return a Value
C Programming Tutorial – Part I
Programming Languages and Paradigms
ICS103 Programming in C Lecture 3: Introduction to C (2)
C Fundamentals & Pointers
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
C Short Overview Lembit Jürimägi.
Programming Paradigms
Prepared By: G.UshaRani B.Pranalini A.S.Lalitha
Introduction to C Programming Language
Chapter 2 - Introduction to C Programming
Programmazione I a.a. 2017/2018.
Programming Languages and Paradigms
Java Programming: From Problem Analysis to Program Design, 4e
11/10/2018.
Computer Science 210 Computer Organization
Chapter 3 Introduction to Classes, Objects Methods and Strings
C Stuff CS 2308.
7 Arrays.
Pointers, Dynamic Data, and Reference Types
PHP.
Classes and Objects.
C Programming Getting started Variables Basic C operators Conditionals
WEEK-2.
1-6 Midterm Review.
Programming Languages and Paradigms
Module 2 Variables, Data Types and Arithmetic
C Language B. DHIVYA 17PCA140 II MCA.
Introduction to C Programming
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

Programming Languages and Paradigms The C Programming Language

Components of a C program A C program is a collection of function definitions, (global) variable definitions, declarations and compiler directives Functions ( e.g., void push( char c ) { … } ) Variables ( e.g., int top; char Store[MAX]; ) Declarations ( e.g., void push( char c ); ) Compiler directives ( e.g., #define MAX 100 #include <stdio.h> ) 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

C program compilation Single source Multiple source prog.c => prog.o => prog.exe prog.c must contain a function definition for main() Multiple source Compile step: prog1.c => prog1.o, prog2.c => prog2.o, prog3.c => prog3.o, … Link step: prog1.o prog2.o prog3.o … => prog.exe There should be only one main() definition found in all of the source programs 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Built-in data types in C int, char, float Short and long versions of these data types short, long, double, long double, … Storage size/capacity of a variable of a given type is dependent on hardware and O/S platform No explicit boolean type in C Control structures that require conditions expect an int value 0 => false, non-zero => true 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Variable declarations Declaring a single variable: type varname; Declaring multiple variables of the same type: type varname1, varname2, … ; Variable name Rules for identifiers: sequence of letters, digits, and underscores, but start with a letter/underscore Variable scope Global: outside of functions Local: within functions 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Operators Arithmetic Logical Others + - * / % grouping: ( ) + - * / % grouping: ( ) increment: ++ -- assignment: = += -= *= /= %= Logical && || ! Others 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Input/Output Output: through the printf function printf( “hello, world” ); printf( “the price of %d calls is %5.2f pesos\n”, num_calls, num_calls*6.50 ); Input: through the scanf function int val; scanf( “%d”, &val ); Takes input from the keyboard (white spaces are separators) Type specifiers: %d – int, %f – float, %c – char, %s – string (numbers after % sign specify format) 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Composite types Arrays Structures Collection of elements of the same type Declaration: type varname[integer-value]; Structures Collection of elements of different types or with different names Declaration: struct strname { field declarations… }; struct strname varname; dot operator: varname.field 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Pointers (to be discussed in detail in another slide set) Address operator: & De-referencing operator: * Pointers and arrays Array names as addresses Pointer arithmetic Array access [] Dynamic allocation malloc, free, NULL Multi-dimensional arrays Pointers and structures: the -> operator 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Strings (to be discussed in detail in another slide set) String: sequence of characters (stored in a character array) The null (‘\0’) terminator String literals ( e.g., “hello” ) String functions (<string.h>) strcpy strlen strcmp strcat … 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Statements expression-statement block-statement Expression followed by a semicolon expr; where expr consists of variables, constants, operators, parentheses, function calls block-statement 0 or more statements enclosed in { } Variable definitions may precede statements { def def def … stmt stmt stmt … } 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Statements (decision) if-statement Condition bounded by parentheses Numeric expression expected in condition Optional else clause switch-statement Can be viewed as a special kind of block Has entry points (through the case labels) and exit points (through break; statements ) switch expression should be of ordinal type 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Statements (loops) while-statement do-while-statement for-statement Loop test at the beginning of the loop Loop body may not be executed at all do-while-statement Loop test at the end of the loop Loop body executed at least once for-statement for( expr1; expr2; expr3 ) stmt Has an equivalent while-loop formulation Used if there is an explicit loop control variable 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Statements (others) break; continue; return-statement Breaks out of the nearest enclosing loop or switch continue; Proceeds to the loop test return-statement return expr; Provides value return by a function Without expr (if return type is void), the statement simply causes control to be returned to the caller 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Functions Function definition: Return type Name Parameters Body (block) 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Parameter Passing In C, all parameters are pass by value Note: pointers and the address operator enable a function to update data outside of the function But the parameter passed (the address) is still a value Example: int val; scanf( “%d”, &val ); 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Definitions vs declarations Variables Definitions associate attributes (e.g., type and scope) and allocate space for the variable Declarations just associate attributes Functions Definitions specify signatures and specify method details (statements) Declarations just specify a method’s signature There should be only one definition, but possibly many declarations for a variable/function When are declarations needed? 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Module Organization in C Suppose mod.c is a module containing functions to be used by other C source files A mod.h file contains function prototypes and #define directives An #include directive is placed in the file using the module (i.e., #include “mod.h”). (Do you need to include it in mod.c?) Modules can be separately compiled and then linked with other modules to produce an executable 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Scope Names in a { … } block are limited to that block Local variables Formal parameters of a function are local to the function body/block Declarations outside the functions are accessible from other functions extern declaration: compiler permits use of variables before they are defined static definition: variable use restricted to functions within file 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Library Functions The C standard library I/O, math, string functions and others Prototypes are in: <stdio.h> <math.h> <string.h> <stdlib.h> … 5/14/2019 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.