[Week 2] Programming Basics 1
[Week 2] Programming Basics 1
▪ Semantics
meaning associated with each syntactically correct sequence of characters.
▪ Preprocessor Directives
For telling the preprocessor to take specific actions
Syntax
#
Example
#include, #ifdef, #define
▪ Block Structures
Block: one or more declarations and statements
Nested blocks are permitted.
Syntax
{ <lines of code> }
▪ Statement
A command instructing a computer to take a specific action
Syntax
<a line of code>;
▪ Expression
An expression is a collection of operators and operands.
An expression can be reduced to some kind of "value".
Expressions are parts of statements.
Typical example:
#include <stdio.h>
int main()
{
printf("Hello! World!\n");
return 0;
}
▪ A program temporarily stores data in variables.
All variables must be defined before they can be used in a program.
▪ Data types
char, int, float, double , …
pointers
▪ Assignment statement
Assigning values to variables
Assigned values are used until re-assigned by the program.
Assignment operator (=) is used for assigning a value to a variable.
below code has an error
#include <stdio.h>
int main()
{
int num1;
int _Num2;
int nuM1;
int 1sum; // Wrong name!
// Initializing section
num1 = 1;
_Num2 = 2;
nuM1 = 3;
1sum = num1 + _Num2 + num3; // Unknown variable
return 0;
}
▪ Variable declarations with Initializations
We can give initial values for variables while declaring them.
Simply done by using assignment operators.
int num1 = 1;
#include <stdio.h>
int main()
{
int num1 = 1, num2 = 2, num3 = 3;
int sum = num1 + num2 + num3;
printf("sum1: %d\n", sum);
return 0;
}
▪ Identifiers and Case Sensitivity
A variable name in C is any valid identifier. An identifier is a series of characters consisting of letters,
digits and underscores (_) that does not begin with a digit.
C is case sensitive—uppercase and lowercase letters are different in C,
so a1 and A1 are different identifiers.
▪ Assignment Statement:
sum = integer1 + integer2; // assign total to sum
calculates the total of variables integer1 and integer2 and assigns the result to variable sum using
the assignment operator =.
The statement is read as, “sum gets the value of integer1 + integer2.”
The = operator and the + operator are called binary operators because each has two operands.
The + operator’s two operands are integer1 and integer2.
The = operator’s two operands are sum and the value of the expression integer1 + integer2.
#include <stdio.h>
▪ stdio.h
A header file in which functions for standard input/output are defined.
▪ #include <stdio.h>
You should write this statement in your source code to utilize standard
input/output functions.
printf
▪ Syntax
printf( format, . . . );
format : " characters and/or conversion specifications "
▪ Syntax
scanf( format, . . . );
format : " characters and/or conversion specifications "
#include <stdio.h>
int main()
{
int num1 = 1, num2 = 2, num3 = 3;
int sum = num1 + num2 + num3;
printf("sum1: %d\n", sum);
return 0;
}
▪ Follow-up question
Print multiple variable with one printf.
printf, scanf
#include <stdio.h>
int main()
{
int current_age, duration;
printf("Enter your age : ");
scanf("%d", ¤t_age);
printf("Your age is %d,\n", current_age);
printf("and now you fall asleep…\n\n\n");
calls function printf to print the literal Sum is followed by the numerical value of variable
sum on the screen.
This printf has two arguments, "Sum is %d\n" and sum.
The first argument is the format control string. It contains some literal characters to be
displayed, and it contains the conversion specifier %d indicating that an integer will be
printed.
The second argument specifies the value to be printed. Notice that the conversion
specifier for an integer is the same in both printf and scanf.
▪ Binary Arithmetic Operators
Operation Operator C expression
Addition + a+b
Subtraction - a–b
Multiplication * a*b
Division / a/b
Remainder % a%b
▪ Assignment Operator
v = e is to evaluate the expression e and copy its value into v
i = 5; /* i is now 5 */
j = i; /* j is now 5 */
k = 10 * i + j; /* k is now 55 */
▪ Integer division yields an integer result
The results are the quotients of division. (Floor operation is used implicitly.)
Ex: 7 / 4 → 1, 17 / 5 →3
▪ Remainder operator ( % )
yields the remainder after integer division
can be used only with integer operands
Ex: 7 % 4 → 3, 17 % 5 → 2
▪ Remainder operator with negative numbers
Follows the arithmetic. (divisor x quotient + remainder = dividend)
This causes an "implicit type cast" to take place, casting the 17 → 17.0
#include <stdio.h>
int main()
{
int intNum = 0;
printf("%d\n", intNum++);
//printf("%d\n", ++intNum);
//printf("%d\n", --intNum);
//printf("%d\n", intNum--);
return 0;
}
▪ Check the effects of implicit type casting. Variations will be shown.
#include <stdio.h>
int main()
{
int intNum1 = 10;
int intNum2 = 3;
int intNum3 = intNum1 / intNum2;
int intNum4 = intNum1 % intNum2;
return 0;
}
▪ Make a program that receives a five-digit number from a user and prints out
the digit in the middle of the number. Assume that there are no wrong
inputs from a user.
Input: 12345, output: 3
Input: 23456, output: 4
Often, an average is a value such as 7.2 or –93.5 that contains a fractional part. These
values are referred to as floating-point numbers and can be represented by the data type
float. The variable average is defined to be of type float to capture the fractional result of
our calculation.
However, the result of the calculation total / counter is an integer because total and
counter are both integer variables. Dividing two integers results in integer division in
which any fractional part of the calculation is truncated (i.e., lost). Because the calculation
is performed first, the fractional part is lost before the result is assigned to average.
To produce a floating-point calculation with integer values, we must create temporary
values that are floating-point numbers. C provides the unary cast operator to accomplish
this task.
▪ Converting Between Types Explicitly and Implicitly
average = ( float ) total / counter;
includes the cast operator (float), which creates a temporary floating-point copy of its
operand, total. The value stored in total is still an integer. Using a cast operator in this
manner is called explicit conversion. The calculation now consists of a floating-point value
(the temporary float version of total) divided by the unsigned int value stored in counter.
C provides a set of rules for conversion of operands of different types. Cast operators are
available for most data types—they’re formed by placing parentheses around a type name.
Each cast operator is a unary operator, i.e., an operator that takes only one operand. C
also supports unary versions of the plus (+) and minus (-) operators, so you can write
expressions such as -7 or +5.
Cast operators associate from right to left and have the same precedence as other unary
operators such as unary + and unary -. This precedence is one level higher than that of the
multiplicative operators *, / and %.