0% found this document useful (0 votes)
3 views10 pages

PSPC Unit I C Basics of Programming in C

The document provides an overview of programming in C, covering its history, basic structure, and key components such as preprocessor directives, data types, variables, functions, and operators. It explains the software development cycle and includes examples of basic functions like printf() and scanf(). Additionally, it details various operators used in C programming, including arithmetic, logical, relational, and bitwise operators.

Uploaded by

gdrivee515
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)
3 views10 pages

PSPC Unit I C Basics of Programming in C

The document provides an overview of programming in C, covering its history, basic structure, and key components such as preprocessor directives, data types, variables, functions, and operators. It explains the software development cycle and includes examples of basic functions like printf() and scanf(). Additionally, it details various operators used in C programming, including arithmetic, logical, relational, and bitwise operators.

Uploaded by

gdrivee515
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/ 10

10/12/2023

Dr. Dileep Kumar Singh


Head, JLU-SOET
[email protected]

Problem Solving and Program


Design using C

Dr. Dileep Kumar Singh 1

Problem Solving and Program Design using C

B Tech/B Tech (Hons.) CSE – 1st Sem.

Basics of Programming in C

Dr. Dileep Kumar Singh 2

General Aspect of ‘C’


• C was originally developed in the 1970s, by Dennis Ritchie at Bell
Telephone Laboratories, Inc.
• C is a High level , general –purpose structured programming language.
• Instructions of C consists of terms that are very closely same to algebraic
expressions, consisting of certain English keywords such as if, else,
for ,do and while
• C contains certain additional features that allows it to be used at a lower
level , acting as bridge between machine language and the high level
languages.
• This allows C to be used for system programming as well as for
applications programming

Dr. Dileep Kumar Singh 3

Dr. Dileep Kumar Singh 1


10/12/2023

Software Development Cycle

Source Program IDE

Compile
Library routines
Edit Link
Other object files
Think Load

Execute

Dr. Dileep Kumar Singh 4

Structure of a C Program

Dr. Dileep Kumar Singh 5

Preprocessor Directives

• The first statement to be checked by the compiler


• Preprocessor Directives always preceded with ‘#’ sign
• They contain information to the compiler which are required by the
compiler during compilation.
• For example:
– #include
• Tells the compiler to include the file stdio.h during compilation
• Anything in the header file will be included a part of the program
– #define VALUE 15
• Tells the compiler to substitute the word VALUE with 15 during compilation

Dr. Dileep Kumar Singh 6

Dr. Dileep Kumar Singh 2


10/12/2023

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

• Comment means explanations, included in a program for


documentation and clarification purpose.
• Comments are completely ignored by the compiler during
compilation and have no effect on program execution.
• Multi line Comments starts with ‘/*’ and ends with ‘*/’
• Single line comments starting with ‘//’

Dr. Dileep Kumar Singh 8

The Character set of ‘C’


• C language consist of some characters set, numbers and some special
symbols. The character set of C consist of all the alphabets of English
language.
• C consist of
– Alphabets a to z, A to Z
– Numeric 0,1 to 9
– Special Symbols {,},[,],?,+,-,*,/,%,!,;,and more
• The words formed from the character set are building blocks of C and
are sometimes known as tokens. These tokens represent the individual
entity of language.
• The following different types of token are used in C
– 1) Identifiers 2)Keywords 3)Constants
– 4) Operators 5)Punctuation Symbols
Dr. Dileep Kumar Singh 9

Dr. Dileep Kumar Singh 3


10/12/2023

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

Dr. Dileep Kumar Singh 10

10

Keywords
auto double int struct
• Keywords are nothing but system
defined identifiers. break else long switch

• Keywords are reserved words of the case enum register typedef


language.
• They have specific meaning in the char extern return union
language and cannot be used by the
const float short unsigned
programmer as variable or constant
names continue for signed void
• C is case sensitive, it means these must
be used as it is default goto sizeof volatile

do if static while

Dr. Dileep Kumar Singh 11

11

Variables & Data Types


• A variable is nothing but a name given to a storage area that our programs can
manipulate. Each variable in C has a specific type, which determines the size and layout
of the variable's memory; the range of values that can be stored within that memory;
and the set of operations that can be applied to the variable.
• The name of a variable can be composed of letters, digits, and the underscore character.
It must begin with either a letter or an underscore. Upper and lowercase letters are
distinct because C is case-sensitive. There are following basic variable types −
Data Type Description
– char typically a single octet(one byte). This is an integer type.
– int The most natural size of integer for the machine.
– float A single-precision floating point value.
– double A double-precision floating point value.
– void Represents the absence of type.

Dr. Dileep Kumar Singh 12

12

Dr. Dileep Kumar Singh 4


10/12/2023

Variables & Data Types

Dr. Dileep Kumar Singh 13

13

Variable Initialization

Dr. Dileep Kumar Singh 14

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)

Dr. Dileep Kumar Singh 15

15

Dr. Dileep Kumar Singh 5


10/12/2023

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.

Dr. Dileep Kumar Singh 16

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

Basic Functions – printf()


• Used to send data to the standard output (usually the monitor)
to be printed according to specific format.
• General format:
– printf(“control string”, variables);
• Control string is a combination of text, format specifier and
escape sequence.
• Example:
– printf(“Thank you”);
– printf (“Total sum is: %d\n”, global_var);
• %d is a format Specifier
• \n is an escape sequence
Dr. Dileep Kumar Singh 18

18

Dr. Dileep Kumar Singh 6


10/12/2023

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

Basic Functions – scanf()


• Reads data from the standard input device (usually keyboard)
and store it in a variable. The General format is:
– scanf(“Control string”, &variable);
• The general format is pretty much the same as printf() except
that it passes the address of the variable (notice the & sign)
instead of the variable itself to the second function argument.
• Example:
– int sal;
– printf(“Enter your salary: “);
– scanf(“%d”, &sal);

Dr. Dileep Kumar Singh 21

21

Dr. Dileep Kumar Singh 7


10/12/2023

Basic Functions – getchar() and putchar()


• getchar() - reads a character from standard input
• putchar() - writes a character to standard output
• Example:
#include <stdio.h>
void main(void)
{
char my_char;
printf(“Please type a character: “);
my_char = getchar();
printf(“\nYou have typed this character: “);
putchar(my_char);
}

Dr. Dileep Kumar Singh 22

22

Operators in C

Operators

Arithmetic Logical Relational Bitwise Assignment Other


operators Operators Operators Operators Operators Operators

Dr. Dileep Kumar Singh 23

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

-- Decrement operator – decreases the value of operand by 1


Dr. Dileep Kumar Singh 24

24

Dr. Dileep Kumar Singh 8


10/12/2023

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

Dr. Dileep Kumar Singh 25

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

Dr. Dileep Kumar Singh 26

26

Bitwise Operators
Operator Description
& Binary AND
| Binary OR
^ Binary XOR
~ Binary one's complement
<< Binary left shift operator
>> Binary right shift operator

Dr. Dileep Kumar Singh 27

27

Dr. Dileep Kumar Singh 9


10/12/2023

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

Dr. Dileep Kumar Singh 28

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

Dr. Dileep Kumar Singh 29

29

Thanks

Dr. Dileep Kumar Singh 30

30

Dr. Dileep Kumar Singh 10

You might also like