Introduction of C

Introduction of C

Project Type: Presentation (ppt)

Downloads: 10 - 50 Saturday 18th March 2017 Report

Introduction of C - Overview

------------ PAGE 1 ------------ Introduction to C ------------ PAGE 2 ------------ Writing the first program # include< stdio. h> int main() { printf(“ Hello”); return 0; } „Y This program prints Hello on the screen when we execute it ------------ PAGE 3 ------------ Header files „Y The files that are specified in the include section is called as header file „Y These are precompiled files that has some functions defined in them „Y We can call those functions in our program by supplying parameters „Y Header file is given an extension . h „Y C Source file is given an extension . c ------------ PAGE 4 ------------ Main function „Y This is the entry point of a program „Y When a file is executed, the start point is the main function „Y From main function the flow goes as per the programmers choice. „Y There may or may not be other functions written by user in a program „Y Main function is compulsory for any c program ------------ PAGE 5 ------------ Comments in C „Y Single line comment „U // ( double slash) „U Termination of comment is by pressing enter key „Y Multi line comment /*…. …….*/ This can span over to multiple lines ------------ PAGE 6 ------------ Data types in C „Y Primitive data types „U int, float, double, char „Y Aggregate data types „U Arrays come under this category „U Arrays can contain collection of int or float or char or double data „Y User defined data types „U Structures and enum fall under this category. ------------ PAGE 7 ------------ Variables „Y Variables are data that will keep on changing „Y Declaration << Data type>> << variable name>>; int a; „Y Definition << varname>>=<< value>>; a= 10; „Y Usage << varname>> a= a 1; // increments the value of a by 1 ------------ PAGE 8 ------------ Operators „Y Arithmetic ( ,-,*,/,%) „Y Relational (<,>,<=,>=,==,!=) „Y Logical (andand,||,!) „Y Bitwise (and,|) „Y Assignment (=) „Y Compound assignment( =,*=,-=,/=,%=,and=,|=) „Y Shift ( right shift >>, left shift <<) ------------ PAGE 9 ------------ Variable names- Rules „Y Should not be a reserved word like int etc.. „Y Should start with a letter or an underscore(_) „Y Can contain letters, numbers or underscore. „Y No other special characters are allowed including space „Y Variable names are case sensitive „U A and a are different. ------------ PAGE 10 ------------ Input and Output „Y Input „U scanf(“% d”,and a); „U Gets an integer value from the user and stores it under the name “ a” „Y Output „U printf(“% d”, a) „U Prints the value present in variable a on the screen ------------ PAGE 11 ------------ Task01- I/ O Write a program to accept 3 numbers a , b and c as coefficients of the quadratic equation ax 2 bx c = 0 And output the solution. Hint: 1. There will be two answers for every input combination. 2. Look in a math textbook for the solution formula. ------------ PAGE 12 ------------ For loops „Y The syntax of for loop is for( initialisation; condition checking; increment) { set of statements } Eg: Program to print Hello 10 times for( I= 0; I< 10; I ) { printf(“ Hello”); } ------------ PAGE 13 ------------ While loop „Y The syntax for while loop while( condn) { statements; } Eg: a= 10; while( a != 0) Output: 10987654321 { printf(“% d”, a); a--; } ------------ PAGE 14 ------------ Do While loop „Y The syntax of do while loop do { set of statements } while( condn); Eg: i= 10; Output: do 10987654321 { printf(“% d”, i); i--; } while( i!= 0) ------------ PAGE 15 ------------ Task02 - loops „Y Write a program to identify which day ( sat – fri) a particular given date ( e. g. : 3/ 3/ 2015) falls upon. „Y Hint: http:// en. wikipedia. org/ wiki/ Zeller% 27s_ congruence „Y Read through the algorithm and implement it. „Y You can make your own algorithm and get bonus marks. ------------ PAGE 16 ------------ Conditional statements if ( condition) { stmt 1; // Executes if Condition is true } else { stmt 2; // Executes if condition is false } ------------ PAGE 17 ------------ Conditional statement switch( var) { case 1: // if var= 1 this case executes stmt; break; case 2: // if var= 2 this case executes stmt; break; default: // if var is something else this will execute stmt; } ------------ PAGE 18 ------------ Functions and Parameters „Y Syntax of function Declaration section << Returntype>> funname( parameter list); Definition section << Returntype>> funname( parameter list) { body of the function } Function Call Funname( parameter); ------------ PAGE 19 ------------ Example function # include< stdio. h> void fun( int a); // declaration int main() { fun( 10); // Call } void fun( int x) // definition { printf(“% d”, x); } ------------ PAGE 20 ------------ Task03 - Primality „Y Write a function to check if a given number is prime or not. „Y http:// en. wikipedia. org/ wiki/ Prime_ number „Y Try and implement at least 2 algorithms and check which one returns faster. „Y Use this for time stamping : http:// www. chemie. fu- berlin. de/ chemnet/ use/ info/ libc/ libc_ 17. html ------------ PAGE 21 ------------ Arrays „Y Arrays fall under aggregate data type „Y Aggregate – More than 1 „Y Arrays are collection of data that belong to same data type „Y Arrays are collection of homogeneous data „Y Array elements can be accessed by its position in the array called as index ------------ PAGE 22 ------------ Arrays „Y Array index starts with zero „Y The last index in an array is num – 1 where num is the no of elements in a array „Y int a[ 5] is an array that stores 5 integers „Y a[ 0] is the first element where as a[ 4] is the fifth element „Y We can also have arrays with more than one dimension „Y float a[ 5][ 5] is a two dimensional array. It can store 5x5 = 25 floating point numbers „Y The bounds are a[ 0][ 0] to a[ 4][ 4] ------------ PAGE 23 ------------ Task04 - Arrays Produce a spiral array. A spiral array is a square arrangement of the first N 2 natural numbers, where the numbers increase sequentially as you go around the edges of the array spiralling inwards. For example, given 5, produce this array: 0 1 2 3 4 15 16 17 18 5 14 23 24 19 6 13 22 21 20 7 12 11 10 9 8
Back to top of page