Download Building Blocks In C-Programming Concepts-Lecture Slides and more Slides Computer Programming in PDF only on Docsity!
C Building Blocks
Variables
Constants
Operators
Input/Output
Variable Definition
X
int x;
All variables must be defined as to specify their
name, type and set aside storage.
Variables
Variables, can be declared anywhere in the
program.
Name & type must be declared before use
However they are only visible to the program
within the block of code in which they are
defined.
void main() { int x = 4; }
Values and Variables
Basic Types:
Integers (signed and unsigned)
short int long or long int
Floating point numbers
float double
Characters
Boolean
C Variables
Variables in C store data in memory so that the data can be accessed throughout the execution of a program A variable stores data corresponding to a specific type. Each variable that a C program uses must be declared at the beginning of the function before it can be used.
specify the type of the variable
numeric types: int short long float double character type: char
specify a name for the variable
any previously unused C identifier can be used (with some more exceptions discussed later) int x; float sum, product;
What Does a Variable Declaration Do?
A declaration tells the compiler to allocate enough
memory to hold a value of this data type, and to
associate the identifier with this location.
int apartmentNumber;
float tax_rate_Y2K;
char middleInitial;
4 bytes for taxRateY2K 1 byte for
middleInitial
Keywords of C (32)
Keyw ord s auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while
Identifiers
VALID
apartment_number tax_rate_Y2K
PrintHeading ageOfHorse_
NOT VALID
apartment# 2000TaxRate
Age-Of-Cat day of week
Using meaningful variable names is a good
programming practice.
Using _ as the first character is not
recommended.
Assignment Statement
An assignment statement is used to put a value into a
variable.
The previous value is replaced
Syntax:
= ;
is any declared variable in the program is anything that produces a value of the appropriate type. first, the expression on right is evaluated. then the resulting value is stored in the memory location of variable on left.
C Variable Covered Range
Data Type Possible Range of Values
char (^) -127 to 127 short (^) -32,767 to 32, int (^) -2,147,483,648 to 2,147,483, float (^) 6 Digits of Precision double (^) 10 Digits of Precision boolean (^) True or False
Sizeof
Sizeof: returns size of variable or data type in
number of bytes
sizeof(int) /* sizeof a type */
sizeof x /* sizeof a data object */
Printing variables and constants to the screen with printf
- Use the following conversion specifications: %d for an integer %c for a character %f for a float %f for a double Example: int sum = 5; float avg = 12.2; char ch = „A‟; printf(“The sum is %d\n”, sum); printf(“avg = %f”\n”, avg); prinf(“ch = %c\n”, ch); printf(“%d, %f, %c\n”, sum, avg, ch); printf(“%d\n”, 5);
Example:
Program to find the average of two numbers Note that you can declare more than one variable per line. We divide by 2.0 instead of 2 so that the right hand side is a float (i.e. no truncation takes place) scanf is used similarly to printf, except that you need to put an ampersand (&) before the variable name The first argument of the scanf function is the conversion specifier(s) in quotes, then the variable name(s) #include <stdio.h> int main () { int num1, num2; float avg; /* get two inputs / printf( "Enter the first integer:”); scanf(“%d”, &num1); printf(“Enter the second integer:”); scanf(“%d”, &num2); / compute and print the avg */ avg = (num1 + num2) / 2.0; printf(“The average is %f\n”, avg);
Operators
Arithmetic Operators
Increment/ Decrement Operators
Relational Operators
Logical Operators