Unit 1 CP
Unit 1 CP
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?
// 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
/herself
Primitive Data type
Derived Data type
• Function
• Array
• Pointer
• Reference
#include <stdio.h>
int main()
{
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
& Bitwise AND Performs bit-by-bit AND 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
%ld long int Used for I/O long signed integer value
#ifndef Used to include a section of code if a certain macro is not defined by #define