Вы находитесь на странице: 1из 48

C Fundamentals

Ramesh Tamang
1
THE C CHARACTER SET
C uses the uppercase letters A to Z, the lowercase
letters a to z, the digits 0 to 9, and certain special
characters as building blocks to form basic program
elements (e.g., constants, variables, operators,
expressions, etc.).
The special characters are listed below.

+ - * / - % & #

! ? ^ -
< > ( )

, - (blank space)
2
IDENTIFIERS AND KEYWORDS
Identifiers are names that are given to
various program elements, such as variables,
functions and arrays.
Identifiers consist of letters and digits, in any
order, except that the first character must be
A letter.
upper- and lowercase letters are permitted,
though common usage favors the use of
lowercase letters for most types of identifiers.
3
Contd
The underscore character ( _ ) can also be
included, and is considered to be a letter.
An underscore is often used in the middle of
an identifier. An identifier may also begin with
an underscore, though this is rarely done in
practice.
Eg:
x y12 sum_1 _temperature
names area tax_rate TABLE
4
Contd
Invalid identifiers

4th The first character must be a letter.

x Illegal characters ().


order-no Illegal character (-).
error flag Illegal character (blank space).
5
Reserved words/Keywords
Have standard predefined meanings in C
Keywords can be used only for their intended purpose; they
cannot be used as programmer-defined identifiers
E.g.:
auto extern sizeof
break float static
case for struct
char goto switch
const if typedef

6
Contd
Note that the keywords are all lowercase
uppercase and lowercase characters are not
equivalent, it is possible to utilize an upper-
case keyword as an identifier
7
DATA TYPES
int
integer quantity 2 bytes or one word
char
single character 1 byte
float
floating-point number 1 word (4 bytes)
a decimal point andor an exponent)
double double-precision floating-point number
(i.e., more 2 words (8 bytes))
8
CONSTANTS
There are four basic types of constants in C.
They are integer constants, floating-point
constants, character constants and string
constants
Integer and floating-point constants represent
numbers
often referred to collectively as numeric-type
constants
9
Rules apply to all numeric-type
constants
Commas and blank spaces cannot be included
within the constant
The constant can be preceded by a minus (-)
sign if desired
value of a constant cannot exceed specified
bounds
bounds will vary from one C compiler to
another
10
Integer Constants
An integer constant is an integer-valued
number
Integer constants can be written in three
different number systems: decimal, octal and
hexadecimal.
11
Decimal integer
A decimal integer constant can consist of any
combination of digits taken from the set 0
through 9
If the constant contains two or more digits, the
first digit must be something other than 0
Valid decimal integer
E.g.
0 1 743 5280 32767 9999
12
Invalid decimal integer
12,245 illegal character (, ).

36.0 illegal character (.).

10 20 30 illegal character (blank space).


123-45-6789 illegal character (-).

0900 the first digit cannot be a zero


