A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Introduction to C Programming
Introduction to C Programming
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
TDBA66, VT-03 Lecture - Ch. 21 A complete C-program Display Fig. 2.1 and comment on different things such as Preprocessor directives Header files Identifiers.
1 CS 201 Introduction to C (1) Debzani Deb. 2 Outline Overview of C General form of a C program C Language Elements.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Structure of a C program
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.
Topic 13 – Various Other Topics. Enumerated Types.
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
1 CS 201 Introduction to C (2) Debzani Deb. 2 Overview C Arithmetic Expressions Formatting Numbers in Program Output Interactive Mode, Batch Mode, and.
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Introduction to C Programming
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
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.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
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.
Chapter 2 Overview of C Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Pengantar C/C++. 2 Outline  C overview  C language elements  Variable declarations and data types  Executable statements  General form of a C program.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
#include int main(void) { printf("Hello, world!\n"); return 0; } entry point called on program start only one main( ) in any program # for preprocessor.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Introduction to Programming
ELE118 Introduction to Programming
CC112 Structured Programming Lecture 2 1 Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer.
Chapter 3 Input and Output
Khalid Rasheed Shaikh Computer Programming Theory 1.
Overview of C. C—a high-level programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories. We will discuss: –the elements of a.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 ELE118 lecture 3 Dr. Mehmet Demirer Dr. Seniha Esen Yuksel.
CISC105 – General Computer Science Class 2 – 6/7/2006.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
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.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
Lecture2.
Numbers in ‘C’ Two general categories: Integers Floats
Lesson #2 Introduction to C.
Lesson #2 Introduction to C.
Input and Output: I/O Finish reading chapters 1 and 2 of the text
Basic Elements of C++.
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
Chapter 2 Overview of C.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Lecture2.
Basic Elements of C++ Chapter 2.
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.
INPUT & OUTPUT scanf & printf.
CS111 Computer Programming
Lecture3.
Lesson #2 Introduction to C.
WEEK-2.
An Overview of C.
Variables in C Topics Naming Variables Declaring Variables
ICS103: Programming in C 6: Pointers and Modular Programming
Computer Science II CS132/601* Lecture #C-1.
Presentation transcript:

A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive Mode, Batch Mode and Program Redirection

A.Abhari CPS1252 C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library

A.Abhari CPS1253 C Language Elements Preprocessor Directives Constant Macros Program does not change their values # define Name value #define KILOS_PER_POUND #define MILES_PER_KM #define PI

A.Abhari CPS1254 C Language Elements Function main Every C program has main function Main function has heading and body {} A function body has declarations and executable statements int main(void) { printf("This is a C program\n"); return (0); } The control returns back to OS Program was executed without error

A.Abhari CPS1255 C Language Elements Reserved Words They have special meaning and can not be used for something else All are lowercase some of the reserved words are: void case switch return for signed else long if do int while

A.Abhari CPS1256 C Language Elements Identifiers Standard identifiers: For example scanf or printf are the names of defined operations User-defined identifiers (should begin with a letter and only can contain digit or _ ) Invalid Valid 1rate rate1 int Rate1 joe’s joe Uppercase & lowercase User-defined identifiers are considered different identifiers by C compiler Meaningful name should be used for User- defined identifiers It is a reserved word Different identifiers

A.Abhari CPS1257 C Language Elements Variable and data types Variables are the name (identifiers) for memory cells that can be changed It should start with a data type: int count; double x,y,z; char first_initial;

A.Abhari CPS1258 C Language Elements Data types int: integer between to double: for real numbers 3.14, e5, 1.23E5= 1.23 * 10 to the power e-4 = char : for showing individual character ‘A’, ‘a’, ‘ ’,...

A.Abhari CPS1259 /* Convert the weight from pounds to kilograms */ comment #include standard header file preprocessor #define KILOS_PER_POUND constant macro directive int reserved word main(void) { double pounds, variable kilos; printf(" Enter the weight in pounds"); standard identifier scanf(“%lf”, &pounds); kilos = pounds * KILOS_PER_POUND; printf(" That equals %f kilos. \n”, kilos); punctuation return (0); } special symbol

