C Chapter 01
C Chapter 01
Oguzhan KARAHAN
Software Systems Lab
1
Books
10 Best C Books (Recommended by the Programming Community)
Worth consulting (in the library):
C Programming Absolute Beginner's Guide 3rd Edition by Greg Perry
(Author)
The C Programming Language, (2nd Edition), Kernighan & Ritchie, 1988
Learn C the Hard Way: Practical Exercises on the Computational Subjects
You Keep Avoiding (Like C) (Zed Shaw's Hard Way Series) 1st Edition by
Zed A. Shaw (Author)
C Puzzles
Highly recommended is that you try to solve C puzzles, which anybody
can get by doing a simple Google search.
https://www.sanfoundry.com/c-puzzles-external-variables/
As you learn C, you will recognize its
many virtues
4
C: History
Developed in the 1970s – in conjunction with
development of UNIX operating system
When writing an OS kernel, efficiency is crucial
This requires low-level access to the underlying hardware:
e.g. programmer can leverage knowledge of how data is laid out in
memory, to enable faster data access
UNIX originally written in low-level assembly language – but
there were problems:
No structured programming (e.g. encapsulating routines as
“functions”, “methods”, etc.) – code hard to maintain
Code worked only for particular hardware – not portable
5
C: Characteristics
C takes a middle path between low-level assembly
language…
Direct access to memory layout through pointer manipulation
Concise syntax, small set of keywords
… and a high-level programming language like Java:
Block structure
Some encapsulation of code, via functions
Type checking (pretty weak)
6
Many software houses use C as the preferred language for
producing word processing programs, spreadsheets,
compilers, and other products.
C produces compact and efficient programs.
These programs will be easy to modify and easy to adapt to new models of computers
7
ABOUT “C”
C programming language - Structured and disciplined approach to
program design.
Libraries
Linker
Executable code
a.out
10
The preprocessor
The preprocessor takes your source code and – following
certain directives that you give it – tweaks it in various ways
before compilation.
A directive is given as a line of source code starting with the #
symbol
The preprocessor works in a very crude, “word-processor”
way, simply cutting and pasting –
it doesn’t really know anything about C!
Preprocessor Compiler
11
In short, an object file and an executable file both consist of machine language
instructions.
The object file contains the machine language translation only for the code you
used,
The executable file also has machine code for the library routines you use and
for the startup
code.
12
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
Turbo (obsolete and
not recommended)
Anatomy of a C program
14
Use of comments
/*
** This program reads input lines from the standard input and prints
** each input line, followed by just some portions of the lines, to
** the standard output.
**
** The first input is a list of column numbers, which ends with a
** negative number. The column numbers are paired and specify
** ranges of columns from the input line that are to be printed.
** For example, 0 3 10 12 -1 indicates that only columns 0 through 3
** and columns 10 through 12 will be printed.
*/
15
Comments on comments
Can’t nest comments within comments
/* is matched with the very next */ that comes along
Don’t use /* … */ to comment out code – it won’t work
if the commented-out code contains comments
16
Preprocessor directives
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
17
Preprocessor directives
#define MAX_COLS 20
#define MAX_INPUT 1000
18
The seven steps of programming
19
A Simple C Program
#include <stdio.h> //This is preprosessor 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
int main ( )
{
statement(s)
return 0 ;
}
The main() function
The main () function should be present in all C programs as your program won’t
begin without this function.
Return type of main () function: The return type for main () function should
always be int.