Documente Academic
Documente Profesional
Documente Cultură
Advantages of pseudocode
Pseudocode is understood by the programmers of all types.
It enables the programmer to concentrate only on the algorithm part of the code
development.
It cannot be compiled into an executable program. Example, Java code : if (i < 10) { i++; }
pseudocode :if i is less than 10, increment i by 1.
Variables
A variable is a named storage location, where data may be stored and later changed.
An identifier is a more general term for a named location, which may contain either data or
code.
o Identifiers must begin with a letter or an underscore, preferable letters for user
programs.
o The remaining characters must be either alphanumeric or underscores.
o Identifiers may be of any length, but only the first 31 characters are examined in most
implementations.
o Identifiers are case sensitive, so "NUMBER", "number", and "Number" are three
different identifiers.
o By convention ordinary variables begin with a lower case letter, globals with a Single
Capital, and constants in ALL CAPS.
Multi-word variables may use either underscores or "camel case", such as
"new_value" or "newValue".
o Integers are usually assigned variable names beginning with the letters I, J, K, L, M, or N,
and floating point variables are usually assigned names beginning with other letters.
o Identifiers may not be the same as reserved words. ( See text for a full list. )
All variables must be declared before they can be used.
o In C, all variables must be declared before the first executable statement of the
program.
o C++ allows variables to be declared any time before they are used, but it is still normally
good practice to declare all variables at the beginning of the program, unless there is a
very good reason to do otherwise.
( Exceptions: Loop counter variables are often declared as part of the loop
structure. Occasionally it is beneficial to declare variables within a reduced
scope, to be discussed later. )
Variables may be given an initial value at the time they are declared. This is called
"initialization", or "initializing the variables".
o Initialization in C should be done using an equals sign.
o Example: double x1 = 0.0, x2 = 0.0;
o UNINITIALIZED VARIABLES ARE DANGEROUS, AND SHOULD BE CONSIDERED TO HOLD
RANDOM VALUES.
Variables may be declared "const", meaning that their values cannot be changed.
o const variables MUST be initialized at the time they are declared.
o By convention, const variables are named using ALL CAPS.
o Examples:
const double PI = 3.14159;
const int MAXROWS = 100;
o Note: K&R C did not have the const keyword, and so the #define pre-processor macro
was used to define constant values. The const qualifier is a better approach when it is
available, because it allows the compiler to perform type checking among other reasons.
For CS 107 we will defer the discussion of #define until we get to the chapter on the pre-
processor.
Data Types
int
The most basic and commonly used integral type is "int".
The int data type is always the "best" size for the particular computer it is running
on, typically 32 bits
Format specifiers for ints are either %d or %i, for either printf or scanf.
long int
A long int typically uses twice as many bits as a regular int, allowing it to hold much
larger numbers.
o ( The C standard only specifies that a long cannot use a fewer number of
bits than a regular int )
printf and scanf replace %d or %i with %ld or %li to indicate the use of a long int.
long int may also be specified as just long.
short int
A short int may use fewer bits than a regular int, thereby saving storage space.
o ( The C standard only specifies that a short int cannot use more bits than a
regular int. On many machines short ints use the same number of bits as
regular ints. )
printf and scanf replace %d or %i with %hd or%hi to indicate the use of a short int.
short int may also be specified as just short.
unsigned ints
Unless otherwise specified, all of the aforementioned int types are signed
numbers.
Any of the above may be preceded by the keyword "unsigned" to indicate that
they are unsigned.
o e.g. "unsigned int", "unsigned long int", "unsigned long", etc.
o "unsigned" by itself implies "unsigned int"
The format specifier for unsigned integers in decimal form is %u. The u may be
preceded by l, ll, or h for long, long long, and short unsigned types respectively.
Unsigned integers can also be printed or scanned in octal or hexidecimal form
using the %o, %x, or %X format specifiers in place of %u.
char
Normally chars are interpreted as characters ( see below )
Technically the char data type is an integral type, always having exactly 8 bits, (
known as a byte. )
Signed chars can have values from -128 to +127, and can be printed as integers
using %d or %i format specifiers
chars can also be specified as unsigned, giving them a range from 0 to+255.
o Unsigned chars can be printed using %u, %o, %x, or %X format specifiers.
Integral Constants
int constants in decimal form must begin with a digit from 1 to 9, and can be
followed by additional digits from 0 to 9.
o in octal form they must begin with a 0, and following digits must be in the
range from 0 to 7.
o in hexidecimal form they begin with 0x. Followng digits must be in the
range 0 to 9, or A to F, or a to f.
Any of the above may be preceded by a minus sign.
int constants may be followed by a u, l, or ll to indicate unsigned, long, or long long
constants respectively.
Allowable formats are as follows, where the [ square brackets ] denote optional characters:
Decimal: [±]1-9[0-9...][Ll][Uu]
Octal: [±]0[0-7...][Ll][Uu]
Hexadecimal: [±]0x[0-9a-fA-F...][Ll][Uu]
Integer Overflow
When integer math yields a result that is too big ( or too negative ) to be
represented by the corresponding integer type, then overflow ( or underflow ) is
said to have occurred.
With unsigned numbers the result is defined to "wrap around" to the other end of
the integer's range, so if you add 1 to an integer that is at the maximum value for
it's type, the result is zero.
With signed numbers the result of overflow is undefined. In some cases adding 1
to the maximum positive value will wrap around to the most negative value, but in
other cases erratic behaviour or even a program crash may occur.
float
The most basic type of floating point number is the float type.
According to the IEEE standard, a single precision floating point number is exactly
32 bits long, comprised of:
o one bit for indicating the sign
o 23 bits ( plus one implied ) for recording the digits as a 24-bit integer. This
works out to about 6 or 7 decimal digits of precision.
o 8 bits for a binary exponent, to shift the digits left or right. This works out
to an absolute range from about 10^-38 to 10^38
Note that because a float only has 24 bits available for storing the digits of the
number, it can actually be less precise than a 32-bit int for certain large integers.
double
The double precision data type uses twice as many bits as a float, yielding
approximately twice the number of digits of precision.
According to the IEEE standard, a double precision floating point number is 64 bits
long, comprised of:
o one sign bit.
o 52 bits ( plus one implied ) for recording digits, which works out to about
15 decimal digits of precision.
o 11 bits for the exponent, which works out to a range of about 10^-308 to
10^308
The double data type is the preferred floating point type for most scientific and
engineering calculations.
long double
The long double type is guaranteed to have more bits than a double, but the exact
number my vary from one hardware platform to another. The most typical
implementations are either 80 or 128 bits.
The IEEE standard for quadruple precision floating point numbers is 128 bits
consisting of:
o one sign bit
o 112 bits ( plus one implied ) for digits, working out to about 34 decimal
digits of precision
o 15 bits for the exponent, giving a range from about 10^-4932 to 10^4932
Characters
Although the char data type is technically a small integer ( see above ), its most common use is
to hold numeric codes representing ( ASCII ) characters.
For example, the ASCII code for the capital letter 'A' is 65.
Note that the character '9' is not the same as the integer value 9.
o In this case the ASCII code for the character '9' is 57.
o The numerical value 9 in the ASCII code set happens to represent a horizontal tab
character.
The full ASCII code table can be found in the back of most programming textbooks, or online at
http://www.asciitable.com/ and many other sites.
Character Constants
Character constants are enclosed in single quotes, such as 'A'.
Character constants may also include escape characters such as '\n' or '\t', as
shown here:
Escape Sequence
Meaning
\a alarm ( bell )
\b Backspace
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\? Question mark
Character Strings
C stores character strings as arrays of type char, terminated by a null byte.
Constant character strings are enclosed in double quotes, and may include escape characters,
such as "\n\n\t Please enter X > "
These notes will postpone further discussion of character strings until after arrays have been
covered.
1. #include<stdio.h>
2. #include<math.h>
3.
4. main()
5. {
6. float radius, area;
7.
8. printf("Enter the radius of circle\n");
9.
10. scanf("%f",&radius);
11.
12. /* M_PI (pi) is a constant in math.h header file */
13.
14. area = M_PI*radius*radius;
15.
16. printf("Area of circle = %.2f\n", area);
17.
18. return 0;
19.}
***
*****
*******
*********
1. #include <stdio.h>
2.
3. int main()
4. {
5. int row, c, n, s;
6.
7. printf("Enter the number of rows in pyramid of stars you wish to see\n");
8. scanf("%d", &n);
9.
10. s = n;
11.
12. for (row = 1; row <= n; row++) // Loop to print rows
13. {
14. for (c = 1; c < s; c++) // Loop to print spaces in a row
15. printf(" ");
16.
17. s--;
18.
19. for (c = 1; c <= 2*row - 1; c++) // Loop to print stars in a row
20. printf("*");
21.
22. printf("\n");
23. }
24.
25. return 0;
26.}
Factorial program in C using a for loop
1. #include <stdio.h>
2.
3. int main()
4. {
5. int c, n, fact = 1;
6.
7. printf("Enter a number to calculate its factorial\n");
8. scanf("%d", &n);
9.
10. for (c = 1; c <= n; c++)
11. fact = fact * c;
12.
13. printf("Factorial of %d = %d\n", n, fact);
14.
15. return 0;
16.}
C programming code
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, t, sum = 0, remainder;
6.
7. printf("Enter an integer\n");
8. scanf("%d", &n);
9.
10. t = n;
11.
12. while (t != 0)
13. {
14. remainder = t % 10;
15. sum = sum + remainder;
16. t = t / 10;
17. }
18.
19. printf("Sum of digits of %d = %d\n", n, sum);
20.
21. return 0;
22.}
20.}