A.Abhari CPS12510 Executable Statements Data can be stored/changed in memory cells by Assignment statement variable = expression; ( =) is not equal sign x= x + z / 6.9; x= -9; new_x = -x; Input operations such as scanf function that requires : #include

A.Abhari CPS12511 Output Operation - printf printf( format string, printlist) printf( “ Hi %c - your age is %d\n”, na,age); printf (“It is 1th line\n”); printf (“\n and 2th line); printf(“ Enter the weight in pounds”); scanf( “%lf”, &pounds); printf(“ %f pounds was entered”, pounds);

A.Abhari CPS12512 Input Operation-scanf scanf (format string, input list) scanf (“%c%d”, &initial, &age); & is using for each int, double and char variables (it means address of memory) The order of placeholders correspond to the order of variables in the input list When reading the numbers, scanf scans the digits and characters until it reaches non-digits, blank or cr. For characters only first character before cr is considered

A.Abhari CPS12513 /* This program calculates ? */ #include #define PI int main(void) { double r,a,c; scanf(“%lf”, &r); a= PI * r * r; c= 2 * PI * r; printf( “? is %f cm^2. \n”, a); printf( “? is %f cm. \n”, c); return (0); }

A.Abhari CPS12514 Formating Values of Type int printf ( “Result is: %3d meters”,… Examples: Value format display 234 %6d %1d %6d %1d %d 234

A.Abhari CPS12515 Formating Values of Type double printf ( “Result is: %6.2f meters”,… Examples: Value format display %6.1f %6.2f %8.5f %.3f %.1f %f 99.67

A.Abhari CPS12516 #include int main(void) { double x= ; int y=12345; printf( “ %f %.3f %.1f \n”,x,x,x ); printf( “ %3d %5d %8d \n\n”,y,y,y ); return (0); }  

A.Abhari CPS12517 Batch Mode In batch mode the program gets its input from a data file instead of the user and sends the result to a data file instead of the screen. It is also called redirection. For example the following program can be run by: weightconvert output /* weightconvert program */ include #define KILOS_PER_POUND int main(void) { double pounds, kilos; scanf(“%lf”, &pounds); printf(“Weight in pounds is %.2f. \n“, pounds); kilos = pounds * KILOS_PER_POUND; printf(" That equals %f kilos. \n”, kilos); return (0); }

A.Abhari CPS12518 Program-controlled Input and Output files File pointer variable include #define KILOS_PER_POUND int main(void) { double pounds, kilos; FILE *inp, /* pointer to input file */ *outp; /* pointer to output file */

A.Abhari CPS12519 Os preparing the files for access /* open the input and output files */ inp = fopen (“a:weight.txt”, “r”); outp= fopen (“a:distance.out”, “w”); /* get the input from file */ fscanf(inp,“%lf”, &pounds); fprintf(outp, “Weight in pounds is %.2f. \n“, pounds);

A.Abhari CPS12520 kilos = pounds * KILOS_PER_POUND; /* Display the result in output file */ fprintf(output, “That equals %f kilos. \n”, kilos); /* Close files */ fclose(inp); fclose(outp); return (0); } We will discuss program-controlled input and output files in more details later.

A.Abhari CPS12521 Common programming Errors Syntax Error Missing ; or variable definition double pounds instead of double pounds, kilos; Last comment is not closed /* Close files instead of /* Close files */ Correct the errors in declaration part first

A.Abhari CPS12522 Runtime Errors and Undetected Errors If we use scanf for numbers and then after that again we use scanf for the characters, the first character gets different value than what we entered. For example in a program scanf (“%lf”, &radius); scanf (“%c%c%c”, &a, &b, &c); is used instead of scanf (“%c%c%c”, &a, &b, &c); scanf (“%lf”, &radius); If we run this program with these inputs: I 50 ABC The values stored in the variables are:  a b c radius \n A B 50 One of the way for fixing this problem is placing space before first %C: scanf (“%lf”, &radius); scanf (“ %c%c%c”, &a, &b, &c);

A.Abhari CPS12523 Logic Errors Logic errors is shown by the wrong result of the program. For example forgetting & in the following statement: scanf( “%d%d”, a, b) produces => incorrect results Preventing logic errors is done by Deskchecking the algorithm and Debugging the program