0% found this document useful (0 votes)
19 views

Unit 1 CP

Uploaded by

dhanu.srii.002
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Unit 1 CP

Uploaded by

dhanu.srii.002
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

PROGRAMMING IN C

UNIT 1
INTRODUCTION
• C is a procedural programming language initially
developed by Dennis Ritchie in the year 1972 at Bell
Laboratories of AT&T Labs.
• It was mainly developed as a system programming
language to write the UNIX operating system.
The main features of the C language include:
• General Purpose and Portable
• Low-level Memory Access
• Fast Speed
• Clean Syntax
Why Should We Learn C?

• Many later languages have borrowed


syntax/features directly or indirectly from the C
language.
• Like syntax of Java, PHP, JavaScript, and many other
languages are mainly based on the C language.
• C++ is nearly a superset of C language
• The C programming language has several standard
versions, with the most commonly used ones being
C89/C90, C99, C11, and C18.
APPLICATIONS OF C
• Operating systems: C is widely used for developing operating systems such as
Unix, Linux, and Windows.
• Embedded systems: C is a popular language for developing embedded systems
such as microcontrollers, microprocessors, and other electronic devices.
• System software: C is used for developing system software such as device drivers,
compilers, and assemblers.
• Networking: C is widely used for developing networking applications such as web
servers, network protocols, and network drivers.
• Database systems: C is used for developing database systems such as Oracle,
MySQL, and PostgreSQL.
• Gaming: C is often used for developing computer games due to its ability to
handle low-level hardware interactions.
• Artificial Intelligence: C is used for developing artificial intelligence and machine
learning applications such as neural networks and deep learning algorithms.
• Scientific applications: C is used for developing scientific applications such as
simulation software and numerical analysis tools.
• Financial applications: C is used for developing financial applications such as
stock market analysis and trading systems.
STRUCTURE OF C PROGRAM
// Documentation
/*
* file: sum.c
* author: you
* description: program to find sum.
*/

// Link
#include <stdio.h>

// Definition
#define X 20
// Global Declaration
int sum(int y);

// Main() Function
int main(void)
{
int y = 55;
printf("Sum: %d", sum(y));
return 0;
}

// Subprogram
int sum(int y)
{
return y + X;
}
C Character Set
The characters in C are grouped into the
following categories:
• Letters
• Digits
• Special characters
• White spaces
KEYWORDS IN C
• Keywords are predefined, reserved words
used in programming that have special
meanings to the compiler.
• Keywords are part of the syntax and they
cannot be used as an identifier.
• As C is a case sensitive language, all keywords
must be written in lowercase.
IDENTIFIERS IN C
• Identifier refers to name given to entities such as
variables, functions, structures etc.
• Identifiers must be unique. They are created to
give a unique name to an entity to identify it
during the execution of the program. For example:
• int money;
• double accountBalance;
• Here, money and accountBalance are identifiers.
Rules for naming identifiers

• A valid identifier can have letters (both uppercase and


lowercase letters), digits and underscores.
• The first letter of an identifier should be either a letter or an
underscore.
• You cannot use keywords like int, while etc. as identifiers.
• There is no rule on how long an identifier can be. However, you
may run into problems in some compilers if the identifier is
longer than 31 characters.
• Must not contain white space.
 You can choose any name as an identifier if you follow the
above rule, however, give meaningful names to identifiers that
make sense.
DATA TYPES IN C

/herself
Primitive Data type
Derived Data type
• Function
• Array
• Pointer
• Reference
#include <stdio.h>

int main()
{

// declaring array of integers


int arr_int[5];
// declaring array of characters
char arr_char[5];

return 0;
}
User Defined Data type
• Structure
• Union
• Enum
• Typedef
• Typedef is used to redefine the existing data type names.
• Basically, it is used to provide new names to the existing data
types.
• The “typedef” keyword is used for this purpose
Syntax :
typedef existing_name alias_name;
Example :
#include <stdio.h>
typedef char Company;

