Introduction to Computer Algorithmics and Programming Ceng 113 Variables and Operators in C.

Slides:



Advertisements
Similar presentations
Modular Programming With Functions
Advertisements

Computer Programming w/ Eng. Applications
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
Structure of a C program
41 A Depth Program #include int main(void) { int inches, feet, fathoms; //declarations fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“Wreck.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
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.
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.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Computer Science 210 Computer Organization Introduction to C.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
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.
CH Programming An introduction to programming concepts.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Dennis Ritchie 1972 AT & T Bell laboratories (American Telephone and Telegraph) USA 1www.gowreeswar.com.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Introduction to Programming
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Khalid Rasheed Shaikh Computer Programming Theory 1.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
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.
Controlling Program Flow with Decision Structures.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 1 Scott Marino.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
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.
INTRODUCTION TO PROGRAMING System Development Mansoura October 2015.
From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language.
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.
User-Written Functions
Computer Science 210 Computer Organization
Chapter 2 - Introduction to C Programming
C Programming Tutorial – Part I
Lecture 13 & 14.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
C Short Overview Lembit Jürimägi.
Introduction to C Programming Language
Chapter 2 - Introduction to C Programming
Lecture2.
By: Syed Shahrukh Haider
Introduction to C Programming
Computer Science 210 Computer Organization
Chapter 2 - Introduction to C Programming
Introduction to CS Your First C Programs
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Introduction to Programming
Chapter 2 - Introduction to C Programming
Lecture3.
C Programming Getting started Variables Basic C operators Conditionals
Chapter 2 - Introduction to C Programming
DATA TYPES There are four basic data types associated with variables:
C Language B. DHIVYA 17PCA140 II MCA.
Course Outcomes of Programming In C (PIC) (17212, C203):
Introduction to C Programming
C Programming Mr. KAJAL MAJI ASSISTANT PROFESSOR DEPARTMENT OF PHYSICS
Getting Started With Coding
Presentation transcript:

Introduction to Computer Algorithmics and Programming Ceng 113 Variables and Operators in C

C Language C was developed at Bell Laboratories. (Brian Kernighan and Dennis Richie)-1972 C is known as a high low-level language.

Reference Book In Library: C The Complete Reference, Covers C++ & ANSI C Author; Herbert Schildt Press; Osborne, McGraw-Hill ISBN X E-book: The C Programming Language (ANSI C) Authors: Brian W. Kernighan, Dennis M. Ritchie

Program Definitions Variable: a named location in memory for storing data. Constant: a variable whose value is determined when a program description is written and doesn’t change from that value during program execution. Label: a named location in memory for storing a program instruction.

General Form of a C Program global declarations return-type main(parameter list) { statement sequence } return-type f1(parameter list) { statement sequence } return-type f2(parameter list) { statement sequence }.... Consist of one or more functions. “main(..)”, which is the first function called, when program execution begins.

A Sample C Program /* Letter guessing game */ #include main() {int tries = 0; char compAns, userGuess; /* Save the computer's letter */ srand(time(NULL)); /* Randomize the number generator */ compAns = (rand() %26) +65; /* generate a random letter */ printf("I am thinking of a letter...");...

A Sample C Program do { printf("What is your guess? "); scanf(" %c", &userGuess); tries++; /* Add 1 to the guess counter */ if (userGuess > compAns) { printf(" Your guess was too high \n"); printf(" \n Try again..."); } if (userGuess < compAns) { printf(" Your guess was too low \n"); printf(" \n Try again..."); } } while (userGuess != compAns); /* Quit when a match is found */ /* User got it right, announce it */ printf("*** Congratulations! You got it right! \n"); printf("It took you only %d tries to guess.", tries); return 0; }

The Library and Linking All C compilers come with a standard C library of functions that perform most commonly needed tasks. When you call a function that is not parf of your program, the C compiler “remembers” its name. Later, the “linker” combines the code you wrote with the object code already found in the standard library. This process is called linking.

Separate Compilation C allows a program to be contained in many files and lets you compile each file separately. The advantage of separate compilation is that if you change the code of one file, you don’t need to recompile the entire program.

The Terms Source Code; The text of a program that a user can read. The source code is input in to the C compiler. Object Code; Translation of the source code of a program into machine code, which the computer can read and execute directly. Object code is the input to the linker. Linker; A program links separately-compiled functions into one program.The output of the linker is an executable program. Library; The file containing the standard functions that your program may use. (all I/O operations, useful routins..) Source Code Compiler Object Code Linker Executable program

Basic Data Types Data types; char int float double Type Approximate Size in Bits Minimal Range char8-127 to 127 unsigned char80 to 255 int to unsigned int160 to signed int16same as int short int16same as int long int32-2,147,483,647 to 2,147,483,647 unsigned long int.320 to 4,294,967,295 float32Six digits of precision double64Ten digits of precision long double128Ten digits of precision Modifiers; signed unsigned long short (long float has the same meaning as double).

