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

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.
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.
Software Development Method & C Language Elements H&K Chapter 1-2
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.
Topic 2 – Introduction to the C Programming Language.
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.
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)
1 Chapter 3 Fundamentals of the C Programming Language.
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.
(1 - 2) C Language Elements H&K Chapter 2 Instructor - Andrew S. O’Fallon CptS 121 (August 28, 2015) Washington State University.
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
C++ Programming: Basic Elements of C++.
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.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
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.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 Lexical Elements, Operators, and the C System. 2 Outline Characters and Lexical Elements Syntax Rules Comments Keywords Identifiers Constants String.
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
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.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes Contrasting MATLAB with C MATLABC language Workspace - interactive computation No real.
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
ICS103 Programming in C Lecture 3: Introduction to C (2)
Lecture2.
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.
CS111 Computer Programming
Lecture3.
Lesson #2 Introduction to C.
WEEK-2.
Chapter 1 Fundamentals 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:

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

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

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

C Language Elements Reserved Word They have special meaning and can not be used for something else All are lowercase some of them are: void case switch return for signed else long If do int while

C Language Elements Identifiers Standard identifiers: scanf, printf User-defined identifiers (should begin with a letter and only can contain digit or _ ) Invalid Valid 1rate rate1 int Rate1 joe’s x Can be uppercase & lowercase Meaningful name should be used for identifiers They should not be redefined

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;

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’, ‘ ’,...

/* 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); special symbol kilos = pounds * KILOS_PER_POUND; printf(" That equals %f kilos. \n”, kilos); punctuation return (0); } special symbol

Executable Statements Data can be stored/changed in memory cells by Assignment statement variable = expression; x= x + z / 6.9; x= -9; Input operation that required #include

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);

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 For numbers the characters are scans until non-digits, blank or cr. For characters only first character before cr is considered

/* 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); }

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

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

#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); }  

Batch Mode Example: weightconvert output 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); }

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 */

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);

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); }

Common programming Errors Syntax Error Missing ; or variable definiation 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

Runtime Errors scanf (“%lf”, &radius); scanf (“%c%c%c”, &a, &b, &c); instead of scanf (“%c%c%c”, &a, &b, &c); scanf (“%lf”, &radius); Input 50 ABC  a b c radius \n A B 50

Logic Errors scanf( “%d%d”, a, b) => incorrect results Deskchecking Debugging