Introduction to computer programming - C Lecture 1
Computer Components
Computer Components cont’d Processor: The processor, also called the Central Processing Unit (CPU), is the brain of the computer. It is responsible for carrying out the instructions that make up a computer program. Datapath: The datapath is the pathway that carries data between the various components of the CPU. Memory: Memory is where the computer stores data and instructions. Control: The control unit is responsible for fetching instructions from memory, decoding them, and sending out signals to other parts of the CPU to execute them. Input: Input devices such as keyboards, mice, and scanners allow users to enter data and instructions into the computer. Output: Output devices such as monitors and printers allow the computer to display information and data to the user
Programming Language
Machine Code Works at different levels for different models of cpu.
Low/High level
Source code But programming for us..humans is more about the sourcecode
What is a program A sequence of instructions that a computer can interpret and execute;  If I tell you the way from Chib Plaza to Executive Block … I will tell sequence of instructions…. Any wrong instruction leads to a undesired result. A program is something that runs on your computer. In case of MS Windows program is of .EXE or .COM extensions MS Word, Power point, Excel are all computer programs
Why we need programming language(s) Writing machine language code is very difficult if not impossible Standard manner to type instructions on computers  Why standard? << Any comments >> If there was no such standard then everyone would have to write his/her own compiler…. Or use machine language One problem with using machine language is the machine language expert of one machine cannot be an expert of other machine as both machines might have totally different architectures and calls Another use is that makers of programming language often supply us with pre-built functions that help us save time (hence money  )
Evolution of programming languages The lack of portability between different computers led to the development of high-level languages—so called because they permitted a programmer to ignore many low-level details of the computer's hardware
How people used to program Machine Language….. Damn! It was difficult Assembly Language…. Remember ADD? Required too much user involvement To much to remember Less semantic C Language B Language.. Bell Labs Improved to C Language Is a compiled language
What is C • C is a procedural programming language developed by Dennis Ritchie in 1972. • Versatile: Used in operating systems (Windows, macOS), databases (Oracle, MySQL), and software (Adobe, Python interpreter, Git). • Strengths: – Fast execution speed – Simple and flexible – Foundation for other languages (easy to learn if you know C) • Case-sensitive: Uppercase and lowercase letters are treated differently.
Features of C Language • Procedural language: Breaks code into smaller, manageable modules for better organization. • Dynamic memory allocation: Allocates memory during runtime, helpful for unknown memory requirements. • Beginner-friendly: Simple and forms the base for many other languages. • Portable: Code can run on different platforms with minimal modifications. • Rich in libraries: Provides built-in and user-defined functions for efficient coding. • Fast execution: Faster than languages like Java and Python.
Writing C Programs A programmer uses a text editor to create or modify files containing C code. Code is also known as source code. A file containing source code is called a source file. After a C source file has been created, the programmer must invoke the C compiler before the program can be executed (run).
Compiler converts human readable language to a language which is understandable by the operating system/hardware Examples of C/C++ compilers of today: Visual C++ GCC/G++ DJGPP (open source for windows like GCC) Borland C Codeblocks
Three (3) Stages of Compilation Stage 1: Preprocessing  Performed by a program called the preprocessor  Modifies the source code (in RAM) according to preprocessor directives (preprocessor commands) embedded in the source code Strips comments and white space from the code The source code as stored on disk is not modified.
3 Stages of Compilation (con’t) Stage 2: Compilation Performed by a program called the compiler Translates the preprocessor-modified source code into object code (machine code) Checks for syntax errors and warnings Saves the object code to a disk file, if instructed to do so (we will not do this).  If any compiler errors are received, no object code file will be generated.  An object code file will be generated if only warnings, not errors, are received.
Object code: It is machine language code containing various calls specific to operating system… e.g the object code written by compiler is not only hardware dependent but also operating system dependent. So if you have linux and windows both operating systems then object file compiled by one Operating System (OS) will not get executed on the other OS
3 Stages of Compilation (con’t) Stage 3: Linking o Combines the program object code with other object code to produce the executable file. o The other object code can come from the Run-Time Library, other libraries, or object files that you have created. o Saves the executable code to a disk file. On the Linux system, that file is called a.out. o If any linker errors are received, no executable file will be generated.
Program Development Using gcc Source File pgm.c Executable File a.out Preprocessor Modified Source Code in RAM Compiler Linker Program Object Code File pgm.o Other Object Code Files (if any) Editor
Compiled and Interpreted Languages
Compiled and Interpreted Languages
Language examples
C Language
C Language
Getting started We shall need Editor Compiler Linker Some of these are already provided by your operating system. Another option is to obtain an IDE. This combines the editor, compiler and linker but also provides debugging and other tools. Recommended for this course CODE BLOCKS
A Simple C Program /* I am a comment */ #include <stdio.h> //This is preprocessor directive int main ( void ) //this tells the starting point of your program { printf (“Hello World” ); //print the text on monitor return 0 ; //return to operating system }
Anatomy of a C Program program header comment preprocessor directives (if any) int main ( ) { statement(s) return 0 ; }
Program Header Comment A comment is descriptive text used to help a reader of the program understand its content. All comments must begin with the characters /* and end with the characters */ These are called comment delimiters The program header comment always comes first.
Preprocessor Directives Lines that begin with a # in column 1 are called preprocessor directives (commands). Example: the #include <stdio.h> directive causes the preprocessor to include a copy of the standard input/output header file stdio.h at this point in the code. This header file was included because it contains information about the printf ( ) function that is used in this program.
stdio.h When we write our programs, there are libraries of functions to help us so that we do not have to write the same code over and over. Some of the functions are very complex and long. Not having to write them ourselves make it easier and faster to write programs. Using the functions will also make it easier to learn to program!
int main ( void ) Every program must have a function called main. This is where program execution begins. main() is placed in the source code file as the first function for readability. The reserved word ―int‖ indicates that main() returns an integer value. The parentheses following the reserved word ―main‖ indicate that it is a function. The reserved word ―void‖ means nothing is there.
The Function Body A left brace (curly bracket) -- { -- begins the body of every function. A corresponding right brace -- } -- ends the function body. The style is to place these braces on separate lines in column 1 and to indent the entire function body 3 to 5 spaces.
Printf( “Hello, World!”) ; This line is a C statement. It is a call to an object with a single argument (parameter), Even though a string may contain many characters, the string itself should be thought of as a single quantity. Notice that this line ends with a semicolon. All statements in C end with a semicolon.
return 0 ; Because function main() returns an integer value, there must be a statement that indicates what this value is. The statement return 0 ; indicates that main() returns a value of zero to the operating system. A value of 0 indicates that the program successfully terminated execution. Do not worry about this concept now. Just remember to use the statement.
Another C Program #include <stdio.h> int main( void ) { int value1, value2, product ; printf(“Enter two integer values: “) ; scanf(“%d%d”, &value1, &value2) ; product = value1 * value2 ; printf(“Product = %dn”, product) ; return 0 ; }
Good Programming Practices C programming standards and indentation styles are available in the book You are expected to conform to these standards for all programming projects in this class The program just shown conforms to these standards, but is uncommented (later). Subsequent lectures will include more ―Good Programming Practices‖ slides.
Tokens The smallest element in the C language is the token. It may be a single character or a sequence of characters to form a single item.
Tokens are: Tokens can be: Numeric constants Character constants String constants Keywords Names (identifiers) Punctuation Operators
Numeric Constants Numeric constants are an uninterrupted sequence of digits (and may contain a period). They never contain a comma. Examples: 123 98.6 1000000
Character Constants Singular! One character defined character set. Surrounded on the single quotation mark. Examples: ‗A‘ ‗a‘ ‗$‘ ‗4‘
String Constants A sequence characters surrounded by double quotation marks. Considered a single item. Examples: ―UMBC‖ ―I like ice cream.‖ ―123‖ ―CAR‖ ―car‖
Key words
Keywords Sometimes called reserved words. Are defined as a part of the C language. Can not be used for anything else! Examples: int while for
Names Sometimes called identifiers. Can be of anything length, but on the first 31 are significant (too long is as bad as too short). Are case sensitive: abc is different from ABC Must begin with a letter and the rest can be letters, digits, and underscores.
Punctuation Semicolons, colons, commas, apostrophes, quotation marks, braces, brackets, and parentheses. ; : , ‗ ― [ ] { } ( )
Operators There are operators for:  assignments  mathematical operations  relational operations  Boolean operations  bitwise operations  shifting values  calling functions  subscripting  obtaining the size of an object  obtaining the address of an object  referencing an object through its address  choosing between alternate subexpressions

