Sunteți pe pagina 1din 30

C- Language

Lets Learn fundamentals !!

C Language
In 1969-70 In 1971-73

B
Ken Thomson Dennis Ritchie

C Language In 1971-73
In 1989
In 1990
ANSI/ISO C (International Standards Organization)

C
Dennis Ritchie

ANSI-C (American National Standards Institute)

Why C is still used ?


C replaces assembly language and it is easy to code in C rather in assembly. C is a structured language unlike assembly i.e. the code is well formatted & easily understandable than assembly. It can easily interact with the hardware. C is a cross-platform language which means the code written in C can run on any OS.

Software & Compilers used in programming


Compilers Visual studio GCC Xcode Language C, C++, C#, Visual Basic C, C++ Objective C Company Microsoft Corporation GNU project Apple Computers Supported OS & platforms Windows (Intel 32 &64 bit) Unix, Linux (32 & 64 bit) Macintosh (32 & 64 bit)

Getting started with C Language


#include<stdio.h> //header file inclusion Void main() //main function declaration { // main function definition starts here printf(Hello World\n); //calling the functions declared in header files }

The Desi C !!

Maans Room

Maans Cycle

Maan

Papa Haider

The Desi C !! Contd


Desi C Language Maan Papa Haider Papas Car Maans Room or Papas house Maans cycle with a motor Needs & situations in Maans life Dennis Ritchies C language main() Header file - #include<stdio.h> Functions defined by header files File in which main() or Header functions are defined User defined functions Various conditional expressions used a program e.g. if-else, while & for loops, switchcase

DATA TYPES
Type
char unsigned char signed char int unsigned int signed int short int unsigned short int signed short int long int signed long int unsigned long int float double long double

Typical Size in Bits


8 8 8 16 or 32 16 or 32 16 or 32 16 16 16 32 32 32 32 64 80

Minimal Range
-127 to 127 0 to 255 -127 to 127 -32767 to 32767 0 to 65535 Same as int -32767 to 32767 0 to 65535 Same as short int 2,147,483,647 to 2,147,483,647 same as long int 0 to 4,294,967,295 Six digits of precision Ten digits of precision Ten digits of precision

Syntax in C
In C, the names of variables, functions, labels, and various other user-defined objects are called identifiers. The first character must be a letter or an underscore, and subsequent characters must be either - letters, digits, or underscores. In C, identifiers may be of any length. In an identifier, upper and lowercase are treated as distinct. An identifier cannot be the same as a C keyword, and should not have the same name as functions that are in the C library.

Variables
A variable is a named location in memory that is used to hold a value that may be used by the program. All variables must be declared before they can be used. Type variable_list; //type is the type of data to be used int i,j,l; char ch; float f; unsigned int k;

Operators used in C
Operator type :Operators The assignment operator = Arithmetic operators , +, *, /, % Increment and decrement ++ , Relational operators >, >= , < , <= , = =, != Logical operators && , || , ! Bitwise operators &, |, ^ , ~, >>, <<

Example programs
#include<stdio.h> void main() { int a,b,sum; sum = a + b; printf(The addition is %d,sum); }

Conditional expression
A conditional expression determines what course of action is to be taken. A conditional expression evaluates to either a true or false value. C supports two types of selection statements: if and switch. The general form of the if statement is

if (expression) statement; else statement; Where a statement may consist of a single statement, a block of statements. The else clause is optional.

Conditional expression | If statement


if (condition is true) { execute this code block; } else //if above condition is false { execute this code block; }

Example programs | If statement


#include<stdio.h> # void main() { int a, b; scanf(%d %d, &a, &b); if(a<b) { printf(The no. %d is less than %d, a, b); } else { printf(The no. %d is greater than %d, a, b); } }

Switch-case
C has a built-in multiple-branch selection statement, called switch, which successively tests the value of an expression against a list of integer or character constants. switch (expression) { case constant1: statement sequence break; . . . default: statement sequence } The default statement is executed if no matches are found.

Switch-case
Standard C specifies that a switch can have at least 257 case statements. When break is encountered in a switch, program execution "jumps" to the line of code following the switch statement. There are three important things to know about the switch statement: The switch differs from the if in that switch can only test for equality, whereas if can evaluate any type of relational or logical expression. No two case constants in the same switch can have identical values. Of course, a switch statement enclosed by an outer switch may have case constants that are the same. If character constants are used in the switch statement, they are automatically converted to integers.

Switch-case
void main(void) { char ch; int maths, english; printf(Enter your marks in maths and english\n"); scanf(%d %d,& maths, &english); printf(Enter 1 to calculate average of the subjects or Enter 2 to know your grade percentage\n"); ch = getchar(); /* read the selection from the keyboard */ switch(ch) { case '1: case '2': default : } }

average(); break; grade(); break; printf(Please enter a valid choice\n);

Iteration statements
Iteration statements (also called loops) allow a set of instructions to be executed repeatedly until a certain condition is reached. This condition may be :Predefined : - for loop. Open-ended - while and do-while loops.

The for loop


The general form of the for statement is for(initialization; condition; updation) statement; The for loop continues to execute as long as the condition is true. Once the condition becomes false, program execution resumes on the statement following the for.

//print the numbers 1 through 100 on the screen #include <stdio.h> int main(void) { int x; for(x=1; x <= 100; x++) printf("%d ", x); return 0; }

The while loop


The second loop available in C/C++ is the while loop. while(condition) statement; The condition may be any expression, and true is any nonzero value. When the condition becomes false, program control passes to the line of code immediately following the loop.

The while loop


int main() { int i=0; while(i<=2) { printf("Hello World! %d \n", i); i++; } printf("Bye Bye world\n"); return 0; } Output is

Hello World! 0 Hello World! 1 Hello World! 2 Bye Bye world

Functions
Functions are the blocks in which we define the steps that how the program will work.

The general form of a function is:


return-type function-name(parameter list) { body of the function } The return-type specifies the type of data that the function returns. The parameter list is a comma-separated list of variable names .

Need of Functions
Functions are used so that we it dont have to write the code repeatedly over function of print & call whenever needed. and over again.

If a task is required to be done frequently then we can write this task code in a function and call this function whenever needed.

How to create a Functions


STEP 1 is to decide a name for function. Step 2 is to declare the function at the top of the C file.

The declaration only consists the functions return type, its name and the type of parameters it accepts. return-type function-name (parameter type)
STEP 3 is to define what a function will do i.e. its definition. The function is generally defined in the end of the main function.

How to create a Functions


#include<stdio.h> int add(int , int); //function declaration main() { int a,b,sum; printf(Enter two numbers to add\n); scanf(%d %d, &a, &b); sum=add(a, b); //function calling printf(The addition is %d,sum); return 0; } int add(int x, int y) { int z = x + y; return z; } //function definition

Comparison between C and other Languages


Language C C++ Implementation System programming Application, System programming Methodology Procedural, imperative Imperative, procedural, object-oriented, generic

C#
Java

Application, Web
Application, Web

Imperative, object-oriented, functional, generic, reflective


Imperative, object-oriented, generic, reflective

Types of variables
Local Variables
Variables that are declared inside a function are called local variables & they are available only to the function in which they are declared.

Formal parameters
Variables that will accept the values of the arguments in a function are called formal parameters.

Global Variables
Global variables are known throughout the program and may be used by any piece of code & they are declared outside of any function.

S-ar putea să vă placă și