0% found this document useful (0 votes)
35 views44 pages

Lec 7&8

Com

Uploaded by

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

Lec 7&8

Com

Uploaded by

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

What is C?

• C programming is a general-purpose, procedural, imperative


computer programming language developed in 1972 by Dennis M.
Ritchie at the Bell Telephone Laboratories to develop the UNIX
operating system.
• C is the most widely used computer language.
• It keeps fluctuating at number one scale of popularity along with Java
programming language, which is also equally popular and most widely
used among modern software programmers.
What is C used for?

• C is a versatile language that has uses in many different areas. It’s mainly
used for creating system applications, meaning that operating systems
such as Windows and Unix use a lot of C programming.
• Many programmers use C to create games, graphics, and apps that use
lots of calculations.
• Easy to learn
• Structured language
• It produces efficient programs
• It can handle low-level activities
• It can be compiled on a variety of computer platforms
Facts about C
• C was invented to write an operating system called UNIX.
• C is a successor of B language which was introduced around the early
1970s.
• The language was formalized in 1988 by the American National
Standard Institute (ANSI).
• The UNIX OS was totally written in C.
• Today C is the most widely used and popular System Programming
Language.
• Most of the state-of-the-art software have been implemented using C.
A C program basically consists of the following parts −

• Preprocessor Commands
• Functions
• Variables
• Statements & Expressions
• Comments
C Keywords and Identifiers

Uppercase: A B C ................................... X Y Z
Sample Program

#include <stdio.h>
int main()
{
/* my first program in C */
printf("Hello, World! \n");
return 0;
}
Let us take a look at the various parts of the above program −
• The first line of the program #include <stdio.h> is a preprocessor
command, which tells a C compiler to include stdio.h file before going
to actual compilation.
• The next line int main() is the main function where the program
execution begins.
• The next line /*...*/ will be ignored by the compiler and it has been
put to add additional comments in the program. So such lines are
called comments in the program.
• The next line printf(...) is another function available in C which causes
the message "Hello, World!" to be displayed on the screen.
• The next line return 0; terminates the main() function and returns the
value 0.
Compile and Execute C Program
Following are the simple steps −
• Open a text editor and add the above-mentioned code.
• Save the file as hello.c
• Open a command prompt and go to the directory where you have saved the
file.
• Type gcc hello.c and press enter to compile your code.
• If there are no errors in your code, the command prompt will take you to the
next line and would generate a.out executable file.
• Now, type a.out to execute your program.
• Display the output "Hello World" printed on the screen.
Tokens in C

A C program consists of various tokens and a token is either a keyword, an


identifier, a constant, a string literal, or a symbol.
For example,
the following C statement consists of five tokens −

printf("Hello, World! \n");


• Semicolons
• Comments
• Identifiers
• A C identifier is a name used to identify a variable, function, or any other
user-defined item. An identifier starts with a letter A to Z, a to z, or an
underscore '_' followed by zero or more letters, underscores, and digits (0 to
9).
Keywords

These reserved words may not be used as constants or variables or any other
identifier names.

auto else long switch

break enum register typedef

case extern return union

char float short unsigned

const for signed void

continue goto sizeof volatile

default if static while

do int struct _Packed

double
Whitespace in C
• A line containing only whitespace, possibly with a comment, is known as a
blank line, and a C compiler totally ignores it.
Data Types

Data types in c refer to an extensive system used for declaring variables


or functions of different types. The type of a variable determines how
much space it occupies in storage and how the bit pattern stored is
interpreted.
The types in C can be classified as follows −

Sr.No. Types & Description


1 Basic Types
They are arithmetic types and are further classified into: (a) integer types and (b) floating-
point types.
2 Enumerated types
They are again arithmetic types and they are used to define variables that can only assign
certain discrete integer values throughout the program.
3 The type void
The type specifier void indicates that no value is available.

4 Derived types
They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and
(e) Function types.
C - Variables
Sr.No. Type & Description
1 char
Typically a single octet(one byte). It is an integer type.

2 int
The most natural size of integer for the machine.

3 float
A single-precision floating point value.

4 double
A double-precision floating point value.

5 void
Represents the absence of type.
Integer Types
The following table provides the details of standard integer
types with their storage sizes and value ranges −

