Introduction : Introduction C is one of the most popular high level, procedure oriented computer language.
C was an offspring of “Basic combined programming language” developed in 1960’s at Cambridge University by DENNIS RITCHIE.
It is mainly associated with UNIX operating system.
Format of a Simple C Program : Format of a Simple C Program main() Function Name
{ Start of the program
---------------
--------------- Function Body
---------------
} End of program
Basic Data Types and Operators : Basic Data Types and Operators char -a character.
int -an integer, in the range -32,767 to 32,767.
long int -a larger integer (up to +-2,147,483,647).
float -a floating-point number 3.4e -38 to 3.4e +38
double -a floating-point number 1.7e –308 to
1.7e +308
e=2.714
Declarations : Declarations A declaration tells the compiler the name and type of a variable you'll be using in your program. In its simplest form, a declaration consists of the type, the name of the variable, and a terminating semicolon:
char c; int i; float f;
Example of a simple C program : Example of a simple C program main()
{
printf("hello, world\n");
}
OUTPUT:
hello world
#include : #include This first line of the program is a preprocessing directive, #include.
This causes the preprocessor — the first tool to examine source code when it is compiled — to substitute the line with the entire text of the stdio.h file.
The header file stdio.h contains declarations for standard input and output functions such as printf.
C Operators : C Operators An operator is a symbol that tells the operator to perform certain mathematical or logical operations.
They are used in program to manipulate data and variables they are the part of mathematical and logical expression.
C Operators can be classified into : C Operators can be classified into Arithmetic Operators.
Relational Operators.
Logical Operators.
Increment Operators.
Conditional Operators.
Arithmetic Operators. : Arithmetic Operators. C provides all the basic arithmetic operators as shown below:-
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Division
Relational Operators : Relational Operators We often compare 2 quantities and depending upon their relation take certain decision.
These comparisons can be done with the help of relational operators.
The value of relational expression is either 1 or 0.
If the specified relation is true then it is 1 and if the relation is false it is 0.
C supports 6 relational operations : C supports 6 relational operations Operator Meaning
< less than
> greater than
<= less that or =
>= greater than =
== is equal to
!= not equal to
Logical Operators. : Logical Operators. C has 3 logical operators
&& Means Logical AND
!! Means Logical OR
! Means Logical NOT
We use these operators when we want to test more than one condition and take decision.
Increment and Decrement Operators : Increment and Decrement Operators C has these two very useful operators i.e
++ and --
The ++ operator adds one to the operand while – subtracts one from the operand.
They take the following form
++a; or a++;
--a; or a--;
++a; is equivalent to a=a+1; or a+=1;
Theses operators are used in for and while loops extensively.
Conditional Operators. : Conditional Operators. A turnery operators pair “? :” is available in C to construct conditional expression of the form.
Example:
exp1?exp2:exp3;
Where exp1, exp2 and exp3 are expressions.
The operator ?: works as follows : The operator ?: works as follows exp1 is evaluated first if it is true then the exp2 is evaluated and becomes the value of the expression, if exp1 is false then exp3 is evaluated and its value becomes the value of the expression.
Example:
x=5, y=10;
a=(x>y) ? x : y (in this example a will be assigned the value y)
Printing a Line of Text : Printing a Line of Text printf( "Welcome to C!\n" );
Instructs computer to perform an action
Specifically, prints the string of characters within quotes (“ ”)
Entire line called a statement
All statements must end with a semicolon (;)
Escape character (\)
Indicates that printf should do something out of the ordinary
\n is the newline character
Slide 17 : Token Meaning Examples Comments
%f print floating point "%f" unformatted float "%0.3f“ print 3 digits after decimal.
%i print int,decimal "%i" unformatted int "%5i" 5 digits, with leading spaces
"%0.5i" 5 digits with leading 0s.
%c print character "%c"
%li print long "%li" use for variables of type long
%s print string "%s" print a string
Scanf Statement : Scanf Statement scanf is a function that reads data with specified format.
Input functions, called scanf
Format string specifications : Format string specifications '%d' : Scan an integer as a signed decimal number.
‘%s’: Scan a String
'%f' : Scan a floating-point number in normal
'%c' : Scan a character
Slide 20 : WAP to add two numbers
/* To add two numbers*/
#include
main()
{
int a,b,sum;
printf (“Enter two numbers/n”);
scanf(“%d%d”&a&b);
sum=a+b;
printf(“Sum of two numbers is %d”,sum);
}