Variables A variable is a named location in memory that is used to hold a value that may be modified by the program. All variables must be declared before they can be used. General declaration form is; type variable_list; Example; int i, j, l; unsigned int u; double balance, profit, loss;

Declaration of Variables Variables will be declared in three basic places; 1. inside functions  local variables 2. in the definition of function parameters  formal parameters 3. outside of all functions  global variables

Sample – local variables /* Addition */ #include main() {int a, b, c; printf(“\n insert a = %d”); scanf(" %d", &a); printf(“\n insert b = %d”); scanf(" %d", &b); c = a + b; printf(“\n a + b = %d”, c); }

Sample – formal parameters /* Addition */ #include int addition(int x, int y); main() {int a, b, c; printf(“\n insert a =”); scanf(" %d", &a); printf(“\n insert b =”); scanf(" %d", &b); c = addition(a, b); printf(“\n a + b = %d”, c); } int addition(int x, int y) { int z; z = x + y; return(z); }

Sample – global parameters /* Addition */ #include int a, b, c; void addition( ); main() {printf(“\n insert a = “); scanf(" %d", &a); printf(“\n insert b = ”); scanf(" %d", &b); addition(); printf(“\n x + b = %d”, c); } void addition( ) { c = a + b; }

Local Variables void func1(void) { int x; x = 10; } void func2(void) { int x; x = -199; } void f(void) { int t; scanf(“%d”, &t); if (t==1) { char s[80]; printf(“enter name:”); gets(s);.... /* do something */ } #include “stdio.h” void f(void) void main(void) { int i; for(i=0; i<10; i++) f(); } void f(void) { int j = 10; printf(“%d”, j); }

Formal Parameters If a function is to use arguments, it must declare variables that will accept the values of the arguments. These variables are called the formal parameters of the function. /* Return 1 if c is part of string s; 0 otherwise */ is_in(char *s, char c) { while (*s) if (*s==c) return 1; else s++; return 0; }

Global Variables #include “stdio.h” int count; /* count is global */ void func1(void); void func2(void); void main(void) { count = 100; func1(); } void func1(void) { int temp; temp = count; func2(); printf(“count is %d”, count); } Global variables are known throughout the program and may be used by any piece of code. void func2(void) { int count; for (count=1; count<10; count++) putchar(‘.’); }

const Variables of type const may not be changed by your program. A const variable can be given an initial value. const int a = 10; #include “stdio.h” void sp_to_dash(const char *str); void main(void) { sp_to_dash(“this is a test”); } void sp_to_dash(const char *str) { while (*str) { if (*str==‘ ‘) printf(“%c”, ‘-’); else printf(“%c”, *str); str++; } #include “stdio.h” void sp_to_dash(const char *str); void main(void) { sp_to_dash(“this is a test”); } void sp_to_dash(const char *str) { while (*str) { if (*str==‘ ‘) *str=‘-’; printf(“%c”, *str); str++; }

Operators The assignment operator; variable_name = expression; The target, or left part of the assignment must be a variable or a pointer, not a function or a constant. Multiple assignment ; x = y = z = 0; The conversion assignment; When variables of one type are mixed with variables of another type, a type conversion will occur. int x; char ch; float f; void func(void) { ch = x; x = f; f = ch; f = x; }

Common type conversion (Samples) Target TypeExpression TypePosssible info loss charshort intHigh order 8 bits charintHigh order 8 bits intlong intHigh order 16 bits intfloatFractional part floatdoublePrecision, result rounded

Arithmetic Operators OperatorAction -Subtraction +Addition *Multiplication /Division %Modulus --Decrement ++Increment

Relational or Logical Operators Relational Operators OperatorAction >Greater than <Less than >=Greater than or equal <=Less than or equal ==Equal !=Not Equal Logical Operators OperatorAction &&AND ||OR !NOT In C, true is any value other than zero. False is zero.

Logical Operators AND p qp&&q OR p qp || q NOT p!p 01 10

Sample for Logical Operations #include “stdio.h” int xor(int a, int b); void main(void) {printf(“%d”, xor(1, 0)); printf(“%d”, xor(1, 1)); printf(“%d”, xor(0, 1)); printf(“%d”, xor(0, 0)); } /*perform logical XOR operation using the two argumants. */ int xor(int a, int b); { return (a||b)&& !(a&&b); } aba XOR b

Lab. Exercise #1 Write a program that implements SWAP operation for two integer variables –with using a temp space, –without using a temp space.

Next Course Program Control Statements Selection –if and switch Iteration –while, for, do-while Jump –break, continue, goto, return Label Expression Block –A block begins with a { and ends with a }.