C Inroduction
C Programming
What is C?
• C is a programming language developed at
AT & T’s Bell Laboratories of USA in 1972.
• It was designed and written by a man named
Dennis Ritchie.
• Major parts of popular operating systems like
Windows, UNIX, Linux is still written in C.
Steps in Learning C
The C Character Set
• A character denotes any alphabet, digit or
special symbol used to represent information.
• Alphabets
– *A,B, ... . . , Y, Z, a, b, ... ... , y, z+
• Digits
– *0, 1, 2, 3, 4, 5, 6, 7, 8, 9+
• Special symbols
– *~
‘ ! @ # % ^ & ∗ ( ) _ − + = | * + , - ∶ ;
" ′ < >
, . ? /+
Constants, Variables and Keywords
• constants, variables and keywords are formed
with the help of alphabets, numbers and
special symbols.
Types of C Constants
Integer Constants
• An integer constant
– must have at least one digit.
– must not have a decimal point.
– can be either positive or negative.
– can have no commas or blanks.
• Example:
– 426
– +782
– -8000
– -7605
Real Constants
• A real constant
– must have at least one digit.
– can have a decimal point.
– could be either positive or negative.
– default sign is positive.
– must not have commas or blanks.
• Example:
– +325.34
– 426.0
– -32.76
– -48.5792
Character Constants
• A character constant is a single alphabet, a single
digit or a single special symbol enclosed within
single inverted commas.
• The maximum length of a character constant can
be one character.
• Example
– 'A'
– 'I'
– '5'
– „@'
Types of C Variables
• Following are commonly used primary data
types in C language
– Integers (int)
– Real Numbers (float, double)
– Characters (char)
Rules for Constructing Variable Names
• A variable name is any combination of 1 to 31
alphabets, digits or underscores.
• The first character in the variable name must
be an alphabet or underscore (_).
• No commas or blanks are allowed within a
variable name.
• No special symbol other than an underscore
(_) (as in gross_sal) can be used in a variable
name.
C Variables
• Which of the following variable names are
correct
– si_int(correct)
– emp_hra(Corr.)
– cart_e_89 (correct)
– 23alpha (incorrect0
– Beta-gama (incorrect)
– ____theta (correct)
– calory
C Variables
• These rules remain same for all the types of
primary and secondary variables (arrays, string,
structure, union etc.).
• It is compulsory for you to declare the type of any
variable name that you wish to use in a program.
• Examples
– int age;
– float salary;
– char grade;
C Keywords
• Keywords are the words with special meaning
that has already stored in the C compiler.
• The keywords cannot be used as variable
names.
• There are 32 keywords in the C language.
C Keywords
A simple addition program
#include<stdio.h>
int main( )
// main() is the function where execution starts
{
int num_1, num_2, sum ;
num_1=34;
num_2=45;
sum = num_1+num_2;
printf("%d\n",sum);
}
printf() Function
• The general form of printf( ) function is
printf ( "<format string>", <list of variables> ) ;
• <format string> can contain
– %f :for printing real values.
– %d :for printing integer values.
– %c :for printing character values.
– Printf(“%f”, salary);
Input from Keyboard
#include<stdio.h>
int main()
{
int num_1, num_2, sum;
scanf("%d", &num_1);
scanf("%d", &num_2);
sum = num_1+num_2;
printf("%d\n",sum);
}
scanf() Function
• scanf() is the function used for taking input
from keyboard.
scanf ( "<format string>", &<variable> ) ;
A Simple Interest program
#include<stdio.h>
int main( )
{
int p, n ;
//p- Pricnciple Amount
//n- number of years
float r, si ;
//r-rate of interest
//si- simple interest
p = 1000 ;
n = 3;
r = 8.5;
/* formula for simple interest */
si = p * n * r / 100 ;
printf ( "%f\n" , si ) ;
}
A Simple Interest Program with
Keyboard Input
#include<stdio.h>
int main( )
{
int p, n ;
//p- Pricnciple Amount
//n- number of years
float r, si ;
//r-rate of interest
//si- simple interest
scanf ( "%d %d %f", &p, &n, &r ) ;// taking input from keyboard
/* formula for simple interest */
si = p * n * r / 100 ;
printf ( "%f\n" , si ) ;
}
Comments
Post a Comment