C workshop Yuli Kaplunovsky - Today - Introduction to C Recommended book: The C programming Language / Kernighan & Ritchie.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Introduction to C Creating your first C program. Writing C Programs  The programmer uses a text editor to create or modify files containing C code. 
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
C workshop #3 flow control / strings.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
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:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
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:
Prof. Béat Hirsbrunner Fulvio Frapolli, PhD Student (exercises) Bachelor students : - Major in computer science (first year, 2nd term) - Major in information.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
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.
Introduction to C Language
Computer Science 210 Computer Organization Introduction to C.
C Programming A Modern Approach
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Programming Language  C Tutorial Introduction 主講人:虞台文.
CSI2172
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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;
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP C Part III.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
Pointers. Pointer Fundamentals  When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable. –int.
Introduction to C Programming I Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
C language--Introduction. History 1970: B by Ken Thompson at AT&T Bell Lab 1972: C by Dennis Ritchie and Ken Tompson at At&T Bell Lab for UNIX 1978: “The.
Basic concepts of C++ Presented by Prof. Satyajit De
Computer Science 210 Computer Organization
CSE 220 – C Programming C Fundamentals.
INC 161 , CPE 100 Computer Programming
Programming Languages and Paradigms
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
C Short Overview Lembit Jürimägi.
Programming Paradigms
Introduction to C Programming Language
Iteration statement while do-while
B. Ramamurthy University at Buffalo
Computer Science 210 Computer Organization
B. Ramamurthy University at Buffalo
B. Ramamurthy University at Buffalo
B. Ramamurthy University at Buffalo
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
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.
B. Ramamurthy University at Buffalo
Relational, Logical, and Equality Operators
C Programming Getting started Variables Basic C operators Conditionals
Introduction to C Topics Compilation Using the gcc Compiler
Programming Language C Language.
Course Outcomes of Programming In C (PIC) (17212, C203):
Presentation transcript:

C workshop Yuli Kaplunovsky - Today - Introduction to C Recommended book: The C programming Language / Kernighan & Ritchie

My first program #include void main() { printf("Hello World!\n"); } Output: Hello World!

C structure Function oriented (‘goto’ is not recommended) First function is always called main Contains many libraries (e.g. stdio.h, stdlib.h, math.h) with many predefined functions. CaSe SeNsItIvE (e.g. ‘Main’ instead of ‘main’ won’t work) ALWAYS USE: –Indentation –Meaningful names for functions and variables –Plenty of remarks

Variables int – an integer number maximum value is 2,147,483,647 (or 2^31) minimum value is -2,147,483,648 double – real number, represented as floating point (64 bits long) char – represents a single character

Variables sample #1 #include void main() { int I,J,K; I = 10; J = 20; K = I + J; printf("K is %d\n", K); } Output: K is 30

Variable sample #2 #include void main() { double X,Y,Z; X = 10.0; Y = 20.0; Z = X / Y; printf("Z is %g\n", Z); } Output: Z is 0.5

while #include /* Print Fahrenheit-Celsius table for fahr = 0, 20,.., 300 */ void main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temerature table */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while ( fahr <= upper ) { celsius = 5 * (fahr - 32) / 9; printf("%d\t%d\n", fahr, celsius ); fahr = fahr + step; } Output:

for Syntax: for ( initialization ; condition ; do ) { block } #include void main() { int I; printf("I = "); for ( I = 0 ; I < 10 ; I++ ) printf("%d, ", I ); printf("\n"); } Output: I = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,

for - another example #include void main() { int fahr; for ( fahr = 0 ; fahr <= 300 ; fahr = fahr + 20 ) printf("%d %g\n", fahr, (5.0 / 9.0) * (fahr - 32) ); } Output:

if if ( condition ) { block #1 } else { block #2 } (optional) Example: if ( Y > X ) Z = X; else Z = Y; For comparisons use: > = == Important remark: a block should be surrounded with {} if it contains more than one command.

If - multiple conditions if ( (condition1 || condition2 ) && condition3)... Examples: if ( Y > X && Y > Z ) Z = X; int bTerminate; if ( I == 10 || bTerminate ) break; break is used to get out of for & while loops condition2 is FALSE when bTerminate is 0 and is TRUE when bTerminate is NOT 0

Functions Return-value function-name( parameters ) { … return value; } Calling the function: I = function-name( 10, 20 );

Function example #include int Add2( int A, int B ) { int C; C = A + B; return C; } void main() { int I; I = Add2( 10, 20 ); printf("Add2 function returns = %d\n", I); } Output: Add2 function returns = 30

Arrays ALWAYS start from 0 Last item is N-1 Example: int Ar[10]; Ar[0] = 22; Ar[9] = Ar[0] + 22; I = 4; Ar[I] = Ar[I+1];

#include // Guess what this program does... void main() { double X[10] = { 2, 4.2, 11.2, 3, 99.2, -23.2, 33, 11, 43, 9 }; double Y; int I, J; for ( I = 0 ; I < 9 ; I++ ) { for ( J = I+1 ; J < 10 ; J++ ) { if ( X[I] > X[J] ) { // Switch variables in array Y = X[I]; X[I] = X[J]; X[J] = Y; } // print results for ( I = 0 ; I < 10 ; I++ ) printf("%g, ", X[I]); } Output: -23.2, 2, 3, 4.2, 9, 11, 11.2, 33, 43, 99.2,