Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Introduction to C Programming
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Lecture 2 Introduction to C Programming
Introduction to C Programming
Introduction to C Programming
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
Introduction to C Systems Programming. Systems Programming: Introduction to C 2 Systems Programming: 2 Introduction to C  A ‘C’ Program –Variable Declarations.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Introduction to C Programming
Basic Elements of C++ Chapter 2.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
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.
Introduction to Programming
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
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.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Chapter Topics The Basics of a C++ Program Data Types
Computer Science 210 Computer Organization
ECE Application Programming
Week 3 - Friday CS222.
- Standard C Statements
Chapter 2 - Introduction to C Programming
Basic Elements of C++.
Revision Lecture
Chapter 2 - Introduction to C Programming
Basic Elements of C++ Chapter 2.
Computer Science 210 Computer Organization
Introduction to the C Language
Introduction to the C Language
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Programming Assignment #1 12-Month Calendar—
Chapter 2 - Introduction to C Programming
Differences between Java and C
Conversion Check your class notes and given examples at class.
Chapter 2 - Introduction to C Programming
Dale Roberts, Lecturer IUPUI
EECE.2160 ECE Application Programming
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Presentation transcript:

Introduction to C Systems Programming Concepts

Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and Running a C Program Compiling and Running a C Program Sizeof Program Sizeof Program –#include What is True in C? What is True in C? –if example Another C Program Another C Program –#define –scanf ( ) Systems Programming Introduction to C 2

Introduction to C Another C Program (continued) Another C Program (continued) –for loop –Promotion Other C topics Other C topics –Increment and Decrement Operators –Casting –Operator Precedence –Value of Assignment Operator Systems Programming Introduction to C 3

Variables Variable names correspond to memory locations in memory. Every variable has a type, a name and a value. int i; i i = 4; ( the address of i ) &i 4 Systems Programming Introduction to C 4

printf Two components of printf statement: – –Formatting template {within quotes} – –Argument list – variables separated by commas. int main() { … printf(%d %c\n, i, ch); } Systems Programming Introduction to C 5

printf Formatting template: Argument list matches up with % Some of the argument types: – –%d integers – –%f floating-point numbers – –%c characters int main() { … printf(%d %f %c\n, i, fvar, ch); } Systems Programming Introduction to C 6

printf Width of variable printing: %4d – decimal integers at least 4 digits wide %5f – floating point at least 5 digits wide %6.2f – floating point at least 6 digits wide with at least 2 after the decimal point int main() { … printf(%4d %5f %6.2f\n, i, fvar, f2var); } Systems Programming Introduction to C 7

A Simple C Program /* Example of a simple C Program */ #include int main() { i int i; char c, s; i = 2303; s c = 'C'; s = 'S'; printf("\nHello"); printf(" %c%c %d Students!!\n", c, s, i); return 0; } 2303 C S c Type declarations specify memory sizes Comments in C Systems Programming Introduction to C 8

Compiling and Running simple %ls simple.c %gcc simple.c %ls a.out simple.c %./a.out Hello CS 2303 Students!! % Alternate Version %ls simple.c %gcc –o simple simple.c %ls simple simple.c %./simple Systems Programming Introduction to C 9

sizeof Operator Figure 7.17 (part 1) preprocessor directive Systems Programming Introduction to C 10

sizeof Operator Figure 7.17 (part 2) char 1 short 2 int 4 long 4 long long 8 float 4 double 8 long double 12 from typelen.c Systems Programming Introduction to C 11

Conditional Testing for True /* check to see what conditional does with negative integers */ int main () { int i = 0; // zero is the only value for false in C if (i) printf("%d = true\n", i); else printf("%d = false\n", i); i = 4; if (i) printf("Positive integer %d = true\n", i); else printf("Positive integer %d = false\n", i); i = -4; if (i) printf("Negative integer %d = true\n", i); else printf("Negative integer %d = false\n", i); return 0; } $./a.out 0 = false Positive integer 4 = true Negative integer -4 = true Systems Programming Introduction to C 12

#define SIZE 5 int main () { int i, start, finish; float celsius; scanf("%d", &start); finish = start + SIZE; for (i=start; i<finish; i++) { celsius = (5.0/9.0)* (i ); printf("%3d %6.1f\n", i, celsius); } return 0; } Another C Program preprocessor directive scanf needs the address use of define Systems Programming Introduction to C 13

#define SIZE 5 int main () { int i, start, finish; float celsius; scanf("%d", &start); finish = start + SIZE; for (i=start; i<finish; i++) { celsius = (5.0/9.0)* (i ); printf("%3d %6.1f\n", i, celsius); } return 0; } Another C Program initial value continue to loop if True after each interation Systems Programming Introduction to C 14

#define SIZE 5 int main () { int i, start, finish; float celsius; scanf("%d", &start); finish = start + SIZE; for (i=start; i<finish; i++) { celsius = (5.0/9.0)* (i ); printf("%3d %6.1f\n", i, celsius); } return 0; } Another C Program $./a.out example of promotion Systems Programming Introduction to C 15

Other C Topics Increment and decrement operators Casting operator ( type ) Operator precedence Danger :: the value of the assignment operator Variable scope Switch Conditional operator ?: Systems Programming Introduction to C 16 Will cover later !!

Increment and Decrement Operators Fig Increment and decrement operators Systems Programming Introduction to C 17

Casting Cast is a unary operator. Cast is often useful when an iteration index is used in mixed type arithmetic. Later, it will be important to make sure arguments passed are properly matched between called and calling routines. Example: int total, count; float average; … average = (float) total / count; When in doubt, be conservative and use cast to be sure! Systems Programming Introduction to C 18

Fig 4.16 Operator Precedence cast arithmetic boolean logical Systems Programming Introduction to C 19

Value of Assignment The value of assignment is the same as the contents deposited into the variable type on the left. Note: There are several potential dangers here – especially when the programmer creates new types!! Examples (for now): if ( i = 0 ) if ( i = 4 ) What is the problem ?? if ( i == 0) if ( i == 4) Systems Programming Introduction to C 20

Review of Introduction to C This presentation covers many important C topics quickly including: – –Declaration of variable types memory allocation by type The address of a variable & –printf ( ), scanf ( ) –C arithmetic (operators, precedence, casting, promotion, assignment value) –C booleans (true and false) –if –Preprocessor directives #define, #include#define, #include –for You are now ready to due lab 1 and once we cover functions everyone should be able to due Program 1. Systems Programming Introduction to C 21