C – Program Code – Basics
- Comments
e.g. /* ----anything ---*/
- Every statement end with a semicolon (;)
e.g. printf(“Hello World”);
- Block Diagram
main()
{
…..
Statements;
……
}
- C – main function
- main()
- int main()
- void main()
- main(void)
- void main(void)
- int main(void)
- void means function does not return any value
- #define is a pre-processor compiler directive and not a statement
#define should not end with semicolon
placed at the beginning before main() function
- Two or more values declared using comma
e.g. int a, b, c;
-
#include – directive to import the necessary header files (library functions)
#include <stdio.h>
- Variables that are used in more than one function, are declared outside of all functions known as Global Declaration Section and the variables known as global variables.
- main() function contains 1. Declaration part 2. Executable part
- Sub program section contains user defined functions
- C – free form language – i.e., compiler does not care, where on the line we begin typing.
- In C, lowercase letters are significant
- main() is the function where the program begins its execution
-
File extensions :
*.c – C – Program Source code
*.o – Object Code (Unix), *.obj – Object Code (Windows)
*.out – Executable (Unix), *.exe – Executable (Windows)
- Trigraph Characters
Many non- English keyboards do not support all the special characters mentioned in C Character set, C introduces the concept of “trigraph” sequences to provide a way to enter certain characters that are not available on some keyboards. Each trigraph sequence consists of three characters (tow question marks followed by another character).
e..g.
??= |
# |
??( |
[ |
??) |
] |
??< |
{ |
??> |
} |
??! |
| |
??- |
~ |
- In a C program the smallest individual units are known as C tokens.
- 6 type of tokens
- Keywords (float, while)
- Identifiers (main, total)
- Constants (3.14, 100)
- Strings (“abc”, “test”)
- Operators (+-*)
- Special Symbols ([]{})
- C Keywords
C89 has 32 keywords (reserved words with special meaning):
auto
break
case
char
const
continue
default
do
double
else
enum
extern
float
for
goto
if
int
long
register
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
while
C99 adds five more keywords:
_Bool
_Complex
_Imaginary
inline
restrict
C11 adds seven more keywords:[16]
_Alignas
_Alignof
_Atomic
_Generic
_Noreturn
_Static_assert
_Thread_local
- C11 – Latest revision of C published on December 8, 2011
Informally called as C1X
It includes type generic macros, anonymous structures, improved Unicode support, atomic operations, multi-threading, and bounds-checked functions. It also makes some portions of the existing C99 library optional, and improves compatibility with C++.
- Rules for Identifiers
- First character must be an alphabet (or underscore)
- Must consist of only letters, digits or underscore
- Only first 31 characters are significant
- Cannot use a keyword
- Must not contain white space