100% found this document useful (1 vote)
38 views

C Chapter 01

Uploaded by

dogkaaan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
38 views

C Chapter 01

Uploaded by

dogkaaan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Introduction to C

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.

o C is a structured programming language


o C supports functions that enables easy maintainability of code, by breaking
large file into smaller modules
o Comments in C provides easy readability
o C is a powerful language.
o C programs built from
• Variable and type declarations
• Functions
• Statements
• Expressions
Structure Of “C” Programs
1. C's Character Set
2. C's Keywords
3. The General Structure of a 'C' Program
4. How To End A Statement
5. Free Format Language
6. Header Files & Library Functions
Separate compilation
 A C program consists of source code in one or more files
 Each source file is run through the preprocessor and compiler,
resulting in a file containing object code
 Object files are tied together by the linker to form a single
executable program
Source code Preprocessor/ Object code
file1.c Compiler file1.o

Source code Preprocessor/ Object code


file2.c Compiler file2.o

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!

Your Enhanced and


source obfuscated Object
code source code code

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.
*/

 Only /* … */ for comments – no // like Java or C++

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

/* Comment out the following code


int f(int x) {
Only this will be
return x+42; /* return the result */ commented out
}
*/ This will not!
 Anyway, commenting out code is confusing, and
dangerous (easy to forget about) – avoid it

16
Preprocessor directives
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

 The #include directives “paste” the contents of the files


stdio.h, stdlib.h and string.h into your source code, at
the very place where the directives appear.
 These files contain information about some library
functions used in the program:
 stdio stands for “standard I/O”, stdlib stands for “standard
library”, and string.h includes useful string manipulation
functions.

17
Preprocessor directives
#define MAX_COLS 20
#define MAX_INPUT 1000

 The #define directives perform


“global replacements”:
 every instance of MAX_COLS is replaced with 20, and every
instance of MAX_INPUT is replaced with 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

preprocessor directives (if any)

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.

Int main( void )


{


}

Return type of main () function: The return type for main () function should
always be int.

Why it has a return type and what’s the need of it?


• The compiler should know whether your program compiled successfully
or it has failed.
• In order to know this, it checks the return value of function main ().
• If return value is 0 then it means that the program is successful
• Otherwise it assumes that there is a problem
• 22This is why we have a return 0 statement at the end of the main function.
A C Program
This program prompts the user for two integer values then
displays their product.
#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 = %d\n”, product) ;
return 0 ;
}
Question?
or
END

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy