0% found this document useful (0 votes)
2 views72 pages

C Programming PDF International (Unit 1,2,7,8,9)

The document provides an overview of programming in C using Turbo C/C++ IDE, covering essential concepts such as C tokens, keywords, identifiers, constants, and data types. It explains the structure of a C program, the character set, and the rules for naming variables and identifiers. Additionally, it categorizes constants and data types, detailing their characteristics and usage in C programming.

Uploaded by

yamkumar077a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views72 pages

C Programming PDF International (Unit 1,2,7,8,9)

The document provides an overview of programming in C using Turbo C/C++ IDE, covering essential concepts such as C tokens, keywords, identifiers, constants, and data types. It explains the structure of a C program, the character set, and the rules for naming variables and identifiers. Additionally, it categorizes constants and data types, detailing their characteristics and usage in C programming.

Uploaded by

yamkumar077a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

C Tokens, Escape Sequences, Delimiters,

Variables, Data Types, Constants/Literals,


Expressions, Statements and Comments,
Preprocessor Directives, typedef Statement

1
 We will do programming in a Turbo C/C++ IDE
(Integrated Development Environment).
 Turbo C is a Borland C compiler for the C
programming language.
 It will be named TC and placed in C-drive.
 To write a program in TC, goto
C:\TC\BIN\TC.exe and double click it. Now
write your program.
 The programs that will be written by us will
be saved with a .c extension and placed in
the BIN directory. E.g. abc.c
 The filename should be <= 8 characters.

2
/* This program is written by ……….*/
// Date: 11/29/2011.........Documentation Section
#include <stdio.h> // Link Section
#include <conio.h> // Link Section
void main() // main() starts here
{
clrscr(); // clears the screen
printf(“Hello World”); // prints Hello World
getch(); // to hold the output screen
}

3
 The set of characters that are used to form
words, numbers and expressions in C is called
C character set.
 The combination of these characters form
words, numbers and expressions.
 The C character set is grouped into the
following four categories:
1) Letters or alphabets
2) Digits
3) Special Characters
4) White Spaces
4
 Letters
 Uppercase: A……Z
 Lowercase: a……z
 Digits
 All decimal digits: 0……9
 Special Characters
 , comma
 . period
 ; semicolon
 : colon
 ? question mark
 „ apostrophe
 “ quotation mark
 ! exclamation mark
 | vertical bar

5
 / slash
 \ backslash
 ~ tilde
 _ underscore
 $ dollar sign
 % percent sign
 & ampersand
 ^ caret
 * asterisk
 - minus sign
 + plus sign
 < opening angle bracket (or less than sign)
 > closing angle bracket (or greater than sign)
 ( left parenthesis
 ) right parenthesis

6
 [ left bracket
 ] right bracket
 { left brace
 } right brace
 # number sign

 White Spaces The carriage return (CR)


commands a display system to
 Blank space move the position of the cursor to
 Horizontal tab the first position on the same line.
It is mostly used along with line
 Carriage return feed (LF), a move to the next line,
 New line so that together they start a new
line. Together, this sequence can
 Form feed be referred to as CRLF.

7
 In a passage of text, individual words and
punctuation marks are called tokens.
Similarly, in a C program the smallest
individual units are known as C tokens.
 The basic elements recognized by the C
compiler are known as “C tokens”.
 E.g. of C tokens are: keywords (e.g. float,
while), identifiers (e.g. num, sum), constants
(e.g. -15.5, 100), string literals (e.g. “ABC”,
“year”), operators (e.g. +,-,*,/) and special
symbols (e.g. [], {}, (), ,).

8
 Every C word is classified as either a keyword
or an identifier.
 Keywords are predefined words in C
programming language.
 All keywords have fixed meaning and these
meanings cannot be changed.
 Keywords serve as basic building blocks for
program statements.
 Keywords are also called reserved words
because they are used for pre-defined
purposes and cannot be used as identifiers.
9
There are generally 32 keywords:
 auto
 break
 case
 char
 const
 continue
 default
 do

10
 double
 else
 enum
 extern
 float
 for
 goto
 if
 int
long

