Lec 7&8
Lec 7&8
• 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
These reserved words may not be used as constants or variables or any other
identifier names.
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
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 −
int i, j, k;
char c, ch;
float f, salary;
double d;
Constants
#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
> 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
&& Called Logical AND operator. If both the operands are non- (A && B) is false.
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
#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
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
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);
}