Type Storage size Value range


char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
-32,768 to 32,767 or -2,147,483,648
int 2 or 4 bytes to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 8 bytes or (4bytes -9223372036854775808 to
for 32 bit OS) 9223372036854775807
unsigned long 8 bytes 0 to 18446744073709551615
Floating-Point Types
The following table provide the details of standard floating-point types with
storage sizes and value ranges and their precision −

Type Storage size Value range Precision

float 4 byte 1.2E-38 to 3.4E+38 6 decimal places

double 8 byte 2.3E-308 to 1.7E+308 15 decimal places

long double 10 byte 3.4E-4932 to 1.1E+4932 19 decimal places


The void Type
The void type specifies that no value is available. It is used in three kinds of
situations −

Sr.No. Types & Description


1 Function returns as void
There are various functions in C which do not return any value or you
can say they return void. A function with no return value has the return
type as void. For example, void exit (int status);

2 Function arguments as void


There are various functions in C which do not accept any parameter. A
function with no parameter can accept a void. For example, int
rand(void);
3 Pointers to void
A pointer of type void * represents the address of an object, but not its
type. For example, a memory allocation function void *malloc( size_t
size ); returns a pointer to void which can be casted to any data type.
Variable Definition in C
type variable_list;

int i, j, k;
char c, ch;
float f, salary;
double d;
Constants

• There are two simple ways in C to define constants −


• Using #define preprocessor.
• Using const keyword.
const type variable = value;

