Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2.
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Lecture 2 Introduction to C Programming
Introduction to C Programming
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.
Introduction to C Programming
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
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.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Introduction to C Programming
Principles of Programming - NI Chapter 3 Fundamental of C Programming Language and Basic Input/Output Function.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
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.
Computer Science 210 Computer Organization Introduction to C.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Yu Yuanming CSCI2100B Data Structures Tutorial 3
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Khalid Rasheed Shaikh Computer Programming Theory 1.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Program Development Cycle 1.Edit program 2.Compile program - translates it from C to machine language 3. Run/execute your program. 4. If not satisfied,
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
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.
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.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
CSCE 206 Structured Programming in C
Chapter Topics The Basics of a C++ Program Data Types
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
Lesson #2 Introduction to C.
Lesson #2 Introduction to C.
Chapter 2 - Introduction to C Programming
Basic Elements of C++.
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Getting Started with C.
Chapter 2 - Introduction to C Programming
Basic Elements of C++ Chapter 2.
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.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Getting Started With Coding
Presentation transcript:

Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable declaration Variable definition

Comments Explanations or annotations that are included in a program for documentation and clarification purpose. Completely ignored by the compiler during compilation and have no effect on program execution. Starts with ‘/*’ and ends with ‘*/’ Some compiler support comments starting with ‘//’

Preprocessor Directives The first thing to be checked by the compiler. Starts with ‘#’. Tell the compiler about specific options that it needs to be aware of during compilation. There are a few compiler directives. But only 2 of them will be discussed here. –#include Tell the compiler to include the file stdio.h during compilation Anything in the header file is considered a part of the program –#define VALUE 10 Tell the compiler to substitute the word VALUE with 10 during compilation

Preprocessor Directives #define PI main() { ….. perimeter = 2*PI*radius; area = PI*radius*radius; …... } main() { ….. perimeter = 2* *radius; area = *radius*radius; …... } The result of the compilation is the same for both program. Which one is preferred (less typing)? Which more readable? The one with awful long series of digits the one with a meaningful name? Before compilation, the pre-processor will replace all PI with

Basic Data Types 3 examples of basic data types: –int (used to declare numeric program variables of integer type) –char (used to declare character variable) –double (used to declare floating point variable) In addition, there are float, void, short, long, etc. Declaration: specifies the type of a variable. –Example: int local_var; Definition: assigning a value to the declared variable. –Example: local_var = 5;

A variable can be declared globally or locally. A globally declared variable can be accessed from all parts of the program. A locally declared variable can only be accessed from inside the function in which the variable is declared.

Statements A specification of an action to be taken by the computer as the program executes. In the previous example, there are 2 lines following variable declaration and variable definition that terminate with semicolon ‘;’. – global_var = local_var + VALUE; – printf (“Total sum is: %d\n”, global_var); Each line is a statement.

Basic Functions A C program consists of one or more functions that contain a group of statements which perform a specific task. A C program must at least have one function: the function main. We can create our own function or use the functions that has been created in the library, in which case we have to include the appropriate header file (example: stdio.h).

In this section, we will learn a few functions that are pre-defined in the header file stdio.h These functions are: –printf() –scanf() –getchar() & putchar() In addition to those functions, we will also learn about Format Specifier and Escape Sequence which are used with printf() and scanf().

printf() Used to send data to the standard output (usually the monitor) to be printed according to specific format. General format: –printf(“control string”, variables); Control string is a combination of text, format specifier and escape sequence. Example: –printf(“Thank you”); –printf (“Total sum is: %d\n”, global_var); %d is a format specifier \n is an escape sequence

Format Specifier Tells the printf() function the format of the output to be printed put.

Escape Sequence Escape sequence is used in the printf() function to do something to the output.

scanf() Read data from the standard input device (usually keyboard) and store it in a variable. General format: –scanf(“Control string”, &variable); The general format is pretty much the same as printf() except that it passes the address of the variable (notice the & sign) instead of the variable itself to the second function argument. Example:

getchar() and putchar() getchar() - read a character from standard input putchar() - write a character to standard output Example: #include void main(void) { char my_char; printf(“Please type a character: “); my_char = getchar(); printf(“\nYou have typed this character: “); putchar(my_char); }