11
 register
 return
 short
 signed
 sizeof
 static
 struct
 switch
typedef

12
 union
 unsigned
 void
 volatile
 while

 note: keywords are written in lower case

13
 Every word used in C program to refer to the
names of variables, functions, arrays,
pointers and symbolic constants are called
identifiers.
 These are user-defined names and consist of
a sequence of letters and digits, with a letter
as the first character.
 Both uppercase and lowercase letters can be
used, although lowercase letters are
commonly preferred.
 The underscore character can also be used to
link between two words in long identifiers.

14
1) First character must be an alphabet (or
underscore).
2) Must consist of only letters, digits or
underscore.
3) Must not contain white space. Only
underscore is permitted.
4) Keywords cannot be used.
5) Only first 31 characters are significant.
6) It is case-sensitive, i.e. uppercase and
lowercase letters are not interchangeable.

15
Determine which of the following are valid
identifiers? If invalid, explain why?
(a) keyword (b) n1+n2
(c) file_3 (d) #ph_no
(e) double (f) Rs2000
(g) doubles (h) first_name
(i) 2var (j) first-name
(k) $1000 (g) return
(h) _1 (j) inta
(k) _ (l) __
16
#include <stdio.h>
#include <conio.h>
void main()
{
int abcdefghijklmnopqrstuvwxyz123456789;
int abcdefghijklmnopqrstuvwxyz123456;
clrscr();
getch();
}

17
 Constants in C refer to fixed values that do
not change during the execution of a
program.
 C constants can be divided into different
categories:
 Numeric Constants
 Integer Constants
 Real Constants
 Character Constants
 Single Character Constants
 String Constants

18
 Integer Constant refers to a sequence of
digits (at least one digit) with no decimal
point and either positive or negative.
 If no sign precedes an integer constant, it is
assumed to be positive.
 No commas or blank spaces are allowed
within the integer constant.
 E.g. 0, 123, +365, -555, etc.
 $1000, 20,000, 22 329, are not allowed.

19
 Three types:
 Decimal Integer Constants
 Octal Integer Constants
 Hexadecimal Integer Constants

Decimal Integer Constants: Decimal integers


consist of a set of digits, 0 through 9, preceded
by an optional – or + sign. E.g. +78, 0, 123, etc.

Octal Integer Constants: An octal integer constant


consists of any combination of digits from the set
0 through 7, with a leading 0. E.g. 056, 0, 0123,
etc
20
Hexadecimal Integer Constants:
• A hexadecimal integer constant consists of any
combination of digits from the set 0 to 9, and
alphabets A through F or a through f with a
leading 0X or 0x.
• The letters A through F represent the numbers 10
through 15.
• E.g. 0X2, 0x9F, 0Xabc, 0x0, etc.

• NOTE: Octal and Hexadecimal numbers are rarely


used in programming.
21
#include <stdio.h>
#include <conio.h>

void main()
{
int a,b,c;
clrscr();
a=10;
b=010;
c=0xFF;
printf("Decimal integer is %d",a);
printf("\nOctal integer is %d",b);
printf("\nHexadecimal integer is %d",c);
getch();
}

22
 Integer numbers are inadequate to represent
quantities that vary continuously, such as
distances, heights, temperatures, prices and
so on.
 These quantities are represented by numbers
having fractional parts like 3.14.
 Such numbers are called real (or floating
point) constants.
 E.g. 0.00123, -0.25, +125.0, 23.35, etc
 Real constants can be written into two
forms: fractional form and exponential form.
23
 Fractional form constants must have at least
one digit and a decimal point.
 It can either be positive or negative but the
default sign is positive.
 Commas or blank spaces are not allowed
within a real constant.
 E.g. +23.45, 456.0, -23.35, -5544.312, etc
are in fractional form.

24
 In exponential form of representation, the
real constant is represented in two parts as,
mantissa e exponent
 The digits before „e‟ is called mantissa and
after is called exponent.
 The mantissa part may have a positive or
negative sign, but the default is positive.
 The exponent must have at least one digit
(must be integer), which can be either
positive or negative.
 E.g. -3.2e-4 implies [-3.2*10-4]
