CSI2172

Slides:



Advertisements
Similar presentations
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Advertisements

C Intro.
Lecture 1 Introduction to Java Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Hello World Program The source code #include int main() { printf("Hello World\n"); return(0); }
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Structure of a C program
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Java Syntax Primitive data types Operators Control statements.
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?
Please pick up an attendance question and submit in 5 minutes CS 1003 Lecture #3 Sept 12, 2006 Knarig Arabshian.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
COMP2004 Programming Practice Sam Holden Department of Computer Science University of Sydney.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
The little language that could Remember C is a “small language”
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.
CS150 Introduction to Computer Science 1
Chapter 2: C Fundamentals Dr. Ameer Ali. Overview C Character set Identifiers and Keywords Data Types Constants Variables and Arrays Declarations Expressions.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
CS 161 Introduction to Programming and Problem Solving Chapter Hello World Program in C++ And Lexical Rules Herbert G. Mayer, PSU Status 10/18/2014.
C Programming Lecture 4 : Variables , Data Types
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
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.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Character Set It is set of Characters/digits/symbol which is valid in C++. Example – A-Z, (white space) C++ Character Set It is set of.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Intro to Programming in C. Motivation for Using C  High level, compiler language  Efficiency over user-friendliness  Allows programmer greater freedom,
5. Arrays, Pointers and Strings 7 th September IIT Kanpur C Course, Programming club, Fall
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
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.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Matlab Data types, input and output. Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64.
Minimal standard C program int main(void) { return 0 ; }
CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Objects Variables and Constants. Our Scuba Problem #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
C is a high level language (HLL)
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
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:
C++ Lesson 1.
Introduction to ‘c’ language
C/C++ Tutorial.
A bit of C programming Lecture 3 Uli Raich.
LESSON 3 IO, Variables and Operators
Wel come.
TMF1414 Introduction to Programming
5. Arrays, Pointers and Strings
C++ Basics.
פרטים נוספים בסילבוס של הקורס
Introduction to Java Programming
Basics of ‘C’.
An overview of Java, Data types and variables
פרטים נוספים בסילבוס של הקורס
Govt. Polytechnic,Dhangar
Variables in C Declaring , Naming, and Using Variables.
Your questions from last session
C Programming Getting started Variables Basic C operators Conditionals
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Fundamental Programming
Module 2 Variables, Data Types and Arithmetic
Section 6 Primitive Data Types
Presentation transcript:

CSI2172

Bjarne: Bjarne Stroustrup is the guy who invented C++ C++ means: “a C incremented”. It’s a clever way to say “C 2.0” If it works in C, it’s easy to make it work in C++

#include /* my first C program */ int main() { printf("Hello World\n"); return 0; } Start with C Library Function result. Typed Output Result

#include double func1(char, int); int func2(double, float); int main() { printf(“Value is %d”, func2(func1(‘a’, 2), 5.2f); return 0; } double func1(char a, int b){return a+b;} int func2(double a, float b){return a-b;}

The Tao of main() E There is one and only one main(…) E main(…) can be anywhere E All program begin where main(…) begins and end where main(…) ends E main(…) also returns

Variables: int a, b = 2; char x, y = 'a'; unsigned long int i1 = 0, i2 = 3; double pi = 3.14; double d = 5*pi;

Types : char int float double E char: 1 byte E int: signed integer E float: real number. 6 digits after decimal. E double: real number. 10 digits after decimal short, long signed, unsigned  Exemples: char (signed char), unsigned char, short int (signed short int), unsigned short int, int (signed int), unsigned int, long int (signed long int), unsigned long int, float, double, long double

More than you want to know about the int type int (default: signed short) short: 2 bytes (16 bits) signed: to unsigned: 0 to long: 4 bytes (32 bit) signed: to unsigned: 0 to In general: sizeof(short) <= sizeof(int) <= sizeof(long)

Some of my favorite chars char (default: signed) signed: -128 to 127 unsigned: 0 to 255 C '\a' keyboard bell C '\\' back slash C '\b' back space C '\?' question mark C '\f' form feed C '\n' new line C '\"' double quote C '\r' carriage return C '\t' horizontal tab C '\v' vertical tab C '\ooo' octal byte C '\xhh' hex byte C '\0' null byte

array int a[12]; //Array of 12 int char a[]; //Array of char, sized on initialization a = “hjah”; //” = short hand for NULL terminated array of chars, 5 elements float a[5][4]; //Matrix. Array of pointers Arrays are indexed from 0 to n-1 where n is the number of elements in the array /*INITIALISATION D'1 TABLEAU */ #define ROW 2 #define COLUMN 3 int main(){ int n[100], ctr; for(ctr = 0; ctr < 99; ctr++){ n[ctr] = 0; } int w[5] = {32, 21, 56, 32, 45}; char a[] = "Hello"; char b[6] = {'H','e','l','l','o','\0'}; int mat[ROW][COLUMN] = {{1,2}, {2,6}, {5,7}}; mat[0][2] = 3; }

strings H "a\tb" --> ab H "abcd\b\bx" --> abx H "\"hello world\"" --> "hello world" H "I don\'t know" --> I don't know H "hello\nworld" --> hello world H "\a" --> (rings the keyboard bell) There isn’t any string type in C. Strings are just arrays of char. Then there is just a bit of built-in syntactic sugar and some conventions. char [ ]; Examples: char name[20]; char firstName[20]; char sentence[300]; The last symbol is always '\0' (NULL). So for n characters, you need n+1 bytes.

/* Class example. Create an array of 11 chars, fill it with letters from ‘b’ to ‘j’ and print it to the output as a string */ char a[11]; a[10] = 'a'-'a'; for(int ctr=0; ctr < 10; ctr++){ a[ctr] = 'b'+ ctr; } printf("%s", a);

Decision,decision… if (...) stmt if (...) stmt1 else stmt2 if (...) { body } else { body } if (1)... true if (2)... true if (-1.5)... true if (0)... false int x = 2, y; if (x < 3) y = 5; else y = 4; The odd guy: cond ? e1 : e double x = n % 2 == 0 ? 4.3 : -2.3;

Branching(++): int x = 0, y = 0; switch(n) { case 1: case 2: x = 2; case 3: y = 3; break; case 4: x = 1; y = 4; case 5: x = 2; break; default: y = -1; }; nx y >5 0 -1

Repeat after me: for(e1;e2;e3)... int f=1, i; for(i=2; i<=n; ++i) f = f *i; for(;;)... while(cond)... int f=1, i=2; while(i <= n) f = f * i++; do body while(cond); int f=1, i = 1; do { f = f * i++; } while(i <= n);

Take me outta here break & continue int i, r = 0; for(i=0; i<n; ++i) { if (r % 2 == 0) continue; r += i; } int choice = 0; while(1) { choice = user_input(); if (choice 4) break; switch(choice) { case 1:...} goto(rare) void f() {... for(... ) { while(... ) {... if (wrong) goto error;... } for(... ) { if (wrong) goto error;... } }... error:...