Sie sind auf Seite 1von 32

CHAPTER 2

THE STRUCTURE OF C LANGUAGE


PART 1

Prepared by: Shazana Md Zin


Faculty of Info.Technology & Multimedia
A216-05
CONTENTS
Part 1

Introduction to C language

Basic C

Function Variable

Part 2

Constant and Identifier

Data Types

Pre-processor Directives

2
CHAPTER 2
Introduction to C Language
• What is C language?

– C is a computer programming language


– C is one of thousands of programming languages currently in
use
– C is what we called as compiled language. This means that
once you write your C program, you must run it through a
compiler to turn your program into an executable that the
computer can run (execute)
– Not a user friendly language and provides a terse and
unreadable syntax

3
CHAPTER 2
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 new
software packages for different platforms
and on many computers
Compiler

• 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 that
you have one before you start programming.
• There are many compilers available on the internet and
sold 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 as
gcc or g++ installed already.
• Windows: Microsoft Visual C++

5
Comment in Program

Programmers can make a program easier to understand
by using comments


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


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


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


In C a comment will start with /* and ends with */
6
CHAPTER 2
• 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
****************************************************/

7
CHAPTER 2
Questions
1) C programs are converted into machine
language with the help of?

a) An Editor
b) A compiler
c) An operating system
d) None of the above

Answer:

8
CHAPTER 2
Questions
2) What is the error?

/*This is a single line comment */

/* This is a multiline
* comment in C /

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

Answer:

9
CHAPTER 2
Basic C

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

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


follow

• The processor command causes the library file called stdio.h to be


included 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, in


the similar manner
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 the
first line of executable code in the program
• Has a header and a body
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”); Function
body

return 0;
}
Variables (1 of 2)
Function Variable
• 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 can


be stored for use by a program

 For example, if your program requests a value from the user, or


if it calculates a value, you will want to remember it somewhere
so you can use it later. The way your program remembers
things is by using variables. For example:

int b;

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

printf("%d", b);
14
CHAPTER 2
Variable Names
 In C all the variables should be declared before it can be used.

 You cannot re-declare a variable once it has been already


declared.

 A variable name can either begin with an alphabet or


underscore

• No other special character except underscore is allowed

• Maximum characters recognized is 31 (compiler dependent)

• Variable names are case sensitive and keywords cannot be used


as variable names
15
CHAPTER 2
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 on
variable names.

16
CHAPTER 2
•Some examples of valid (but not very descriptive) C
variable names:
 uthm
 Uthm
 UTHM
 parit_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 as
names)
17
CHAPTER 2
Example: Declaring the variables

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

18
CHAPTER 2
Question
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:

19
CHAPTER 2
Common Errors
• Starting a comment line with /* but forgetting
to 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 ();
End of Part 1

Thank you !

21
CHAPTER 2

THE STRUCTURE OF C LANGUAGE


PART 2
Constant
• A constant can be defined as “a quantity that does not
change during the execution of a program”.
#include <stdio.h>
void main(void)
{
int a = 10;
printf(“%d”,a);
}

output :?

• Here we are declaring a variable “ a ” with its initial


value 10. 10 here is an integer constant and every time
you try to execute this program, the output will always
be 10.

23
CHAPTER 2
Type of Constants

 Integer Constant
 An integer constant must have at least one digit and
should 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;

24
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

25
cont.
Character Constant
A 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? ___

26
Identifier

• An identifier is used for any variable, function, data


definition, 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.

27
cont.
• Two rules must be kept in mind when naming
identifiers.

I. The case of alphabetic characters is significant.


Using INDEX for a variable name is not the same as
using index and neither of them is the same as using
InDeX for a variable name. All three refer to different
variables.
II. According to the ANSI-C standard, at least 31
significant characters can be used and will be
considered significant by a conforming ANSI-C
compiler. If more than 31 are used, all characters
beyond the 31st may be ignored by any given
compiler.

28
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
29
CHAPTER 2
30

Preprocessor Directives

Preprocessor Directives is a message from
programmer to the preprocessor.

It is a unique feature of C language.

A program can use the tools provided by
preprocessor to make his program easy to
read, modify, portable and more efficient.

Preprocessor directives follow the special
syntax rules and begin with the symbol #bin
column1 and do not require any semicolon at
the end. A set of commonly used preprocessor
directives.

30
CHAPTER 2
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

31
End of Part 2

Thank you !

32