Professional Documents
Culture Documents
C is a general-purpose computer
programming language
Combines the features of a high level
language with it's ability to access low level
commands– so suitable for System Sw &
appln sw development
The C programming language was designed
by Dennis Ritchie at Bell Laboratories in the
early 1970s
Specially Designed for systems programming
◦ Operating systems
◦ Utility programs
◦ Compilers
Evolved from B, which evolved from BCPL
Influenced by
◦ ALGOL 60 (1960),
◦ CPL (Cambridge, 1963),
◦ BCPL (Martin Richard, 1967),
◦ B (Ken Thompson, 1970)
The rapid growth of C led to development of
different versions similar but often
incompatible.
So in 1983 a committee was formed by ANSI
to define a standard for C
Standardized in 1989 by ANSI (American
National Standards Institute) known as ANSI C
Structured language
Uses features of High-level language
Handle bit-level operations
Supports a variety of data types & operators
Supports dynamic memory mgt
Extends itself by addition of functions to its
library
#include<stdio.h>
main()
{
/* the following st displays welcome to c */
printf(“welcome to C”);
}
The files that are specified in the include
section is called as header file
These are precompiled files that has some
functions defined in them
We can call those functions in our program by
supplying parameters
Header file is given an extension .h
C Source file is given an extension .c
This is the entry point of a program
When a file is executed, the start point is the
main function
From main function the flow goes as per the
programmers choice.
There may or may not be other functions
written by user in a program
Main function is compulsory for any c
program
C permits the following form of main fn
main()
int main()
void main()
main(void)
void main(void)
int main(void)
comment
/*….
…….*/
This can span over to multiple lines
A character denotes any alphabet ,digit or symbols to
represent information. The following are the valid
alphabets, numbers and special symbols permitted in
C
Numerals: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Alphabets: a, b, ….z
A, B, ……...Z
Special Characters:
Example:
Const int class_size = 40;
The const data type qualifier tells the compiler that the value of the int
variable class_size may not be modified in the program.
Integer Constants.
◦ e.g. 426
◦ +756
◦ +756
◦ 588 etc.
Character Constants.
◦ e.g.
◦ ‘ E’
◦ ‘h ’
◦ ‘8 ’
Real Constant
String constants
Backslash character constants
A variable is data name that may be used to
store data value
Takes different values at times during
execution
Declaration
<<Data type>> <<variable name>>;
int a;
<<varname>>=<<value>>;
a=10;
Usage
<<varname>>
a=a+1; //increments the value of a by 1
Primary data types
◦ int, float, char,void
Derived data types
◦ Arrays come under this category
◦ Arrays can contain collection of int or float or char
or double data
User defined data types
◦ Structures and enum fall under this category.
C has special shorthand that simplifies
coding of certain type of assignment
statements.
Example
a = a + 2; can be written as a + =2;
This shorthand works for all binary
operators in C.
General form:
<variable> < operator> =
<variable/constant/expression>
Formatted Input For Scanf:
The formatted input refers to input data that has been arranged in a particular
format. Input values are generally taken by using the scanf function. The scanf
function has the general form.
Scanf (“control string”, arg1, arg2, arg3 ………….argn);
The format field is specified by the control string and the arguments
arg1, arg2, …………….argn specifies the address of location where address is to
be stored.
The control string specifies the field format which includes format specifications
and optional number specifying field width and the conversion character % and
also blanks, tabs and newlines.
The Blanks tabs and newlines are ignored by compiler. The conversion character %
is followed by the type of data that is to be assigned to variable of the assignment.
The field width specifier is optional.
Formatted Input - Scanf:
The general format for reading a integer number is
%xd
Here percent sign (%) denotes that a specifier for
conversion follows and x is an integer number which
specifies the width of the field of the number that is
being read. The data type character d indicates that
the number should be read in integer mode.
Example :
scanf (“%4d %4d”, &sum1, &sum2);
Output
◦ printf(“%d”,a)
◦ Prints the value present in variable a on the screen
Code Format
---- ------
%c character
%d signed integers
%e scientific notation, with a lowercase "e"
%E scientific notation, with a uppercase "E"
%f floating point
%g use %e or %f, whichever is shorter
%G use %E or %f, whichever is shorter
%o octal
%s a string of characters
%u unsigned integer
%x unsigned hexadecimal, with lowercase letters
%X unsigned hexadecimal, with uppercase letters
Reading a character
◦ getchar () ; The given value is displayed on the
screen and the compiler wait for another character
to be typed
◦ getch() reads a single character directly from the
keyboard, without echoing to the screen.
◦ getche() is used to get a character from console,
and echoes to the screen
Reading a character
# include < stdio.h >
void main (
{
char C;
printf (“Type one character:”) ;
C = getchar () ;
Printf (” The character you typed is = %c”, C) ;
}
writing a character
The putchar function which in analogus to
getchar function can be used for writing
characters one at a time to the output
terminal. The general form is
Eg:
i=10; Output:
do 10987654321
{
printf(“%d”,i);
i--;
}while(i!=0)
C language permits to jump out of the loop.
The break statement allows us to accomplish
this task.
It provides an early exit from for, while, do
and switch constructs.
A break causes the innermost enclosing loop
or switch to be exited immediately.
Format is break;
During loop operations it may be necessary
to skip a part of the body of the loop under
certain conditions. Like the break statement C
supports similar statement called continue
statement.
It causes the loop to be continued with the
next iteration after skipping any statement in
between.
the format of the continue statement is
simply
◦ continue;
Write a program
that finds the sum
of five positive
integers. If a
negative number is
entered, the sum
should not be
performed
Example
The goto statement is a jump statement which
jumps from one point to another point within a
function.
The goto statement is marked by label statement.
Label statement can be used anywhere in the
function above or below the goto statement.
Example
Avoiding goto
◦ It generates less efficient code
◦ Careful program design can usually avoid use of goto
◦ Many of goto will make the program logic complicated
and less readable
◦ In case any goto is absolutely necessary, it should be
properly documented
Just as you can break out of a loop, you can
break out of a program by using the standard
library function exit().
This function causes immediate termination
of the entire program, forcing a return to the
operating system. In effect, the exit() function
acts as if it were breaking out of the entire
program.
The general form of the exit() function is:
void exit(int return_code);