Structure of a C program

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Chapter 3: Beginning Problem Solving Concepts for the Computer Programming Computer Programming Skills /1436 Department of Computer Science.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
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 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?
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
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 PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
Syntax of C Programming Language #Lesson 2 Sen Zhang.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Lecture #5 Introduction to C++
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Dennis Ritchie 1972 AT & T Bell laboratories (American Telephone and Telegraph) USA 1www.gowreeswar.com.
1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.Semantic © In this session we will.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
CC112 Structured Programming Lecture 2 1 Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
DOCUMENTATION SECTION GLOBAL DECLARATION SECTION
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
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.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Lecture 4 Monday Sept 9, Variables and Data Types ►A►A►A►A variable is simply a name given by the programmer that is used to refer to computer storage.
Numbers in ‘C’ Two general categories: Integers Floats
C++ Lesson 1.
Data types Data types Basic types
BASIC ELEMENTS OF A COMPUTER PROGRAM
LESSON 3 IO, Variables and Operators
ICS103 Programming in C Lecture 3: Introduction to C (2)
C Short Overview Lembit Jürimägi.
Introduction to C Programming Language
Lecture2.
CMSC 104, Section 4 Richard Chang
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
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.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
פרטים נוספים בסילבוס של הקורס
Variables in C Topics Naming Variables Declaring Variables
Basics of ‘C’.
פרטים נוספים בסילבוס של הקורס
Govt. Polytechnic,Dhangar
Variables in C Topics Naming Variables Declaring Variables
Variables in C Declaring , Naming, and Using Variables.
Chapter 2: Introduction to C++.
WEEK-2.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
Variables in C Topics Naming Variables Declaring Variables
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
C Language B. DHIVYA 17PCA140 II MCA.
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
INTRODUCTION TO C.
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Structure of a C program Preprocessor Directives Global Declarations int main(void) { } Local Declarations Executable Statements

Preprocessor Directives Special instructions to the preprocessor that tell it how to prepare your program for compilation. Two common directives are #include and #define. #include <standard header file> Tells the preprocessor that you want the header file in the pointed brackets (< >) included in your program. (e.g. stdio.h) All preprocessor directives start with a # standard library #define NAME value This directive instructs the preprocessor to replace each occurrence of NAME (known as a constant macro)in the text of the C program by the value. An executing C program cannot change the value of a name defined as a constant macro.

Function Main { } int main(void) function header void means no parameters function body int means that the function returns an integer to the operating system } body of the function is enclosed in curly brackets The executable part of your program begins with function main. The executable part of your program begins with function main.

comments /* this is a comment */ /* *** /*this is a comment */ *** */ Comments begin with “/*” and end with “*/ “ The compiler ignores comments when it translates the program into executable code. Comments are used for internal program documentation and can appear anywhere in the progrma. They help explain the meaning and logic of the program to the reader. Nested comments (comments inside comments) are invalid. /* *** /*this is a comment */ *** */ Second opening token ignored This token terminates the comment This will now cause an error All of your programs should begin with documentation explaining the purpose of the program.

identifiers Identifiers allow us to name data and other objects in the program. Syntax rules for naming identifiers: 1) First character must be alphabetic character or underscore. 2) Must consist only of alphabetic characters, digits, or underscores. 3) First 31 characters of an identifier are significant. 4) A C reserved word cannot be used as an identifier. Reserved words have special meaning in C and cannot be used for other purposes. auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while An identifier defined in a C Standard Library (e.g. printf, scanf) also know as standard identifiers, should not be redefined.

identifiers The C compiler is case sensitive. The identifiers miles, Miles and MILES are all different identifiers. Program Style use lower case for all identifiers except constant macros use underscores to separate words ( e.g. miles_per_gallon) good identifier names are descriptive but short (don’t use single letters for identifiers) don’t use identifiers that can be confused in the same program ( e.g. Miles, miles)

Data types A type defines a set of values and a set of operations that can be applied on those values. Type also defines the size of the field ( number of bits) in which data can be stored. C contains four standard types: int - sort for integer. An integer is a number without a fractional part ( eg 1, 2, 3 … 456, … 6789,…) There are three different sizes of the integer data type: short int or just short int long int or just long Each type can be further subdivided into two different subtypes: signed unsigned

Data types Typical integer sizes for the PC char - short for character. Holds the value of one of the characters in the ASCII (American Standard Code for Information Interchange) alphabet. (e.g. ‘a’, ‘A’, ‘b’,’?’, …) The size of char is machine dependent but normally is one byte or 8 bits.

Variable Declarations Data types float - a floating-point type is a number (real number) with a fractionalpart. (e.g. 56.24, .0058 …) which is separated by a decimal point. There are three different sizes of theFloating- point data type: float double long double Variable Declarations The memory cells used for storing a program’s input data and its computational results are called variables because the values stored in variables can change as the program executes.When we create variables, the declaration gives them a symbolic name and reserves memory for them. Each variable in your program must be delcared (defined). Variable name Syntax: int variable_list; double variable_list; char variable_list; Type of data to be stored in this variable All C statements end with a semicolon

Variable Declarations Examples: double miles; float pay_rate; int class_size; long int national_debt; double gpa, accum; char class grade; Variable Initialization We can initialize a variable at the same time that we declare it by placing an equal sign and a constant after the variable name. This establishes the first value that the variable will contain. Syntax: type variable_name = constant; Examples: double miles = 0.0; float pay_rate = 4.5; int count = 0; char grade = ‘A’;

Constants Constants are data values that cannot be changed during the execution of your program. A literal is an unnamed constant used to specify data. If we know the data cannot be changed, then we simply code the data value itself in a statement. C constants can be of any of the vasic data types. The way each constant is represented depends upon its type Examples: 123, -123, +123 /*examples of integers*/ 0., .005, 8769.098 /*examples of floating-point*/ ‘a’, ‘C’, ‘&’ /*examples of characters*/ Character constants are always enclosed in single quotes. Numeric constants in C are considered non-negative numbers and cannot contain commas.