SlideShare a Scribd company logo
1 of 7
C is a general-purpose high level language that was originally developed by Dennis Ritchie for the Unix operating system. It was first implemented on the Digital
Eqquipment Corporation PDP-11 computer in 1972.
The Unix operating system and virtually all Unix applications are written in the C language. C has now become a widely used professional language for various reasons.
• Easy to learn
• Structured language
• It produces efficient programs.
• It can handle low-level activities.
• It can be compiled on a variety of computers.
Facts about C
• C was invented to write an operating system called UNIX.
• C is a successor of B language which was introduced around 1970
• The language was formalized in 1988 by the American National Standard Institue (ANSI).
• By 1973 UNIX OS almost totally written in C.
• Today C is the most widely used System Programming Language.
• Most of the state of the art software have been implemented using C
Why to use C ?
C was initially used for system development work, in particular the programs that make-up the operating system. C was adoped as a system development language
because it produces code that runs nearly as fast as code written in assembly language. Some examples of the use of C might be:
• Operating Systems
• Language Compilers
• Assemblers
• Text Editors
• Print Spoolers
• Network Drivers
• Modern Programs
• Data Bases
• Language Interpreters
• Utilities
C Program File
All the C programs are writen into text files with extension ".c" for example hello.c. You can use "vi" editor to write your C program into a file.
This tutorial assumes that you know how to edit a text file and how to write programming insturctions inside a program file.
C Compilers
When you write any program in C language then to run that program you need to compile that program using a C Compiler which converts your program into a
language understandable by a computer. This is called machine language (ie. binary format). So before proceeding, make sure you have C Compiler available at your
computer. It comes alongwith all flavors of Unix and Linux.
If you are working over Unix or Linux then you can type gcc -v or cc -v and check the result. You can ask your system administrator or you can take help from anyone
to identify an available C Compiler at your computer.
If you don't have C compiler installed at your computer then you can use below given link to download a GNU C Compiler and use it
A C program basically has the following form:
• Preprocessor Commands
• Functions
• Variables
• Statements & Expressions
• Comments
The following program is written in the C programming language. Open a text file hello.c using vi editor and put the following lines inside that file.
#include <stdio.h>
int main()
{
/* My first program */
printf("Hello, World! n");
return 0;
}
Preprocessor Commands: These commands tells the compiler to do preprocessing before doing actual compilation. Like #include <stdio.h> is a preprocessor
command which tells a C compiler to include stdio.h file before going to actual compilation. You will learn more about C Preprocessors in C Preprocessors session.
Functions: are main building blocks of any C Program. Every C Program will have one or more functions and there is one mandatory function which is
called main() function. This function is prefixed with keyword int which means this function returns an integer value when it exits. This integer value is retured
using return statement.
The C Programming language provides a set of built-in functions. In the above example printf()is a C built-in function which is used to print anything on the screen.
Check Builtin functionsection for more detail.
You will learn how to write your own functions and use them in Using Function session.
Variables: are used to hold numbers, strings and complex data for manipulation. You will learn in detail about variables in C Variable Types.
Statements & Expressions : Expressions combine variables and constants to create new values. Statements are expressions, assignments, function calls, or control
flow statements which make up C programs.
Comments: are used to give additional useful information inside a C Program. All the comments will be put inside /*...*/ as given in the example above. A comment
can span through multiple lines.
Note the followings
• C is a case sensitive programming language. It means in C printf and Printf will have different meanings.
• C has a free-form line structure. End of each C statement must be marked with a semicolon.
• Multiple statements can be one the same line.
• White Spaces (ie tab space and space bar ) are ignored.
• Statements can continue over multiple lines.
C Program Compilation
To compile a C program you would have to Compiler name and program files name. Assuming your compiler's name is cc and program file name is hello.c, give
following command at Unix prompt.
$cc hello.c
This will produce a binary file called a.out and an object file hello.o in your current directory. Here a.out is your first program which you will run at Unix prompt like any
other system program. If you don't like the name a.out then you can produce a binary file with your own name by using -o option while compiling C program. See an
example below
$cc -o hello hello.c
Now you will get a binary with name hello. Execute this program at Unix prompt but before executing / running this program make sure that it has execute permission
set. If you don't know what is execute permission then just follow these two steps
$chmod 755 hello
$./hello
This will produce following result
Hello, World
Congratulations!! you have written your first program in "C". Now believe me its not difficult to learn "C"
The following names are reserved by the C language. Their meaning is already defined, and they cannot be re-defined to mean anything else.
auto else long switch
break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct _Packed
double
While naming your functions and variables, other than these names, you can choose any names of reasonable length for variables, functions etc.
C has a concept of 'data types' which are used to define a variable before its use. The definition of a variable will assign storage for the variable and define the type of
data that will be held in the location.
The value of a variable can be changed any time.
C has the following basic built-in datatypes.
• int
• float
• double
• char
Please note that there is not a boolean data type. C does not have the traditional view about logical comparison, but thats another story.
int - data type
int is used to define integer numbers.
{
int Count;
Count = 5;
}
float - data type
float is used to define floating point numbers.
{
float Miles;
Miles = 5.6;
}
double - data type
double is used to define BIG floating point numbers. It reserves twice the storage for the number. On PCs this is likely to be 8 bytes.
{
double Atoms;
Atoms = 2500000;
}
char - data type
char defines characters.
{
char Letter;
Letter = 'x';
}
Modifiers
The data types explained above have the following modifiers.
• short
• long
• signed
• unsigned
The modifiers define the amount of storage allocated to the variable. The amount of storage allocated is not cast in stone. ANSI has the following rules:
short int <= int <= long int
float <= double <= long double
What this means is that a 'short int' should assign less than or the same amount of storage as an 'int' and the 'int' should be less or the same bytes than a 'long int'.
What this means in the real world is:
Type Bytes Range
---------------------------------------------------------------------
short int 2 -32,768 -> +32,767 (32kb)
unsigned short int 2 0 -> +65,535 (64Kb)
unsigned int 4 0 -> +4,294,967,295 ( 4Gb)
int 4 -2,147,483,648 -> +2,147,483,647 ( 2Gb)
long int 4 -2,147,483,648 -> +2,147,483,647 ( 2Gb)
signed char 1 -128 -> +127
unsigned char 1 0 -> +255
float 4
double 8
long double 12
These figures only apply to todays generation of PCs. Mainframes and midrange machines could use different figures, but would still comply with the rule above.
You can find out how much storage is allocated to a data type by using the sizeof operator discussed in Operator Types Session.
Here is an example to check size of memory taken by various datatypes.
int
main()
{
printf("sizeof(char) == %dn", sizeof(char));
printf("sizeof(short) == %dn", sizeof(short));
printf("sizeof(int) == %dn", sizeof(int));
printf("sizeof(long) == %dn", sizeof(long));
printf("sizeof(float) == %dn", sizeof(float));
printf("sizeof(double) == %dn", sizeof(double));
printf("sizeof(long double) == %dn", sizeof(long double));
printf("sizeof(long long) == %dn", sizeof(long long));
return 0;
}
Qualifiers
A type qualifier is used to refine the declaration of a variable, a function, and parameters, by specifying whether:
• The value of a variable can be changed.
• The value of a variable must always be read from memory rather than from a register
Standard C language recognizes the following two qualifiers:
• const
• volatile
The const qualifier is used to tell C that the variable value can not change after initialisation.
const float pi=3.14159;
Now pi cannot be changed at a later time within the program.
Another way to define constants is with the #define preprocessor which has the advantage that it does not use any storage
The volatile qualifier declares a data type that can have its value changed in ways outside the control or detection of the compiler (such as a variable updated by the
system clock or by another program). This prevents the compiler from optimizing code referring to the object by storing the object's value in a register and re-reading
it from there, rather than from memory, where it may have changed. You will use this qualifier once you will become expert in "C". So for now just proceed.
What are Arrays:
We have seen all baisc data types. In C language it is possible to make arrays whose elements are basic types. Thus we can make an array of 10 integers with the
declaration.
int x[10];
The square brackets mean subscripting; parentheses are used only for function references. Array indexes begin at zero, so the elements of x are:
Thus Array are special type of variables which can be used to store multiple values of same data type. Those values are stored and accessed using subscript or index.
Arrays occupy consecutive memory slots in the computer's memory.
x[0], x[1], x[2], ..., x[9]
If an array has n elements, the largest subscript is n-1.
Multiple-dimension arrays are provided. The declaration and use look like:
int name[10] [20];
n = name[i+j] [1] + name[k] [2];
Subscripts can be arbitrary integer expressions. Multi-dimension arrays are stored by row so the rightmost subscript varies fastest. In above example name has 10
rows and 20 columns.
Same way, arrays can be defined for any data type. Text is usually kept as an array of characters. By convention in C, the last character in a character array should be
a `0' because most programs that manipulate character arrays expect it. For example, printf uses the `0' to detect the end of a character array when printing it out
with a `%s'.
Here is a program which reads a line, stores it in a buffer, and prints its length (excluding the newline at the end).
main( ) {
int n, c;
char line[100];
n = 0;
while( (c=getchar( )) != 'n' ) {
if( n < 100 )
line[n] = c;
n++;
}
printf("length = %dn", n);
}
Array Initialization
• As with other declarations, array declarations can include an optional initialization
• Scalar variables are initialized with a single value
• Arrays are initialized with a list of values
• The list is enclosed in curly braces
int array [8] = {2, 4, 6, 8, 10, 12, 14, 16};
The number of initializers cannot be more than the number of elements in the array but it can be less in which case, the remaining elements are initialized to 0.if you
like, the array size can be inferred from the number of initializers by leaving the square brackets empty so these are identical declarations:
int array1 [8] = {2, 4, 6, 8, 10, 12, 14, 16};
int array2 [] = {2, 4, 6, 8, 10, 12, 14, 16};
An array of characters ie string can be initialized as follows:
char string[10] = "Hello";
A variable is just a named area of storage that can hold a single value (numeric or character). The C language demands that you declare the name of each variable that
you are going to use and its type, or class, before you actually try to do anything with it.
The Programming language C has two main variable types
• Local Variables
• Global Variables
Local Variables
• Local variables scope is confined within the block or function where it is defined. Local variables must always be defined at the top of a block.
• When a local variable is defined - it is not initalised by the system, you must initalise it yourself.
• When execution of the block starts the variable is available, and when the block ends the variable 'dies'.
Check following example's output
main()
{
int i=4;
int j=10;
i++;
if (j > 0)
{
/* i defined in 'main' can be seen */
printf("i is %dn",i);
}
if (j > 0)
{
/* 'i' is defined and so local to this block */
int i=100;
printf("i is %dn",i);
}/* 'i' (value 100) dies here */
printf("i is %dn",i); /* 'i' (value 5) is now visable.*/
}
This will generate following output
i is 5
i is 100
i is 5
Here ++ is called incremental operator and it increase the value of any integer variable by 1. Thus i++ is equivalent to i = i + 1;
You will see -- operator also which is called decremental operator and it idecrease the value of any integer variable by 1. Thus i-- is equivalent to i = i - 1;
Global Variables
Global variable is defined at the top of the program file and it can be visible and modified by any function that may reference it.
Global variables are initalised automatically by the system when you define them!
Data Type Initialser
int 0
char '0'
float 0
pointer NULL
If same variable name is being used for global and local variable then local variable takes preference in its scope. But it is not a good practice to use global variables
and local variables with the same name.
int i=4; /* Global definition */
main()
{
i++; /* Global variable */
func();
printf( "Value of i = %d -- main functionn", i );
}
func()
{
int i=10; /* Local definition */
i++; /* Local variable */
printf( "Value of i = %d -- func() functionn", i );
}
This will produce following result
Value of i = 11 -- func() function
Value of i = 5 -- main function
i in main function is global and will be incremented to 5. i in func is internal and will be incremented to 11. When control returns to main the internal variable will die
and and any reference to i will be to the global.

More Related Content

What's hot (20)

C basics
C   basicsC   basics
C basics
 
Features of c language 1
Features of c language 1Features of c language 1
Features of c language 1
 
Brief introduction to the c programming language
Brief introduction to the c programming languageBrief introduction to the c programming language
Brief introduction to the c programming language
 
Features of c
Features of cFeatures of c
Features of c
 
Introduction to c programming language
Introduction to c programming languageIntroduction to c programming language
Introduction to c programming language
 
C Language
C LanguageC Language
C Language
 
C program
C programC program
C program
 
C language
C languageC language
C language
 
The smartpath information systems c pro
The smartpath information systems c proThe smartpath information systems c pro
The smartpath information systems c pro
 
C PROGRAMMING
C PROGRAMMINGC PROGRAMMING
C PROGRAMMING
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Tokens_C
Tokens_CTokens_C
Tokens_C
 
Structures
StructuresStructures
Structures
 
Basic C Programming language
Basic C Programming languageBasic C Programming language
Basic C Programming language
 
OpenGurukul : Language : C Programming
OpenGurukul : Language : C ProgrammingOpenGurukul : Language : C Programming
OpenGurukul : Language : C Programming
 
Introduction to C Programming - I
Introduction to C Programming - I Introduction to C Programming - I
Introduction to C Programming - I
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Hands-on Introduction to the C Programming Language
Hands-on Introduction to the C Programming LanguageHands-on Introduction to the C Programming Language
Hands-on Introduction to the C Programming Language
 
C programming language
C programming languageC programming language
C programming language
 
C languaGE UNIT-1
C languaGE UNIT-1C languaGE UNIT-1
C languaGE UNIT-1
 

Similar to Basic c

67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdfRajb54
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance levelsajjad ali khan
 
Unit 2 introduction to c programming
Unit 2   introduction to c programmingUnit 2   introduction to c programming
Unit 2 introduction to c programmingMithun DSouza
 
00 C hello world.pptx
00 C hello world.pptx00 C hello world.pptx
00 C hello world.pptxCarla227537
 
c-introduction.pptx
c-introduction.pptxc-introduction.pptx
c-introduction.pptxMangala R
 
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptxIIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptxrajkumar490591
 
C language introduction geeksfor geeks
C language introduction   geeksfor geeksC language introduction   geeksfor geeks
C language introduction geeksfor geeksAashutoshChhedavi
 
C notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semC notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semKavita Dagar
 
Basic Information About C language PDF
Basic Information About C language PDFBasic Information About C language PDF
Basic Information About C language PDFSuraj Das
 
Programming in C - interview questions.pdf
Programming in C - interview questions.pdfProgramming in C - interview questions.pdf
Programming in C - interview questions.pdfSergiuMatei7
 
C Programming UNIT 1.pptx
C Programming  UNIT 1.pptxC Programming  UNIT 1.pptx
C Programming UNIT 1.pptxMugilvannan11
 
Learn c language Important topics ( Easy & Logical, & smart way of learning)
Learn c language Important topics ( Easy & Logical, & smart way of learning)Learn c language Important topics ( Easy & Logical, & smart way of learning)
Learn c language Important topics ( Easy & Logical, & smart way of learning)Rohit Singh
 

Similar to Basic c (20)

67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance level
 
Unit 2 introduction to c programming
Unit 2   introduction to c programmingUnit 2   introduction to c programming
Unit 2 introduction to c programming
 
C programming day#1
C programming day#1C programming day#1
C programming day#1
 
00 C hello world.pptx
00 C hello world.pptx00 C hello world.pptx
00 C hello world.pptx
 
c-introduction.pptx
c-introduction.pptxc-introduction.pptx
c-introduction.pptx
 
C language tutorial
C language tutorialC language tutorial
C language tutorial
 
The basics of c programming
The basics of c programmingThe basics of c programming
The basics of c programming
 
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptxIIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
Lecture 1 progrmming with C
Lecture 1 progrmming with C Lecture 1 progrmming with C
Lecture 1 progrmming with C
 
C programming
C programmingC programming
C programming
 
C language introduction geeksfor geeks
C language introduction   geeksfor geeksC language introduction   geeksfor geeks
C language introduction geeksfor geeks
 
C programming
C programmingC programming
C programming
 
C notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semC notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-sem
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
Basic Information About C language PDF
Basic Information About C language PDFBasic Information About C language PDF
Basic Information About C language PDF
 
Programming in C - interview questions.pdf
Programming in C - interview questions.pdfProgramming in C - interview questions.pdf
Programming in C - interview questions.pdf
 
C Programming UNIT 1.pptx
C Programming  UNIT 1.pptxC Programming  UNIT 1.pptx
C Programming UNIT 1.pptx
 
Learn c language Important topics ( Easy & Logical, & smart way of learning)
Learn c language Important topics ( Easy & Logical, & smart way of learning)Learn c language Important topics ( Easy & Logical, & smart way of learning)
Learn c language Important topics ( Easy & Logical, & smart way of learning)
 

Recently uploaded

PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
vip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Book
vip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Bookvip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Book
vip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Bookmanojkuma9823
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort servicejennyeacort
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一F sss
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 

Recently uploaded (20)

PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
vip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Book
vip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Bookvip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Book
vip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Book
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 

Basic c

  • 1. C is a general-purpose high level language that was originally developed by Dennis Ritchie for the Unix operating system. It was first implemented on the Digital Eqquipment Corporation PDP-11 computer in 1972. The Unix operating system and virtually all Unix applications are written in the C language. C has now become a widely used professional language for various reasons. • Easy to learn • Structured language • It produces efficient programs. • It can handle low-level activities. • It can be compiled on a variety of computers. Facts about C • C was invented to write an operating system called UNIX. • C is a successor of B language which was introduced around 1970 • The language was formalized in 1988 by the American National Standard Institue (ANSI). • By 1973 UNIX OS almost totally written in C. • Today C is the most widely used System Programming Language. • Most of the state of the art software have been implemented using C Why to use C ? C was initially used for system development work, in particular the programs that make-up the operating system. C was adoped as a system development language because it produces code that runs nearly as fast as code written in assembly language. Some examples of the use of C might be: • Operating Systems • Language Compilers • Assemblers • Text Editors • Print Spoolers • Network Drivers • Modern Programs • Data Bases • Language Interpreters • Utilities C Program File All the C programs are writen into text files with extension ".c" for example hello.c. You can use "vi" editor to write your C program into a file. This tutorial assumes that you know how to edit a text file and how to write programming insturctions inside a program file.
  • 2. C Compilers When you write any program in C language then to run that program you need to compile that program using a C Compiler which converts your program into a language understandable by a computer. This is called machine language (ie. binary format). So before proceeding, make sure you have C Compiler available at your computer. It comes alongwith all flavors of Unix and Linux. If you are working over Unix or Linux then you can type gcc -v or cc -v and check the result. You can ask your system administrator or you can take help from anyone to identify an available C Compiler at your computer. If you don't have C compiler installed at your computer then you can use below given link to download a GNU C Compiler and use it A C program basically has the following form: • Preprocessor Commands • Functions • Variables • Statements & Expressions • Comments The following program is written in the C programming language. Open a text file hello.c using vi editor and put the following lines inside that file. #include <stdio.h> int main() { /* My first program */ printf("Hello, World! n"); return 0; } Preprocessor Commands: These commands tells the compiler to do preprocessing before doing actual compilation. Like #include <stdio.h> is a preprocessor command which tells a C compiler to include stdio.h file before going to actual compilation. You will learn more about C Preprocessors in C Preprocessors session. Functions: are main building blocks of any C Program. Every C Program will have one or more functions and there is one mandatory function which is called main() function. This function is prefixed with keyword int which means this function returns an integer value when it exits. This integer value is retured using return statement. The C Programming language provides a set of built-in functions. In the above example printf()is a C built-in function which is used to print anything on the screen. Check Builtin functionsection for more detail. You will learn how to write your own functions and use them in Using Function session. Variables: are used to hold numbers, strings and complex data for manipulation. You will learn in detail about variables in C Variable Types. Statements & Expressions : Expressions combine variables and constants to create new values. Statements are expressions, assignments, function calls, or control flow statements which make up C programs. Comments: are used to give additional useful information inside a C Program. All the comments will be put inside /*...*/ as given in the example above. A comment can span through multiple lines. Note the followings • C is a case sensitive programming language. It means in C printf and Printf will have different meanings. • C has a free-form line structure. End of each C statement must be marked with a semicolon. • Multiple statements can be one the same line. • White Spaces (ie tab space and space bar ) are ignored. • Statements can continue over multiple lines.
  • 3. C Program Compilation To compile a C program you would have to Compiler name and program files name. Assuming your compiler's name is cc and program file name is hello.c, give following command at Unix prompt. $cc hello.c This will produce a binary file called a.out and an object file hello.o in your current directory. Here a.out is your first program which you will run at Unix prompt like any other system program. If you don't like the name a.out then you can produce a binary file with your own name by using -o option while compiling C program. See an example below $cc -o hello hello.c Now you will get a binary with name hello. Execute this program at Unix prompt but before executing / running this program make sure that it has execute permission set. If you don't know what is execute permission then just follow these two steps $chmod 755 hello $./hello This will produce following result Hello, World Congratulations!! you have written your first program in "C". Now believe me its not difficult to learn "C" The following names are reserved by the C language. Their meaning is already defined, and they cannot be re-defined to mean anything else. auto else long switch break enum register typedef case extern return union char float short unsigned const for signed void continue goto sizeof volatile default if static while do int struct _Packed double While naming your functions and variables, other than these names, you can choose any names of reasonable length for variables, functions etc. C has a concept of 'data types' which are used to define a variable before its use. The definition of a variable will assign storage for the variable and define the type of data that will be held in the location. The value of a variable can be changed any time. C has the following basic built-in datatypes. • int • float • double • char Please note that there is not a boolean data type. C does not have the traditional view about logical comparison, but thats another story. int - data type int is used to define integer numbers. { int Count; Count = 5; }
  • 4. float - data type float is used to define floating point numbers. { float Miles; Miles = 5.6; } double - data type double is used to define BIG floating point numbers. It reserves twice the storage for the number. On PCs this is likely to be 8 bytes. { double Atoms; Atoms = 2500000; } char - data type char defines characters. { char Letter; Letter = 'x'; } Modifiers The data types explained above have the following modifiers. • short • long • signed • unsigned The modifiers define the amount of storage allocated to the variable. The amount of storage allocated is not cast in stone. ANSI has the following rules: short int <= int <= long int float <= double <= long double What this means is that a 'short int' should assign less than or the same amount of storage as an 'int' and the 'int' should be less or the same bytes than a 'long int'. What this means in the real world is: Type Bytes Range --------------------------------------------------------------------- short int 2 -32,768 -> +32,767 (32kb) unsigned short int 2 0 -> +65,535 (64Kb) unsigned int 4 0 -> +4,294,967,295 ( 4Gb) int 4 -2,147,483,648 -> +2,147,483,647 ( 2Gb) long int 4 -2,147,483,648 -> +2,147,483,647 ( 2Gb) signed char 1 -128 -> +127 unsigned char 1 0 -> +255 float 4 double 8 long double 12 These figures only apply to todays generation of PCs. Mainframes and midrange machines could use different figures, but would still comply with the rule above. You can find out how much storage is allocated to a data type by using the sizeof operator discussed in Operator Types Session. Here is an example to check size of memory taken by various datatypes. int
  • 5. main() { printf("sizeof(char) == %dn", sizeof(char)); printf("sizeof(short) == %dn", sizeof(short)); printf("sizeof(int) == %dn", sizeof(int)); printf("sizeof(long) == %dn", sizeof(long)); printf("sizeof(float) == %dn", sizeof(float)); printf("sizeof(double) == %dn", sizeof(double)); printf("sizeof(long double) == %dn", sizeof(long double)); printf("sizeof(long long) == %dn", sizeof(long long)); return 0; } Qualifiers A type qualifier is used to refine the declaration of a variable, a function, and parameters, by specifying whether: • The value of a variable can be changed. • The value of a variable must always be read from memory rather than from a register Standard C language recognizes the following two qualifiers: • const • volatile The const qualifier is used to tell C that the variable value can not change after initialisation. const float pi=3.14159; Now pi cannot be changed at a later time within the program. Another way to define constants is with the #define preprocessor which has the advantage that it does not use any storage The volatile qualifier declares a data type that can have its value changed in ways outside the control or detection of the compiler (such as a variable updated by the system clock or by another program). This prevents the compiler from optimizing code referring to the object by storing the object's value in a register and re-reading it from there, rather than from memory, where it may have changed. You will use this qualifier once you will become expert in "C". So for now just proceed. What are Arrays: We have seen all baisc data types. In C language it is possible to make arrays whose elements are basic types. Thus we can make an array of 10 integers with the declaration. int x[10]; The square brackets mean subscripting; parentheses are used only for function references. Array indexes begin at zero, so the elements of x are: Thus Array are special type of variables which can be used to store multiple values of same data type. Those values are stored and accessed using subscript or index. Arrays occupy consecutive memory slots in the computer's memory. x[0], x[1], x[2], ..., x[9] If an array has n elements, the largest subscript is n-1. Multiple-dimension arrays are provided. The declaration and use look like: int name[10] [20]; n = name[i+j] [1] + name[k] [2]; Subscripts can be arbitrary integer expressions. Multi-dimension arrays are stored by row so the rightmost subscript varies fastest. In above example name has 10 rows and 20 columns.
  • 6. Same way, arrays can be defined for any data type. Text is usually kept as an array of characters. By convention in C, the last character in a character array should be a `0' because most programs that manipulate character arrays expect it. For example, printf uses the `0' to detect the end of a character array when printing it out with a `%s'. Here is a program which reads a line, stores it in a buffer, and prints its length (excluding the newline at the end). main( ) { int n, c; char line[100]; n = 0; while( (c=getchar( )) != 'n' ) { if( n < 100 ) line[n] = c; n++; } printf("length = %dn", n); } Array Initialization • As with other declarations, array declarations can include an optional initialization • Scalar variables are initialized with a single value • Arrays are initialized with a list of values • The list is enclosed in curly braces int array [8] = {2, 4, 6, 8, 10, 12, 14, 16}; The number of initializers cannot be more than the number of elements in the array but it can be less in which case, the remaining elements are initialized to 0.if you like, the array size can be inferred from the number of initializers by leaving the square brackets empty so these are identical declarations: int array1 [8] = {2, 4, 6, 8, 10, 12, 14, 16}; int array2 [] = {2, 4, 6, 8, 10, 12, 14, 16}; An array of characters ie string can be initialized as follows: char string[10] = "Hello"; A variable is just a named area of storage that can hold a single value (numeric or character). The C language demands that you declare the name of each variable that you are going to use and its type, or class, before you actually try to do anything with it. The Programming language C has two main variable types • Local Variables • Global Variables Local Variables • Local variables scope is confined within the block or function where it is defined. Local variables must always be defined at the top of a block. • When a local variable is defined - it is not initalised by the system, you must initalise it yourself. • When execution of the block starts the variable is available, and when the block ends the variable 'dies'. Check following example's output main() { int i=4; int j=10; i++; if (j > 0) { /* i defined in 'main' can be seen */ printf("i is %dn",i); }
  • 7. if (j > 0) { /* 'i' is defined and so local to this block */ int i=100; printf("i is %dn",i); }/* 'i' (value 100) dies here */ printf("i is %dn",i); /* 'i' (value 5) is now visable.*/ } This will generate following output i is 5 i is 100 i is 5 Here ++ is called incremental operator and it increase the value of any integer variable by 1. Thus i++ is equivalent to i = i + 1; You will see -- operator also which is called decremental operator and it idecrease the value of any integer variable by 1. Thus i-- is equivalent to i = i - 1; Global Variables Global variable is defined at the top of the program file and it can be visible and modified by any function that may reference it. Global variables are initalised automatically by the system when you define them! Data Type Initialser int 0 char '0' float 0 pointer NULL If same variable name is being used for global and local variable then local variable takes preference in its scope. But it is not a good practice to use global variables and local variables with the same name. int i=4; /* Global definition */ main() { i++; /* Global variable */ func(); printf( "Value of i = %d -- main functionn", i ); } func() { int i=10; /* Local definition */ i++; /* Local variable */ printf( "Value of i = %d -- func() functionn", i ); } This will produce following result Value of i = 11 -- func() function Value of i = 5 -- main function i in main function is global and will be incremented to 5. i in func is internal and will be incremented to 11. When control returns to main the internal variable will die and and any reference to i will be to the global.