13
Octal integer constant
An octal integer constant can consist of any
combination of digits taken from the set 0
through 7
the first digit must be 0,in order to identify the
constant as an octal number
valid octal integer constants
0 01 0743 077777
14
Contd
Invalid octal integer constants
743 Does not begin with 0.
05280 Illegal digit (8).
0777.777 Illegal character ( .).
15
Hexadecimal integer constant
A hexadecimal integer constant must begin
with either Ox or OX
It can then be followed by any combination of
digits taken from the sets 0 through 9 and a
through f (either upper- or lowercase)
Valid hexadecimal integer constants
E.g.
ox oX1 OX7FFF Oxabcd
16
Invalid hexadecimal integer constants
OX12.34 Illegal character ( .)
OBE38
Does not begin with Ox or OX
Ox. 4bf f Illegal character ( .)
OXDEFG Illegal character (G)
17
Floating-Point Constants
A floating-point constant is a base- 10 number
that contains either a decimal point or an
exponent (or both)
valid floating-point
0. 1. 0.2 827.602
50000. 0.000743 12.3 31 5.0066
2E-8 0.006e-3 1.6667E+8
.12121212e12
21
Invalid floating-point constants
1 Either a decimal point or an exponent
must be present
1,000.0 Illegal character (, )
2E+10.2 The exponent must be an integer
quantity (it cannot contain a decimal
point).
3E 10 Illegal character (blank space) in
the exponent
22
Character Constants
A character constant is a single character,
enclosed in apostrophes (i.e., single quotation
marks)
E.g.
A X 3 ?
23
Escape Sequences
Nonprinting characters, as well as the
backslash (\) and the apostrophe ( ), can be
expressed in terms of escape sequences
24
Contd
Commonly used escape sequences are listed below
Character
bell (alert)
backspace
horizontal tab
vertical tab
newline (line feed)
form feed
carriage return
quotation mark (")
apostrophe (')
question mark (?)
backslash (\)
null
Escape Sequence
\a
\b
\t
\v
\n
\f
\r
\
\
\?
\ \
\O
25
String Constants
A string constant consists of any number of
consecutive characters (including none),
enclosed in (double) quotation marks.
E.g.
green Washington, D.C. 20005
270-32-3456 $19.95 2* ( I+3)/J
26
VARIABLES
A variable is an identifier that is used to
represent some specified type of information
within a designated portion of the program
A variable is an identifier that is used to
represent a single data item; i.e., a numerical
quantity or a character constant
E.g.
int a, b, c;
char d;
27
Array
Array is also kind of variable that is used
extensively in C
An array is an identifier that refers to a
collection of data items that all have the same
name
data items must all be of the same type
individual array elements are distinguished
from one another by the value that is assigned
to a subscript
28
Contd
E.g.
x is a 10-element array
first element is referred to as x [ 0] ,the second
as x [ 1] ,and so on. The last element will be x
[9]
for an n-element array, the subscripts
always range from 0 to n-1
29
Contd
subscript sometimes called an index
Note that an n-character string will require
an (n+l )-element array, because of the null
character (\O) that is automatically placed at
the end of the string
30
DECLARATIONS
A declaration associates a group of
variables with a specific data type
All variables must be declared before they
can appear in executable statements
A declaration consists of a data type,
followed by one or more variable names,
ending with a semicolon
31
Contd
E.g. Declaring variables:
int a, b, c ;
float root1 , root2;
char flag , text[8 ] ;
Or
int a;
int b;
int c;
float rootl;
float root2;
char flag;
char text[80];
32
Contd
e.g. declaring variable with initialization
int c = 12;
char star = * ;
float sum = 0. ;
double factor = 0.21023e-6;
33
EXPRESSIONS
An expression represents a single data item, such as a number
or a character or the combination of them using operators
use of expressions involving operators
expressions can also represent logical conditions that are
either true (1) or false (0)
E.g.
a + b
x = y
c = a + b
x <= y
x == Y
++i
34
STATEMENTS
A statement causes the computer to carry out
some action
three different classes of statements in C
expression statements
compound statements
control statements
35
Expression Statements
An expression statement consists of an
expression followed by a semicolon
The execution of an expression statement
causes the expression to be evaluated
E.g.
assignment-type statements
a = 3;
c = a + b ;
36
Contd
incrementing-type statement
++i;
37
Compound Statement
Consists of several individual statements
enclosed within a pair of braces { }
The individual statements may themselves
be expression statements, compound
statements or control statements
the compound statement provides a capability
for embedding statements within other
statements
Unlike an expression statement, a compound
statement does not end with a semicolon
38
Contd
E.g.
{
pi = 3.141593;
circumference = 2. * p i * radius;
area = p i * radius * radius;
}
39
Control Statements
Control statements are used to create special progr-
am features, such as logical tests, loops and branches
E.g.
while(count <= n) {
printf( ' x = ' ) ;
scanf( %f , &x) ;
sum += x;
++count;
}
40
SYMBOLIC CONSTANTS
A symbolic constant is a name that substitutes
for a sequence of characters
a symbolic constant allows a name to appear
in place of a numeric constant, a character
constant or a string
When a program is compiled, each occurrence
of a symbolic constant is replaced by its
corresponding character sequence
41
Contd
Note: The characters may represent a numeric
constant, a character constant or a string constant
symbolic constant is defined by writing
#define name text
E.g.
#define PI 3.141593
#define TRUE 1
#define FALSE 0
#define FRIEND Susan
42
E.g.
#include<stdio.h>
#define PI 3.14
main()
{
printf("Value of PI is %f",PI);
}
43
STANDARD INPUT OUTPUT
FUNCTIONS
printf is used to output to a standard output device
scanf is used to take input from the standard input device
The parentheses ( ) are known as the function call operator.

Type printf Format Specifier


int %d
float %f
char %c
Type scanf Format Specifier
int %d
float %f
char %c
REQUIREMENTS
Text Editor (gedit)
Compiler (gcc)
C Standard Library
OPENING TERMINAL
ApplicationsSystem toolsTerminal
CHECK FOR gcc COMPILER
gcc
An error message such as gcc: no input files
CREATING WORKING DIRECTORY
mkdir yourname_fac_rollno (eg ramesh_CE_1)
ls
cd ramesh_CE_1
pwd
gedit sample.c
COMPILATION
gcc sample.c -o sample
Creates object code file named sample
gcc sample.c
Creates object code file named a.out
EXECUTION
./sample
./a.out
#include<stdio.h> // this is needed to run printf() function
int main()
{
printf(Hello, World!); // displays the content inside the quotation
return 0;
}
#include<stdio.h>
int main()
{
int a = 5;
printf(Number = %d,a);
return 0;
}
#include<stdio.h>
int main()
{
int a;
printf(Enter a number\n);
scanf(%d,&a)
printf(Number = %d,a);
return 0;
}
//start of program
#include <stdio.h> //header file
int main()
{
printf(Kathmandu University\n");
printf(This is my first C program\t);
return 0;
}
//end of program
#include <stdio.h>
int main( )
{
int a, b, c;
float x = 11.5, y = 3.3, z = -7.7;
printf( Input two integers:\n );
scanf( %d%d, &b, &c );
a = b + c;
x = y + z;
printf(the value of a is %d\n,a);
printf(the value of x is %f\n,x);
return 0;
}
Declaration Declaration with
initialization always
initialize!!!
Function calls
Assignment
statements

Вам также может понравиться