CS 251 C Programming Language Spring 2010 Instructor: Dick Lang TA: Joseph Sturtevant
Goals We will learn Read: Understand programs written in C language Write: Design and implement programs using C language Compile: Use compiler to convert C code into executable file in the UNIX environment. Execute: Run corresponding code to get results  Debug: Identify and fix syntax and semantic errors in C code . Use of Visual Studio in the Windows environment Appropriate for Technically oriented people with little or no programming experience, but with a strong mathematics backround
View of Computers From a programmer’s viewpoint Computers are tools A computer program turns raw data into meaningful information A program is the driving force behind any job that any computer does A program is a list of detailed instructions These instructions are written in a particular programming language
Your Mindset… From an engineer’s point of view: A computer/program may control a mechanism or process A computer/program may analyze data and aid in drawing conclusions or decision-making A computer can model a mechanism or process in advance of constructing it Embedded computers are components of most mechanical systems and products
Available Programming Languages Machine Languages Assembly Languages High-level Languages C/C++ COBOL (obsolete) Pascal (academic) Fortran (nearing obsolesense) Java Python Etc…
Machine Languages System of instructions and data directly understandable by a computer's central processing unit.  Example: 100011 00011 01000 00000 00001 000100   000010 00000 00000 00000 10000 000001   000000 00001 00010 00110 00000 100000   Every CPU model has its own machine code, or instruction set, although there is considerable overlap between some
Assembly Languages Human-readable notation for the machine language that a specific computer architecture uses representing elementary computer operations (translated via assemblers) Example: load hourlyRate, r1 mul workHours, r1 store  salary, r1  Even into the 1990s, the majority of console video games were written in assembly language.
High-level Languages Higher level of abstraction from machine language  Codes “similar” to everyday English  Use mathematical expressions (translated via compilers)  Example: salary = hourlyRate * workHours Make complex programming simpler => make programming more productive & reliable
Why Program using C Initial development occurred at Bell Labs in early 70’s by Ritchie, as part of Unix OS development General-purpose computer programming language  high-level assembly Simplicity and efficiency of the code Most widely used programming language Commonly used for writing system software Widely used for writing applications Hardware independent (portable, mostly) Great influence on many other popular languages (C++, Java)
Textbooks Required Programming in C (3rd Edition) by Stephen Kochan. ISBN: 0672326663. Link for the book from amazon: http://www.amazon.com/gp/product/0672326663   Recommended Reading  Absolute Beginner's Guide to C by Greg Perry. ISBN: 0672305100. Link http://www.amazon.com/gp/product/0672305100
Outline of the Course – I Introductory information C program structure Basic data types and variables declaration Arithmetic expressions and operators Control statements. Conditional statements  The while loop  The do while loop  The for loop  The if else statement The switch statement  The break statement  The continue statement
Outline of the Course – II Formatted Input and Output  Arrays and Strings Functions  Declarations  Calling Pointers  Struct(ures)  Preprocessor * Advanced Material  Debug using gdb and/or Visual Studio Binary Trees  Link Lists Recursive Functions * may be adjusted according to time and interests of students
History- I ENIAC I ( E lectrical  N umerical  I ntegrator  A nd  C alculator) John Mauchly and J Presper Eckert 500,000 dollars Thousand times faster  17,468 vacuum tubes 70,000 resistors 10,000 capacitors, etc 800 square feet floor space 30 tons 160 kilowatts of electrical power  The ENIAC 1946
History- II First home computer Mark-8 Altair  IBM 5100 Computers 1974/1975 Altair 8080 CPU 256 Byte RAM card $400 The consumer needs to put them together, make it work and write any needed software.  Paul Allen and Bill Gates develop BASIC for the Altair 8800 Mark-8 Altair
History- V Personal computer Apple II in 1977 IBM PC in 1981 Apple Macintosh in 1984  Microsoft Windows 1.0 ships in November, 1985 original IBM PC 1981
Operating System What is an OS? A program that allows you to interact with the computer -- all of the software and hardware With a command-line operating system (e.g., DOS) With a graphical user interface (GUI) operating system (e.g., Windows) Two major classes of operating systems Windows Nice interface ?, easy to learn ? Unix reliable timesharing operating system  Embedded computers use “real-time” OS’s which do not require disks…
Why choose UNIX Powerful  Multi-user operating system Good programming tools Most heavy-duty database management systems started out on Unix  Flexible   Thousands of tools that can be combined and recombined. Reliable  Unix is hard to crash. Simple things are simple in Unix…  We’ll move to Windows later in the semester.
Your First Program #include <stdio.h>  int main()  {  printf(&quot;Hello World\n&quot;);  return 0;  }  Preprocessor: interact   with input/output of your computer  You will see this at the beginning of nearly all programs Tells computer to load file named <stdio.h> <stdio.h> allows standard input/output operations
Your First Program #include <stdio.h>  int main()  {  printf(&quot;Hello World\n&quot;);  return 0;  }  Start point of the program  Preprocessor: interact   with input/output of your computer  C programs contain one or more functions, exactly one of which must be  main int  means that the function  main  will &quot;return&quot; an integer value
Your First Program #include <stdio.h>  int main()  {  printf(&quot;Hello World\n&quot;);  return 0;  }  Start point of the program  Preprocessor: interact   with input/output of your computer  Start and finish of function
Your First Program #include <stdio.h>  int main()  {  printf(&quot;Hello World\n&quot;);  return 0;  }  Printing a line of Text Start point of the program  Preprocessor: interact   with input/output of your computer  Start and finish of function
Your First Program #include <stdio.h>  int main()  {  printf(&quot;Hello World\n&quot;);  return 0;  }  Printing a line of Text Start point of the program  Preprocessor: interact   with input/output of your computer  Start and finish of function New line character
Your First Program #include <stdio.h>  int main()  {  printf(&quot;Hello World\n&quot;);  return 0;  }  Printing a line of Text Start point of the program  Preprocessor: interact with input/output of your computer  Start and finish of function Finish and return value 0 A way to exit a function It means that the program terminated normally in this case
Comments for programs Why comments are needed Good habit Readable to others Remind yourself How to comment /* … */ // … Examples
Compiler What is compiler A computer program (or set of programs) that translates text written in a computer language ( the  source code ) into another computer language (usually, an  executable file ) Why we need a compiler Available C compiler in UNIX system: gcc gcc sourcefile.c –o exefile.exe
Procedure This is your C program. Type the code in any standard text editor, and save it as helloworld.c  Type  gcc helloworld.c –o helloworld to compile helloworld.c into helloworld using the gcc compiler The gcc compiler generates the corresponding executable code named  helloworld . The computer can execute this machine readable code if you type  ./helloworld #include <stdio.h>  int main()  {  printf(&quot;Hello World\n&quot;);  return 0;  }  helloworld.c C-compiler 0011 0000 1010 0110 1100 0110 1011 0101 1010 1110 0110 1110 helloworld.exe

