C Programming Getting Started Getting Started Hello World Hello World Data types and variables Data types and variables main() main() I/O routines I/O.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Introduction to C Programming
Introduction to C Programming
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
C Language.
Programming Languages and Paradigms The C Programming Language.
Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2.
Lecture 2 Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
True or false A variable of type char can hold the value 301. ( F )
Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introduction to C Language
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 3 Getting Started with C++
C Tokens Identifiers Keywords Constants Operators Special symbols.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Dennis Ritchie 1972 AT & T Bell laboratories (American Telephone and Telegraph) USA 1www.gowreeswar.com.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Chapters 1-5 Review C++ Class. Chapter 1 – the big picture Objects Class Inheritance Reusability Polymorphism and Overloading.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
By Mr. Muhammad Pervez Akhtar
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
FUNCTIONS (CONT). Midterm questions (21-30) 21. The underscore can be used anywhere in an identifier. 22. The keyword void is a data type in C. 23. Floating.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
Numbers in ‘C’ Two general categories: Integers Floats
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
ECE Application Programming
Introduction to C Programming Language
Introduction to C Programming
Visit for more Learning Resources
11/10/2018.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Basics of ‘C’.
Introduction to C Programming
Govt. Polytechnic,Dhangar
Conversion Check your class notes and given examples at class.
C Programming Getting started Variables Basic C operators Conditionals
WEEK-2.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
C – Programming Language
Module 2 Variables, Data Types and Arithmetic
Programming Languages and Paradigms
EECE.2160 ECE Application Programming
Course Outcomes of Programming In C (PIC) (17212, C203):
INTRODUCTION TO C.
Presentation transcript:

C Programming Getting Started Getting Started Hello World Hello World Data types and variables Data types and variables main() main() I/O routines I/O routines Program Module Program Module Basics of C Programming Basics of C Programming Advanced C Programming Advanced C Programming

Hello World #include Header file for I/O routines int main() Main entry point for all C programs { Start of function body printf(Hello World\n); Display the message Hello World on screen printf(Hello World\n); Display the message Hello World on screen return 0; Exit code for program (0 means no error) return 0; Exit code for program (0 means no error) } End of function body Above is the first program a student will write in any programming language. It illustrates some of the basics that must be mastered to write a program. Note that each line is terminated with a semicolon (;) and that the function body is enclosed is a set of matching braces {}. These are key features of the syntax of the C language.

Data Types Simple Data Types Simple Data Types charsingle byte used for character & strings charsingle byte used for character & strings intinteger default 4 bytes intinteger default 4 bytes floatsingle precision floating point floatsingle precision floating point doubledouble precision floating point doubledouble precision floating point Complex Data Types Complex Data Types struct struct unions unions enum enum

Data Constants integer integer floating point floating point e e × e e-84.56×10 -8 character character alower case a – note use of single quote alower case a – note use of single quote char(43)plus sign + char(43)plus sign + string string this is a stringnote strings are enclosed in double quotes this is a stringnote strings are enclosed in double quotes athis is a string containing the single character a not a single character constant (see above for a character constant) athis is a string containing the single character a not a single character constant (see above for a character constant)

Variable Declaration All variables must be declared prior to their use. Variables must have a type. In C, all tokens are case sensitive id, ID and Id are three different tokens. Examples: int i, j, k; float x, y; double a, b, c;

Variable Initialization int n=10; int n=10; float pi= ; float pi= ; char oper=+; char oper=+; struct { char suit, rank; } card = {0,11}; struct { char suit, rank; } card = {0,11}; char vowels[5] = {a, e, i, o, u}; char vowels[5] = {a, e, i, o, u};

main() The function main() is the entry point for all C programs. Every program must have exactly one function called main. Typically this is the first function listed in a file. All actions performed by the program must be initiated from main or a function that main invokes. The function declaration for main should be: int main()

I/O Routines Output Output printf(fmt, val_list); printf(fmt, val_list); fprintf(FILE, fmt, val_list); fprintf(FILE, fmt, val_list); fwrite(buffer, size, count, FILE); fwrite(buffer, size, count, FILE); Input Input scanf(fmt, var_list); scanf(fmt, var_list); fscanf(FILE, fmt, var_list); fscanf(FILE, fmt, var_list); gets(); gets(); fread(buffer, size, count, FILE); fread(buffer, size, count, FILE);

