Introduction C
Introduction C
C
• C is a procedural programming language.
• It was initially developed by Dennis Ritchie in
the year 1972.
• Many later languages have borrowed
syntax/features directly or indirectly from the
C language. Like syntax of Java, PHP,
JavaScript, and many other languages are
mainly based on the C language. C++ is nearly
a superset of C language
Features
• Mother language
• System programming language
• Procedure-oriented programming language
• Structured programming language
• Mid-level programming language
• Header Files Inclusion: The first and foremost component is the
inclusion of the Header files in a C program.
A header file is a file with extension .h which contains C function
declarations and macro definitions to be shared between several
source files.
Some of C Header files:
– stddef.h – Defines several useful types and macros.
– stdint.h – Defines exact width integer types.
– stdio.h – Defines core input and output functions
– stdlib.h – Defines numeric conversion functions, pseudo-random
network generator, memory allocation
– string.h – Defines string handling functions
– math.h – Defines common mathematical functions
– Main Method Declaration: The next part of a C
program is to declare the main() function. The
syntax to declare the main function is:
Syntax to Declare the main method:
• int main() {}
• Variable Declaration: It refers to the variables
that are to be used in the function. No
variable can be used without being declared.
Also in a C program, the variables are to be
declared before any operation in the function.
Example:
int main() {
printf("Hello World!");
return 0;
}
Input (scanf())
• The scanf() function is used for input. It reads the input data from
the console.
• scanf("format string",argument_list);
• Example
• #include<stdio.h>
• int main(){
• int number;
• printf("enter a number:");
• scanf("%d",&number);
• printf("cube of number is:%d ",number*number*number);
• return 0;
• }
Format Specifier
• Format specifiers in C are used to take inputs
and print the output of a type. The symbol we
use in every format specifier is %. Format
specifiers tell the compiler about the type of
data that must be given or input and the
type of data that must be printed on the
screen.
Prints Addition Of 2 Numbers.
• #include<stdio.h>
• int main(){
• int x=0,y=0,result=0;
•
• printf("enter first number:");
• scanf("%d",&x);
• printf("enter second number:");
• scanf("%d",&y);
•
• result=x+y;
• printf("sum of 2 numbers:%d ",result);
•
• return 0;
• }
Variables
• A variable is a name of the memory location.
It is used to store data.
• Its value can be changed, and it can be reused
many times.
Rules for defining variables
#include <stdio.h>
int main()
{
// Integer value with positive data.
int a = 9;
return 0;
}
Operators
int main()
{
int a = 10, b = 5;
// Arithmetic operators
printf("Following are the Arithmetic operators in C\n");
printf("The value of a + b is %d\n", a + b);
printf("The value of a - b is %d\n", a - b);
printf("The value of a * b is %d\n", a * b);
printf("The value of a / b is %d\n", a / b);
printf("The value of a % b is %d\n", a % b);
printf("The value of a++ is %d\n",a++); // First print (a) and then increment it
printf("The value of a-- is %d\n",a--); // First print (a) and then decrease it
// by 1
printf("The value of ++a is %d\n",++a); // Increment (a) by (a+1) and then print
printf("The value of --a is %d\n",--a); // Decrement (a+1) by (a) and then print
// Comparison operators
// Output of all these comparison operators will be (1)
// if it is true and (0) if it is false
printf("\nFollowing are the comparison operators in C\n");
printf("The value of a == b is %d\n", (a == b));
printf("The value of a != b is %d\n", (a != b));
printf("The value of a >= b is %d\n", (a >= b));
printf("The value of a <= b is %d\n", (a <= b));
printf("The value of a > b is %d\n", (a > b));
printf("The value of a < b is %d\n", (a < b));
// Logical operators
printf("\nFollowing are the logical operators in C\n");
printf("The value of this logical and operator ((a==b) "
"&& (a<b)) is:%d\n",
((a == b) && (a < b)));
printf("The value of this logical or operator ((a==b) "
"|| (a<b)) is:%d\n",
((a == b) || (a < b)));
printf("The value of this logical not operator "
"(!(a==b)) is:%d\n",
(!(a == b)));
return 0;
Precedence
• Operator precedence determines the grouping of
terms in an expression and decides how an
expression is evaluated. Certain operators have
higher precedence than others; for example, the
multiplication operator has a higher precedence
than the addition operator.
• Operator precedence determines which
operation is performed first in an expression with
more than one operators with different
precedence.
Associativity
• Operators Associativity is used when two
operators of same precedence appear in an
expression. Associativity can be
either Left to Right or Right to Left.
For example: ‘*’ and ‘/’ have same
precedence and their associativity
is Left to Right, so the expression “100 / 10 *
10” is treated as “(100 / 10) * 10”.
Write a C program to input principle, time and rate (P, T, R) from user and find
Simple Interest. How to calculate simple interest in C programming. Logic to
find simple interest in C program.
int main() {
return 0; }
Thanks