Computer programing C++

32
THE STRUCTURE OF C LANGUAGE CHAPTER 2 CHAPTER 2 PART 1 Prepared by: Shazana Md Zin Faculty of Info.Technology & Multimedia A216-05

Transcript of Computer programing C++

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 1/32

THE STRUCTURE OF C LANGUAGE

CHAPTER 2CHAPTER 2

PART 1

Prepared by: Shazana Md Zin

Faculty of Info.Technology & Multimedia

A216-05

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 2/32

2

CONTENTS

Part 1Introduction to C language

Basic C

Function Variable

Part 2Constant and Identifier

Data Types

Pre-processor Directives

CHAPTER 2CHAPTER 2

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 3/32

3

Introduction to C Language

• What is C language?

– C is a computer programming language

– C is one of thousands of programming languages currently inuse

– C is what we called as compiled language. This means thatonce you write your C program, you must run it through acompiler to turn your program into an executable that thecomputer can run (execute)

– Not a user friendly language and provides a terse andunreadable syntax

CHAPTER 2CHAPTER 2

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 4/32

Introduction to C Language…cont.

• Advantages of C language:

– It lets you write programs that resemble

everyday English. – C is an easy language to learn – One of the most important/popular

programming language for developing newsoftware packages for different platformsand on many computers

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 5/32

5

• A compiler is necessary to make your source code(.c, .cpp, or .cc files) into a running program.

• If you're just starting out, you'll need to make sure thatyou have one before you start programming.

• There are many compilers available on the internet andsold commercially in stores or online.

• If you have Mac OS X, Linux, or other *nix variant (such

as Unix or FreeBSD), you likely have a compiler such asgcc or g++ installed already.• Windows: Microsoft Visual C++

Compiler

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 6/32

6

Programmers can make a program easier to understandby using comments

This is to describe the purpose of the program, the useof identifier and the purpose of each program step

Comments are part of the program documentationbecause it help others read and understand the program

Compiler will IGNORE the comments and there are nottranslated into machine language.

In C a comment will start with /* and ends with */

Comment in Program

CHAPTER 2CHAPTER 2

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 7/32

7

• Example Comment of Program

/*This is a single line comment */

/* This is a multiline* comment in C */

/***************************************************** This style of commenting is used for functions****************************************************/

CHAPTER 2CHAPTER 2

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 8/32

8

Questions

1) C programs are converted into machinelanguage with the help of?

a) An Editorb) A compilerc) An operating systemd) None of the above

Answer:

CHAPTER 2CHAPTER 2

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 9/32

9

/*This is a single line comment */

/* This is a multiline* comment in C /

/***************************************************** This style of commenting is used for functions

****************************************************/

2) What is the error?

Answer:

CHAPTER 2CHAPTER 2

Questions

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 10/32

10

Basic C

CHAPTER 2CHAPTER 2

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 11/32

Library File• E.g : #include <stdio.h>

• # sign indicates that a preprocessor command (directive) willfollow

• The processor command causes the library file called stdio.h to beincluded in your program

•Typically, include files are library files, also called header files ,that are provided by the language compiler

• We can include our own library files, called user-defined libraries, inthe similar manner

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 12/32

Main () Function

• C program is a collection of functions• Each function is dedicated to do a specific job in

the program• Function main() indicates the logical beginning

of the program• When you run a C program, the main() is

invoked and program execution starts from thefirst line of executable code in the program

• Has a header and a body

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 13/32

First C program

/*A simple C program that displays a message*/

#include <stdio.h>

int main() /*function header*/{

printf(“This is my first C program.\n”);return 0;

}

Functionbody

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 14/32

14

Function Variable

CHAPTER 2CHAPTER 2

Variables (1 of 2)

• The function of variable is to hold the data in your program

It is a location (or set of locations) in memory where a value canbe stored for use by a program

For example, if your program requests a value from the user, orif it calculates a value, you will want to remember it somewhereso you can use it later . The way your program remembersthings is by using variables. For example:

int b;

You can use the value in b by saying something like:

printf("%d", b);

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 15/32

15

Variable NamesIn C all the variables should be declared before it can be used.

You cannot re-declare a variable once it has been alreadydeclared.

A variable name can either begin with an alphabet orunderscore

• No other special character except underscore is allowed

• Maximum characters recognized is 31 (compiler dependent)

• Variable names are case sensitive and keywords cannot be usedas variable names

CHAPTER 2CHAPTER 2

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 16/32

16

cont.• Variable names in C are made up of letters (upper and lower

case) and digits.

• The underscore character (" _ ") is also permitted.

• Names must not begin with a digit.

• Unlike some languages (such as Perl and some BASIC

dialects), C does not use any special prefix characters onvariable names.

