Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Programming Language (C) : Nalini Vasudevan Columbia University

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

ProgrammingLanguage

(C)
NaliniVasudevan
ColumbiaUniversity

WhyCProgramming?

WhyCProgramming?
Provideslowlevelaccesstomemory
Provideslanguageconstructsthatmap
efficientlytomachineinstructions

Aboutthecourse
Instructor: NaliniVasudevan
Email: naliniv@cs.columbia.edu
OfficeHours:

467CSB(OpenDoor)

CourseTime: Wednesdays,11amto1pm
CourseLocation:

1127Mudd

Credits:1

CourseDuration:

9/9/09to10/14/09

CourseWebsite:

http://www.cs.columbia.edu/~naliniv/2009fall3101.php

Grading

Homeworks:60%

Project:40%

ClassParticipation:Extracredit(Not
applicableforCVNstudents)
NoExam

Textbook

Miscellaneous
Questionnaire
Notes

Let'saddtwonumbers

Addingtwonumbers

x=5;
y=10;

Addingtwonumbers

x=5;
y=10;
z=x+y;

Addingtwonumbers

intx,y,z;
x=5;
y=10;
z=x+y;

Addingtwonumbers

intx,y,z;
x=5;
y=10;
z=x+y;
printf("Thesumis%d",z);

Addingtwonumbers

main()
{
intx,y,z;
x=5;
y=10;
z=x+y;
printf("Thesumis%d",z);
}

Addingtwonumbers
#include<stdio.h>
main()
{
intx,y,z;
x=5;
y=10;
z=x+y;
printf("Thesumis%d",z);
}

Addingtwonumbers
#include<stdio.h>/*Headerfile*/
main()/*Themainfunction*/
{
intx,y,z; /*Variable
Declaration*/
x=5;
y=10;
z=x+y;
printf("Thesumis%d",z);
}

ProgramCompilationandExecution

Tocompile

gccoaddadd.c

oplacetheoutputinfileadd

addistheexecutablefile

Torun

./add

Cstatements

Examples

x=y+3;/*Assignment*/

printf(hello);/*Functioncall*/

intx;/*VariableDeclaration*/

Endwithasemicolon

Variables

Holdvalues,mustbedeclaredbeforeuse

Example

a=3+4;/*aisavariable*/

Types

inta; /*Integervalueslike1,
44,26*/

chara; /*Characterslikea,b,$,
#,\n*/

floata; /*Decimalfractionslike

0.1,2.3*/

int

4bytes(compilerdependent)

Atotalof2^32values

2^31to2^311

Variants

shortinta;/*2bytes*/

longinta; /*8bytes*/

unsignedinta; /*Onlypositive
numbers*/

0to2^321

char

Example

1byte

var='x';
Atotalof2^8values

ASCIIrepresentation

Asciivalueof'a'is97

Asciivalueof'b'is98

float

Floatingdecimalpoint

Example

floata;
a=2.54;

4bytes

IEEEformat

3.4e^38to3.4e^38

double

Twicethememoryasfloat

8bytes(generally)

sizeof
#include<stdio.h>/*Headerfile*/
main()/*Themainfunction*/
{
intx; /*VariableDeclaration*/
printf("xis%dbytes",sizeof(x));
}

Casting

Wecancastavariabletoadifferenttypethanits
actualtype

intx;
floaty;
x=3;
y=(float)x;/*Explicitcasting*/
y=x; /*Implicitcasting*/

printf

Example

printf(Thesumof%dand%dis
%d,x,y,z);

Ouput

Thesumof5and10is15

Placeholders

%d int

%f float

%c char

printf

Example

printf(Hello!);
printf(Thesumof%dand%dis
%d,x,y,z);

Ouput

Hello!Thesumof5and10is15

printf

Example

printf(Hello!\n);
printf(Thesumof%dand%dis
%d,x,y,z);

Ouput

Hello!
Thesumof5and10is15

Addingtwonumbers
#include<stdio.h>/*Headerfile*/
main()/*Themainfunction*/
{
intx,y,z; /*Variable
Declaration*/
x=5;
y=10;
z=x+y;
printf("Thesumis%d",z);
}

scanf
#include<stdio.h>/*Headerfile*/
main()/*Themainfunction*/
{
intx,y,z; /*VariableDeclaration*/
printf(Enterx:);
scanf(%d,&x); /*Waitforinput*/
printf(Entery:);
scanf(%d,&y);/*Waitforinput*/
z=x+y;
printf("Thesumis%d",z);
}

You might also like