C programming notes

 the following are my random (refresh) notes on the C programming language.  

Found an excellent tutorial on line at http://www.tutorialspoint.com/cprogramming/c_quick_guide.htm
I like the way they have it organized, it's clean and simple, very methodical and the parallels to UNIX and Linux
appeal to me greatly.  Outstanding work, so, I am working through their site and making notes and modifying
small sections to provide a terse summary for myself to get back into C. Many of the details they mention are also helpful 
to understanding UNIX and Linux.  Recommend going to their page for the complete tutorial and examples.

C was developed by Dennis M. Ritchie to create the UNIX operating system at Bell Labs. 
Brian Kernighan and Dennis Ritchie produced the first publicly available version of C, known as K&R C.

Kernighan: He has said that if stranded on an island with only one programming language it would have to be C.

Kernighan coined the term Unix and helped popularize Thompson's Unix philosophy.
Kernighan is also known as a coiner of the expression "What You See Is All You Get" (WYSIAYG), 
which is a sarcastic variant of the original "What You See Is What You Get" (WYSIWYG).

Kernighan's term is used to indicate that WYSIWYG systems might throw away information in a document that 
could be useful in other contexts.    - (this is a exactly the reason why GUI interfaces are not good.)

UNIX began life in 1970, but the UNIX kernel was rewritten from B on PDP-7 to C on PDP-11 around 1973.  
In 1978 K&R C was released.  

I didn't realize that it was this late... I was teaching assembly language programming in 1981 and 1982... 
and in 1985 began working on UNIX as a circuit designer using CAE tools.  I began teaching UNIX and C
around 1992. Since then I've had a few students who actually worked with Kernighan, Ritchie and Thomson.
It's actually a small world and I hadn't realized how involved I've been in it.  I love teaching UNIX
and Linux classes, and am constantly learning and relearning things about it.  These notes are the
result of refreshing on C to help my son with an engineering project. What's really funny is he taught
himself C from my teaching notes and books back when he was still in junior high.  He should be teaching me.


White Space, Special Characters and CASE

Whitespaces are blanks, tabs, newline characters and comments. Whitespace separates one part of a statement from another and enables the compiler or the command line interpter to identify separate elements, options and commands. There are cases where white space is not required, but if used for readability is accepted, compare: fruit = apples + oranges; // get the total fruit fruit=apples + oranges; // get the total fruit Special Characters C, UNIX, Linux and the various shells and tools used, are all CASE sensitive.

33 Key or Reserved Words

The following words are not to be used for variable or function names - these along with normal commands found in the operating system should be avoided or the program may not work or produce unexpected results.
  1. auto
  2. break
  3. case
  4. char
  5. const
  6. continue
  7. default
  8. do
  9. double
  10. else
  11. enum
  12. extern - keyword to declare a variable at any place.
  13. float
  14. for
  15. goto
  16. if
  17. int
  18. long
  19. _Packed
  20. register
  21. return
  22. signed
  23. sizeof
  24. short
  25. static
  26. struct
  27. switch
  28. typedef
  29. union
  30. unsigned
  31. void
  32. volatile
  33. while

Tokens, semicolons and comments

A C program consists of tokens, they are: keywords, identifiers, constants, string literals, or symbols. Semicolons (;) are statement terminators. Each statement (logical entity) must be ended with a semicolon. e.g., two different statements: printf("Hello, World! \n"); return 0; Comments are ignored by the compiler. They start with /* and terminate with */ as shown below: /* this is a comment found in a C program */ C++ introduced a double slash comment prefix // as a way to comment single lines. The following is an example of this: // Author: TechOnTheNet.com This form of commenting may be used with most modern C compilers if they also understand the C++ language. (see also: http://www.techonthenet.com/c_language/comments.php)

C Types

The types in C can be classified as follows:

S.N. 	Types and Description

1 	Basic Types: They are arithmetic types and consists of the two types: 
		(a) integer types and 
		(b) floating-point types.

2 	Enumerated types: They are again arithmetic types and they are used 
	to define variables that can only be assigned certain discrete integer 
	values throughout the program.

3 	The type void: The type specifier void indicates that no value is available.

