Vous êtes sur la page 1sur 17

Module: C programming

AITI-KACE All Rights Reserved

Module Objectives:

Learn how to write basic programs using the C programming language:


Learn variable definition and value assignment Implement various programming constructs in C Implement modular solutions using C Understand the implementation and processing of Array in C Accept inputs from the terminal and Files Display output to the terminal and Files
AITI-KACE All Rights Reserved

Literature

Let Us C Yashwant Kanetkar Programming with C Schaums Outlines Practical C Programming Steve Oualline C by Example by Greg Perry Assessment

Theory Exam 30% Practical Exams 30% Assignment 20% Class Test 15% Attendance 5%
AITI-KACE All Rights Reserved

Part 1 Introduction to C

AITI-KACE All Rights Reserved

Section Objectives

Give brief Introduction to C Discuss C program structure How to execute a C program Learn about variables and constants in C Learn about Character Array and String Get to know what preprocessor directives are Learn about simple input and ouput in C

AITI-KACE All Rights Reserved

History of C

Evolved by Ritchie from two previous programming languages, BCPL and B Used to develop UNIX Used to write modern operating systems

Standardization

Many slight variations of C existed, and were incompatible Committee formed to create a "unambiguous, machine-independent" definition Standard created in 1989, updated in 1999
AITI-KACE All Rights Reserved

Why C?

AITI-KACE All Rights Reserved

The C Standard Library

C programs consist of pieces/modules called functions

A programmer can create his own functions


Advantage: the programmer knows exactly how it works Disadvantage: time consuming

Programmers will often use the C library functions

Use these as building blocks

Avoid re-inventing the wheel

If a built-in function exists, generally make best use it rather than write your own Library functions carefully written, efficient, and portable
AITI-KACE All Rights Reserved

Steps in writing a C program

(With a Compiler)

(with an editor)

AITI-KACE All Rights Reserved

C program design

Define problem to be solved by the computer Define program's output Break problem into logical steps Write the program (using an editor)

Any editor can be used gedit, jedit, etc... Your C program must be saved with extension

{.c}

firstprogram.c

AITI-KACE All Rights Reserved

10

How it works!
Write / Edit Program Source Code

Compile Object Code Library Files Link Object Code

Executable

AITI-KACE All Rights Reserved

11

How to compile a C program

For the sake of simplicity


Go to the bin directory inside your home directory Create a folder call c_programs

We have different types of C compilers


On MS Windows Turbo C, QuickC, etc... On Linux/Unix gcc, g++, etc...

To compile a c program: use the C compiler and provide a file which end with extension (.c)
AITI-KACE All Rights Reserved

12

Compiling a C program example

C program with file Name first.c

C compiler on Linux

AITI-KACE All Rights Reserved

13

Anatomy of C Program

AITI-KACE All Rights Reserved

14

First C Program Example 1


1 /* Fig. 2.1: fig02_01.c 2
3 4 5 6 7 8 9 10 }

A first program in C */ #include <stdio.h> int main() { printf( "Welcome to C!\n" ); return 0;

int main() C program contain one or more functions, exactly one of which must be main

Parenthesis used to indicate a function ( ) int means that main "returns" an integer value Braces ({ and }) indicate a block printf () - display function A way to exit a function return 0, in this case, means that the program terminated normally

Welcome to C!

Comment /* */ #include <stdio.h> Preprocessor directive


<stdio.h> allows standard input/output operations

AITI-KACE All Rights Reserved

15

Second C Program Example 2


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 } Enter first integer 45 Enter second integer 72 Sum is 117 return 0; /* indicate that program ended successfully */ printf( "Enter first integer\n" ); scanf( "%d", &integer1 ); scanf( "%d", &integer2 ); sum = integer1 + integer2; printf( "Sum is %d\n", sum ); /* prompt */ /* read an integer */ /* read an integer */ /* assignment of sum */ /* print sum */ int main() { int integer1, integer2, sum; /* declaration */ /* Fig. 2.5: fig02_05.c Addition program */ #include <stdio.h>

1. Initialize variables

2. Input

printf( "Enter second integer\n" ); /* prompt */

3. Sum 4. Print

Program Output

AITI-KACE All Rights Reserved

16

Simple input and output

AITI-KACE All Rights Reserved

17