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

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Introduction to Programming G51PRG University of Nottingham Revision 1
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.
C Intro.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
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?
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
‘C’ LANGUAGE PRESENTATION.  C language was introduced by Dennis Ritchie..  It is a programming language, which can make a interaction between user and.
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.
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.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Fundamental Programming: Fundamental Programming Introduction to C++
Introduction to Programming
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
C is a high level language (HLL)
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.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Numbers in ‘C’ Two general categories: Integers Floats
Computer Science 210 Computer Organization
Data types and variables
C programming language
C Basics.
Intro to Java.
Chapter 2.
Introduction to the C Language
IDENTIFIERS CSC 111.
Introduction to C++ Programming
Computers & Programming Languages
An Introduction to Java – Part I, language basics
Introduction to Primitive Data types
Chapter 2: Java Fundamentals
Sridhar Narayan Java Basics Sridhar Narayan
Java Programming Review 1
WEEK-2.
Unit 3: Variables in Java
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Primitive Data Types and Operations
Variables and Constants
Introduction to Primitive Data types
Presentation transcript:

IT 325 OPERATING SYSTEM C programming language

Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level features like complex data-structures Access to all the details of the implementation  Explicit memory management  Explicit error detection Better performance than Java All this make C a far better choice for system programming.

Goals of Tutorial Introduce basic C concepts:  Need to do more reading on your own Warn you about common mistakes:  More control in the language means more room for mistakes  C programming requires strict discipline Provide additional information to get you started  Compilation and execution

Example1 /* hello world program */ #include void main() { printf("Hello World\n"); // print to screen }

Introduction A C Program contains functions and variables. The functions specify the tasks to be performed by the program. Ex: The above program has one function called main. This function tells your program where to start running. main functions are normally kept short and calls different functions to perform the necessary sub-tasks. All C codes must have a main function.

Introduction C is case-sensitive. C also denotes the end of statement with a semi- colon like Java. The // or /* comment */ designates a comment. #include simply includes a group of functions from the filename specified by( ). Ex:above stdio.h contains a list of standard functions for C to use, the function that our program above uses is printf. Printf takes a string of characters between quotation marks, and outputs them to the screen.

Primitive Types Integer types:  char : used to represent characters or one byte data(not 16 bit like in Java)  int, short and long : versions of integer (architecture dependent)  can be signed or unsigned Floating point types: float and double like in Java. No boolean type, int or char used instead.  0 => false  ≠ 0 => true

Primitive Types Examples char c=’A’; int i=-2234; unsigned int ui=10000; float pi=3.14; double long_pi= e+1;

Arrays and Strings Arrays: /* declare and allocate space for array A */ int A[10]; for (int i=0; i<10; i++) A[i]=0; Strings: arrays of char terminated by \0 char[] name=“IT421"; name[4]=’5’;  Functions to operate on strings in string.h.  strcpy, strcmp, strcat, strstr, strchr.

printf function Syntax: printf(formating_string, param1,...) Formating string: text to be displayed containing special markers where values of parameters will be filled: %d for int %c for char %f for float %lf for double %s for string Example: printf("The number of students in %s is %d.\n", “IT421", 95);

Pointers address of variable: index of memory location where variable is stored (first location). pointer : variable containing address of another variable. type* means pointer to variable of type type. Example: int i; int* ptr_int; /* ptr_int points to some random location */ ptr_int = &i; /* ptr_int points to integer i */ (*ptr_int) = 3; /* variable pointed by ptr_int takes value 3 */ & address operator, * dereference operator. Similar to references in Java.

Pointers (cont.) Attention: dereferencing an uninitialized pointer can have arbitrary effects (including program crash). Good programming advice:  if a pointer is not initialized at declaration, initialize it with NULL, the special value for uninitialized pointer  before dereferencing a pointer check if value is NULL int* p = NULL;. if (p == NULL){ printf("Cannot dereference pointer p.\n"); exit(1); }

Common Syntax with Java Operators:  Arithmetic: +,-,*,/,% ++,--,*=,...  Relational:, =,==,!=  Logical: &&, ||, !, ? :  Bit: &,|,ˆ,!, >

Common Syntax with Java (cont.) Language constructs:  if( ){ } else { }  while( ){ }  do { } while( )  for(i=0; i<100; i++){ }  switch( ) { case 0:... }  break, continue, return No exception handling statements.

Functions Provide modularization: easier to code and debug. Code reuse. Additional power to the language: recursive functions. Arguments can be passed:  by value: a copy of the value of the parameter handed to the function  by reference: a pointer to the parameter variable is handed to the function Returned values from functions: by value or by reference

Functions – Basic Example #include int sum(int a, int b); /* function declaration or prototype */ int psum(int* pa, int* pb); void main(void){ int total=sum(2+2,5); /* call function sum with parameters 4 and 5 */ printf("The total is %d.\",total); } /* definition of function sum; has to match declaration signature */ int sum(int a, int b){ /* arguments passed by value */ return (a+b); /* return by value */ } int psum(int* pa, int* pb){ /* arguments passed by reference */ return ((*a)+(*b)); }

Why pass by reference? #include void swap(int, int); void main(void){ int num1=5, num2=10; swap(num1, num2); printf("num1=%d and num2=%d\n", num1, num2); } void swap(int n1, int n2){ /* pass by value */ int temp; temp = n1; n1 = n2; n2 = temp; } $./swaptest num1=5 and num2=10 NOTHING HAPPENED

Why pass by reference?(cont.) #include void swap(int*, int*); void main(void){ int num1=5, num2=10; int* ptr = &num1; swap(ptr, &num2); printf("num1=%d and num2=%d\n", num1, num2); } void swap(int* p1, int* p2){ /* pass by reference */ int temp; temp = *p1; (*p1) = *p2; (*p2) = temp; } $./swaptest2 num1=10 and num2=5 CORRECT NOW

Example2 #include void main() { int numpens; // declare a number variable double cost; // declare a variable that can store decimals printf("How many pens do you want: "); scanf("%d", &numpens); // get input from user cost = 0.55 * numpens; // do some math printf("\nPlease pay %f to the cashier!\n", cost); }

Variable Before a variable can be used it must be declared. Declaring a variable in C is easy, specify the type and the name for your variables. (int, double, char) Ex: double age; int number; char * name; char x;

Input & output function scanf function simply gets a value from the user. Printf takes a string of characters between quotation marks, and outputs them to the screen. Notice some special character sequences contained in both the scanf and printf :  %f, %c, %s and %d. These tell printf and scanf what type of variables to expect.  %f corresponds to double, %d is for int % s for string and %c for character.

Example3: Dealing with string #include void main() { char * name; printf("what is your name: "); scanf("%s", name); // get input from user printf(“ your name %s\n", name); }

Programs with Multiple Files File mypgm.h: void myproc(void); /* function declaration */ int mydata; /* global variable */ Usually no code goes into header files, only declarations. File mypgm.c: #include #include "myproc.h" void myproc(void){ mydata=2;... /* some code */ }

Programs with Multiple Files (cont.) File main.c: #include #include "mypgm.h" void main(void){ myproc(); } Have to compile files mpgm.c and main.c to produce object files mpgm.obj and main.obj (mpgm.o and main.o on UNIX). Have to link files mpgm.obj, main.obj and system libraries to produce executable. Compilation usually automated using nmake on Windows and make on UNIX.

Things to remember Initialize variables before using, especially pointers. Make sure the life of the pointer is smaller or equal to the life of the object it points to.  do not return local variables of functions by reference  do not dereference pointers before initialization or after deallocation C has no exceptions so have to do explicit error handling. Need to do more reading on your own and try some small programs.

Compile and run your C program under Linux The program hello.c can be compiled with the GCC as follows: gcc -o hello hello.c The -o option informs the compiler of the desired name for the executable (i.e., runnable) file that it produces. The name used in this example is hello. If the -o option is omitted, the compiler will give the name a.out to the executable file.executable./hello you run by the program typing 'hello' at the command line.