1 Why C? History of C Generic C Program Structure Variables –Type and Name –Assignment Operator Simple I/O –scanf/printf Today’s Material.

Slides:



Advertisements
Similar presentations
Chapter 3: Beginning Problem Solving Concepts for the Computer Programming Computer Programming Skills /1436 Department of Computer Science.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
CSE1301 Computer Programming Lecture 4: C Primitives I.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
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.
Introduction to C Programming
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Basic Input/Output and Variables Ethan Cerami New York
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Objectives You should be able to describe: Data Types
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
C Programming Lecture 4 : Variables , Data Types
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
DATA TYPE AND DISPLAY Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
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.
CSE1301 Computer Programming Lecture 2: Introduction to C Joselito (Joey) Chua
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.
Lecture2.
Introduction to ‘c’ language
CSCE 206 Structured Programming in C
7. BASIC TYPES.
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Getting Started with C.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Introduction to the C Language
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Chapter 2 - Introduction to C Programming
UMBC CMSC 104 – Section 01, Fall 2016
Introduction to C EECS May 2019.
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Getting Started With Coding
Presentation transcript:

1 Why C? History of C Generic C Program Structure Variables –Type and Name –Assignment Operator Simple I/O –scanf/printf Today’s Material

2 Why C? Simple and StructuralSimple and Structural –Minimal syntax Availability and PortabilityAvailability and Portability –C compilers are available for a wide range of platforms Windows, UNIX, embedded systemsWindows, UNIX, embedded systems Language of choice for system programming Most OS code are written in CMost OS code are written in C Widely knownWidely known –Almost every programmer knows C –There are lots of public code written in C

3 History of C C evolved as follows:C evolved as follows: –CPL Combined Programming Language (Barron, 1963) –BCPL Basic CPL (Richards, 1969) –B (Thompson, 1970) –C (Kernighan & Ritchie, 1973) –ANSI C American National Standards Institute C (X3J11, 1989) !! The fact that a language originated in 1973 has required as little change as this one has in thirty years of heavy use is truly remarkable, and without parallels anywhere else in computer science or engineering.

4 Generic C Program Structure #include /* main designates the starting place of our program */ main() { /* Variables hold Data Items Needed in the Program */ Variable Declarations; /* Steps of our program: I/O, computation (expressions) */ Statement1; Statement2; … StatementN; } /* end-of-the program */

5 First C Program #include /* Convert fahrenheit to celsius */ main() { float fahrenheit; float celsius; printf(“Enter a temp in fahrenheit: “); scanf(“%f”, &fahrenheit); celsius = (fahrenheit-32)/1.8; printf(“%f degrees fahrenheit equals %f degrees celsius\n”, fahrenheit, celsius); } Get the fahrenheit temperature celsius = (fahrenheit-32)/1.8 Print the fahrenheit and celsius degrees Start End Prompt the user for a temperature PROBLEM Convert a fahrenheit temperature to celsius

6 First C Program Dissected #include /* Convert fahrenheit to celsius */ main() { float fahrenheit; float celsius; printf(“Enter a temp in fahrenheit: “); scanf(“%f”, &fahrenheit); celsius = (fahrenheit-32)/1.8; printf(“%f degrees fahrenheit equals %f degrees celsius\n”, fahrenheit, celsius); } Load the standard library to handle I/O Comments: In between /* …. */ main(): Designates where the execution will start Curly braces Determines the beginning and the end of a code block

7 First C Program Dissected (cont) #include /* Convert fahrenheit to celsius */ main() { float fahrenheit; float celsius; printf(“Enter a temp in fahrenheit: “); scanf(“%f”, &fahrenheit); celsius = (fahrenheit-32)/1.8; printf(“%f degrees fahrenheit equals %f degrees celsius\n”, fahrenheit, celsius); } Statements Steps of your program. Statements end with a semicolon (;) Variables A variable is a memory location whose contents can be filled and changed during program execution float is the variable type fahrenheit & celsius are variable names

8 First C Program Dissected (cont) #include /* Convert fahrenheit to celsius */ main() { float fahrenheit; float celsius; printf(“Enter a temp in fahrenheit: “); scanf(“%f”, &fahrenheit); celsius = (fahrenheit-32)/1.8; printf(“%f degrees fahrenheit equals %f degrees celsius\n”, fahrenheit, celsius); } Output Use printf function to print something on the screen Input Use scanf function to read something from the keyboard Computation Use mathematical operators to perform computation

