PSPC Unit I C Basics of Programming in C
PSPC Unit I C Basics of Programming in C
Basics of Programming in C
Compile
Library routines
Edit Link
Other object files
Think Load
Execute
Structure of a C Program
Preprocessor Directives
Preprocessor Directives
#define PI 3.14159
main() • The result of the compilation is the same
{ ….. for both C program (One with #define and
perimeter = 2*PI*radius; the other without it).
area = PI*radius*radius;
• Which one is preferred (less typing)?
…...
• Which one is more readable?
}
The one with constant definition using
main()
#define preprocessor directive
{
….. • Before compilation, the pre-processor will
perimeter = 2* 3.14159 *radius; replace all PI with 3.14159.
area = 3.14159 *radius*radius;
…...
}
Dr. Dileep Kumar Singh 7
Comments
Identifiers
• A 'C' program consist of two types of elements, user defined
and system defined. Identifier is nothing but a name given to
these elements.
• An identifier is a word used by a programmer to name a
variable, function, or label.
• identifiers consist of letters and digits, in any order, except that
the first character must be letter.
• Both Upper and lowercase letters can be used
10
Keywords
auto double int struct
• Keywords are nothing but system
defined identifiers. break else long switch
do if static while
11
12
13
Variable Initialization
14
Constants
• A constant is a value or an identifier whose value cannot be altered in a
program. For example: 1, 2.5,
• As mentioned, an identifier also can be defined as a constant. eg. const
double PI = 3.14
• Here, PI is a constant. Basically what it means is that, PI and 3.14 is same
for this program.
Integer constants
• A integer constant is a numeric constant (associated with number)
without any fractional or exponential part. There are three types of
integer constants in C programming:
– decimal constant(base 10)
– octal constant(base 8)
– hexadecimal constant(base 16)
15
Constants
Floating-point constants
• A floating point constant is a numeric constant that has either a fractional
form or an exponent form. For example: 2.0,0.0000234 etc.
Character constants
• A character constant is a constant which uses single quotation around
characters. For example: 'a', 'l', 'm', 'F'
String constants
• String constants are the constants which are enclosed in a pair of double-
quote marks. For example: "good" ,"x", “How are you\n“ etc.
16
Basic Functions
• A C program consists of one or more functions that contain a group of statements which
perform a specific task.
• A C program must at least have one function: the function main.
• We can create our own function or use the functions that has been declared in C library
(called Predefined function).
• In order to use Predefined functions, we have to include the appropriate header file
(example: stdio.h).
• We will see a few functions that are pre-defined in the header file stdio.h
– These functions are:
• printf()
• scanf()
• getchar() & putchar()
• In addition to those functions, we will also learn about Format Specifier and Escape
Sequence which are used with printf() and scanf().
Dr. Dileep Kumar Singh 17
17
18
Format Specifier
• Tells the printf() function the format of the output to be printed put.
Format Output Type Output Example
Specifier
%d Signed decimal integer 16
%i Signed decimal integer 16
%o Unsigned octal integer 133
%u Unsigned decimal integer 16
%x Unsigned hexadecimal (small letter) 8c
%X Unsigned hexadecimal (capital letter) 8C
%f Integer including decimal point (float) 16.00
%e Signed floating point (using e notation) 1.6000e+01
%E Signed floating point (using E notation) 1.6000E+01
%c Character ‘a’ or ‘8’
%s String “Hello” or “234”
Dr. Dileep Kumar Singh 19
19
Escape Sequences
Sometimes, it is necessary to use characters which cannot be typed or has special meaning in C
programming. For example: newline(enter), tab, question mark etc. In order to use these
characters, escape sequence is used.
• For example: \n is used for newline. The backslash ( \ ) causes "escape" from the normal way the
characters are interpreted by the compiler.
– Escape Sequences Character
• \b Backspace
• \f Form feed
• \n Newline
• \r Return
• \t Horizontal tab
• \v Vertical tab
• \\ Backslash
• \' Single quotation mark
• \" Double quotation mark
• \? Question mark
• \0 Null character
Dr. Dileep Kumar Singh 20
20
21
22
Operators in C
Operators
23
Arithmetic Operators
Operator Description
+ Addition of two operands
- Subtraction of two operands
* Multiplication of two operands
/ Division of two operands
% Modulus operator (the result is the remainder of the
division)
++ Increment operator – increases the value of operand by 1
24
Logical Operators
Operator Description
&& Logical AND: returns true if both conditions are true
otherwise returns false
|| Logical OR: returns true if one of the conditions is true.
Returns false when both conditions are false
! Logical NOT: negates the condition
25
Relational Operators
Operator Description
== Evaluates whether two operands are equal
!= Not equal to
< Less than
> Greater than
<= Less than equal to
>= Greater than equal to
26
Bitwise Operators
Operator Description
& Binary AND
| Binary OR
^ Binary XOR
~ Binary one's complement
<< Binary left shift operator
>> Binary right shift operator
27
Assignment Operators
Operator Description
= Assigns the value of RHS operand to LHS operand
+= x+=y is equal to x = x+y
-= X-=y is equal to x = x-y
*= X*=y is equal to x = x*y
/= x/=y is equal to x = x/y
28
Other Operators
Operator Description
sizeof Returns the size of its operand (variables, arrays or
Operator expressions)
Ternary Condition? expression1:expression2;
Operator
Member To access the members of structures or unions (dot
Access operator (.) and arrow (->) operator)
Operator
29
Thanks
30