C programming language

Slides:



Advertisements
Similar presentations
#include void main() { float x = 1.66, y = 1.75; printf(%f%f,ceil(x), floor(y)); }
Advertisements

Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Computer Programming w/ Eng. Applications
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Introduction to Programming Lecture 39. Copy Constructor.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
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.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Kernighan/Ritchie: Kelley/Pohl:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
Chapter 11-12, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
C Programming language
Introduction to Programming
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
Computer Programming for Engineers
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define,
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
CS 108 Computing Fundamentals Notes for Thursday, February 18, 2016.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
1 C++ Programming C++ Basics ● C++ Program ● Variables, objects, types ● Functions ● Namespace ● Tests ● Loops ● Pointers, references.
Computer Organization and Design Pointers, Arrays and Strings in C
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Chapter 8 Arrays, Strings and Pointers
User Interaction and Variables
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
A bit of C programming Lecture 3 Uli Raich.
ECE Application Programming
INC 161 , CPE 100 Computer Programming
C Programming Tutorial – Part I
C++, OBJECT ORIENTED PROGRAMMING
Getting Started with C.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Student Book An Introduction
void Pointers Lesson xx
Andy Wang Object Oriented Programming in C++ COP 3330
Basic notes on pointers in C
Computer Science 210 Computer Organization
Dynamic Memory Allocation
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
CS 2308 Exam I Review.
Topics discussed in this section:
Programming Assignment #1 12-Month Calendar—
Differences between Java and C
Pointers Pointers point to memory locations
Fundamental Programming
Exercise Arrays.
Chapter 9: Pointers and String
Programming in C Pointer Basics.
Course Outcomes of Programming In C (PIC) (17212, C203):
C Programming Mr. KAJAL MAJI ASSISTANT PROFESSOR DEPARTMENT OF PHYSICS
Presentation transcript:

C programming language CS 210 Tutorial 10 Studwww.cs.auckland.ac.nz/ ~mngu012/public.html/210/10/

Materials from: http://gd.tuwien.ac.at/languages/c/programming-dmarshall/ http://gd.tuwien.ac.at/languages/c/programming-dmarshall/chapter2_22.html

Simple C program main() {   printf( “I Like C \n” ); exit ( 0 );   }

Simple input output Output: printf Input: scanf Check simpleInputOutput.c C uses formatted output. The printf/scanf function has a special formatting character (%) -- a character following this defines a certain format for a variable: %c -- characters %d -- integers %f -- floats     e.g. printf(“%c %d %f”,ch,i,x);

Variable types Similar to Java and C++ Declaration: Int abc; Const int abc; or int const abc; Operations are like JAVA: +,-,*,/,<,>,=,…

Conditionals 3 ways: if else, ?, switch If else and switch are the same as Java ?: z = (a>b) ? a : b; which is the same as: if (a>b) z = a; else z=b;

Loops For loop While loop Do while loop While (expression) for (expression1; expression2; expression3) {statements;} While loop While (expression) Do while loop do statement; while (expression);

Arrays and Strings int listofnumbers[50]; thirdnumber=listofnumbers[2]; listofnumbers[5]=100; int tableofnumbers[50][50];

Arrays and Strings Strings In C Strings are defined as arrays of characters. For example, the following defines a string of 50 characters: char name[50]; C has no string handling facilities built in and so the following are all illegal:   char firstname[50],lastname[50],fullname[100];   firstname= "Arnold"; /* Illegal */ lastname= "Schwarznegger"; /* Illegal */ fullname= "Mr"+firstname +lastname; /* Illegal */ However, there is a special library of string handling routines which we will come across later. To print a string we use printf with a special %s control character:    printf(``%s'',name);

Read and print String char string[80]; scanf("%s",string); printf(string); printf("\n");

Functions float findaverage(float a, float b) { float average;   > average=(a+b)/2; return(average); } void squares() int loop;   for (loop=1;loop<10;loop++) printf("%d n",loop*loop);

Pointers Pointers Pointer are a fundamental part of C. If you cannot use pointers properly then you have basically lost all the power and flexibility that C allows. The secret to C is in its use of pointers. C uses pointers a lot. Why?: It is the only way to express some computations. It produces compact and efficient code. It provides a very powerful tool. C uses pointers explicitly with: Arrays, Structures, Functions. NOTE: Pointers are perhaps the most difficult part of C to understand. C's implementation is slightly different DIFFERENT from other languages.

What is a Pointer? A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type. The unary or monadic operator & gives the ``address of a variable''. The indirection or dereference operator * gives the ``contents of an object pointed to by a pointer''. To declare a pointer to a variable do:    int *pointer;

Example int x = 1, y = 2; int *ip;   ip = &x;   y = *ip;   x = ip;   *ip = 3;

Explanation Pointer, Variables and Memory Now the assignments x = 1 and y = 2 obviously load these values into the variables. ip is declared to be a pointer to an integer and is assigned to the address of x (&x). So ip gets loaded with the value 100. Next y gets assigned to the contents of ip. In this example ip currently points to memory location 200 -- the location of x. So y gets assigned to the values of x -- which is 1. We have already seen that C is not too fussy about assigning values of different type. Thus it is perfectly legal (although not all that common) to assign the current value of ip to x. The value of ip at this instant is 100. Finally we can assign a value to the contents of a pointer (*ip). More on POINTERS check pointers.pdf