2_Overview of C Programming Language
2_Overview of C Programming Language
You may write the variables of the same datatype in one line:
int mobileNumber, number_Of_Children;
float salary, age;
double lightSpeed, Weight;
Executable Statements
Executable Statements: C statements used to write or code the algorithm
C compiler translates the executable statements to machine code
Assignment Statements
Input/Output Operations and Functions (printf, scanf )
return Statement
The assignment statement above assigns a value to the variable kms. The value
assigned is the result of the multiplication of the constant KMS_PER_MILE by the
variable miles.
More on Assignments
In C, the symbol = is the assignment operator
Read it as “becomes”, “gets”, or “takes the
value of” rather than “equals” because it is
not equivalent to the equal sign of
mathematics.
In C, == tests equality. (We will study it
later)
In C you can write assignment statements of
the form:
sum = sum + item;
where the variable sum appears on both sides
of the assignment operator.
This is obviously not an algebraic equation, but
it illustrates a common programming practice.
This statement instructs the computer to add
the current value of sum to the value of item;
the result is then stored back into sum.
Input/Output Operations
Input operation: data transfer from the outside world into computer memory
Output operation: program results can be displayed to the program user
Input/output functions: special program units that do all input/output
operations
output function: printf
input function: scanf
Function call:
In C, a function call is used to call or activate a function
Calling a function means asking another piece of code to do some work for you
Output Function: printf
The printf function may display string and values of the expressions and
variables in the print list in the same order from left-to-right
String are given between two double quotations
Syntax:
printf(“formatted
string”)
printf(“formatted string”, print list)
Placeholders
Placeholder always begins with the symbol %
It marks the place in a format string where a value will be printed out or
will be inputted
Format strings can have multiple placeholders, if you are printing
multiple values
Placeholder Variable Type Function Use
%c char printf/scanf
%d int printf/scanf
%f float / double Printf
%lf double Scanf
Placeholders for char type
Characters are actually represented in C as integer values.
Each character is represented by its ASCII code (e.g A = 65. B = 66, etc).
The table after the program below shows the printable ASCII characters and their
corresponding ASCII codes.
Printing a char variable using “%c” will print the character but printing it with “%d”
will print the ASCII code.
Similarly, printing an integer variable with “%c” will also print the character
provided the value is within the range of character values.
The following example demonstrates this:
Escape Sequence
The backslash \ is called an escape character.
Indicates that printf is supposed to do something unusual
When encountering a backslash, printf looks to the next character and combines
it with the backslash to form an escape sequence.
Examples:
printf(“This is one line\n“);
printf(“and\nthis\tis\tanother\n“);
Important: When input data is needed in an interactive program, it is better to
use the printf function to prompt a message that tells the user what data to enter
printf(“Enter the distance in miles: “);
Escape Sequence Example
Output Formatting
Integer Formatting
You can specify how printf will display numeric integer values as follows:
%#d
Where # is a number that represents the field width and it is optional
If # is less than the integer size, it will be ignored
If # is greater than the integer size, extra spaces will be added on the left
Value Format Displayed Value Format Displayed
output output
234 %4d ░234 -234 %4d -234
234 %5d ░░234 -234 %5d ░-234
234 %6d ░░░234 -234 %6d ░░-234
234 %1d 234 -234 %2d -234
The symbol ░ represents one blank space
Output Formatting
float and double Formatting
You can specify how printf will display real numbers (float and double) as
follows:
%n.mf
n is a field width (optional):
it is equal to the number of digits in the whole number, the decimal point,
and fraction digits
If n is less than what the real number needs it will be ignored
m: Number of decimal places (optional)
Value Format Displayed output Value Format Displayed output
3.14159 %5.2f ░3.14 3.14159 %4.2f 3.14
3.14159 %3.2f 3.14 3.14159 %5.1f ░░3.1
3.14159 %5.3f 3.142 3.14159 %8.5f ░3.14159
.1234 %4.2f 0.12 -.006 %4.2f -0.01
-.006 %8.3f ░░-0.006 -.006 %8.5f -0.00600
-.006 %.3f -0.006 -3.14159 %.4f -3.1416
Input Function: scanf
The scanf function copies into memory data entered during the program execution
The order of the placeholders must correspond to the order of the variables in the input list
The data must be entered in the same order in the input list
You should insert one or more blank characters or carriage returns between numeric items.
Syntax:
scanf(“placeholders”, input list addresses)
In the following figure:
Possible inputs:
Bad: Good:
Arithmetic Expressions
To solve most programming problems, you will need to write arithmetic
expressions that manipulate type int, float, and double data.
Each operator manipulates two operands, which may be constants, variables,
or other arithmetic expressions.
Example
Arithmeti
Works with
5 + 2 c Examples
datatype
Operator
sum + (incr * 2) 5 + 2 is 7
+ int and double
(b/c) + (a + 0.5) 5.0 + 2.0 is 7.0
5 - 2 is 3
The following table shows all - int and double
5.0 - 2.0 is 3.0
arithmetic operators. 5 * 2 is 10
* int and double
5.0 * 2.0 is 10.0
5 / 2 is 2
/ int and double 29
5.0 / 2.0 is 2.5
% int 5 % 2 is 1
Operators: / (division) and % (remainder)
Division: When applied to two positive integers, the division operator (/)
computes an integral part of the result by dividing its first operand by its second.
For example: 7.0 / 2.0 is 3.5
7 / 2 is only 3
The reason for this is that C makes the answer be of the same type as the
operands.
Remainder: The remainder operator (%) returns the integer remainder of the
result of dividing its first operand by its second.
Examples: 7 % 2 = 1,
6%3=0
The value of m%n must always be less than the divisor n.
The operations / and % are undefined when the divisor (second operator) is 0.
30
Data Type of an Expression
The data type of each variable must be specified in its declaration, but how does C
determine the data type of an expression?
Example: What is the type of expression x + y when both x and y are of type
int?
The data type of an expression depends on the type(s) of its operands.
If both are of type int, then the expression is of type int
If either one or both is of type double, then the expression is of type double.
Example: what is the result of 5 / 2.0