9 From C Code-2-Machine Code Now that we have our C program, how does it get translated to machine code, i.e., to 0s and 1s –Remember a computer understands just 0s and 1s We use several system software components to translate out C program to machine code –Compiler, assembler, linker, loader+OS prog.c COMPILER ASSEMBLER LINKER LOADER + OS prog.s prog.o printf.o scanf.o prog.exe program running

10 First C Program Executed #include /* Convert fahrenheit to celsius */ main() { float fahrenheit, celsius; printf(“Enter a temp in fahrenheit: “); scanf(“%f”, &fahrenheit); celsius = (fahrenheit-32)/1.8; printf(“%f degrees fahrenheit equals %f degrees celsius\n”, fahrenheit, celsius); } /* end-main */ ? fahrenheit ? celsius DATA ? PROGRAM Enter a temp in fahrenheit: 75.4 degrees fahrenheit equals degrees celsius 75.4

11 What’s a Variable? A memory location whose contents can be filled and changed during program execution –Each variable has a type and name –Type defines the type of information that can be stored in that memory location –Name is a label that allows us to refer to that memory location Memory name number2 number1 –int number1; /* stores a natural number (152) */ –float number2; /* stores a real number (65.324) */

12 Variable Declaration The basic format for declaring variables is datatype varName, varName,... ; where datatype may be int /* Stores a natural number, e.g., */ float /* Stores a real number, e.g., */ double /* Also stores a real number, but more precision */ /* e.g., */ char /* Stores an ASCII char, e.g., ‘A’ */

13 Variable (Identifier) Names C identifiers consist of letters and digits in any order, except that: –The first character must be a letter fahrenheit, celsius, sum, a123, i1, i2, i3 –The identifier can be in lowercase or uppercase –The upper and lower cases may be mixed sum, Sum, SUM –The underscore (_) can be included and it can also be the first char of the identifier total_sum, num_students, _localVar

14 Variable (Identifier) Names (cont) C identifiers consist of letters and digits in any order, except that: –The identifiers should not contain a blank space, hyphen or quotes “sum” : quotes(“) is illegal total-sum : illegal character ( - ) total sum : blank space should not be there –Identifier names are case-sensitive sum, Sum and sUm are all different identifiers

15 Variable Examples & Assignment int x; /* Define 1 int */ float fahrenheit, celsius; /* Define 2 floats */ double d; /* Define 1 double */ x = -85; /* Assign -85 to x */ d = ; /* Assign to d */ fahrenheit = 75.0; /* Assign 75.0 to fahrenheit */ celsius = 23.4; /* Assign 23.4 to celsius */ = is the assignment operator –Syntax: identifier = value; –Changes the contents of the memory location that the variable refers to -85 x d 75.0 fahrenheit 23.4 celsius

16 Variable Declaration & Initialization int sum = 32000; /* can be positive */ int x = -23, y = 458; /* and negative */ float f1 = 34.5; float fahrenheit = 75.3, celsius = 0; double d1, d2 = ; /* Can be negative */ double d3 = 5e+3; /* Scientific notation: 5x10^3 */ d1 = 675e-4; /* 675x10^-4 = */ Variables can be initialized during declaration sum -23 x 458 y 34.5 f fahrenheit 0.0 celsius d d d3

17 Character Variables Represents a single character –Characters are letters of the alphabet (both upper and lower case) –Ten digits 0 through 9 –Special symbols such as + $., - ! Characters need to be enclosed in single quotes – e.g. 'A' char letter; letter = 'A'; /* Letter A */ letter = ‘9’; /* Digit 9 */ char letter = ‘c’; /* initialization */

18 Character Variables (more) In fact, char corresponds to 1 byte natural number –char type variables occupy 1 byte in memory What’s really stored in a char variable is the ASCII value of the character –ASCII value of ‘A’ is 65 –ASCII value of ‘B’ is 66 –ASCII value of ‘0’ is 48 –ASCII value of ‘1’ is 49 –

19 ASCII Table