-0.2e+3 implies [-0.2*103]
25
#include <stdio.h>
#include <conio.h>

void main()
{
float x;
clrscr();
x=-3.2e-4;
printf("\nFloating point constant is %f",x);
getch();
}

26
A single character constant (or simply
character constant) contains a single
character alphabet, a digit or a special
symbol enclosed within a pair of single quote
marks.
 E.g. „5‟, „X‟, „;‟ „ ‟, etc.
 Character constants have integer values
known as ASCII values.
 NOTE: The character constant „5‟ is not the
same as the number 5.
 NOTE: „A‟ is a valid character constant but
„AA‟ is not.
27
#include <stdio.h>
#include <conio.h>

void main()
{
int n = 4;
char c = „4‟;
clrscr();
n = n + 5; //n=4+5=9
c = c + 5; //c=52+5=57
printf(“Integer = %d”,n);
printf(“\nCharacter = %d”,c);
getch();
}
28
#include <stdio.h> Note: To find the ASCII value of
#include <conio.h> \, we have to write a=„\\‟;.
Writing a=„\‟; gives error.
void main() Similary to find out ASCII values
{ of enter and backspace keys we
have to write „\n‟ and „\b‟.
char x;
x='A';
clrscr();
printf("The character %c's ASCII value is:%d", x,
x);
getch();
}

29
 A string constant is a sequence of characters
enclosed in double quotes.
 The characters may be letters, numbers, special
characters and blank space. However, it does not
have an equivalent ASCII value.
 E.g. “Hi!”, “2011”, “WELL DONE”, “?...!”,
“5+3”, “X”, etc.

 NOTE:
A character constant (e.g. „X‟) is not
equivalent to the single character string constant
(e.g. “X”).
Also, “5+3” is a string rather than an
arithmetic operation

30
 A variable is a data name that is used to store a data
value.
 Unlike constants that remain unchanged during the
execution of a program, a variable may take different
values at different times during execution.
 Since a variable is an identifier, the rules for naming
variables are similar to those of identifiers.
 A variable name can be chosen by the programmer in
a meaningful way so as to reflect its function or
nature in the program.
 E.g. Average, sum, counter, first_name, etc.
 123, (area), %, 25th, Price$, blood group, etc. are
not allowed.

31
 Any variable should be defined before using
it in a program.
 The variables are defined or declared using
following syntax:
data_type variable_name;
where variable_name is the name of the variable.

 E.g. int a;
float radius;
char gender;
int x1,x2,x3;

32
 There are various types of data. For e.g., data
10 and 10.5 are of different types. The data 10 is
an integer while 10.5 is a fractional number.
 C language is rich in its data types. There are
other varieties of data types available in C, each
of which may be represented differently within
computer‟s memory. These varieties of data
types allow the programmer to select the type
needed in his/her application.
 ANSI C supports 3 classes of data types:
 Primary (or fundamental) data types
 Derived data types
 User-defined data types

33
 User-defined data types, and Derived data
types (such as arrays, functions, structures
and pointers) will be discussed later.
 The primary data types are categorized into
five types:
I. Integer type (int)
II. Floating point type (float)
III. Double-precision floating type (double)
IV. Character type (char)
V. Void type (void)

34
 Integers are whole numbers (positive, negative
and 0), i.e. non-fractional numbers.
 Generally, integers occupy one word of storage,
and since the word sizes of machines vary
(typically, 16 or 32 bits), the size of an integer
that can be stored depends on the computer.
 If we use a 16 bit word length, the size of the
integer value is limited to the -32768 to +32767
(i.e. -215 to +215-1). This is because one bit is
used for sign and other 15-bits for magnitude.
 Note: By default all integers are signed unless
explicitly specified
 Note: -32768=1000000000000000 (Computer uses
2‟s complement method to transform positive
numbers to negative numbers).
35
 In C, integers are divided into three classes:
 Integer (int)
 Short Integer (short int)
 Long Integer (long int)
in both signed and unsigned forms.

36
 By default, all integers are signed implicitly.
 It represents both positive and negative integers.
 The data type qualifier is signed int or int.