int main()
{
Company name = “David Smith";
printf(“My name is: %s \n", name);
return 0;
}
• Enum is short for “Enumeration”. It allows the
user to create custom data types with a set of
named integer constants. The “enum” keywo
Syntax :
• enum enum_name {const1, const2, ..., constN};
• Here, the const1 will be assigned 0, const2 = 1,
and so on in the sequence.
#include <stdio.h>

// Declaring a enum
enum Cafes {
Dyu_Art_Cafe,
Tea_Villa_Cafe,
The_Hole_in_the_Wall_Cafe,
Cafe_Azzure,
The_Banaglore_Cafe,
Dialogues_Cafe,
Cafe_Coffee_Day
};

// driver code
int main()
{
enum Cafes today = The_Banaglore_Cafe;
printf("Today is %d\n", today);
return 0;
}
Declarations in C
• Declaration of Variables
• Declaring a Variable as Constant
• Declaring a variable as Volatile
Declaration of Variables

• Declaration does two things:


1. It tells the compiler what the variable name is
2. It specifies what type of data the variable will hold
Syntax:
data-type v1, v2, ….. Vn;
Example:
int count;
int number, total;
double ratio;
Declaring a Variable as Constant

• The value of variables remain constant during


the execution of a program.
Example:
const int class_size = 40;
This tells the compiler that the value of the int
variable class_size must not be modified by the
program
Declaring a variable as Volatile
• It tell explicitly the compiler that a variable’s
value may be changed at any time by some
external sources
Example:
volatile int date;
When we declare a variable as volatile, the
compiler will examine the value of the variable
each time it is encountered to see whether any
external alteration has changed the value.
Statements
• A computer program is a list of "instructions" to be
"executed" by a computer.
• In a programming language, these programming instructions
are called statements.
Example:
printf("Hello World!");
printf("Have a good day!");
return 0;
• The first statement is executed first (print "Hello World!" to
the screen).
• Then the second statement is executed (print "Have a good
day!" to the screen).
• And at last, the third statement is executed (end the C
program successfully).
Symbolic Constants
• These parameters are frequently used to increase
the readability, maintainability.
Syntax:
#define symbolic-name value of constant
VALID Examples: INVALID Examples:
#define STRENGTH 100 #define X = 2.5 #define N 5, M 10
#define PASS_MARK 50 # define MAX 10 #define PRICE$ 100
#define MAX 200 #define N 25;
#define PI 3.14159 #Define ARRAY 11
 We may use the name STRENGTH to define the no. of students and
PASS_MARK to define the pas marks required in a subject. Constant
values are assigned to these names at the beginning of the program.
 Subsequent use of these names in the program has the effect of
causing their defined values to be automatically substituted at the
appropriate places.
Operators in C
Unary Operators
Symbol Operator Description Syntax

++ Increment Increases the value of the a++


operand by 1.

— Decrement Decreases the value of the a–-


operand by 1.
Arithmetic Operators
Symbol Operator Description Syntax

+ Plus Adds two numeric values. a+b

– Minus Subtracts right operand a–b


from left operand.

* Multiply Multiply two numeric a*b


values.

/ Divide Divide two numeric values. a/b

Returns the remainder after


% Modulus diving the left operand with a%b
the right operand.
Relational Operators
Symbol Operator Description Syntax

Returns true if the left


< Less than operand is less than the right a<b
operand. Else false

Returns true if the left


> Greater than operand is greater than the a>b
right operand. Else false

Returns true if the left


<= Less than or equal to operand is less than or equal a <= b
to the right operand. Else
false

Returns true if the left


>= Greater than or equal to operand is greater than or a >= b
equal to right operand. Else
false

== Equal to Returns true if both the a == b


operands are equal.

!= Not equal to Returns true if both the a != b


operands are NOT equal.
Logical Operators
Symbol Operator Description Syntax

&& Logical AND Returns true if both the a && b


operands are true.

|| Logical OR Returns true if both or any a || b


of the operand is true.

! Logical NOT Returns true if the !a


operand is false.
Bitwise Operators
Symbol Operator Description Syntax

& Bitwise AND Performs bit-by-bit AND operation and returns a && b
the result.

| Bitwise OR Performs bit-by-bit OR operation and returns a || b


the result.

^ Bitwise XOR Performs bit-by-bit XOR operation and returns a^b


the result.

~ Bitwise First Flips all the set and unset bits on the number. ~a
Complement

<< Bitwise Leftshift Shifts the number in binary form by one place a << b
in the operation and returns the result.

>> Bitwise Rightshilft Shifts the number in binary form by one place a >> b
in the operation and returns the result.
Assignment Operators
Symbol Operator Description Syntax

= Simple Assignment Assign the value of the right operand to the left operand. a=b

+= Plus and assign Add the right operand and left operand and assign this value a += b
to the left operand.

-= Minus and assign Subtract the right operand and left operand and assign this a -= b
value to the left operand.

*= Multiply and assign Multiply the right operand and left operand and assign this a *= b
value to the left operand.

/= Divide and assign Divide the left operand with the right operand and assign a /= b
this value to the left operand.

%= Modulus and assign Assign the remainder in the division of left operand with the a %= b
right operand to the left operand.
Conditional Operator
• The conditional operator is the only ternary operator in
C.
Syntax:
Condition? Expression1 : Expression2;
• Here, Condition is the condition to be evaluated. If the
condition is true then we will execute and return the
result of Expression1 otherwise if the condition
is false then we will execute and return the result of
Expression2.
• We may replace the use of if..else statements with
conditional operators.
Example:
a = 10;
b = 15;
x = (a>b) ? a : b;
Expressions
Constant expressions
Examples:5, 10 + 5 / 6.0, ‘x’
Integral expressions
Examples: x, x * y, x + int( 5.0)
Floating expressions
Examples: x + y, 10.75

Relational expressions
Examples: x <= y, x + y > 2

Logical expressions
Examples: x > y && x == 10, x == 10 || y == 5

Pointer expressions
Examples: &x, ptr, ptr++

Bitwise expressions
Examples: x << 3
Formatted I/O Functions
• Formatted I/O functions are used to take
various inputs from the user and display
multiple outputs to the user.
• These types of I/O functions can help to
display the output to the user in different
formats using the format specifiers.
• These functions are called formatted I/O
functions because we can use format
specifiers in these functions according to
our needs.
1.printf()
2.scanf()
Format Specifiers
Format Specifier Type Description

%d int/signed int used for I/O signed integer value

%c char Used for I/O character value

%f float Used for I/O decimal floating-point value

%s string Used for I/O string/group of characters

%ld long int Used for I/O long signed integer value

%u unsigned int Used for I/O unsigned integer value

%i unsigned int used for the I/O integer value

%lf double Used for I/O fractional or floating data


Printf()
• printf() function is used in a C program to
display any value like float, integer, character,
string, etc on the console screen.
• It is a pre-defined function that is already
declared in the stdio.h(header file).
Syntax 1: To display any variable value.
printf(“Format Specifier”, var1, var2, …., varn);
Syntax 2: To display any string or a message
printf(“Enter the text”);
Example
#include<stdio.h>
int main()
{
int a,b;
a = 20, b = 30;
printf(" The value of a&b is :
");
printf("%d,%d", a,b);
}
Scanf()
• scanf() function is used in the C
program for reading or taking any
value from the keyboard by the user
• These values can be of any data type
like integer, float, character, string,
and many more.
Syntax:
scanf(“Format Specifier”, &var1, &var2,
…., &varn);
Example
#include <stdio.h>
int main()
{
int num1;
printf("Enter a integer number: ");
scanf("%d",&num1);
printf("You have entered %d", num1);
}
Unformatted I/O Functions
• Unformatted I/O functions are used only for
character data type or character array/string and
cannot be used for any other datatype.
• These functions are used to read single input from
the user at the console and it allows to display the
value at the console.
• These functions are called unformatted I/O
functions because we cannot use format specifiers
in these functions and hence, cannot format these
functions according to our needs.
1. getch()
2. putch()
3. getchar()
4. putchar()
getch():
getch();
or
variable-name = getch();
getchar():
variable-name = getchar();
putch():
putch(variable_name);
putchar():
putchar(variable_name);
Example
#include <conio.h>
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character: ");
ch = getchar();
printf("Entered character is: ");
putchar(ch);
return 0;
}
Preprocessor in C
• Preprocessor programs provide
preprocessor directives that tell the
compiler to preprocess the source code
before compiling.
• All of these preprocessor directives
begin with a ‘#’ (hash) symbol. The ‘#’
symbol indicates that whatever
statement starts with a ‘#’ will go to the
preprocessor program to get executed.
• We can place these preprocessor
directives anywhere in our program.
Types of Preprocessor
Directives
1.Macros
2.File Inclusion
3.Conditional Compilation
4.Miscellaneous directives
Preprocessor
Description
Directives

#define Used to define a macro

#include Used to include a file in the source code program

#ifdef Used to include a section of code if a certain macro is defined by #define

#ifndef Used to include a section of code if a certain macro is not defined by #define

#if Check for the specified condition

#else Alternate code that executes when #if fails

#endif Used to mark the end of #if, #ifdef, and #ifndef

#undef Used to undefine a macro


Storage Classes
• C Storage Classes are used to describe
the features of a variable/function.
• These features basically include the
scope, visibility, and lifetime which help
us to trace the existence of a particular
variable during the runtime of a program.
4 Types:
1. Auto
2. Extern
3. Static
4. Register
Declaration of Storage Class
Syntax:
Storage class data-type variable name;
Examples:
auto int count;
register char ch;
static int x;
extern long total;

You might also like