20 Special Characters Characters are enclosed in single quotes –How do I denote a single quote? char letter; letter = '''; char letter; letter = '\'';  When a backslash (\) is used in front of a character, the combination is called an Escape Sequence

21 Escape Sequences Combinations of a backslash (\) and a character that tells the compiler to escape from the ways these character would normally be interpreted Commonly used escape sequences –\n move to next line –\t move to next tab setting –\\ backslash character –\' single quote –\" double quote

22 sizeof Operator int a = sizeof(char); /* Returns 1 */ int b = sizeof(short); /* Returns 2 */ int c = sizeof(int); /* Returns 4 */ int d = sizeof(long); /* Returns 4 or 8 */ int e = sizeof(float); /* Returns 4 */ int f = sizeof(double); /* Returns 8 */ You can learn the number of bytes that a certain type occupies in memory using the sizeof operator 1 a 2 b 4 c 4/8 d 4 e 8 f

23 Keyboard Input/ Display Output #include int main() { scanf(); printf(); } /* end-main */ Achieved using scanf/printf functions –Definitions of these functions are in –So we must include these definitions at the start

24 printf/scanf Functions Formatted input/output functions in C –Definitions in standard I/O library –If we are using these functions in our programs, we must include these definitions by #include at the beginning of our programs printf(FormatString, expression1, expression2, …); scanf(FormatString, &variable1, &variable2, …); FormatString is enclosed in double quotes –“abcdxyz” –“number is %d\n” Format specification %d is used to print an int

25 printf Examples int x = 45; float y = 56.7; printf(“What’s up?\n”); /* Print a msg only */ printf(“Number x: %d, 2 times y: %f\n”, x, y); printf(“Sum of x and y is %f\n”, x+y); Format section Expressionsection What’s up? Number x: 45, 2 times y: Sum of x and y is

26 printf Format Specifiers CharacterTypeOutput Format ccharA single-byte character dintSigned decimal integer ffloatSigned value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits lfdoubleSigned value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits efloat double Signed real number having the form [ – ]d.dddd e [sign]ddd

27 printf Examples printf(“First line.\nSecond line. Percent char % XYZ\n”); printf(“3rd line. Slash \\, single quote \`\n”); printf(“4th line. Double quote \”. End\n”); printf(“Line 5. char A: %c, ASCII value: %d\n”, ‘A’, ‘A’); printf(“Line 6. Tab\tTab.\n”); printf(“Line 7. Carriage return:\rOK\n”); printf(“Line 8.\n”); First line. Second line. Percent char % XYZ 3rd line. Slash \\, single quote ` 4th line. Double quote “. End Line 5. char A: A, ASCII value: 65 Line 6. TabTab. OKne 7. Carriage return: Line 8.

28 printf Examples char c1 = ‘A’, c2 = ‘c’, c3 = ‘9’, c4 = ‘\\’; int x = 11, y = 16; float f = 56.7; double d = ; printf(“c1 is, c2 is, c3:, c4: \n”, c1, c2, c3, c4); printf(“x is, y is, avg is \n”, x, y, (x+y)/2); printf(“Sum of and is \n”, f, d, f+d); c1 is, c2 is, c3:, c4: x is, y is, avg is Sum of and is

29 printf: Specifying Field Length & Justification char c1 = ‘A’, c2 = ‘c’; int x = 11, y = 16; float f = 56.7; double d = ; printf(“ \n”); printf(“|%7c|%-7c|\n”, c1, c2); printf(“|%-7d|%7d|\n”, x, y); printf(“|%7.2f|%-7.1lf|\n”, f, d); Printf(“ \n”); | A|c | |11 | 16| | 56.70|456.7 |

30 More printf Examples printf("%.9f\n", f); printf("%.19lf\n", ); float has 7 digit precision here ( ) double has 16 digits ( )

31 scanf Examples char c; int x; float y; double d; scanf(“%d”, &x); /* Read 1 int */ scanf(“%c”, &c); /* Read 1 char */ scanf(“%f”, &y); /* Read 1 float */ scanf(“%lf”, &d); /* Read 1 double */ /* Read 1 int, 1 float, 1 double */ scanf(“%d%f%lf”, &x, &y, &d); Formatsection VariableAddress “%c” char “%d” int “%f” float “%lf” double

32 getchar and putchar Functions getchar reads a single character from the keyboard putchar writes a single character on the screen Example: char c; printf(“What-to-do Menu \n”); printf(“(a) To write a C program \n”); printf(“(b) To go swimming \n”); printf(“(c) To watch TV \n”); printf(“Select one option: ”); c = getchar(); /* Read the user choice */ getchar(); /* Skip newline ‘\n’ char */ putchar(‘B’); /* Prints B on the screen */ c = ‘Z’; putchar(c); /* prints Z on the screen */