Introduction

  • 1.
    CS 251 CProgramming Language Spring 2010 Instructor: Dick Lang TA: Joseph Sturtevant
  • 2.
    Goals We willlearn Read: Understand programs written in C language Write: Design and implement programs using C language Compile: Use compiler to convert C code into executable file in the UNIX environment. Execute: Run corresponding code to get results Debug: Identify and fix syntax and semantic errors in C code . Use of Visual Studio in the Windows environment Appropriate for Technically oriented people with little or no programming experience, but with a strong mathematics backround
  • 3.
    View of ComputersFrom a programmer’s viewpoint Computers are tools A computer program turns raw data into meaningful information A program is the driving force behind any job that any computer does A program is a list of detailed instructions These instructions are written in a particular programming language
  • 4.
    Your Mindset… Froman engineer’s point of view: A computer/program may control a mechanism or process A computer/program may analyze data and aid in drawing conclusions or decision-making A computer can model a mechanism or process in advance of constructing it Embedded computers are components of most mechanical systems and products
  • 5.
    Available Programming LanguagesMachine Languages Assembly Languages High-level Languages C/C++ COBOL (obsolete) Pascal (academic) Fortran (nearing obsolesense) Java Python Etc…
  • 6.
    Machine Languages Systemof instructions and data directly understandable by a computer's central processing unit. Example: 100011 00011 01000 00000 00001 000100 000010 00000 00000 00000 10000 000001 000000 00001 00010 00110 00000 100000 Every CPU model has its own machine code, or instruction set, although there is considerable overlap between some
  • 7.
    Assembly Languages Human-readablenotation for the machine language that a specific computer architecture uses representing elementary computer operations (translated via assemblers) Example: load hourlyRate, r1 mul workHours, r1 store salary, r1 Even into the 1990s, the majority of console video games were written in assembly language.
  • 8.
    High-level Languages Higherlevel of abstraction from machine language Codes “similar” to everyday English Use mathematical expressions (translated via compilers) Example: salary = hourlyRate * workHours Make complex programming simpler => make programming more productive & reliable
  • 9.
    Why Program usingC Initial development occurred at Bell Labs in early 70’s by Ritchie, as part of Unix OS development General-purpose computer programming language high-level assembly Simplicity and efficiency of the code Most widely used programming language Commonly used for writing system software Widely used for writing applications Hardware independent (portable, mostly) Great influence on many other popular languages (C++, Java)
  • 10.
    Textbooks Required Programmingin C (3rd Edition) by Stephen Kochan. ISBN: 0672326663. Link for the book from amazon: http://www.amazon.com/gp/product/0672326663 Recommended Reading Absolute Beginner's Guide to C by Greg Perry. ISBN: 0672305100. Link http://www.amazon.com/gp/product/0672305100
  • 11.
    Outline of theCourse – I Introductory information C program structure Basic data types and variables declaration Arithmetic expressions and operators Control statements. Conditional statements The while loop The do while loop The for loop The if else statement The switch statement The break statement The continue statement
  • 12.
    Outline of theCourse – II Formatted Input and Output Arrays and Strings Functions Declarations Calling Pointers Struct(ures) Preprocessor * Advanced Material Debug using gdb and/or Visual Studio Binary Trees Link Lists Recursive Functions * may be adjusted according to time and interests of students
  • 13.
    History- I ENIACI ( E lectrical N umerical I ntegrator A nd C alculator) John Mauchly and J Presper Eckert 500,000 dollars Thousand times faster 17,468 vacuum tubes 70,000 resistors 10,000 capacitors, etc 800 square feet floor space 30 tons 160 kilowatts of electrical power The ENIAC 1946
  • 14.
    History- II Firsthome computer Mark-8 Altair IBM 5100 Computers 1974/1975 Altair 8080 CPU 256 Byte RAM card $400 The consumer needs to put them together, make it work and write any needed software. Paul Allen and Bill Gates develop BASIC for the Altair 8800 Mark-8 Altair
  • 15.
    History- V Personalcomputer Apple II in 1977 IBM PC in 1981 Apple Macintosh in 1984 Microsoft Windows 1.0 ships in November, 1985 original IBM PC 1981
  • 16.
    Operating System Whatis an OS? A program that allows you to interact with the computer -- all of the software and hardware With a command-line operating system (e.g., DOS) With a graphical user interface (GUI) operating system (e.g., Windows) Two major classes of operating systems Windows Nice interface ?, easy to learn ? Unix reliable timesharing operating system Embedded computers use “real-time” OS’s which do not require disks…
  • 17.
    Why choose UNIXPowerful Multi-user operating system Good programming tools Most heavy-duty database management systems started out on Unix Flexible   Thousands of tools that can be combined and recombined. Reliable Unix is hard to crash. Simple things are simple in Unix… We’ll move to Windows later in the semester.
  • 18.
    Your First Program#include <stdio.h> int main() { printf(&quot;Hello World\n&quot;); return 0; } Preprocessor: interact with input/output of your computer You will see this at the beginning of nearly all programs Tells computer to load file named <stdio.h> <stdio.h> allows standard input/output operations
  • 19.
    Your First Program#include <stdio.h> int main() { printf(&quot;Hello World\n&quot;); return 0; } Start point of the program Preprocessor: interact with input/output of your computer C programs contain one or more functions, exactly one of which must be main int means that the function main will &quot;return&quot; an integer value
  • 20.
    Your First Program#include <stdio.h> int main() { printf(&quot;Hello World\n&quot;); return 0; } Start point of the program Preprocessor: interact with input/output of your computer Start and finish of function
  • 21.
    Your First Program#include <stdio.h> int main() { printf(&quot;Hello World\n&quot;); return 0; } Printing a line of Text Start point of the program Preprocessor: interact with input/output of your computer Start and finish of function
  • 22.
    Your First Program#include <stdio.h> int main() { printf(&quot;Hello World\n&quot;); return 0; } Printing a line of Text Start point of the program Preprocessor: interact with input/output of your computer Start and finish of function New line character
  • 23.
    Your First Program#include <stdio.h> int main() { printf(&quot;Hello World\n&quot;); return 0; } Printing a line of Text Start point of the program Preprocessor: interact with input/output of your computer Start and finish of function Finish and return value 0 A way to exit a function It means that the program terminated normally in this case
  • 24.
    Comments for programsWhy comments are needed Good habit Readable to others Remind yourself How to comment /* … */ // … Examples
  • 25.
    Compiler What iscompiler A computer program (or set of programs) that translates text written in a computer language ( the source code ) into another computer language (usually, an executable file ) Why we need a compiler Available C compiler in UNIX system: gcc gcc sourcefile.c –o exefile.exe
  • 26.
    Procedure This isyour C program. Type the code in any standard text editor, and save it as helloworld.c Type gcc helloworld.c –o helloworld to compile helloworld.c into helloworld using the gcc compiler The gcc compiler generates the corresponding executable code named helloworld . The computer can execute this machine readable code if you type ./helloworld #include <stdio.h> int main() { printf(&quot;Hello World\n&quot;); return 0; } helloworld.c C-compiler 0011 0000 1010 0110 1100 0110 1011 0101 1010 1110 0110 1110 helloworld.exe