C
programming (1)
INTEGER NUMBERS
short int
int
long int
REAL NUMBERS
float
:precision 6 decimal digits, maximum value 3.4x10**38, smallest value1.17x10**(-38)
double:
precision 15 decimal digits, maximum 1.79x10**308, smallest value2.22x10(-308)
long double:
precision 19 decimal digits, maximum 1.18x10**4932, smallest value3.36x10**(-4932)
CHARACTER COMMAND
char
EXAMPLES
char destination[12];
char name[10] = "Jeannette";
char name[] = "Jeannette";
int time;
CONSTANTS
const char name[] = "Jeannette";
const float vat = 0.175;
const float pi = 3.14159;
LIBRARIES
<assert.h> |
Diagnostics |
<ctype.h> |
Character handling |
<errno.h> |
Errors |
<float.h> |
Characteristics of floating types |
<limits.h> |
Sizes of Integral Types |
<locate.h> |
Localisation |
<math.h> |
Mathematics |
<setjmp.h> |
Non-local jumps |
<signal.h> |
Signal Handling |
<stdarg.h> |
Variable Arguments |
<stddef.h> |
Common definitions |
<stdio.h> |
Input/Output |
<stdlib.h> |
General Utilities |
<string.h> |
String handling |
<time.h> |
Date and time |
OUTPUT
printf("Hello");
Symbols for printing
\a |
Alert(bell) |
\v |
Vertical tab |
\b |
Backspace |
\\ |
Backslash |
\f |
Form Feed |
\? |
Question mark |
\n |
New Line |
\' |
Single quote |
\r |
Carriage return |
\" |
Double quote |
\t |
Horizontal tab |
|
|
EXAMPLES
printf("Hello\n");
Format specifications corresponding to the list of arguments after the control string:
Form type:
%width[.precision]type
The type must be one of the following:
d,i |
Decimal integer |
o |
Unsigned octal |
x,X |
Unsigned hexadecimal |
u |
Unsigned decimal integer |
c |
Single-character |
s |
String |
f |
Floating-point number |
e,E |
Floating point number in exponential format |
g,G |
Floating point number in either fixed decimal or exponential format, whichever is more compact |
EXAMPLES
printf("alpha = %5d\n", alpha); alpha=31678
printf("beta = %-10.4f", beta); beta=1.2345
printf("Cash amount\t\t%5.2f\n", cash);
Cash amount 20.00
INPUT
scanf("%d", &alpha);
scanf("%10f", &beta);
scanf("%u%u%u", &day, &month, %year);
CHARACTER INPUT/OUTPUT
The routine getchar() will enable a single character to be input at the keyboard and stored as an
integer. So :char character;
int code;
printf("Input a single character");
code = getchar();
character = (char) code;
How to overcome a problem like this:
int price;
char nadia[10];
printf("Enter your name");
gets(nadia);
printf("Price of %s" , nadia);
scanf("%2d", &price); getchar()
INTEGER DIVIDED BY INTEGER ****
int distance;
int speed;
float time;
time = (float) distance / (float) speed;
PROGRAM FORMAT
/*
You can write whatever you want in here....
*/
#include <stdio.h>
main()
{
declaration of constants and variables
sequence of instructions
}
ARITHMETIC
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
THE
if STATEMENTif
(expression) statementif
(expression) statement else statementRELATIONAL OPERATORS
OPERATOR |
MEANING |
OPERATOR |
MEANING |
> |
Greater than |
<= |
Less than or equal to |
< |
Less than |
!= |
Not equal to |
== |
Equal to |
&& |
AND |
>= |
Greater than or equal to |
|| |
OR |
if
(alpha == 2)print("Hello");
if
(beta >= 3)printf("Good morning");
else
printf("Good night");
if
( alpha == beta){
A=B;
C=D;
}
else
{
A=D;
C=B;
}
if
(a <= 5 && b<= 3) reply = reply -32
if
(reply == yes && temperature < 60)a = a+5;
else if
(reply == yes && temperature <= 60)a=a+6;
else if
(reply == no && temperature <= 60)a=a+7;
else
a=a+8;
if (C == V1) { E1; } else if ( C == V2) { E2; } else if (C == V3) { E3; } |
switch (C) { case V1 : { E1; break; } case V2 : { E2; break; } case V3 : { E3; } } |
if (C == V1) { E1; } else if ( C == V2) { E2; } else if (C == V3) { E3; } else { E4; } |
switch (C) { case V1 : { E1; break; } case V2 : { E2; break; } case V3 : { E3; break; } default : { E4; } } |
switch (number)
{
case 5: printf("The input number is 5"); break;
case 10: printf("The input number is 10"); break;
case 29: printf("The input number is 29"); break;
default: printf("sorry, no such number");
}
switch (month)
{
case 1: case 3:
number = 31; break;
case 2: case 4: {
if (year == 0)
printf("Hello");
else
a = 56;
break;
}
default: a = 76;
}
In a string declaration, it is not possible to assign a string literal to a string variable. A string must be copied into a variable using the routine strcpy from the <string.h> header found in the standard library.
Enumerating data type
An enumeration is a collection of integer values that have been given names by the programmer. All enumerated constants start with the integer value 0 unless otherwise specified with the programmer.
enum ( red, blue, green=10, yellow } colour;
C gives the values of red=0, blue=1, green=10, yellow=11. The variable colour can get only one of these four values.
The
typedef commandThis command enables the programmer to explicitly define new commands by using the ones that already exist. It is like having a cope of the commands with other names.
typedef long int FOUR_BYTE_INT;
FOUR_BYTE_INT x;
x is now a long int.
typedef enum {false, true} boolean;
boolean error = false;
Boolean can be either false=0 or true=1. The second command initialises boolean with the value false.
Example:
#include <stdio.h>
#include <string.h>
main()
{
typedef enum{false, true} boolean;
const char yes = 'Y';
const char no = 'N';
char reply;
char garment[9];
int temperature;
boolean error = false;
printf("What is the temperature outside today? ");
scanf("%2d", &temperature); getchar() ;
printf("Is it raining outside? Answer Y or N");
scanf("%c", &reply);
if (reply == yes)
if (temperature < 60)
strcpy(garment, "raincoat");
else
strcpy(garment, "umbrella");
else
if (reply == no)
if (temperature < 60)
strcpy(garment, "overcoat");
else
strcpy(garment, "jacket");
else
error = true;
if (error)
printf("DATA ERROR - reply not input as either Y or N\n");
else
printf("before you go out today take your %s\n", garment);
}