4 	Derived types: They include 
		(a) Pointer types, 
		(b) Array types, 
		(c) Structure types, 
		(d) Union types and 
		(e) Function types.

Integer Types

Following table gives you details about standard integer types with its storage sizes and value ranges:

Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295
example code snippet: #include #include int main() { printf("Storage size for int : %d \n", sizeof(int)); return 0; }
Compiling the source code.... $gcc main.c -o demo -lm -pthread -lgmp -lreadline 2>&1 Executing the program.... $demo Storage size for int : 4

Floating-Point Types

standard floating-point types with storage sizes, value ranges and their precision:

Type Storage size Value range Precision
float 4 byte 1.2E-38 to 3.4E+38 6 decimal places
double 8 byte 2.3E-308 to 1.7E+308 15 decimal places
long double 10 byte 3.4E-4932 to 1.1E+4932 19 decimal places
#include #include int main() { printf("Storage size for float : %d \n", sizeof(float)); printf("Minimum float positive value: %E\n", FLT_MIN ); printf("Maximum float positive value: %E\n", FLT_MAX ); printf("Precision value: %d\n", FLT_DIG ); return 0; } When you compile and execute the above program, it produces the following result on Linux: Storage size for float : 4 Minimum float positive value: 1.175494E-38 Maximum float positive value: 3.402823E+38 Precision value: 6

Variables

Variable names are composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. REMEMBER: C is case-sensitive. Basic variable types are: Type Description char Typically a single octet(one byte). This is an integer type. int The most natural size of integer for the machine. float A single-precision floating point value. double A double-precision floating point value. void Represents the absence of type.

Variable declaration

A variable declaration is useful when you are using multiple files and you define your variable in one of the files which will be available at the time of linking of the program. You will use extern keyword to declare a variable at any place. Though you can declare a variable multiple times in your C program but it can be defined only once in a file, a function or a block of code. extern int a; replaces #define int a (note, no semicolon after #define) #include // Variable declaration: extern int a, b; extern int c; extern float f; int main () { /* variable definition: */ int a, b; int c; float f; /* actual initialization */ a = 10; b = 20; c = a + b; printf("value of c : %d \n", c); f = 70.0/3.0; printf("value of f : %f \n", f); return 0; } When the above code is compiled and executed, it produces the following result: value of c : 30 value of f : 23.333334

Lvalues and Rvalues in C

There are two kinds of expressions in C: lvalue : lvalues may appear on either the left-hand or right-hand side of an assignment. rvalue : rvalues may appear on the right- but NOT left-hand side of an assignment. Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and can not appear on the left-hand side. Following is a valid statement: int g = 20; whereas: 10 = 20; is NOT a valid statement.

Literals

Prefix identifies whether it is a decimal, octal, or hexadecimal constant. 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal. An integer literal can also have a suffix that is a combination of U and L, for unsigned and long. The suffix can be uppercase or lowercase and can be in any order. Here are some examples of integer literals: 212 /* Legal */ 215u /* Legal */ 0xFeeL /* Legal */ 078 /* Illegal: 8 is not an octal digit */ 032UU /* Illegal: cannot repeat a suffix */ Following are other examples of various type of Integer literals: 85 /* decimal */ 0213 /* octal */ 0x4b /* hexadecimal */ 30 /* int */ 30u /* unsigned int */ 30l /* long */ 30ul /* unsigned long */

Floating-point literals

Floating-point literals have an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form. While representing using decimal form, you must include the decimal point, the exponent, or both and while representing using exponential form, you must include the integer part, the fractional part, or both. The signed exponent is introduced by e or E. examples of floating-point literals: 3.14159 /* Legal */ 314159E-5L /* Legal */ 510E /* Illegal: incomplete exponent */ 210f /* Illegal: no decimal or exponent */ .e55 /* Illegal: missing integer or fraction */

Character constants

Character literals are enclosed in single quotes, e.g., 'x' and can be stored in a simple variable of char type. A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'), or a universal character (e.g., '\u02C0'). There are certain characters in C when they are preceded by a backslash they will have special meaning and they are used to represent like newline (\n) or tab (\t). Here, is a list of escape sequence codes:
Escape sequenceMeaning
\\ \ character
\' ' character
\" " character
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ooo Octal number of one to three digits
\xhh . . . Hexadecimal number of one or more digits

String literals

String literals or constants are enclosed in double quotes "". A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters. You can break a long line into multiple lines using string literals and separating them using whitespaces. examples of string literals. (all three examples render the same) "hello, dear" "hello, \ dear" "hello, " "d" "ear"

Defining Constants

There are two simple ways in C to define constants: Using #define preprocessor. Using const keyword. The const Keyword You can use const prefix to declare constants with a specific type as follows: const type variable = value; (the use of extern seems to be preferred to #define where the compiler is C++ capable)

The #define Preprocessor

To define a constant using #define preprocessor: #define identifier value example: #include #define LENGTH 10 #define WIDTH 5 #define NEWLINE '\n' int main() { int area; area = LENGTH * WIDTH; printf("value of area : %d", area); printf("%c", NEWLINE); return 0; } When compiled and executed, it produces the following result: value of area : 50

The const Keyword

To declare constants of a specific type use the const prefix: const type variable = value; example: #include <stdio.h> int main() { const int LENGTH = 10; const int WIDTH = 5; const char NEWLINE = '\n'; int area; area = LENGTH * WIDTH; printf("value of area : %d", area); printf("%c", NEWLINE); return 0; } When compiled and executed, it produces the following result: value of area : 50 It is recommended practice to use CAPITALS to define constants.

C - Operators

arithmetic, relational, logical, bitwise, assignment and other operators one by one

Arithmetic Operators

Following table shows all the arithmetic operators supported by C language. Assume variable A holds 10 and variable B holds 20 then: Show Examples
Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiples both operands A * B will give 200
/ Divides numerator by de-numerator B / A will give 2
% Modulus Operator and remainder of after an integer division B % A will give 0
++ Increments operator increases integer value by one A++ will give 11
-- Decrements operator decreases integer value by one A-- will give 9

Relational Operators

Following table shows all the relational operators supported by C language. Assume variable A holds 10 and variable B holds 20, then: Show Examples
Operator Description Example
== Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true.
!= Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true.
> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.

Logical Operators

all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0, then:

Show Examples
Operator Description Example
&& Called Logical AND operator. If both the operands are non-zero, then condition becomes true. (A && B) is false.
|| Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. (A || B) is true.
! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is true.


Bitwise Operators

The Bitwise operators supported by C language Assume variable A holds 60 and variable B holds 13 then:

Show Examples
Operator Description Example
& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100
| Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 61 which is 0011 1101
^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001
~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.
<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240 which is 1111 0000
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15 which is 0000 1111

Assignment Operators

assignment operators Show Examples
Operator Description Example
= Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assign value of A + B into C
+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C - A
*= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand C *= A is equivalent to C = C * A
/= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C / A
%= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operan C %= A is equivalent to C = C % A
<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator C &= 2 is same as C = C & 2
^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2
|= bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2


SEARCH and Navigation TOOL
Google     select a domain to search or visit.
(use back key to return )

johnmeister.com/jeep/sj

FULL SIZE JEEPS
JeepMeister
"Jeep is America's
only real sports car."
-Enzo Ferrari
JohnMeister.com- fotos LinuxMeister- CS
MeisterTech- Diesels FotoMeister.us- fotos
BibleTech- Bible Overview search the the internet
Everett weather - Seattle traffic - pollen count -
NEWS: BBC: Middle East - Israel - Spiegel
NASB/KJV/ES/D - SE Asian Missions - jihad - persecution info
e-books by john:

AMSOIL product guide,
AMSOIL web, or 1-800-956-5695
use customer #283461

Amsoil dealer since 1983

CAMERAS: Nikon Lumix Canon DSLRs Lenses
Computers: Toshiba Toughbook Apple Dell
BOOKS: Auto Repair Diesels BioDiesel
PARTS: Wagoneer J-truck Benz VW
books and computers


SJ - 1962-1991

XJ - 1984-2001

WJ - 1999-2004

KJ - 2002-2007

WK - 2005-2010

Find the recommended
AMSOIL synthetics
for your Jeep

CJ-10A - 1984-1986

Jeepsters

MJ - 1984-1992

Willys - 1946-1965

Other Jeeps (FC)