E.g. Variables are defined as-
signed int a;
int b;
 For a 16-bit compiler like Turbo C or Turbo C++,
it reserves 16-bits (2 bytes) in memory.
 One bit is used for sign and the other 15 bits for
magnitude of the integer. The sign bit is 1, if the
number is negative and 0 when the number is
positive.
 The range of signed integers is -32768 to 32767
i.e. -215 to 215-1.
 The conversion character is d.

37
 Unsigned integers have to be declared
explicitly.
 It represents only positive integers.
 The data type qualifier is unsigned int or
unsigned.
E.g. Variables are defined as-
unsigned int a;
unsigned b;
 It reserves 16-bits (2 bytes) in memory.
 The range of unsigned integers is 0 to 216-1,
i.e. 0 to 65535.
 The conversion character is u.

38
 By default all short integers are signed.
 It represents both positive and negative integers.
 The data type qualifier is signed short int or
short int or short.
E.g. Variables are defined as-
signed short int a;
short int b;
short c;
 It reserves 16-bits (2 bytes) in memory.
 The range of signed short integers is -215 to 215-
1, i.e. -32768 to 32767.
 The conversion character is d or i.

39
 Unsigned short integers have to be declared
explicitly.
 It represents only positive integers.
 The data type qualifier is unsigned short int or
unsigned short.
E.g. Variables are defined as-
unsigned short int a;
unsigned short b;
 It reserves 16-bits (2 bytes) in memory.
 The range of unsigned short integers is 0 to 216-
1, i.e. 0 to 65535.
 The conversion character is u.

40
 By default, all long integers are signed.
 It represents both positive and negative integers.
 The data type qualifier is signed long int or long
int or long.
E.g. Variables are defined as-
signed long int a;
long int b;
long c;
 It reserves 32-bits (4 bytes) in memory.
 The range for signed long integers is -231 to 231-
1, i.e. -2147483648 to 2147483647.
 The conversion character is ld.

41
 Unsigned long integers have to be declared
explicitly.
 It represents only positive integers.
 The data type qualifier is unsigned long int or
unsigned long.
E.g. Variables are defined as-
unsigned long int a;
unsigned long b;
 It reserves 32-bits (4 bytes) in memory.
 The range for unsigned long integers is 0 to
232-1, i.e. 0 to 4294967295.
 The conversion character is lu.

42
 We declare long and unsigned integers to
increase the range of integer values.
 The use of qualifier signed is optional.
 The conversion character tells the computer
to print the given data into or from the
computer program.
 Table
Compiler short int long

16-bit (Turbo C/C++) 2 2 4

32-bit (Visual C++) 2 4 4

43
 Write a C program to add two integers and
display the result?

44
 Floating point types represent fractional
numbers (i.e. real numbers).
 The data type qualifier is float.
E.g. Variables are defined as-
float a;
 Floating numbers reserve 32 bits (i.e. 4
bytes) of storage, with 6 digits of precision.
 The range is -3.4e38 to +3.4e38.
 The conversion character is f.

45
Double precision floating point type

 When the accuracy provided by a float number is


not sufficient, the type double can be used to
define the number.
 A double data type number uses 64 bits (8 bytes)
giving a precision of 14 digits. These are known
as double precision numbers.
 double represents fractional number of the
range -1.7e308 to +1.7e308.
 The data type qualifier is double.
E.g. Variables are defined as-
double a;
 Its conversion character is lf.

46
 To extend the precision further, long double
can be used which uses 80 bits (10 bytes)
giving 18 digits of precision.
 long double represents fractional number of
the range -1.7e4932 to +1.7e4932.
 The data type qualifier is long double.
E.g. Variables are defined as-
long double a;
 Its conversion character is Lf.

47
A single character can be defined as a
character type data.
 Characters are stored in 8 bits (1 byte).
 The data type qualifier is char.
 The qualifier signed or unsigned may be used
with char.
 The unsigned char has values between 0 and
255 while signed char has values from -128 to
127.
 The conversion character is c.

48
 In character data type, each character is
