0% found this document useful (0 votes)
65 views28 pages

CSC 102 - 044247

This document provides an introduction to variables in the C programming language. It discusses naming variables, declaring variables, and using variables through assignment statements. Variables represent unknown values and are given a name and type when declared. Common variable types in C include integers, floating-point numbers, and characters. The document also covers data type declarations, constants, and good programming practices related to commenting and formatting code.

Uploaded by

Olalekan Ibrahim
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)
65 views28 pages

CSC 102 - 044247

This document provides an introduction to variables in the C programming language. It discusses naming variables, declaring variables, and using variables through assignment statements. Variables represent unknown values and are given a name and type when declared. Common variable types in C include integers, floating-point numbers, and characters. The document also covers data type declarations, constants, and good programming practices related to commenting and formatting code.

Uploaded by

Olalekan Ibrahim
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/ 28

Introduction to Programming

Variables in C
Topics
Why programming?
Naming Variables
Declaring Variables
Using Variables
The Assignment Statement
Programming – Why?
Computers are used for many different purposes in
many different situations.
But, how can they be so versatile?
 Answer: They can be programmed

The ability for a computer to be programmed allows it


to do whatever their programs tell them what to do.
A program is a set of instructions that tell a computer
what to do.
A computer cannot do anything unless it has a program
to tell it what to do.
C
Structure of a C program - A C program is
structured in a specific and particular manner. In C, a
program is divided into the following three sections:
1. Standard Libraries Section

2. Main Function Section

3. Function Body Section


What Are Variables in C?
Variables in C have the same meaning as variables in
algebra. That is, they represent some unknown, or variable,
value.

Remember that variables in algebra are represented by a


single alphabetic character.
Naming Variables
Variables in C may be given representations containing
multiple characters. But there are rules for these
representations.
Variable names in C
May only consist of letters, digits, and underscores
May be as long as you like, but only the first 31 characters are
significant
May not begin with a number
May not be a C reserved word (keyword)
variable name should not consist of whitespace
Naming Conventions
C programmers generally agree on the following
conventions for naming variables.
Begin variable names with lowercase letters
Use meaningful identifiers
Separate “words” within identifiers with underscores or mixed
upper and lower case.
Examples: surfaceArea surface_Area
surface_area
Be consistent!
Naming Conventions (con’t)
Use all uppercase for symbolic constants (used in #define
preprocessor directives).
Examples:

#define PI 3.14159
#define AGE 52
Case Sensitivity
C is case sensitive
It matters whether an identifier, such as a variable name, is
uppercase or lowercase.
Example:
area
Area
AREA
ArEa
are all seen as different variables by the compiler.
Which Are Legal Identifiers?
AREA area_under_the_curve
3D num45
Last-Chance #values
x_yt3 pi
num$ %done
lucky***
Declaring Variables
Before using a variable, you must give the compiler
some information about the variable; i.e., you must
declare it.
The declaration statement includes the data type of
the variable.
Examples of variable declarations:
int meatballs ;
float area ;
Declaring Variables (con’t)
When we declare a variable
Space is set aside in memory to hold a value of the specified
data type
That space is associated with the variable name
That space is associated with a unique address
Visualization of the declaration
 int meatballs ;
More About Variables
C has three basic predefined data types:

Integers (whole numbers)


int, long int, short int, unsigned int
Floating point (real numbers)
float, double
Characters
char
Using Variables: Initialization
Variables may be given initial values, or initialized, when
declared. Examples:

length
int length = 7 ; 7

diameter
float diameter = 5.9 ; 5.9

initial
char initial = ‘A’ ;
‘A’
Using Variables: Initialization (con’t)
Do not “hide” the initialization
put initialized variables on a separate line
a comment is always a good idea
Example:
int height ; /* rectangle height */
int width = 6 ; /* rectangle width */
int area ; /* rectangle area */

NOT int height, width = 6, area ;


Using Variables: Assignment
Variables may have values assigned to them through the
use of an assignment statement.
Such a statement uses the assignment operator =
This operator does not denote equality. It assigns the value
of the righthand side of the statement (the expression) to
the variable on the lefthand side.

Examples:
diameter = 5.9 ;
area = length * width ;
Example: Declarations and Assignments

