C Programming

Unformatted text preview:

C PROGRAMMING NOTES C programming language C is a high level general purpose programming language that was originally designed and developed by Dennis Ritchie at Bell Labs in the 1970s It is one of the most popular and widely used programming languages today known for its speed efficiency and flexibility C is a compiled language which means that the source code must be converted into machine code before it can be executed on a computer It is used for a wide range of applications including operating systems embedded systems software development game programming and scientific research C provides a rich set of programming constructs including variables data types control structures functions and pointers It is a relatively low level language providing direct access to memory and hardware resources which makes it highly efficient and adaptable for systems C has influenced many other programming languages including C Java Python and many others It continues to be an important language for software development and many developers and programmers consider it an essential tool in their arsenal Variables Variable is the name of a memory location which stores some data In the C programming language a variable is a named storage location that can hold a value Variables are used to store data that is used in a program and their value can be changed during the execution of the program To declare a variable in C you specify its data type followed by its name For example to declare an integer variable named myVariable you would use the following code This declares a variable named myVariable of type integer You can also assign an initial value to the variable at the time of declaration by using the assignment operator For example This declares a variable named myVariable of type integer and assigns it an initial value of 42 To assign a new value to a variable you simply use the assignment operator followed by the new value For example programming int myVariable int myVariable 42 myVariable 23 This assigns a new value of 23 to the variable myVariable In C variables have a scope that determines where they can be accessed A variable declared inside a function for example can only be accessed within that function A variable declared outside of any function on the other hand can be accessed by any function in the program It s important to initialize variables before using them to avoid accessing an undefined value which can cause unexpected behavior or errors in the program There are several types of variables Integer variables These variables hold whole number values positive negative or zero and are declared using the keyword int Floating point variables These variables hold decimal or fractional values and are declared using Character variables These variables hold single character values e g a b c and are the keywords float or double declared using the keyword char Boolean variables These variables hold either true or false values and are declared using the keyword bool which is a part of the C99 standard Pointer variables These variables hold memory addresses and are declared using the symbol e g int ptr Array variables These variables hold a collection of values of the same data type and are declared using brackets e g int myArray 10 Structure variables These variables hold a collection of related variables of different data types and are declared using the struct keyword e g struct person char name 20 int age myPerson In the C programming language there are several rules that must be followed when declaring and using variables Variable names must start with a letter or underscore character Variable names cannot be the same as a keyword or reserved word in C Variable names can only contain letters numbers and underscores Variable names are case sensitive i e myVariable and myvariable are two different variables Variable names should be descriptive and meaningful Variables must be declared before they are used in the program Each variable must have a unique name within the same scope The data type of a variable cannot be changed after it has been declared Variables should be initialized with a value before they are used to avoid accessing an undefined value Variables can only hold values of their declared data type In C programming language there are several data types that can be used to define variables These data types are int Used for integer values such as 1 2 3 etc char Used for a single character value such as a b c etc float Used for floating point values with a single precision such as 3 14 1 23 etc double Used for floating point values with double precision such as 3 1415926535 1 23456789 etc void Used to indicate that the variable has no data type This is typically used for function pointers short Used for integers with a smaller range than int long Used for integers with a larger range than int unsigned Used for integers that can only have positive values signed Used for integers that can have both positive and negative values this is the default for int in C Bool Used for Boolean values which can only be true or false Complex Used for complex numbers which have a real and imaginary part enum Used to define a set of named integer constants struct Used to define a collection of related variables of different data types Constant values that don t change fixed In computer programming a constant is a value that cannot be changed during the execution of a program Constants are used to represent fixed values that do not need to be modified such as the value of pi 3 14159 or the number of seconds in a minute 60 In most programming languages constants are declared using the const keyword followed by the data type and value of the constant For example in C programming language you can declare a constant of type integer as follows const int MY CONSTANT 42 In the C programming language there are four types of constants Integer constants These are whole numbers without decimal points For example 12 45 and 0 are integer constants Real constants These are numbers with decimal points They can be expressed in scientific notation such as 3 14159e 6 or 2 0E8 Character constants These are single characters enclosed in single quotes such as a or String constants These are sequences of characters enclosed in double quotes such as hello world Additionally there are two special types of constants Symbolic constants These are identifiers defined with the define preprocessor directive They represent a


View Full Document

C Programming

Download C Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?