INTRO_C_LECTURE 1.pdf with introduction of code blocks

  • 1.
  • 2.
  • 3.
    Computer Components cont’d Processor:The processor, also called the Central Processing Unit (CPU), is the brain of the computer. It is responsible for carrying out the instructions that make up a computer program. Datapath: The datapath is the pathway that carries data between the various components of the CPU. Memory: Memory is where the computer stores data and instructions. Control: The control unit is responsible for fetching instructions from memory, decoding them, and sending out signals to other parts of the CPU to execute them. Input: Input devices such as keyboards, mice, and scanners allow users to enter data and instructions into the computer. Output: Output devices such as monitors and printers allow the computer to display information and data to the user
  • 4.
  • 5.
    Machine Code Works atdifferent levels for different models of cpu.
  • 6.
  • 7.
    Source code But programmingfor us..humans is more about the sourcecode
  • 8.
    What is aprogram A sequence of instructions that a computer can interpret and execute;  If I tell you the way from Chib Plaza to Executive Block … I will tell sequence of instructions…. Any wrong instruction leads to a undesired result. A program is something that runs on your computer. In case of MS Windows program is of .EXE or .COM extensions MS Word, Power point, Excel are all computer programs
  • 9.
    Why we needprogramming language(s) Writing machine language code is very difficult if not impossible Standard manner to type instructions on computers  Why standard? << Any comments >> If there was no such standard then everyone would have to write his/her own compiler…. Or use machine language One problem with using machine language is the machine language expert of one machine cannot be an expert of other machine as both machines might have totally different architectures and calls Another use is that makers of programming language often supply us with pre-built functions that help us save time (hence money  )
  • 10.
    Evolution of programminglanguages The lack of portability between different computers led to the development of high-level languages—so called because they permitted a programmer to ignore many low-level details of the computer's hardware
  • 11.
    How people usedto program Machine Language….. Damn! It was difficult Assembly Language…. Remember ADD? Required too much user involvement To much to remember Less semantic C Language B Language.. Bell Labs Improved to C Language Is a compiled language
  • 12.
    What is C •C is a procedural programming language developed by Dennis Ritchie in 1972. • Versatile: Used in operating systems (Windows, macOS), databases (Oracle, MySQL), and software (Adobe, Python interpreter, Git). • Strengths: – Fast execution speed – Simple and flexible – Foundation for other languages (easy to learn if you know C) • Case-sensitive: Uppercase and lowercase letters are treated differently.
  • 13.
    Features of CLanguage • Procedural language: Breaks code into smaller, manageable modules for better organization. • Dynamic memory allocation: Allocates memory during runtime, helpful for unknown memory requirements. • Beginner-friendly: Simple and forms the base for many other languages. • Portable: Code can run on different platforms with minimal modifications. • Rich in libraries: Provides built-in and user-defined functions for efficient coding. • Fast execution: Faster than languages like Java and Python.
  • 14.
    Writing C Programs Aprogrammer uses a text editor to create or modify files containing C code. Code is also known as source code. A file containing source code is called a source file. After a C source file has been created, the programmer must invoke the C compiler before the program can be executed (run).
  • 15.
    Compiler converts human readable languageto a language which is understandable by the operating system/hardware Examples of C/C++ compilers of today: Visual C++ GCC/G++ DJGPP (open source for windows like GCC) Borland C Codeblocks
  • 16.
    Three (3) Stagesof Compilation Stage 1: Preprocessing  Performed by a program called the preprocessor  Modifies the source code (in RAM) according to preprocessor directives (preprocessor commands) embedded in the source code Strips comments and white space from the code The source code as stored on disk is not modified.
  • 17.
    3 Stages ofCompilation (con’t) Stage 2: Compilation Performed by a program called the compiler Translates the preprocessor-modified source code into object code (machine code) Checks for syntax errors and warnings Saves the object code to a disk file, if instructed to do so (we will not do this).  If any compiler errors are received, no object code file will be generated.  An object code file will be generated if only warnings, not errors, are received.
  • 18.
    Object code: It ismachine language code containing various calls specific to operating system… e.g the object code written by compiler is not only hardware dependent but also operating system dependent. So if you have linux and windows both operating systems then object file compiled by one Operating System (OS) will not get executed on the other OS
  • 19.
    3 Stages ofCompilation (con’t) Stage 3: Linking o Combines the program object code with other object code to produce the executable file. o The other object code can come from the Run-Time Library, other libraries, or object files that you have created. o Saves the executable code to a disk file. On the Linux system, that file is called a.out. o If any linker errors are received, no executable file will be generated.
  • 20.
    Program Development Usinggcc Source File pgm.c Executable File a.out Preprocessor Modified Source Code in RAM Compiler Linker Program Object Code File pgm.o Other Object Code Files (if any) Editor
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
    Getting started We shallneed Editor Compiler Linker Some of these are already provided by your operating system. Another option is to obtain an IDE. This combines the editor, compiler and linker but also provides debugging and other tools. Recommended for this course CODE BLOCKS
  • 27.
    A Simple CProgram /* I am a comment */ #include <stdio.h> //This is preprocessor directive int main ( void ) //this tells the starting point of your program { printf (“Hello World” ); //print the text on monitor return 0 ; //return to operating system }
  • 28.
    Anatomy of aC Program program header comment preprocessor directives (if any) int main ( ) { statement(s) return 0 ; }
  • 29.
    Program Header Comment Acomment is descriptive text used to help a reader of the program understand its content. All comments must begin with the characters /* and end with the characters */ These are called comment delimiters The program header comment always comes first.
  • 30.
    Preprocessor Directives Lines thatbegin with a # in column 1 are called preprocessor directives (commands). Example: the #include <stdio.h> directive causes the preprocessor to include a copy of the standard input/output header file stdio.h at this point in the code. This header file was included because it contains information about the printf ( ) function that is used in this program.
  • 31.
    stdio.h When we writeour programs, there are libraries of functions to help us so that we do not have to write the same code over and over. Some of the functions are very complex and long. Not having to write them ourselves make it easier and faster to write programs. Using the functions will also make it easier to learn to program!
  • 32.
    int main (void ) Every program must have a function called main. This is where program execution begins. main() is placed in the source code file as the first function for readability. The reserved word ―int‖ indicates that main() returns an integer value. The parentheses following the reserved word ―main‖ indicate that it is a function. The reserved word ―void‖ means nothing is there.
  • 33.
    The Function Body Aleft brace (curly bracket) -- { -- begins the body of every function. A corresponding right brace -- } -- ends the function body. The style is to place these braces on separate lines in column 1 and to indent the entire function body 3 to 5 spaces.
  • 34.
    Printf( “Hello, World!”); This line is a C statement. It is a call to an object with a single argument (parameter), Even though a string may contain many characters, the string itself should be thought of as a single quantity. Notice that this line ends with a semicolon. All statements in C end with a semicolon.
  • 35.
    return 0 ; Becausefunction main() returns an integer value, there must be a statement that indicates what this value is. The statement return 0 ; indicates that main() returns a value of zero to the operating system. A value of 0 indicates that the program successfully terminated execution. Do not worry about this concept now. Just remember to use the statement.
  • 36.
    Another C Program #include<stdio.h> int main( void ) { int value1, value2, product ; printf(“Enter two integer values: “) ; scanf(“%d%d”, &value1, &value2) ; product = value1 * value2 ; printf(“Product = %dn”, product) ; return 0 ; }
  • 37.
    Good Programming Practices Cprogramming standards and indentation styles are available in the book You are expected to conform to these standards for all programming projects in this class The program just shown conforms to these standards, but is uncommented (later). Subsequent lectures will include more ―Good Programming Practices‖ slides.
  • 38.
    Tokens The smallest elementin the C language is the token. It may be a single character or a sequence of characters to form a single item.
  • 39.
    Tokens are: Tokens canbe: Numeric constants Character constants String constants Keywords Names (identifiers) Punctuation Operators
  • 40.
    Numeric Constants Numeric constantsare an uninterrupted sequence of digits (and may contain a period). They never contain a comma. Examples: 123 98.6 1000000
  • 41.
    Character Constants Singular! One characterdefined character set. Surrounded on the single quotation mark. Examples: ‗A‘ ‗a‘ ‗$‘ ‗4‘
  • 42.
    String Constants A sequencecharacters surrounded by double quotation marks. Considered a single item. Examples: ―UMBC‖ ―I like ice cream.‖ ―123‖ ―CAR‖ ―car‖
  • 43.
  • 44.
    Keywords Sometimes called reservedwords. Are defined as a part of the C language. Can not be used for anything else! Examples: int while for
  • 45.
    Names Sometimes called identifiers. Canbe of anything length, but on the first 31 are significant (too long is as bad as too short). Are case sensitive: abc is different from ABC Must begin with a letter and the rest can be letters, digits, and underscores.
  • 46.
    Punctuation Semicolons, colons, commas,apostrophes, quotation marks, braces, brackets, and parentheses. ; : , ‗ ― [ ] { } ( )
  • 47.
    Operators There are operatorsfor:  assignments  mathematical operations  relational operations  Boolean operations  bitwise operations  shifting values  calling functions  subscripting  obtaining the size of an object  obtaining the address of an object  referencing an object through its address  choosing between alternate subexpressions