C Program Module Header Files Header Files Header Files Header Files External files containing additional data types, variables and functions. Data Type Definitions Data Type Definitions Data Type Definitions Data Type Definitions Application specific data types Function Prototypes Function Prototypes Function Prototypes Function Prototypes Declaration of function type, name, list of argument name and type. Variable Declarations Variable Declarations Variable Declarations Variable Declarations Global - available externally Static - available internally Function Bodies Function Bodies Function Bodies Function Bodies Code for each declared function

Header Files Data Type Declarations Data Type Declarations Function Prototypes Function Prototypes Global Variable Reference Global Variable ReferenceExample: #include #include

Data Type Definitions Declare new data types to make your code more readable and flexible. A new data type begins with a typedef keyword then is followed by the definition. Example typedef struct { byte suit; byte rank; } cardtype; In the above we have defined a new data type called cardtype. We can use this to declare new variables of this type. Example cardtype card, deck[52];

Function Prototypes Declaration of function type, name, list of argument name and type. type fName(arg_list); arg_list: type vName, … Example void deal(cardtype *hand, int numCards);

Variable Declarations Declare both global and local variables. Example static int currCard; Local variable available only in file cardtype deck[52]; Global variable available in any file

Functions Function Declaration Function Declaration Body Body Example void deal(cardtype *hand, int NumCards) { int i; for (i=0; i < NumCards; i++) hand[i] = deck[currCard++]; } Function Declaration Function Body

Basics of C Programming C programs consists of a series of statements that perform the necessary calculations to accomplish the intended process. Statements may be grouped together inside a set of matching braces {}. Statements enclosed within these braces are called a statement block and are treated as a single statement.

Statements Assignment Assignment card = hand[topCard]; card = hand[topCard]; I/O I/O printf(Slope = %f\n, m); printf(Slope = %f\n, m); Branching Branching if (x != 0) y=5.0/x; if (x != 0) y=5.0/x; switch (oper) { switch (oper) { Loops Loops while (*x != 0) printf(%f\n, *x++); while (*x != 0) printf(%f\n, *x++); do { } while(cond); do { } while(cond);

Assignment Statements Assignment statements are of the form var = expr; Where var is the variable to be assigned the value of expr. The expression expr is a combination of variables, constants and operators.

Operators Arithmetic Arithmetic +Addition Addition Subtraction Subtraction *Multiplication 12*5 60 *Multiplication 12*5 60 /Division 12/5 2 /Division 12/5 2 %Modulo 12%5 2 %Modulo 12%5 2 =Assignment =Assignment

Increment Increment ++ Suffix ++ Suffix ++ Prefix ++ Prefix Decrement Decrement -- Suffix -- Suffix -- Prefix -- Prefix x is 4 y=++x, both x & y will be 5 Operators x is 4 y=x++, y is 4 & x is 5 x is 4 y=x--, y is 4 & x is 3 x is 4 y=--x, both x & y will be 3

Operators Bitwise Bitwise ~Ones Complement ~ ~Ones Complement ~ &And 0110& &And 0110& |Or 0110| |Or 0110| ^Exclusive Or 0110^ ^Exclusive Or 0110^ >>Shift Right 0110>> >>Shift Right 0110>> <<Shift Left 0110<< <<Shift Left 0110<<1 1100

Operators Compound Assignment Compound Assignment += a += b a=a+b += a += b a=a+b -= a -= b a=a-b -= a -= b a=a-b *= a *= b a=a*b *= a *= b a=a*b /= a /= b a=a/b /= a /= b a=a/b %= a %= b a=a%b %= a %= b a=a%b &= a &= b a=a&b &= a &= b a=a&b |= a |= b a=a|b |= a |= b a=a|b ^= a ^= b a=a^b ^= a ^= b a=a^b <<= a <<= b a=a<<b <<= a <<= b a=a<<b >>= a >>= b a=a>>b >>= a >>= b a=a>>b

Branching If Statement if (cond) stmt; else stmt; Optional else clause Example if (hand.cards[i].rank == 1) { naces++; val++; } else if (hand.cards[i].rank <= 10) { val += hand.cards[i].rank; } else { val += 10; }

Branching Switch Statement switch (expr) { case const: default: Optional default } Jumps to case where the value of expr matches const. Jumps to default if no match is found. Will execute code in subsequent case statements unless a break statement is encountered. Example switch(oper) { case -: val2 = -val2; falls thru to next case +: res = val1 + val2; break; drops out of switch case *: res = val1 * val2; break; case /: res = val1 / val2; break; default: printf(Unknown operator\n); }

Operators Relational Relational ==Equals ==Equals !=Not Equals !=Not Equals >Greater than >Greater than >=Greater than or Equal >=Greater than or Equal <Less than <Less than <=Less than or Equal <=Less than or Equal Logical Logical !Not !a returns 1 if a is 0 otherwise returns 1 !Not !a returns 1 if a is 0 otherwise returns 1 &&And a&&b returns 1 if both a and b are not 0, otherwise returns 0 &&And a&&b returns 1 if both a and b are not 0, otherwise returns 0 ||Or a||b returns 0 if both a and b are 0, otherwise returns 1 ||Or a||b returns 0 if both a and b are 0, otherwise returns 1

Pointers A pointer is a variable whose value is a memory address. It specifies where to find a memory location, but does not return the value of the memory location. A pointer may be dereferenced to obtain the value of the memory location. Example: float x, *xp; declare a floating point variable (x) and a pointer to a floating point variable (xp) x=3.0; assign the value 3 to x xp=&x; assign the address of x to xp printf(The value at %x is %f\n, output the address of x and its value xp, *xp); outputs The value at 12ff00 is

Pointers There are four main uses of pointers Passing a value to a function by reference so that the value of the variable may be changed Passing a value to a function by reference so that the value of the variable may be changed Accessing elements of an array Accessing elements of an array Dynamic execution using function pointers Dynamic execution using function pointers Dynamic allocation of variables Dynamic allocation of variables

An array is a contiguous region of memory containing equal sized blocks. Each block is called an element. Think of an array as apartment cluster mailbox. Each Mailbox in the cluster is an element. The contents of the mailbox is the value of the element. The apartment number on the mailbox is the address. Arrays are 0 based, i.e., the first element is index 0. If the array is named a then a[4] refers to the value of the 5 th element of the array (contents of the mailbox). The expression a+4 refers to the address of the 5 th element of the array (the apartment number). Arrays

Array Declarations Arrays are declared by specifying a type and a number of elements, i.e., type var[num] Examples: int order[10]; double x[100]; cardtype deck[52]; Arrays are initialize by following the declaration by a list: int order[10] = {1,2,3,4,5,6,7,8,9,10}; Arrays may also be initialized by using a for loop: double x[100]; int i; for (i=0; i < 100; i++) x[i] = 0.0;

Array Operators a[n] returns value of n+1 st element in arraya[3] 4 n is called the index *a returns value of address pointed to by a *a 1 same as a[0] a+n returns the address of the n+1 st element same as &a[n] *(a+n) returns value of n+1 st element in array *(a+2) 3 same as a[n] Array a IndexValue n-110

Structures User defined data type grouping other data types into a single data type. Members occupy contiguous memory locations. In the example below a struct is defined with 3 members: name, id and exams all grouped together as a single variable student. Example: struct { char name[80], id[10]; int exams[10]; } student; student name id exams

Unions User defined data type grouping other data types into a single data type. All members occupy the same memory location. In the example below a union is defined with 3 members: oper, var and val grouped together as a single variable token. Changing the value of any one member changes the value of all members. Example: union { char oper; int var; float val; } token; token oper var val

Member & Pointer Operators s.b returns the value of member b in struct ss.b 2 sp->c returns the value of member c in pointer sp. Same as (*sp).csp->c 3 &s returns the address of s *sp returns the value of the memory address sp typedef struct { int a, b, c; } stype; stype *sp, s = {1, 2, 3}; sp = &s;