#include <stdio.h>
int main( )
{
int area, length, breadth;
length = 7 ;
breadth = 6 ;
area = length * breadth;


printf (“The Area of a rectangle: \n”) ;
printf (“ %d length \n”, length) ;
printf (“ %d breadth \n”, breadth) ;
printf (“ %d area \n”, area) ;

return 0 ;
}
Enhanced Program
#include <stdio.h>
int main ( )
{
float inches, feet, fathoms ;

printf (“Enter the depth in fathoms : ”) ;


scanf (“%f”, &fathoms) ;
feet = 6 * fathoms ;
inches = 12 * feet ;
printf (“Its depth at sea: \n”) ;
printf (“ %f fathoms \n”, fathoms) ;
printf (“ %f feet \n”, feet) ;
printf (“ %f inches \n”, inches) ;
return 0 ;
}
Good Programming Practices
Place each variable declaration on its own line with a
descriptive comment.
Place a comment before each logical “chunk” of code
describing what it does.
Do not place a comment on the same line as code (with the
exception of variable declarations).
Use spaces around all arithmetic and assignment operators.
Use blank lines to enhance readability.
Good Programming Practices (con’t)
Place a blank line between the last variable declaration
and the first executable statement of the program.
Indent the body of the program 3 to 4 tab stops -- be
consistent!
Data types
‘C’ provides various data types to make it easy for a
programmer to select a suitable data type as per the
requirements of an application. Following are the three
data types:
1. Primitive data types
2. Derived data types
3. User-defined data types
There are five primary fundamental data types,
1. int for integer data
2. char for character data
3. float for floating point numbers
4. double for double precision floating point numbers
5. void
* Array, functions, pointers, structures are derived data types.
‘C’ language provides more extended versions of the above
mentioned primary data types. Each data type differs from one
another in size and range.
• Integer data type
Integer is nothing but a whole number. The range for an integer
data type varies from machine to machine.
An integer typically is of 2 bytes which means it consumes a
total of 16 bits in memory. A single integer value takes 2
bytes of memory. An integer data type is further divided into
other data types such as short int, int, and long int.
Whenever we want to use an integer data type, we have
place int before the identifier such as,
Floating type
The ‘float’ keyword is used to represent the floating point
data type. It can hold a floating point value which means a
number is having a fraction and a decimal part. A floating
point value is a real number that contains a decimal point.
While using a floating point number a keyword
float/double/long double must be placed before an
identifier
Example:
float num;
Character data type
Character data types are used to store a single character
value enclosed in single quotes.
A character data type takes up-to 1 byte of memory space.
Example:
Char letter;

Void data type


A void data type doesn’t contain or return any value. It is
mostly used for defining functions in ‘C’.
Example:
 Void Displaydata ( )
Type Declaration of a variable

int main() {
int x, y;
float salary = 13.48;
char letter = 'K’;
x = 25;
y = 34;
int z = x+y;
printf("%d \n", z);
printf("%f \n", salary);
printf("%c \n", letter);
return 0;}
Constants
Constants are the fixed values that never change during the execution of a
program. Following are the various types of constants:
Integer constants
An integer constant is nothing but a value consisting of digits or numbers.
These values never change during the execution of a program. Integer
constants can be octal, decimal and hexadecimal.
1.Decimal constant contains digits from 0-9 such as,
Example, 111, 1234

2.Octal constant contains digits from 0-7, and these types of constants are
always preceded by 0.
Example, 012, 065

3.Hexadecimal constant contains a digit from 0-9 as well as characters from A-


F. Hexadecimal constants are always preceded by 0X.
Example, 0X2, 0Xbcd

The octal and hexadecimal integer constants are very rarely used in
programming with ‘C’.
Character constants
A character constant contains only a single character enclosed within a
single quote (”). We can also represent character constant by providing
ASCII value of it.
Example, 'A', '9'

String constants
A string constant contains a sequence of characters enclosed within
double quotes (“”).
Example, "Hello", "Programming"

Real Constants
‘C’ also provides real constants that contain a decimal point or a
fraction value. The real constants are also called as floating point
constants. The real constant contains a decimal point and a fractional
value.
Example, 202.15, 300.00
For example, to declare a value that does not change like the classic circle
constant PI, there are two ways to declare this constant:

1.By using the const keyword in a variable declaration which will reserve a
storage memory

#include <stdio.h>
int main() {
const double PI = 3.14;
printf("%f", PI);
//PI++;
// This will generate an error as constants cannot be changed return 0;
}

2.By using the #define pre-processor directive which doesn’t use memory for
storage and without putting a semicolon character at the end of that statement
#include <stdio.h>
#define PI 3.14
int main() {
printf("%f", PI);
return 0;}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy