Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Chapter 2 Introduction to C Programming
Structure of a C program
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Three types of computer languages
C programming.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Computer Science 210 Computer Organization Introduction to C.
Chapter 01: Introduction to Computer Programming
COMPUTER SCIENCE I C++ INTRODUCTION
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
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.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Computer Engineering 1 nd Semester Dr. Rabie A. Ramadan 2.
 2008 Pearson Education, Inc. All rights reserved. 1 CISC 1600 – Computer Science I Fall 2010 Introduction to C++ Programming Chapters 1 and 2 (Deitel.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 1.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
© Copyright by Deitel 1 Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple.
CSCE 206 Structured Programming in C
Chapter 1: Introduction to computers and C++ Programming
Chapter 2 - Introduction to C Programming
Computer Engineering 1nd Semester
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Lecture3.
Introduction to Java Applications
Introduction to C Topics Compilation Using the gcc Compiler
Programs written in C and C++ can run on many different computers
Capitolo 1 – Introduction C++ Programming
Chapter 2 - Introduction to C Programming
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Presentation transcript:

Introduction to C Programming Overview of C Hello World program Unix environment C programming basics

Overview of C Preceded by BCPL and B Developed by Dennis Ritchie –Bell Labs in 1972 Portable (machine-independent) Used in UNIX, networks

Hello World Program /*Will ouput "Aloha!"*/Aloha #include int main(void) { printf("Aloha!\n"); return 0; }

Hello World Program /*comments*/ /*Two lines of comments*/ Ignored by the C compiler

/*no comment*/

Hello World Program #include Directive to C preprocessor # (pound sign) – line processed by preprocessor before the program is compiled –Tells preprocessor to include contents of stdio.h file in the program stdio.h - standard input/output header file –Contains information & declarations about functions –In this case, the printf library function

Hello World Program int main(void){return 0;} int – return type of function return 0; –Returns 0 to operating system –Indicates that program executed successfully main –Every program consists of 1 or more functions –Must have a main function (void) – function arguments {} – body of function = left & right brackets (block)

6 Phases to Execute C Program 1.Edit – write program & store on disk Creates file: program.c 2.Preprocess – add other files & text replacement 3.Compile – creates object (machine) code Creates file: program.o 4.Link – links code & libraries to make executable Creates file: a.out 5.Load – takes from disk & put in memory 6.Execute – CPU executes each instruction

UNIX Environment Edit program with Emacs (Vi or Pico) –emacs program.c Preprocess, Compile & Link (using the GNU C compiler) –gcc program.c (will make “a.out”) gcc program.c -o program (“program” will be the executable) Load & Execute –./a.out (./program)

Makefile You can compile & link separately –gcc –c program.c (creates “program.o”) –gcc program.o (creates “a.out”) Or gcc program.o –o program (creates “program”) Easier to do with makefile –A file that tells how to build executables from multiple files –Name of the file is “makefile” –To run this file, type “make” The command “make” finds the “makefile” and executes the commands listed in it

Creating a Makefile Makefiles consist of sets of three lines 1.Target: list of dependencies 2. Commands needed to build target 3.Blank line (newline character) Example makefile: program: program.o gcc program.o -o program program.o: program.c gcc -c program.c –Note: the last line in this file is a newline!

Running a Makefile Type “make” Should get the following output –gcc -c program.c –gcc program.o -o program Type “./program” to run executable –"." is the current directory –./program is the complete path to program

Problems with Makefile UNIX make program is unreasonably picky about the format of the make file –Must use a tab at the beginning of each command line –Must put a blank line (newline character - \n) after each group of statements, including at the end of the file –If you create it on your PC & ftp it to UNIX, the newline characters will cause problems in UNIX, so need to create with a UNIX editor

File Transfer If you are creating files on your PC & ftping to UNIX, then might get error message –warning: invalid white space character in directive Why? –UNIX newline is one character (0x0A) –PC newline is two characters 0x0Dh = carriage return 0x0Ah = line feed Solution: make sure you ftp in ASCII mode, not in binary mode

Variables int x=1, y=2, z=3; Variables declared at beginning of a function –Before any executable statements –Should initialize variables when declare them –Uninitialized variables have unpredictable values Number of bytes (= 8 bits) for each data type (Unix) –char = 1 byte (-128 to 127) –short = 2 bytes ( to 32767) –int & long = 4 bytes (-2,147,483,648 to +2,147,483,647) –float = 4 bytes (± ~ to ~10 38 ) –double = 8 bytes (± ~ to ~ )

Data Types Unsigned integers –Represents positive integers only –Example: ASCII character codes –Not necessary to indicate a sign, so all 8 or 16 bits can be used for the magnitude: unsigned char = 1 byte = 8 bits = 2 8 = 256 (0 to 255) unsigned short = 2 bytes = 16 bits = 2 16 = 65,536 (0 to 65,535) unsigned int & unsigned long = 4 bytes = 32 bits = 2 32 = 4,294,967,296 (0 to 4,294,967,295)

Data Types Signed integers –Default is signed (int = signed int) –Represents positive and negative integers –MSB (Most Significant Bit – leftmost bit) used to indicate sign 0 = positive, 1 = negative (LSB = Least Significant Bit – rightmost bit)

Variable Names Restrictions –Made up of letters & digits –1 st character must be a letter –Underscore (“_”) counts as a letter Often used in library routines –Case sensitive APPLE & apple are different variables –Less than 31 characters –Cannot use reserved keywords auto, break, case, …, void, volatile, while

Formatted Output - Printf int printf(char *format, arg1, arg2, …) –Translates internal values to character output –Returns the number of characters printed

Conversion Specifiers d –Signed decimal integer x –Unsigned hexadecimal –lowercase output for hex letters c –Single character f –Floating points –m.dddddd u –Unsigned decimal X –Unsigned hexadecimal –UPPERCASE output for hex letters % –Print a “%”

Variable Output See output.c as an example –The output for the same variable will differ based on what kind of format is specified

Arithmetic Operators 1.( ) Parenthesis (highest precedence) 2.*, /, % Multiplication, division, modulus Evaluated left to right 3.+, - Addition and subtraction Evaluated left to right

Arithmetic Operators Integer division produces an integer result –1 / 2 evaluates to 0 (0.5, but no rounding up!) –19 / 5 evaluates to 3 (3.8, but no rounding up!) Modulus (%) = remainder after division –1 % 2 evaluates to 1 (0 remainder 1) –17 % 5 evaluates to 2 (3 remainder 2) Implicit conversion – if an operation has operands of different types, the “narrower” one will be converted to the “wider” one –2.0 / 5 evaluates to 0.4 (a float)

Equality, Relational Operators Equality operators –== (equal) – common error: = instead of == –!= (not equal) Relational operators –> (greater than) –< (less than) –>= (greater than or equal) –<= (less than or equal) Will return a 0 (false) or 1(true)

if Control Structure A program can make a decision using the if control structure and the equality or relational operators /*See if.c*/if.c #include int main(void) { if(1 > 2) {printf("%d > %d\n",1,2);} if(1 <= 2) { printf("%d <= %d\n",1,2); } return 0; }

Class Exercise 1 Trace the following code, then run it to see if you get your expected output –exercise1.c

Input in C /*See input.c*/ #include int main(void){ int x = 0; printf("Enter an integer: "); scanf("%d", &x); printf("The integer is %d\n", x); return 0; }

scanf Function scanf("%d", &x); –First argument: format control string Indicates type of data to be entered –Second argument: location in memory Use an ampersand (&) to give the address where the variable is stored –The computer will wait for the user to enter a value and press the “Enter” key

Class Exercise 2 Write a program that does the following: 1.inputs two integers 2.adds them together 3.outputs the sum 4.determines if the sum is a multiple of 3 (evenly divisible by 3)