Data Types
A "C" language program is compiled to a machine language program. The machine language for that CPU will have it's own system in terms of what types are supported and how big those types are.
void
The void type means no value. It is usually used as a function
return value to indicate that the function does not return anything.
char
This type is used to store character values
char Size 1 byte -128 to 127
unsigned char Size 1 byte 0 to 255
We cannot store characters like "C" , "D" directly in the computer as everything in the computer is a number. There is a mapping that takes place. For the English language it is the ASCII chart.
For the English language we only need 128 numbers for all the characters. This may not work for other languages such as the Asian languages ( Japanese , Chinese ) that have lot for more characters in their alphabet.
integer type
int
unsigned int
short int
unsigned short int
long int
unsigned long int
floating type
This type can hold numbers with decimal points.
float
double
long double
Storage
The smallest unit of storage is a bit. A bit can contain the values 0 or 1 .
1Byte = 8 bits
1KiloByte = 1024 Bytes
1MegaByte = 1024 KiloBytes
1GigaByte = 1024 MegaBytes
1TeraByte = 1024 GigaBytes
Variables
A variable is an object of a particular type. A variable name can be composed of letters and digits. A dash "_" is also considered a part of the letter. The variable name cannot start with a digit. Variable names should be meaningful. Also if there are more words then the word should have a capitol letter such as:
largestTemperatureInGroup
charInString
boardArray
A variable name should not be a single letter such as "n" or "i" . A variable name should not start with "_" because that confuses with system variable names. You can also use dashes to separate the words such as:
largest_Temperature_In_Group
Basic Input/Output
We have seen how "printf" can print a string to a console. We are going to discuss some basic functionality of how to print to console and read from console. The function "printf" can also take multiple arguments with the first argument as a formatted string followed by values. The formatted string can take a place holder with the percentage sign "%" .
File: "pr1.c"
#include <stdio.h>
int main()
{
int x1 = 2 ;
int x2 = 3 ;
printf( "We have a %s in %s \n" , "lock down" , "place" ) ;
printf( "The sum of %d and %d is %d \n" , x1 , x2 , (x1+x2)
) ;
return 0 ;
}
Output:
We have a lock down in place
The sum of 2 and 3 is 5
The above code shows how the "%" place holder can be used in a string with the values being passed from arguments after the formatted strings. The "%s" is a place holder for a string whereas "%d" is a placeholder for an integer.
Some other "%" notations are:
%s -- string
%f -- floating point
%c -- character
We can also read from console using the "scanf" function.
File: "pr2.c"
#include <stdio.h>
int main()
{
int age ;
printf( "Enter your age:" ) ;
scanf( "%d" , &age ) ;
printf( "You are %d years old !\n" , age
) ;
return 0 ;
}
The "scanf" also uses a syntax that is similar to "printf" in terms of formatted string. The value is read into the integer variable age. A different syntax "&age" is used. The "&" means the address of and this will be explained later on in the chapter on pointers. C also has a function "getchar" to read a single character from a console.
File: "pr4.c"
#include <stdio.h>
int main ()
{
char ch ;
printf("Enter character: ");
ch = getchar();
printf("Character entered: ");
putchar( ch );
return(0);
}
Additional formatting can be done in the formatted string.
File: "pr3.c"
#include <stdio.h>
int main()
{
printf( "%6.1f %6.2f\n" , 3.14159, 3.14159 ) ;
printf( "%6.1f %6.2f\n" , 3.14159, 3.14159 ) ;
printf( "%6.1f %6.2f\n" , 3.14159, 3.14159 ) ;
return 0 ;
}
Output:
3.1 3.14
3.1 3.14
3.1 3.14
The "%6.1f" means we are working with a floating point number whose width should be 6 characters long and the ".1" means we only want one decimal point from the fraction.
File: "limit1.c"
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
int main(int argc, char** argv) {
printf("CHAR_BIT : %d\n", CHAR_BIT);
printf("CHAR_aMAX : %d\n", CHAR_MAX);
printf("CHAR_MIN : %d\n", CHAR_MIN);
printf("INT_MAX : %d\n", INT_MAX);
printf("INT_MIN : %d\n", INT_MIN);
printf("LONG_MAX : %ld\n", (long) LONG_MAX);
printf("LONG_MIN : %ld\n", (long) LONG_MIN);
printf("SCHAR_MAX : %d\n", SCHAR_MAX);
printf("SCHAR_MIN : %d\n", SCHAR_MIN);
printf("SHRT_MAX : %d\n", SHRT_MAX);
printf("SHRT_MIN : %d\n", SHRT_MIN);
printf("UCHAR_MAX : %d\n", UCHAR_MAX);
printf("UINT_MAX : %u\n", (unsigned int) UINT_MAX);
printf("ULONG_MAX : %lu\n", (unsigned long) ULONG_MAX);
printf("USHRT_MAX : %d\n", (unsigned short) USHRT_MAX);
return 0;
}
The above program prints the limits and sizes of certain types . We can also use a handy "sizeof" function that is available in "C" .
File: "limit2.c"
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
int main()
{
printf("%d\n", sizeof(char) ) ;
printf("%d\n", sizeof(int) ) ;
printf("%d\n", sizeof(double) ) ;
return 0;
}
Output:
1
4
8
The output might be different on a different hardware system . It is not "C' that is deciding the size of an integer but the machine language of the CPU.
The string "\n" is special and is known as a a newline . The system sees the combination and interprets it as a newline. The other backspace sequences are :
\a alert (bell) character
\\ backslash
\b backspace
\? question mark
\f formfeed
\' single quote
\n newline
\" double quote
\r carriage
\t horizontal tab
\xhh hexadecimal number
\v vertical tab
Comments
C allows single line comments with the "//" notation and "/* */" for multiple line comments
File: "comment.c"
#include <stdio.h>
//This is an example of a single line comment.
/*
This is an example of a multi line comment.
*/
int main ()
{
printf( "Comment line example" /*Comments can
appear anywhere except inside a string*/ ) ;
return(0);
}
Operators
Arithmetic operators are "+ . - , * , / " ; the modulus operator "%" and the increment and decrement operators ( ++ and -- ) .
File: "op1.c"
#include <stdio.h>
int main ()
{
int x1 = 3 ;
int x2 = 4 ;
char ch = 127 ;
printf( "%d\n" , 5 / 3 ) ;
//Something is wrong with the following
printf( "%d\n" , 5 / 3.0 ) ;
printf ( "%d\n" , ++x1 ) ;
printf ( "%d\n" , x1++ ) ;
//Treat a character as an integer
//Doing prefix or postfix makes no difference here
ch++ ;
printf( "%d\n" , ch ) ;
printf( "%f\n" , 5 / 3.0 ) ;
return(0);
}
Output:
1
-1431655765
4
4
-128
1.666667
Prefix and Postfix operators can increment or decrement by 1 . In the following using prefix or postfix makes no difference:
int x1 = 5 ;
x1++ ;
++x1 ;
We use prefix and posfix because it is more concise. The difference is in prefix the variable is incremented first and then the value is assigned to the variable. For postfix the value is incremented afterwards.
File: "pr1.c"
#include <stdio.h>
int main()
{
int x1 = 2 ;
int x2 = 3 ;
int y1 ;
y1 = ++x1 ;
//What is the value of y1
printf( "%d\n" , y1 ) ;
y1 = x1++ ;
printf( "%d\n" , y1 ) ;
return 0 ;
}
Output:
3
3
The modulus operator "%" returns the remainder . So n1 % x1 will return the remainder when n1 is divided by x1 .
File: "mod1.c"
#include <stdio.h>
int main ()
{
int x1 = 12 ;
printf( "%d\n" , x1 % 10 ) ;
return(0);
}
Output:
2
Casting
File: "cast1.c"
#include "stdio.h"
int main()
{
int x1 ;
float f1 ;
x1 = 5 / 2 ;
printf( "%d\n" , x1 ) ;
//Right hand side division is still by integers
//We get 2.0 printed out
f1 = 5 / 2 ;
printf( "%.1f\n" , f1 ) ;
//We force 5 to be considered as a floating point.
//All other lower range types are promoted
f1 = ((float)5) / 2 ;
printf( "%.1f\n" , f1 ) ;
//Another way of stating that the number 5 is floating point
f1 = 5.0 / 2 ;
printf( "%.1f\n" , f1 ) ;
//Both numbers are floating point.
f1 = 5.0 / 2.0 ;
printf( "%.1f\n" , f1 ) ;
//The cast happens after integer division is already done
//so does not make a difference
f1 = (float)( 5 / 2) ;
printf( "%.1f\n" , f1 ) ;
//Not safe. High range to low range
x1 = f1 ;
}
Output:
2
2.0
2.5
2.5
2.5
2.0
Expressions
An expression is a combination of constants , variables and operators that produces a value. An expression is something like
int x1 = 4 ;
(( x1 + 3 ) * 2 )
File: "expr1.c"
#include <stdio.h>
int main()
{
int x1 = 2 ;
int x2 = 3 ;
int y1 ;
printf( "%d\n" , (x1+x2) ) ;
printf( "%d\n" , (( x1 + 2 ) * 3 ) ) ;
return 0 ;
}
Statements
A statement is a command unit that does something. There are different kinds of statements.
1)
What doe the following program do ? Try to figure it out without actually running the program.
//File: "inc1.c"
#include <stdio.h>
int main ()
{
int x1 = 3 ;
int x2 = 4 ;
int x3 ;
x1 = x1 + 1 ;
x3 = ++x1 + ++x2 ;
printf( "%d\n" , x3 ) ;
x3 = x1++ + x2++ ;
//at this point x1 x2 are incremented
printf( "%d %d\n" , x1, x2 ) ;
printf( "%d\n" , x3 ) ;
printf( "%d\n" , ++x3 ) ;
printf( "%d\n" , x3++ ) ;
printf( "%d\n" , x3 ) ;
return(0);
}
What doe the following program do ? Try to figure it out without actually running the program.
Use the ascii chart.
#include <stdio.h>
int main ()
{
char ch1 = 'a' ;
char ch2 = 'B' ;
printf( "%c\n" , ( ch1+1 ) ) ;
printf( "%d %d\n" , ch1, ch2 ) ;
printf( "%c\n" , ('a' - 'A' + 'B') ) ;
return(0);
}
1)
10
6 6
10
11
11
12
2)
b
97 66
b