Introduction to Basic C programming 01

5,343 views

Published on

This presentation offers complete basic users a simple guide to the C programming Language.

Published in: Education, Technology
0 Comments
13 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,343
On SlideShare
0
From Embeds
0
Number of Embeds
36
Actions
Shares
0
Downloads
325
Comments
0
Likes
13
Embeds 0
No embeds

No notes for slide
  • We can also declare and define a variable in single shot like this. int a=10;
  • Format specifiers %d is the format specifier. This informs to the compiler that the incoming value is an integer value. Other data types can be specified as follows: %c – character %f – float %lf – double %s – character array (string) Printf and scanf are defined under the header file stdio.h
  • While – Entry controlled loop Do While – Exit controlled loop
  • Introduction to Basic C programming 01

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

    ×