The document provides an introduction to the C programming language including:
- The C development environment and how a program is compiled from source code to executable code
- A simple "Hello World" example program
- Key elements of a C program like comments, preprocessor directives, data types, and basic functions like printf()
- Details on tokens, variables, statements, and basic data types and functions in C
C programming basics, including development environment, structure, data types, functions, and examples.
Explains the phases of program creation: from code in an editor, through compilation and linking, to execution in memory.
Illustrates the transformation of source code into executable code through pre-processing, compiling, assembling, and linking.
Presents a basic C program that outputs a simple message using printf, demonstrating syntax and function usage.
Key programming rules: case sensitivity, main() function's role, statement structure, and terminating statements with semicolons.
Defines characters as building blocks of C programs and introduces tokens, such as keywords and identifiers, crucial for coding.
Distinguishes between reserved words and identifiers, explaining constants with examples of integer, floating-point, character, and enumeration constants.
Explains string literals, examples in C programming, and the role of punctuators as separators in code.
Presents operators used for computations in expressions and provides an example demonstrating their use.
Details essential components of a C program, including preprocessor directives, variable declarations, comments, and program statements.
Describes comments for documentation and the significance of preprocessor directives in programming.
Introduces basic data types (int, char, double) and explains the difference between variable declaration and definition.
Defines statements as actions in a program, highlighting their syntax and examples of practical statement use.
Describes functions, particularly main(), and introduces pre-defined standard input/output functions (printf, scanf, getchar), along with concepts of format specifiers and escape sequences.
Chapter 3: Introductionto C Programming LanguageC development environmentA simple program exampleCharacters and tokensStructure of a C programcomment and preprocessor directivesbasic data typesdata declarationsstatementsBasic functions
2.
Program iscreated intheEditor and stored on Disk. EditorPhase 1 :DiskDiskCompiler creates objectcode and stores it on Disk.CompilerPhase 3 :DiskLinker links object code with libraries, creates a.out and stores it on Disk. Linker Phase 4 :DiskC Development EnvironmentPreprocessorprogramprocesses the code. PreprocessorPhase 2 :
3.
Primary MemoryLoader Phase 5 :Loader putsProgram inMemory:.Primary MemoryC P UPhase 6 :CPU takes eachinstruction and executes it, storingnew data values asthe program executes.:.
A Simple ProgramExample#include <stdio.h>main(){ printf("Programming in C is easy.\n");}Sample Program OutputProgramming in C is easy.
6.
NOTE ABOUT CPROGRAMSIn C, lowercase and uppercase characters are very important! All commands in C must be lowercase. The C programs starting point is identified by the word main() This informs the computer as to where the program actually starts. The brackets that follow the keyword main indicate that there are no arguments supplied to this program (this will be examined later on). The two braces, { and }, signify the begin and end segments of the program.
7.
The purpose ofthe statement #include <stdio.h> is to allow the use of the printf statement to provide program output. Text to be displayed by printf() must be enclosed in double quotes. The program has only one statement printf("Programming in C is easy.\n"); printf() is actually a function (procedure) in C that is used for printing variables and text. Where text appears in double quotes "", it is printed without modification. There are some exceptions however.
8.
This has todo with the \ and % characters. These characters are modifiers, and for the present the \ followed by the n character represents a newline character. Thus the program printsProgramming in C is easy.and the cursor is set to the beginning of the next line. As we shall see later on, what follows the \ character will determine what is printed, ie, a tab, clear screen, clear line etc. Another important thing to remember is that all C statements are terminated by a semi-colon ;
text strings areenclosed in double quotesCharacters and tokensCharacters are the basic building blocks in C program, equivalent to ‘letters’ in English languageIncludes every printable character on the standard English language keyboard except `, $ and @Example of characters:Numeric digits: 0 - 9Lowercase/uppercase letters: a - z and A - ZSpace (blank)Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < > etc
14.
A token isa language element that can be used in forming higher level language constructsEquivalent to a ‘word’ in English languageSeveral types of tokens can be used to build a higher level C language construct such as expressions and statementsThere are 6 kinds of tokens in C:Reserved words (keywords)IdentifiersConstantsString literalsPunctuatorsOperators
15.
Reserved WordsKeywords thatidentify language entities such as statements, data types, language attributes, etc.Have special meaning to the compiler, cannot be used as identifiers in our program.Should be typed in lowercase.Example: const, double, int, main, void, while, for, else (etc..)
16.
IdentifiersWords used torepresent certain program entities (program variables, function names, etc).Example:int my_name; my_name is an identifier used as a program variablevoid CalculateTotal(int value)CalculateTotal is an identifier used as a function name
17.
ConstantsEntities that appearin the program code as fixed values.4 types of constants:Integer constantsPositive or negative whole numbers with no fractional partExample: const int MAX_NUM = 10;const int MIN_NUM = -90;Floating-point constantsPositive or negative decimal numbers with an integer part, a decimal point and a fractional partExample:const double VAL = 0.5877e2; (stands for 0.5877 x 102)
18.
Character constantsA characterenclosed in a single quotation markExample:const char letter = ‘n’;const char number = ‘1’;printf(“%c”, ‘S’);Output would be: SEnumerationValues are given as a listExample:
19.
String LiteralsA sequenceof any number of characters surrounded by double quotation marks. Example:“REFORMASI”“My name is Salman”Example of usage in C program:printf(“My room number is BN-1-012\n”);Output: My room number is BN-1-012
OperatorsTokens that resultin some kind of computation or action when applied to variables or or other elements in an expression.Example of operators:* + = - /Usage example:result = total1 + total2;
22.
Structure of aC programPreprocessor directive (header file)Program statement}Preprocessor directiveGlobal variable declarationCommentsLocal variable declarationVariable definition
23.
CommentsExplanations or annotationsthat are included in a program for documentation and clarification purpose.Completely ignored by the compiler during compilation and have no effect on program execution.Starts with ‘/*’ and ends with ‘*/’Some compiler support comments starting with ‘//’
24.
Preprocessor DirectivesThe firstthing to be checked by the compiler.Starts with ‘#’.Tell the compiler about specific options that it needs to be aware of during compilation.There are a few compiler directives. But only 2 of them will be discussed here.#include <stdio.h>Tell the compiler to include the file stdio.h during compilationAnything in the header file is considered a part of the program#define VALUE 10Tell the compiler to substitute the word VALUE with 10 during compilation
25.
Basic Data Types3examples of basic data types:int (used to declare numeric program variables of integer type)char (used to declare character variable)double (used to declare floating point variable)In addition, there are float, void, short, long, etc.Declaration: specifies the type of a variable.Example: int local_var;Definition: assigning a value to the declared variable.Example: local_var = 5;
26.
A variable canbe declared globally or locally.A globally declared variable can be accessed from all parts of the program.A locally declared variable can only be accessed from inside the function in which the variable is declared.
27.
StatementsA specification ofan action to be taken by the computer as the program executes.In the previous example, there are 2 lines following variable declaration and variable definition that terminate with semicolon ‘;’. global_var = local_var + VALUE;printf (“Total sum is: %d\n”, global_var);Each line is a statement.
28.
Basic FunctionsA Cprogram consists of one or more functions that contain a group of statements which perform a specific task.A C program must at least have one function: the function main.We can create our own function or use the functions that has been created in the library, in which case we have to include the appropriate header file (example: stdio.h).
29.
In this section,we will learn a few functions that are pre-defined in the header file stdio.hThese functions are:printf()scanf()getchar() & putchar() In addition to those functions, we will also learn about Format Specifier and Escape Sequence which are used with printf() and scanf().
30.
printf()Used to senddata to the standard output (usually the monitor) to be printed according to specific format.General format:printf(“control string”, variables);Control string is a combination of text, format specifier and escape sequence.Example:printf(“Thank you”);printf (“Total sum is: %d\n”, global_var);%d is a format specifier\n is an escape sequence
scanf()Read data fromthe standard input device (usually keyboard) and store it in a variable.General format:scanf(“Control string”, &variable);The general format is pretty much the same as printf() except that it passes the address of the variable (notice the & sign) instead of the variable itself to the second function argument.Example:
34.
getchar() and putchar()getchar()- read a character from standard inputputchar() - write a character to standard outputExample:#include <stdio.h>void main(void){ char my_char; printf(“Please type a character: “); my_char = getchar(); printf(“\nYou have typed this character: “); putchar(my_char);}