CHAPTER 2CHAPTER 2

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 17/32

17

•Some examples of valid (but not very descriptive) C

variable names:uthmUthmUTHMparit_raja

_uthm42 _UtHm

•Some examples of invalid C variable names:2uthm (must not begin with a digit)Parit raja (spaces not allowed in names)$uthm ($ not allowed -- only letters, digits, and _)while (language keywords cannot be used asnames)

CHAPTER 2CHAPTER 2

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 18/32

18

int age;int age, count, i,j;float amount, rateOfInterest, totalBalance;char ch;

Example: Declaring the variables

CHAPTER 2CHAPTER 2

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 19/32

19

1) A C variable cannot start with

a) An alphabet

b) A number

c) A special symbol other than underscore

d) both (b) and (c) above.

Answer:

CHAPTER 2CHAPTER 2

Question

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 20/32

Common Errors

• Starting a comment line with /* but forgettingto terminate it with */

• Placing a space between / and *, such as / *• Using uppercase letters with the C keywords,

such as Printf • Forgetting the ; (semicolon) at the end of each

statement• Placing a semicolon at the end of a preprocessor

command, such as # include <stdio.h>;• Placing a semicolon at the end of a function

header, such as int main ();

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 21/32

21

Thank you !

End of Part 1

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 22/32

THE STRUCTURE OF C LANGUAGE

CHAPTER 2CHAPTER 2

PART 2

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 23/32

23

Constant

• A constant can be defined as “a quantity that does notchange during the execution of a program”.

• Here we are declaring a variable “ a ” with its initialvalue 10. 10 here is an integer constant and every timeyou try to execute this program, the output will alwaysbe 10.

CHAPTER 2CHAPTER 2

#include <stdio.h>void main(void){

int a = 10;printf(“%d”,a);

}

output : ?

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 24/32

24

Integer ConstantAn integer constant must have at least one digit andshould not have decimal point.

It could either be positive or negative.

Example:const int MAX_NUM = 10;const int MIN_NUM = -90;const int Hexadecimal_Number = 0xf87;

Type of Constants

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 25/32

25

cont.

• Real Constant• A real constant must have at least one digit and must

have a decimal point.

Example:

0.0026

-0.97

435.29

+487.0

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 26/32

26

cont.Character ConstantA character constant should be only one character and must

be enclosed in single quotes e.g. ‘A’, ‘b’, etc.

Example:

const char letter = ‘n’;

const char number = ‘1’;

printf(“%c”, ‘S’);Output would be? ___

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 27/32

27

Identifier

• An identifier is used for any variable, function, datadefinition, etc.

• In the C programming language, an identifier are: – A combination of alphanumeric characters, – the first being a letter of the alphabet or an underline, – The remaining being any letter of the alphabet, any

numeric digit, or the underline.

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 28/32

28

cont.

• Two rules must be kept in mind when namingidentifiers.

I. The case of alphabetic characters is significant.Using INDEX for a variable name is not the same asusing index and neither of them is the same as usingInDeX for a variable name. All three refer to differentvariables.

II. According to the ANSI-C standard, at least 31significant characters can be used and will be

considered significant by a conforming ANSI-Ccompiler. If more than 31 are used, all charactersbeyond the 31st may be ignored by any givencompiler.

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 29/32

29

Data Types• Data type is a set of values and a set of operation on those values.

• The 4 basic data type are as below:

– Int

• To represent integers in C• -10500, 435, +15, -25, 34728

• int some_number=3; – Double

• To represent real numbers in C• 3.14159, 0.0005, 150.00

– Char

• To represent an individual character value in C• A letter, a digit or special symbol• ' B ', ' C ', ' * ', ' : ', ' “ '

• Object of data type can be variables or constants

CHAPTER 2CHAPTER 2

30

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 30/32

30

Preprocessor Directives

Preprocessor Directives is a message fromprogrammer to the preprocessor.It is a unique feature of C language.A program can use the tools provided bypreprocessor to make his program easy toread, modify, portable and more efficient.Preprocessor directives follow the specialsyntax rules and begin with the symbol # bin

column1 and do not require any semicolon atthe end. A set of commonly used preprocessordirectives.

CHAPTER 2CHAPTER 2

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 31/32

31

Directive Function

#define Defines a macro substitution

#undef Undefines a macro

#include Specifies a file to be included

#ifdef Tests for macro definition

#endif Specifies the end of #if

#ifndef Tests whether the macro is not def

#if Tests a compile time condition

#else Specifies alternatives when # if test fails

8/14/2019 Computer programing C++

http://slidepdf.com/reader/full/computer-programing-c 32/32

32

Thank you !

End of Part 2