Professional Documents
Culture Documents
DAY 1
SYLLABUS
7
OUTLINE
Program Basics
A. PROGRAM SKELETON D. Variable declarations F. Formatted input/output
PREPROCESSOR DIRECTIVES 1. Memory allocation 1. Files
GLOBAL DECLARATIONS 2. printf (monitor output)
2. Atomic types
FUNCTIONS a. format strings
LOCAL DECLARATIONS void, int, float, char field specifications
STATEMENTS E. Constants b. data list
B. COMMENTS AND 3. scanf (keyboard input)
1. literal
DOCUMENTATION a. format strings
C. NAMES (IDENTIFIERS) 2. defined b. address list
int main () {
}
int y; Local Declaration
Function printf("Enter x and y: ");
scanf(&x,&y); Statements
printf("Sum is %d\n",x+y);
}
PREPROCESS #include <stdio.h> Preprocessor Directive
Statements
}
#include <stdio.h> Preprocessor Directive
FUNCTIONS int x; Global Declaration
int main () {
• CONSISTS OF HEADER AND BODY int y; Local Declaration
• HEADER: INT MAIN () Function printf("Enter x and y: ");
• BODY: CONTAINED BETWEEN { AND } scanf(&x,&y); Statements
• STARTS WITH LOCATION printf("Sum is %d\n",x+y);
}
DECLARATIONS
• FOLLOWED BY SERIES OF STATEMENTS
Preprocessor Directives
• MORE THAN ONE FUNCTION MAY BE
DEFINED Global Declarations
int main () {
• FUNCTIONS ARE CALLED (INVOKED) Local Declarations
Statements
}
main FUNCTION
+) 32 keywords in C.
+) used to define control structures, data types, function
declarations, and other fundamental elements of C
programming
+ Variable’s name cannot duplicate a reserved word
PROGRAM EXECUTION
• DERIVED TYPES
• COMPOSED OF OTHER TYPES
Size Format
Data Type Range
(bytes) Specifier
short int 2 -32,768 to 32,767 %hd
unsigned short int
unsigned int
2
4
0 to 65,535
0 to 4,294,967,295
%hu
%u
TYPE
• SET OF POSSIBLE VALUES
int 4 -2,147,483,648 to 2,147,483,647 %d
• DEFINES SIZE, HOW VALUES
long int 4 -2,147,483,648 to 2,147,483,647 %ld STORED, INTERPRETED
• NO STRING TYPE:
• UNLIKE MANY OTHER PROGRAMMING LANGUAGES, C DOES NOT HAVE A STRING TYPE TO CREATE
STRING VARIABLES;
• MUST USE THE CHAR TYPE AND CREATE AN ARRAY OF CHARACTERS TO MAKE
#include A STRING IN C:
<stdio.h>
• CONTAINED BETWEEN DOUBLE QUOTE CHARS (“)
int main() {
• EXAMPLES:
char greetings[ ] = "Hello World!";
“” - NULL STRING
“A STRING” printf("%s", greetings);
“STRING WITH NEWLINE \N CHAR IN IT”
return 0;
“STRING WITH A DOUBLE QUOTE \” IN IT” }
LITERAL CONSTANTS
PRINTF(“%C %D %F\N”,’A’,35,4.5);
PRODUCES
A 35 4.50000
(VARIES ON DIFFERENT COMPUTERS)
MORE LATER!
FORMATTED INPUT
SCANF(“%D%C %F”,&X,&C,&Y);
AND FOLLOWING IS TYPED IN:
-543A 4.056 56
40