C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.

Slides:



Advertisements
Similar presentations
Chapter 7: Arrays In this chapter, you will learn about
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
ARDUINO CLUB Session 1: C & An Introduction to Linux.
Computer Programming w/ Eng. Applications
Programming and Data Structure
Lecture 2 Introduction to C Programming
Introduction to Systems Programming - Recitation Omer Kotlicki Two instances: 1.Tuesdays 15:00-16:00; Kitot Wednesdays.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
Computer Science 1620 Programming & Problem Solving.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
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.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
CSCI/CMPE 4341 Topic: Programming in Python Chapter 3: Control Structures (Part 1) – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
‘C’ LANGUAGE PRESENTATION.  C language was introduced by Dennis Ritchie..  It is a programming language, which can make a interaction between user and.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
2440: 211 Interactive Web Programming Expressions & Operators.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
1 Working with Data Types and Operators. 2 Using Variables and Constants The values stored in computer memory are called variables The values, or data,
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Principles of programming languages 5: An operational semantics of a small subset of C Department of Information Science and Engineering Isao Sasano.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 5.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
CSC 107 – Programming For Science. The Week’s Goal.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
1 Programming a Computer Lecture Ten. 2 Outline  A quick introduction to the programming language C  Introduction to software packages: Matlab for numerical.
PHP Programming with MySQL Slide 3-1 CHAPTER 3 Working with Data Types and Operators.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 1 Scott Marino.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
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.
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.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Introduction to programming in java Lecture 21 Arrays – Part 1.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
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.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
INC 161 , CPE 100 Computer Programming
© 2016 Pearson Education, Ltd. All rights reserved.
MATLAB: Structures and File I/O
Arrays, For loop While loop Do while loop
OPERATORS (2) CSC 111.
Introduction to Java, and DrJava part 1
Introduction to Java, and DrJava
SELECTIONS STATEMENTS
Introduction to Java, and DrJava part 1
DATA TYPES There are four basic data types associated with variables:
Presentation transcript:

C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

General Info on C n C is a much more powerful language than AWK. In fact, most of the programs for Unix and the Unix system itself is written in C. C is used to write large software systems, network programming, and it is very fast and efficient. However, it’s probably a more difficult language to learn and master and it takes more time to write C programs than AWK for example due to its complexity.

Data Types n C is a bit more picky about its data than AWK is. You’ll see what I mean soon. n There are 4 data types in C: integer - int floating point - float double - double character - char

Integer n These are whole numbers, both positive and negative. n int sum; /* declares an integer variable called sum. */ int num = 5; /* declares an integer variable called num and sets it equal to 5.

Float n These are numbers which contain fractional parts, both positive and negative. n float amount; /* declares a floating point called amount */ float money = 5.15 /* declares a floating point value of 5.15 to variable called money.

Double n These are exponential numbers, both positive and negative. n double bignum; /* declars a double called bignum */ double bignum = 31E+5; /* declares bignum as a double and initializes it to 31E+5 */

Char n These are single characters. n char word; /* declares a char called word */ char letter = ‘B’; /* declares a char variable called letter and assigns B to it. */ n Note: Use single quotes for single character!

Operators operation: operator: expression: value before: value after. n Multiply : * : sum = sum * 2; : 4 : 8 n Divide : / : sum = sum / 2; : 4 : 2 n Addition : + : sum = sum + 2; : 4 : 6 n Subtraction : - : sum = sum -2; : 4 : 2 n Increment : ++ : ++sum; : 4 : 5 n Decrement : -- : --sum; : 4 : 3 n Modulus : % : sum = sum % 3; : 4 : 1

Keyboard Input n Use scanf() function. See Example 3.

Relational Operators n == meaning “equal to” != meaning “not equal to” meaning “greater than” >= meaning “greater than or equal to”

for loop n The logic of the C for-loop is the same as in AWK. Let’s look at some examples… The general format is: for( start condition; continue condition; re-evaulation ) program statement; n See Example 4. n Look at Example 5 for some exercises.

while n The general format is: while( condition ) program statement; n See Example 6.

if, if-else n The general format of if: if( expression ) program statement; n The general format of if-else: n if( condition 1 ) statement1; else if( condition 2 ) statement2; else if( condition 3 ) statement3; else statement4; n See Example 7.

Compound Relationals n LOGICAL AND && Logical AND requires all conditions to evaluate as TRUE. n LOGICAL OR || Logical OR will be executed if any ONE of the conditions is TRUE. n LOGICAL NOT ! Logical NOT negates a condition. n LOGICAL EOR ^ Logical EOR will be executed if either condition is TRUE, but NOT if they are all true. n See Example 8. n Example 9 for more exercises.

Arrays n An array is a type of data structure that is used to hold multiple variables of the same data type, as mentioned last week in the AWK lecture. It is the same in C. n A simple application: Suppose a company wants to keep track of all its employees by assigning the names of each respective employee with an identification number. If one were to do the following, it would be a very tedious task. int employee1 = id1 int employee2 = id2 and so on…. n Arrays are a good solution to this problem. Arrays will create a “file cabinet” and everything in this cabinet will be of the same type. This makes storing data with arrays, in this specific problem, very practical. So you have a file cabinet with only integer variables all holding some integer value.

Array syntax n int employees[3]; employees[0] = 101; employees[1] = 232; employees[2] = 0; n To access an element in the array, use square brackets. name_of_array[i] so that name_of_array[5] refers to the sixth element in an array called name_of_array. In C, array elements start with 0. n Assigning values to an array is done by: x[10] = g; /* x = name of array, 10=index, g=value. */ n and assigning array elements to a variable is done by g = x[10]; /* g=variable name, x=array name, 10=index. */

Arrays (cont.) n See Example 10.

Multi-dimensional arrays n Multi-dimensioned arrays have two or more index values which specify the element in the array. multi[i][j] n In the above example, the first index value i specifies a row index, whilst j specifies a column index. n Example: int m1[10][10]; static int m2[2][2] = { {0,1}, {2,3} }; sum = m1[i][j] + m2[k][l]; n See Example 11 for exercises.

(cont.) n { 0, 1 }, { 2, 3 } n Remember that arrays are split up into row and columns. The first is the row, the second is the column. Looking at the values assigned to m2: m2[0][0] = 0 m2[0][1] = 1 m2[1][0] = 2 m2[1][1] = 3

An aside on printf() n %c single character %d signed decimal integer %f floating-point, decimal notation %s character string %p a pointer % print a percent sign n The format for using printf() is: printf (Control-string, item1, item2, …); n See previous examples for more on printf().

THE END