0% found this document useful (0 votes)
9 views

Chap (Ter 01 - C Language Tutorial For Beginners - Getting Started (Variabls & Constants) - Shared

The document discusses variables, constants, and data types in the C programming language. It explains that C has primary constants like integers, reals, and characters as well as secondary constants like arrays and structures. It also outlines rules for constructing variable names in C and lists C keywords that cannot be used as variable names. The document provides examples of integer, real, and character constants and variables. It describes how to take input and print output using the scanf() and printf() functions in C.

Uploaded by

Mariyam Afreen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Chap (Ter 01 - C Language Tutorial For Beginners - Getting Started (Variabls & Constants) - Shared

The document discusses variables, constants, and data types in the C programming language. It explains that C has primary constants like integers, reals, and characters as well as secondary constants like arrays and structures. It also outlines rules for constructing variable names in C and lists C keywords that cannot be used as variable names. The document provides examples of integer, real, and character constants and variables. It describes how to take input and print output using the scanf() and printf() functions in C.

Uploaded by

Mariyam Afreen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Getting Started

(Variables and Constants)

By
Mohammad Fazlur Rahman
EPS, Bhopal
The C
Characters
Characters…
C uses all sort of characters in computer keyboard.
Alphabets:
“A to Z” and “a to z”
Digits:
0 to 9
Special Characters:
~`!@#%^&*()_-+=|\{}[]:;“‘<>
,?/$
The C
constants and variables
Constants…
C language has data and it can be constants and variables.

C Constants

Primary Secondary
Constant Constant

Integer constant Array


Real constant Pointer
Character Constant Structure
Union
Enum. etc
Integers…
Features of Integers:
• It should have at least one digit
• It must not have a decimal point
• It can be either negative or positive
• If no sign is given, it is assumed to be positive
• No comma or blank is allowed withing an integer
Integers…
Features of Integers:
• Range:
– for visual studio and gcc it is -2147483648 to
+ 2147483647
– for Turbo C and Turbo C++ it is -32768 to
+32767
Reals…
Features of Reals: Real is nothing but float
• It should have at least one digit
• It must have a decimal point
• It can be either negative or positive
• If no sign is given, it is assumed to be positive
• No comma or blank is allowed withing an integer
• It can be written in the form of decimal as well as
exponential form.
Ex. 0.000342 is same as 3.42e-4
Reals…
Features of Integers:
• Range:
– its range remains same for all kind of
compilers…
– It ranges from -3.4e38 to + 3.4e38
Characters…
Features of Character:
• A character constant is a single alphabet, a single
digit or a single special symbol enclosed withing
single inverted commas. Both the inverted commas
should point to the left.
’A’ is a valid character constant whereas ‘A’ is not.
• The maximum length of the character can be 1
character.
• If we try to print the integer value of a character
variable or constant, then it will give its ASCII value.
Variables…

Variables and constants are same as far as its


operations are concerned. Any data which
changes during the operation of the programme is
called variable and if doesn’t change then it is
called constant.
So in this regard, we can have a variable also as
integer or real or character.
Up to now…

Any
Question…
Rules….
for
constructing
variable names
Variable names…

a. A variable can have any combination of 1


to 31 alphabets, digits or underscores.
Ex. EPS_IB, Gr1, gr2, all_grades etc.
b. The first character in the variable name
must be an alphabet or underscore (no
digit).
Ex. _EPS_IB, _8th_grade_EPS are valid
Ex. 6th_grade, 2EPS are not valid
Variable names…

c. No comma or blank is allowed


withing a variable name
d. No special symbol other than “under
score” is allowed in the variable
name.
e. It is matter of convenience to give
logical names to the variables.
C
KEYWORDS
C – Keywords…

Keywords are the words whose meaning


has already been explained to the
compiler. So those keywords can’t be
used as variables as this will conflict
with new meaning of the word to the
compiler.
Total there are 32 keywords C uses.
C – Keywords…
JazakAllahKhair…

Any
Question
Work out
Example
Work out examples…

Do a simple programming in C
Language to calculate a simple interest
for a given principal amount at a given
rate for a given amount of time.
Convert the same programme for taking
inputs from outside.
Work out examples…
// Calculation of simple interest
// Author: Put your name here.
# include <stdio.h>

Int main ()
{
int p, n; // defining
float r, si; // defining
p = 1000; // assigning
n = 3; // assigning
r = 8.5; // assigning
si = p * n * r/100; // formula for simple interest
printf(“Simple Interest calculated is = %f\n”,si);
return(0);
}
Recommended format of C programming…
• Each instruction in a C program is written as a separate
statement.
• The statements in a program must appear in the same order
in which we wish them to be executed.
• Blank spaces may be inserted between two words to improve
the readability of the statement.
• All statements should be in lower case letters.
• C has no specific rules for the position at which a statement
is to be written in a given line. That’s why it is often called a
free-form language.
• Every C statement must end with a semicolon (;), thus “;”
acts as a statement terminator.
Comments on
Example
Work out examples… Form of a C
Programme
What is main()
…?
Variables and
Salient Features of the C Programming
their usage…

printf() and its


purpose.
Compilation and execution.
Comments in C programming…
• Comments in C-programming is any statement which is useful
for our understanding but we don’t want it to be the part of
the programming.
What is main()…?
• main( ) is a function. A function is nothing but a container
for a set of statements. In a C program there can be multiple
functions. To begin with, we would concentrate only on those
programs which have only one function. The name of this
function has to be main( ), it cannot be anything else. All
statements that belong to main( ) are enclosed within a pair
of braces { } as shown below.
What is main()…?
• The way functions in a calculator return a
value, similarly, functions in C also return a
value. main( ) function always returns an
integer value, hence there is an int before
main( ). The integer value that we are returning
is 0. 0 indicates success. If for any reason the
statements in main( ) fail to do their intended
work we can return a non-zero number from
main( ). This would indicate failure.
What is main()…?

• Some compilers like Turbo C/C++


even permit us to return nothing from
main( ). In such a case we should
precede it with the keyword void. But
this is non-standard way of writing the
main( ) function.
Variables and their usage…
• Any variable used in the program must be declared
before using it. For example:

• C-Language programming is rich in operators. There


are as many as 45 operators but unfortunately there
is no operator for exponentiation. It is a slip…
printf() and its purpose…
• It is a readymade library function.
• For us to be able to use the printf( ) function, it is
necessary to use #include <stdio.h> at the beginning
of the program. #include is a preprocessor directive.
• The general form of printf( ) function is,
In addition to format
specifiers like %f, %d and
%c, the format string may
also contain any other
characters. These characters
are printed as they are when
printf( ) is executed.
printf() and its purpose…
• Some examples of printf() function.
printf() and its purpose…
• printf( ) can not only print values of variables, it can
also print the result of an expression.
Compilation
&
Execution
Compilation & Execution…
From designing to final software execution, there is a
series of process, a software development has to go
through.
Compilation & Execution…
Receiving Inputs
Example
Work out examples…

Do a simple programming in C
Language to calculate a simple interest
for a given principal amount at a given
rate for a given amount of time.
Convert the same programme for taking
inputs from outside.
Work out examples…
While receiving inputs from outside, we use the command
“scanf()” and store the data in appropriate variables.
int main()
{
int a, b, c;
printf(“Please enter the number of people…\n”);
scanf(“%d”,&a);
printf(“Total number of people is : %d\n”,a);
}
Work out examples…
The entries must be separated by a blank (Spacebar), a
tab (tab key) or a new line (Enter key).
int main()
{
int a, b, c;
printf(“Please enter three numbers…\n”);
scanf(“%d %d %d”,&a, &b, &c);
printf(“The numbers are: %d %d %d\n”,a,b,c);
}
Summary
of the
Learning
Summary…
• Constant is an entity whose value remains fixed.
• Variable can change during course of execution.
• Keywords are special words whose meaning is known to the
Compiler.
• There are certain rules that must be followed while building
constants or variables.
• The three primary constants and variable types in C are
integer, float and character.
Summary…
• We should not use a keyword as a variable name.
• Comments should be used to indicate the purpose of the
program or statements in a program.
• Comments can be single line or multi-line.
• Input/output in C can be achieved using scanf( ) and printf( )
functions.

You might also like