represented by an ASCII (American Standard
Code for Information Interchange) value
internally.
 For e.g. the character „A‟ is represented by
65, „B‟ by 66 and so on for „Z‟ by 90 and
similarly for others.
 When a character is displayed using the
conversion character d, it will display the
ASCII value; and when it is displayed using
the conversion character c, it will display the
character.

49
# include <stdio.h>
# include <conio.h>
void main()
{
char c, d;
clrscr();
c=„A‟;
d=„B‟;
printf(“The character is %c”, c);
printf(“\nThe ASCII value of character is %d”, d);
printf(“\n\nThe ASCII value of %c is %d”, d,d);
getch();
}

50
 The void type has no values.
 It is usually used to specify a type of function
when it does not return any value to the
calling function.

51
52
 An escape sequence is a non-printing
character used in C.
 It is a character combination consisting of a
backslash (\) followed by a letter or a digit.
 Escape sequences always represent single
characters, even though they are written in
terms of two or more characters.
 Escape sequences has a single ASCII value.
 Escape sequences are useful for formatting
input and output.

53
Escape Sequence Use

\a Audible Alert or beep sound

\b Backspace delete on character to


the left
\n Move cursor to the Next or New line
of the screen

\v Vertical tab

\t Horizontal tab

\‟ Single quote

\” Double quote

\\ Backslash

\0 Null character

54
#include<stdio.h>
#include<conio.h>

void main()
{
clrscr();
printf(“\aHello \t World \n");
printf("He said \"Hello\" ");
getch();
}

55
printf() and scanf()
 Any text written within double quotes (“”) in
printf() function is displayed in the screen.
 For taking input from the user, scanf() is used
along with appropriate data type conversion
character.
 E.g.
int radius;
printf(“Enter the radius of circle”);
scanf(“%d”, radius);

56
57
58
59
 These are placed in the source program
before the main function.
 When our source code is compiled, it is
examined by the preprocessor for any
preprocessor directives. If there are any,
appropriate actions are taken and then the
source program is handed over to the
compiler.
 They follow special syntax rules: They all
begin with the symbol # (hash) and do not
require a ; (semicolon) at the end.

60
 E.g.
#include <stdio.h>
#define PI 3.14
#define TRUE 1
#define FALSE 0
 These statements are called preprocessor
directives as they are processed before
compilation of any source code in the
program.
 NOTE: The other codes in the program are
compiled sequentially line by line.

61
A symbolic constant is a name that is used in
place of a sequence of characters. The
character may represent numeric constant, a
character constant or a string constant.
 When a program is compiled, each
occurrence of a symbolic constant is
replaced by its corresponding character
sequence.
 The symbolic constants are defined at the
beginning of the program.
 SYNTAX: #define name value
 E.g. #define PI 3.1416
62
RULES
 Symbolic constant names are same as
variable names. Convention: Use capital
letters while defining symbolic constants.
 No blank space permitted between # and
name.
 A blank space is required between #define
and symbolic name and between symbolic
name and its value (i.e. constant).

63
64
65
void main()
{
int age of man;
age of man = 10.5;
printf(“The age of man is = %.2f” age of man);
getch();
}

66
void main()
{
int num=31;
clrscr();
printf(“num=%X”,num);
getch();
}

67
void main()
{
int a=10,b=5,c;
clrscr();
c=a+b;
printf("\nThe sum is:%d",c);

printf("\nThe sum is:%x",c);


printf("\nThe sum is:%X",c);
printf("\nThe sum is:%o",c);
getch();
}

68
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
printf("\nab");
printf("\bsi");
printf("\rha");
//printf("\t");
getch();
}
69
 Used for program documentation.
 Comments are not compiled.
 The C syntax for writing comment is
/*
Anything written in between slash and
asterisk and asterisk and slash is comment
*/
 Another way to write comment in C
// Using double slash (This line only)

70
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned char a='A'; //ASCII value of A=65
a=a+255;
clrscr();
printf("\n%c %d", a, a); //???????????
getch();
}
71
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
printf(“\n Character with ASCII value %d is %c",
241, 241);
getch();
}

72

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy