C Programming The Basics
Introduction • The C Programming Language was designed by Dennis Richie in Bell laboratories in early 1970s. • Currently, the most commonly-used language for embedded systems • Easy-to-understand compilation • Produces efficient code
Structure of a C Program • Every C program consists of one or more functions. • One of the functions must be called main. • The program will always begin by executing the main function.
C Function It must contain: •A function heading, which consists of the function name, followed by an optional list of arguments enclosed in parentheses. •A compound statement, which comprises the remainder of the function. •Each compound statement is enclosed within a pair of braces: ‘{’ and ‘}’ •Comments may appear anywhere in a program, enclosed within delimiters ‘/*’ and ‘*/’.
Sample Program Header file includes functions for I/O
#include <stdio.h> /* This is a sample program */ int main() { printf(“Hello, World! \n”); return 0; } Blocks of code are marked by curly braces {….}
This is a comment. Compiler ignores this main function is executed when you run the program Function to print a message. ‘\n’ denotes new line. Function returns integer ‘0’
Header Files • The files that are specified in the include section is called as header file • These are precompiled files that have some • functions defined in them • We can call those functions in our program • by supplying parameters • Header file is given an extension .h • C Source file is given an extension .c
Data types in C • char o Character value – 1 byte quantity
• int o Integer value – 4 byte quantity
• float o floating point number (number with a decimal point) o 4 byte quantity
• double o double precision floating point number o 8 byte quantity
• Some of the basic data types can be augmented by using certain data type qualifiers like ‘unsigned’, ‘short’, ‘long’ etc.
Operators Three types: •Arithmetic Operators •Relational Operators •Logical Operators •Bitwise Operators
Arithmetic Operators Sl. No.
Operation
Operator
1.
Addition
+
2.
Subtraction
-
3.
Multiplication
*
4.
Division
/
5.
Modulo
%
• Priority Order: ( * , / , % ) > ( + , - ) • For operators of same priority, evaluation is from left to right • Parenthesis may be used to change precedence of operators
Relational Operators â&#x20AC;˘ Used to compare two quantities Sl. No.
Function
Operator
1.
Is greater than
>
2.
Is less than
<
3.
Is equal to
==
4.
Is greater than or equal to
>=
5.
Is lesser than or equal to
<=
6.
Is not equal to
!=
â&#x20AC;˘ When arithmetic expressions are used on either side of a relational operator, the arithmetic expressions will be evaluated first and then the results compared.
Logical Operators Sl. No.
Operation
Operator
True when
1.
Logical AND
&&
Both statements are TRUE
2.
Logical OR
||
At least one statement is TRUE
3.
Logical NOT
!
The statement is FALSE
• They act upon logical expressions that are individually TRUE or FALSE and output a Boolean value (TRUE/FALSE) • NOT is a unary operator. It acts on a single logical expression.
Bitwise Operators Sl. No.
Function
Operator
1.
AND
&
2.
OR
|
3.
NOT
~
4.
XOR
^
5.
Left Shift
<<
6.
Right Shift
>>
IF ELSE statement Syntax: if (Expression) { Action 1 } else { Action 2 }
•If the Expression is true then execute Action 1, else execute Action 2 •Action 1 and Action 2 are either a single statement or a group of statements within braces
FOR loop Syntax: for ( initialization; condition; increment ) { set of statements }
Example (to print hello 10 times): for (int i = 0; i < 10; i++) { printf(â&#x20AC;&#x153;Hello \nâ&#x20AC;?); }
WHILE loop Syntax: while ( condition ) { set of statements }
Example (to print Hello 5 times): int a = 5; while ( a != 0 ) { printf(â&#x20AC;&#x153;Hello \nâ&#x20AC;?); a--; }
Macros â&#x20AC;˘ Macro substitution is a process where an identifier in a program is replaced by a pre defined string composed of one or more tokens â&#x20AC;˘ It has the following form #define identifier string â&#x20AC;˘ Example: #define pi 3.141592 #define SQUARE_AREA(side) (side*side)