introduction to c language keerthi nelaturu [email protected] knela006

17
Introduction to C Language Keerthi Nelaturu [email protected] www.site.uottawa.ca/~knel a006/

Upload: blaze-chapman

Post on 23-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to C Language Keerthi Nelaturu knela006@uottawa.ca knela006

Introduction to C Language

Keerthi [email protected]

www.site.uottawa.ca/~knela006/

Page 2: Introduction to C Language Keerthi Nelaturu knela006@uottawa.ca knela006

Quick inside into the language

• Closely associated with UNIX system• System programming language• Stemmed up from Language BCPL• Supports data types, functions, control flow

statements etc.

Page 3: Introduction to C Language Keerthi Nelaturu knela006@uottawa.ca knela006

First C Program

Print “Hello, world”

#include <stdio.h>main(){printf(“hello, world \n”);}

Page 4: Introduction to C Language Keerthi Nelaturu knela006@uottawa.ca knela006

More escape sequences

• \n – Newline character• \t – Tab• \b – Backspace• \” – Double quote• \\ - Backslash

Page 5: Introduction to C Language Keerthi Nelaturu knela006@uottawa.ca knela006

Comments in C

• They start with “/*” and end with “*/”.• Ignored by the compiler• C compilers do not care about how a program

looks, indentation and spacing.• Documentation of your program is always

important! Makes it easy for people to read through.

Page 6: Introduction to C Language Keerthi Nelaturu knela006@uottawa.ca knela006

Variables in C

• Must be declared before they are used• C language is Case Sensitive• Cannot use keywords for variable names• Declaration of variable consists of :– Type name – List of variable names– Terminator (;)

Ex: int fahr, celsius; float lower, upper, step;

Page 7: Introduction to C Language Keerthi Nelaturu knela006@uottawa.ca knela006

List of Keywords

Page 8: Introduction to C Language Keerthi Nelaturu knela006@uottawa.ca knela006

Basic data types in C

• int• float• char• short• long • double

Page 9: Introduction to C Language Keerthi Nelaturu knela006@uottawa.ca knela006

Compiling and Running a C program

In Unix, Command to compile: gcc hello.c

Running a executable file./a.out

Page 10: Introduction to C Language Keerthi Nelaturu knela006@uottawa.ca knela006

Input/Output

Printf is the basic output function we use for displaying content in CSyntax: printf(“%d %6.2f”, intvar1, floatvar2);

Scanf is the function we use to get user input.Syntax: scanf(“%d%f”,&test1,&test2);

Page 11: Introduction to C Language Keerthi Nelaturu knela006@uottawa.ca knela006

Format Specifier

Page 12: Introduction to C Language Keerthi Nelaturu knela006@uottawa.ca knela006

Operators• Arithmetic Operator : +, -, *, /, %• Assignment Operators: +=, -= etc• Relational and Logical Operators: >, >=, <, <=, ==, !=, &&, ||

Always check for the order of precedence when evaluating an expression with operators in it.

• Type conversions: From narrower to wider. int to float – Correctfloat to int – wrong

Check the texbook for table on precedence of all operators.

Page 13: Introduction to C Language Keerthi Nelaturu knela006@uottawa.ca knela006

Increment / Decrement Operators

• Increment : ++ (post & pre)• Decrement: -- (post & pre)

Ex: n++ - will increments n after the value is used++n – will increment n before the value is used

Increment and Decrement operators can only be applied to variables not expressions

Page 14: Introduction to C Language Keerthi Nelaturu knela006@uottawa.ca knela006

Bitwise Operators

Six operators for bit manipulation in C

& - bitwise AND| - bitwise inclusive OR^ - bitwise exclusive OR<< - left shift >> - right shift~ - one’s complement (unary)

Conditional Expression: exp1 ? Exp2 : exp3;

Page 15: Introduction to C Language Keerthi Nelaturu knela006@uottawa.ca knela006

Constants

#define MAXLINE 1000

char esc = ‘\\`;int i = 0;int limit = MAXLINE +1;float aFloat = 1.0e-5;const double e = 2.7856;

Page 16: Introduction to C Language Keerthi Nelaturu knela006@uottawa.ca knela006

Control Flow

Else ifIf-ElseSwitch

switch (expression)

{case const-expr: statement break;

default: statements}

Page 17: Introduction to C Language Keerthi Nelaturu knela006@uottawa.ca knela006

Control Flow (cont’d)

do-whilewhileFor

• Goto! for ( …) {

if (disaster) goto error;

} error: printf(“bad programming!”);