6CS005Lecture1 89718
6CS005Lecture1 89718
Lecture 1
Fundamentals of C
Programming
Jnaneshwar Bohara
Topics Covered
• Introduction to C Programming
• Preprocessor directives
• The main() function
• Input / Output
• The printf() function
• The scanf() function
• Arrays
• Strings in C
• Function prototypes
• Structures
C for Java Programmers
Aims
• To be able to adapt your skills as a Java
programmer to develop programs in the C
language.
• To be able to use C development tools.
• To understand memory allocation, management
and the use of pointers.
Why C?
• This is a High Performance Computing module.
o We are aiming to get the highest possible performance
from our hardware.
o C enables good performance with low level control over the
computer and its operating system.
ISBN 978-0131103627
Java vs.
C
Java C
1.A programming
language
1.A programming
language
2. Object oriented
3. Garbage collector 2. Function oriented
4. No pointers 3.Manage your own
5.Better programming memory
style, security 4. Pointers
5.More efficient and
powerful
About C
• IDE
– Eclipse *
– Microsoft Visual C++ Express Edition
– Codeblocks
My first C program!
#include <stdio.h>
// program prints hello world
int main() {
printf ("Hello world!");
return 0;
}
#include <stdio.h>
// program prints a number of type int
int main() {
int number = 4;
printf (“Number is %d”, number);
return 0;
}
Output: Number is 4
Example 2
#include <stdio.h>
// program reads and prints the same thing
int main() {
int number ;
printf (“ Enter a Number: ”);
scanf (“%d”, &number);
printf (“Number is %d\n”, number);
return 0;
}
#include <stdio.h>
int main() {
/* this program adds two numbers */
int a = 4; //first number
int b = 5; //second number
int answer = 0; //result
answer = a + b;
}
Preprocessor directives
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
• & in scanf.
• Data Hierarchy.
– example:
• int value can be assigned to float not vice-versa
• Type casting.
Type Casting
#include <stdio.h>
main() {
int number = 1;
char character = 'a'; /*ASCII value is 97 */
int sum;
sum = number + character;
/*
Here 'a' is a char data type and sum is int data type, so before
calculating sum the compiler convert char values of 'a' into ASCII
int value 97 is called integer promotion. After that sum is
calculated with int type
Integer promotion: process of converting lower type than int or unsigned int into int or unsigned
int and Converting from smaller data type into larger data type is also called as type promotion
*/
printf("Value of sum : %d\n", sum );
}
Explicit type casting
#include<stdio.h>
int main(){
int var1=10, var2=3;
//var1 and var2 will converted into float type ➔ explicit type casting
float result=(float)var1/var2;
printf("result: %f\n",result);
return 0;
}
Inbuilt Type casting Function
• Declaration
type var_name[size]
e.g
• int A[6];
• float f[15];
Array Initialization
Static initialization
int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
• No “Strings” keyword
• A string is an array of characters.
char msg[] = “hello world”;
char *msg= “hello world”;
Function prototypes
• Why function ?
• Avoids rewriting the same code over and over.
• Using functions it becomes easier to write programs and keep track of what
they doing.
Function
Function
When we use a user-defined function program structure is divided into three parts.
Function Structure
void func1(); Function Prototype
void main()
{
....
func1(); Function call
}
void func1()
{
.... Function definition
//function body
....
}
Function Prototype and Function Definition
Syntax Example
Declaration
return-type function-name (arg-1, arg 2, …); void addition(int, int);
Definition
return-type function-name (arg-1, arg 2, …) void addition(int x, int y)
{ {
//... Function body printf("Addition is=%d“,(x+y));
} }
Function prototypes
Syntax Example
return-type function-name (arg-1, arg 2, …); void addition(int, int);
Category of Function
• Examples:
• Definition of a structure:
struct <struct-type>{
<type> <identifier_list>;
<type> <identifier_list>;
...
} ;
• Example:
struct Date {
int day;
int month;
int year;
} ;
struct examples
⚫ Example:
struct BankAccount{
char Name[15];
int AcountNo[10];
double balance;
Date Birthday;
};
⚫ Example:
struct StudentRecord{
char Name[15];
int Id;
char Dept[5];
char Gender;
};
struct examples
• Example:
struct StudentInfo{
int Id;
int age;
char Gender;
double CGA;
};
• Example:
struct StudentGrade{
char Name[15];
char Course[9];
int Lab[5];
int Homework[3];
int Exam[2];
};
Example Program
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
Example Program
int main( ) {
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
Example Program
• Declaration of union must start with the keyword union followed by the union name and
union’s member variables are declared within braces.
Syntax
1 union union_name union_name is name of custom type.
2 {
3 member1_declaration;
4 member2_declaration;
5 . . . memberN_declaration is individual member
6 memberN_declaration;
7 }; declaration.
• Syntax:
int main(int argc, char *argv[]){
//body
}
Example
End of Lecture 1
Any Questions??☺