Introduction to C Workshop India Wilson Wingston Sharon wingston.sharon@gmail.com
Writing the first program #include<stdio.h> int main() { printf(“Hello”); return 0; }  This program prints Hello on the screen when we execute it
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
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
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
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.
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
Operators  Arithmetic (+,-,*,/,%)  Relational (<,>,<=,>=,==,!=)  Logical (&&,||,!)  Bitwise (&,|)  Assignment (=)  Compound assignment(+=,*=,-=,/=,%=,&=,|=)  Shift (right shift >>, left shift <<)
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.
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
Task01- I/O Write a program to accept 3 numbers a , b and c as coefficients of the quadratic equation ax2 + bx + c = 0 And output the solution. Hint: 6.There will be two answers for every input combination. 7.Look in a math textbook for the solution formula.
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”); }
While loop  The syntax for while loop while(condn) { statements; } Eg: a=10; while(a != 0) Output: 10987654321 { printf(“%d”,a); a--; }
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)
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.
Conditional statements if (condition) { stmt 1; //Executes if Condition is true } else { stmt 2; //Executes if condition is false }
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; }
Functions and Parameters  Syntax of function Declaration section <<Returntype>> funname(parameter list); Definition section <<Returntype>> funname(parameter list) { body of the function } Function Call Funname(parameter);
Example function #include<stdio.h> void fun(int a); //declaration int main() { fun(10); //Call } void fun(int x) //definition { printf(“%d”,x); }
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/
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
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]
Task04 - Arrays Produce a spiral array. A spiral array is a square arrangement of the first N2 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

Introduction to Basic C programming 01

  • 1.
    Introduction to C Workshop India Wilson Wingston Sharon wingston.sharon@gmail.com
  • 2.
    Writing the firstprogram #include<stdio.h> int main() { printf(“Hello”); return 0; }  This program prints Hello on the screen when we execute it
  • 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.
    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.
    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.
    Data types inC  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.
    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.
    Operators  Arithmetic (+,-,*,/,%)  Relational (<,>,<=,>=,==,!=)  Logical (&&,||,!)  Bitwise (&,|)  Assignment (=)  Compound assignment(+=,*=,-=,/=,%=,&=,|=)  Shift (right shift >>, left shift <<)
  • 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.
    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.
    Task01- I/O Write aprogram to accept 3 numbers a , b and c as coefficients of the quadratic equation ax2 + bx + c = 0 And 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.
    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.
    While loop  The syntax for while loop while(condn) { statements; } Eg: a=10; while(a != 0) Output: 10987654321 { printf(“%d”,a); a--; }
  • 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.
    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.
    Conditional statements if (condition) { stmt 1; //Executes if Condition is true } else { stmt 2; //Executes if condition is false }
  • 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; }
  • 18.
    Functions and Parameters Syntax of function Declaration section <<Returntype>> funname(parameter list); Definition section <<Returntype>> funname(parameter list) { body of the function } Function Call Funname(parameter);
  • 19.
    Example function #include<stdio.h> void fun(inta); //declaration int main() { fun(10); //Call } void fun(int x) //definition { printf(“%d”,x); }
  • 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.
    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.
    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.
    Task04 - Arrays Producea spiral array. A spiral array is a square arrangement of the first N2 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

Editor's Notes

  • #8 We can also declare and define a variable in single shot like this. int a=10;
  • #11 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
  • #15 While – Entry controlled loop Do While – Exit controlled loop