#include <stdio.h>
int main()
{
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
C - Operators

• An operator is a symbol that tells the compiler to perform specific


mathematical or logical functions.
• C language is rich in built-in operators and provides the following
types of operators −
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
Arithmetic Operators
A=10 and B= 20

Operato Description Example


r

+ Adds two operands. A + B = 30


− Subtracts second operand from the first. A − B = -10

* Multiplies both operands. A * B = 200


/ Divides numerator by de-numerator. B/A=2

% Modulus Operator and remainder of after B%A=0


an integer division.

++ Increment operator increases the integer A++ = 11


value by one.

-- Decrement operator decreases the integer A-- = 9


value by one.
Relational Operators

Operator Description Example

== Checks if the values of two operands are equal or not. If yes, (A == B) is


then the condition becomes true. not true.

!= Checks if the values of two operands are equal or not. If the (A != B) is


values are not equal, then the condition becomes true. true.

> Checks if the value of left operand is greater than the value of (A > B) is
right operand. If yes, then the condition becomes true. not true.

< Checks if the value of left operand is less than the value of right (A < B) is
operand. If yes, then the condition becomes true. true.

>= Checks if the value of left operand is greater than or equal to (A >= B) is
the value of right operand. If yes, then the condition becomes not true.
true.
<= Checks if the value of left operand is less than or equal to the (A <= B) is
value of right operand. If yes, then the condition becomes true. true.
Logical Operators

Operator Description Example

&& Called Logical AND operator. If both the operands are non- (A && B) is false.
zero, then the condition becomes true.

|| Called Logical OR Operator. If any of the two operands is non- (A || B) is true.


zero, then the condition becomes true.

! Called Logical NOT Operator. It is used to reverse the logical !(A && B) is true
state of its operand. If a condition is true, then Logical NOT
operator will make it false.
Bitwise Operators

• Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for
&, |, and ^ is as follows −
p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

• Assume A = 60 and B = 13 in binary format, they will be as follows −


• A = 0011 1100
• B = 0000 1101
• -----------------
• A&B = 0000 1100
• A|B = 0011 1101
Assignment Operators

Opera Description Example


tor
= Simple assignment operator. Assigns values from right side C = A + B will assign the
operands to left side operand value of A + B to C
+= Add AND assignment operator. It adds the right operand to the left C += A is equivalent to C
operand and assign the result to the left operand. =C+A
-= Subtract AND assignment operator. It subtracts the right operand C -= A is equivalent to C
from the left operand and assigns the result to the left operand. =C-A
*= Multiply AND assignment operator. It multiplies the right operand C *= A is equivalent to C
with the left operand and assigns the result to the left operand. =C*A
/= Divide AND assignment operator. It divides the left operand with C /= A is equivalent to C
the right operand and assigns the result to the left operand. =C/A
%= Modulus AND assignment operator. It takes modulus using two C %= A is equivalent to C
operands and assigns the result to the left operand. =C%A
<<= Left shift AND assignment operator. C <<= 2 is same as C = C
<< 2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C
>> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C
&2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C
^2
|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C |
The Standard Files

• The getchar() and putchar() Functions


• The int getchar(void) function reads the next available character from the
screen and returns it as an integer. This function reads only single character at
a time.
• The int putchar(int c) function puts the passed character on the screen and
returns the same character. This function puts only single character at a time.
#include <stdio.h>
int main( )
{
int c;
printf( "Enter a value :");
c = getchar( );
printf( "\nYou entered: ");
putchar( c );
return 0;
}
• The gets() and puts() Functions
• The char *gets(char *s) function reads a line from stdin into the buffer
pointed to by s until either a terminating newline or EOF (End of File).
• The int puts(const char *s) function writes the string 's' and 'a' trailing
newline to stdout.

#include <stdio.h>
int main( )
{
char str[100];
printf( "Enter a value :");
gets( str );
printf( "\nYou entered: ");
puts( str );
return 0;
}
The scanf() and printf() Functions
• The int scanf(const char *format, ...) function reads the input from the
standard input stream stdin and scans that input according to
the format provided.
• The int printf(const char *format, ...) function writes the output to the
standard output stream stdout and produces the output according to the
format provided.
• The format can be a simple constant string, but you can specify %s, %d, %c,
%f, etc., to print or read strings, integer, character or float respectively.
#include <stdio.h>
int main( )
{
char str[100];
int i;
printf( "Enter a value :");
scanf("%s %d", str, &i);
printf( "\nYou entered: %s %d
", str, i);
return 0;
}
C - Decision Making

• Decision making structures require that the programmer specifies one or more
conditions to be evaluated or tested by the program, along with a statement or
statements to be executed if the condition is determined to be true, and
optionally, other statements to be executed if the condition is determined to be
false.
Sr.No. Statement & Description
1 if statement

An if statement consists of a boolean expression followed by one


or more statements.
2 if...else statement

An if statement can be followed by an optional else statement,


which executes when the Boolean expression is false.
3 nested if statements

You can use one if or else if statement inside another if or else


if statement(s).
The if Statement
Use the if statement to specify a block of C code to be
executed if a condition is true.

Syntax

if (condition)
{
// block of code to be executed if the
condition is true
}
Example
int x = 20;
int y = 18;
if (x > y)
{
printf("x is greater than y");
}
The else Statement

Syntax
if (condition)
{
// block of code to be executed if the condition is true
}
else
{
// block of code to be executed if the condition is false
}
Example
int time = 20;
if (time < 18)
{
printf("Good day.");
}
else
{
printf("Good evening.");
}
The else if Statement

Syntax
if (condition1)
{
// block of code to be executed if condition1 is true
}
else if (condition2)
{
// block of code to be executed if the condition1 is false and condition2 is true
}
else
{
// block of code to be executed if the condition1 is false and condition2 is false

}
Example

int time = 22;


if (time < 10)
{
printf("Good morning.");
}
else if (time < 20)
{
printf("Good day.");
}
else
{
printf("Good evening.");
}
C - Loops

• when a block of code needs to be executed several number of times.


• In general, statements are executed sequentially: The first statement in a function
is executed first, followed by the second, and so on.
Sr.No. Loop Type & Description
1 while loop
Repeats a statement or group of statements while a given
condition is true. It tests the condition before executing
the loop body.
2 for loop
Executes a sequence of statements multiple times and
abbreviates the code that manages the loop variable.

3 do...while loop
It is more like a while statement, except that it tests the
condition at the end of the loop body.
While Loop

while (condition)
{
// code block to be executed
}
Example
int i = 0;

while (i < 5)
{
printf("%d\n", i);
i++;
}
The Do/While Loop

Syntax
Do
{
// code block to be executed
}
while (condition);
Example

int i = 0;

do
{
printf("%d\n", i);
i++;
}
while (i < 5);
For Loop

Syntax
for (statement 1; statement 2; statement 3)
{
// code block to be executed
}
Example

int i;
for (i = 0; i < 5; i++)
{
printf("%d\n", i);
}

You might also like