0% found this document useful (0 votes)
5 views

C Programming

C programming 2nd semester CS calicut university

Uploaded by

najmunneesak209
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C Programming

C programming 2nd semester CS calicut university

Uploaded by

najmunneesak209
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Anarrayis defined as the collection of language. Auto variables can be only the given expressions.

n expressions.&& operator – “if times:"Ask the User to Guess a pre- A union is a special data type available var2\n");//Nested if elseif (var1 >
similar type of data items stored at accessed within the block/function they clause” becomes true only when both determined number between 1 and in C that allows to store different data var2){printf("var1 is g…An if statement
contiguous memory locations. Arrays have been declared and not outside conditions (m>n and m! =0) is true. 100". You have no way of knowing how types in the same memory location. consists of a Boolean expression
are the derived data type in C them (which defines their scope). Of Else, it becomes false.|| Operator – “if many guesses it will take."Randomly You can define a union with many followed by one or more
programming language which can store course, these can be accessed within clause” becomes true when any one of look in an array for a given value." You members, but only one member can statements.SyntaxThe syntax of an 'if'
the primitive type of data such as int, nested blocks within the parent the condition (o>p || p!=20) is true. It have no way of knowing how many contain a value at any given time. statement in C programming language
char, double, float, etc.The break block/function in which the auto becomes false when none of the tries it will take to find the actual Unions provide an efficient way of using is −if(boolean_expression) { /*
statement terminates the execution of variable was declared. extern: Extern condition is true.! Operator – It is used value.Known number of the same memory location for multiple- statement(s) will execute if the boolean
the nearest enclosing do , for , switch , storage class simply tells us that the to reverses the state of the operand.If times:Compute the average grade of purpose.Defining a UnionTo define a expression is true */}If the Boolean
or while statement in which it appears. variable is defined elsewhere and not the conditions (m>n && m!=0) is true, the class. While you (the programmer) union, you must use the union expression evaluates to true, then the
Control passes to the statement that within the same block where it is used. true (1) is returned. This value is might not know how many grades exist statement in the same way as you did block of code inside the 'if' statement
follows the terminated statement.To Basically, the value is assigned to it in a inverted by “!” operator.So, “! (m>n in the class, the computer will know. while defining a structure. The union will be executed. If the Boolean
make an infinite loop, just use true as different block and this can be and m! =0)” returns false (0).Static Usually this is accomplished by using statement defines a new data type with expression evaluates to false, then the
your condition. true is always true, so overwritten/changed in a different memory allocation is an allocation the "length" function on an array.Print more than one member for your first set of code after the end of the 'if'
the loop will repeat forever. Warning: block as well. So an extern variable is technique which allocates a fixed the odd numbers from 1 to 1001.Search program. The format of the union statement (after the closing curly brace)
Please make sure you have a check that nothing but a global variable initialized amount of memory during compile time a list (array) of numbers for the biggest statement is as follows −union [union will be executed.C programming
exits your loop, otherwise it will never with a legal value where it is declared in and the operating system internally grade. Again, the computer "knows" tag] { member definition; member language assumes any non-zero and
end.In C programming, scanf() is one of order to be used elsewhere. It can be uses a data structure known as Stack to how many grades there are, so a for definition; member definition;} [one or non-null values as true and if it is either
the commonly used function to take accessed within any function/block. manage this.An Exit Control Loop loop is appropriate.Opening a fileThe more union variables]; The union tag is zero or null, then it is assumed as false
input from the user. The scanf() Also, a normal global variable can be checks the condition for exit and if fopen() function is used to create a file optional and each member definition is value.C if statementExampleLive Demo
function reads formatted input from made extern as well by placing the given condition for exit evaluate to or open an existing file:fp = fopen(const a normal varia…If else statement #include <stdio.h>int main () { /* local
thestandard input such as keyboards.A ‘extern’ keyword before its true, control will exit from the loop char filename,const char mode);There Syntax of if else statement:If condition variabl…What is a structure? A
null pointer is a pointer which points declaration/definition in any body else control will enter again into are many modes for opening a file:r - returns true then the statements inside structure is a key word that create user
nothing. Some uses of the null pointer function/block. static: This storage class the loop.Such type of loop controls exit open a file in read mode w - opens or the body of “if” are executed and the defined data type in C/C++. A structure
are: a) To initialize a pointer variable is used to declare static variables which of the loop that’s why it is called exit create a text file in write mode a - statements inside body of “else” are creates a data type that can be used to
when that pointer variable isn't are popularly used while writing control loop.Exit Control loopDo…While opens a file in append mode r+ - opens skipped.If condition returns false then group items of possibly different types
assigned any valid memory address yet. programs in C language. Static variables loop:Do …while loop is an exit control a file in both read and write mode a+ - the statements inside the body of “if” into a single type. How to create a
b) To pass a null pointer to a function have the property of preserving their loop.It is a variation of while loop.When opens a file in both read and write are skipped and the statements in structure? ‘struct’ keyword is used to
argument when we don't want to pass value even after they are out of their we want to execute or perform some mode w+ - opens a file in both read and “else” are executed.if(condition) { // create a structure. Following is an
any valid memory address.The scope! Hence, static variables preserve task at least for once, we can use write mode.Now you might be thinking, Statements inside body of if}else { example. struct address{ char
difference between Actual Parameters the value of their last use in their scope. do…while loop structure.In a while loop "This just prints text to the screen. How //Statements inside body of name[50]; char street[100]; char
and Formal Parameters is that Actual So we can say that they are initialized if the expression becomes false at very is this file IO?”The answer isn’t obvious else}Example of if else statementIn this city[50];char state[20]; int pin;};How
Parameters are the values that are only once and exist till the termination first try then loop will never be at first, and needs some understanding program user is asked to enter the age to access structure elements? Structure
passed to the function when it is of the program. Thus, no new memory executed.Since, do…while is of type exit about the UNIX system. In a UNIX and based on the input, the if..else members are accessed using dot (.)
invoked while Formal Parameters are is allocated because they are not re- control loop it checks the condition at system, everything is treated as a file, statement checks whether the entered operator. C#include<stdio.h>struct
the variables defined by the function declared.register: This storage class last so, first time the loop will be meaning you can read from and write age is greater than or equal to 18. If this Point{ int x, y};int main(){ struct Point
that receives values when the function declares register variables that have the execute unconditionally.If the condition to it.This means that your printer can condition meet then display message p1 = {0, 1}; // Accessing members of
is called.C functions are used to avoid same functionality as that of the auto for the exit becomes true then loop will be abstracted as a file since all you “You are eligible for voting”, however if point p1 p1.x = 20; printf ("x = %d, y =
rewriting same logic/code again and variables. The only difference is that the be terminate otherwise it will be do…Call by value in CIn call by value the condition doesn’t meet Nested %d", p1.x, p1.y); return 0} IDEC/C++
again in a program. There is no limit in compiler tries to store these variables executed for the next time.C program method, the value of the actual If..else statement When an if else integrated development environments,
calling C functions to make use of same in the register of the microprocessor if to reverse a parameters is copied into the formal statement is present inside the body of or C/C++ IDEs, are software platforms
functionality wherever required. We a free registration is available. This number#include<stdio.h>int main() { parameters. In other words, we can say another “if” or “else” then this is called that provide programmers and
can call functions any number of times makes the use of register variables to int n, reverse = 0, remainder; that the value of the variable is used in nested if else.Syntax of Nested if else developers a comprehensive set of
in a program and from any place in a be much faster than that of the printf("Enter an integer: "); scanf("%d", the function call in the call by value statement:if(condition) { //Nested if tools for software development in a
program.Storage Classes are used to variables stored in the memory during &n); while (n != 0) { remainder = n % method.In call by value method, we can else inside the body of "if" single product, specifically in the C
describe the features of a the runtime of the program. Type 10; reverse = reverse * 10 + not modify the value of the actual if(condition2) { //Statements inside and/or C++ programming languages:
variable/function. These features casting refers to changing an variable remainder; n /= 10; } parameter by the formal parameter.In the body of nested "if else { Elements of
basically include the scope, visibility of one data type into another. The printf("Reversed number = %d", call by value, different memory is //Statements inside the body of nested cTokens.Comments.Keywords.Identifier
and life-time which help us to trace the compiler will automatically change one reverse); return 0;}.A "For" Loop is allocated for actual and formal "else }else { //Statements inside the s.Constants.String literals.Punctuation
existence of a particular variable during type of data into another if it makes used to repeat a specific sblock of code parameters since the value of the body of "else"}Example of nested and special characters. Tokens are the
the runtime of a program.C language sense. For instance, if you assign an a known number of times. For example, actual parameter is copied into the if..else#include <stdio.h>int main(){ int smallest elements of a program, which
uses 4 storage classes, namely:auto: integer value to a floating-point if we want to check the grade of every formal parameter.The actual var1, var2; printf("Input the value of are meaningful to the compiler. The
This is the default storage class for all variable, the compiler will convert the student in the class, we loop from 1 to parameter is the argument which is var1:"); scanf("%d", &var1); following are the types of tokens:
the variables declared inside a function int to a float.logical operatorsIn this that number. When the number of used in the function call whereas printf("Input the value of var2:"); Keywords, Identifiers, Constant, Strings,
or a block. Hence, the keyword auto is program, operators (&&, || and !) are times is not known before hand, we use formal parameter is the argument scanf("%d",&var2); if (var1 != var2) Operators, etc. Keywords are
rarely used while writing programs in C used to perform logical operations on a "While" loop.Unknown number of which is used in the function definition.: {printf("var1 is not equal to predefined, reserved words used in
programming that have special and logical NOT (!).The result of an tabular form (in row-major order). The the address of the variable. For arguments.Register variables tell the
meanings to the compiler. Keywords assignment expression is an lvalue. All total number of elements that can be example, if number is an int variable, compiler to store the variable in CPU
are part of the syntax and they cannot assignment operators have the same stored in a multidimensional array can &number returns the address of the register instead of memory. Frequently
be used as an identifier.Identifier refers precedence and have right-to-left be calculated by multiplying the size of variable number.: A character string is used variables are kept in registers and
to name given to entities such as associativity.: The conditional operator all dimensions.Modularization is a often specified by enclosing the they have faster accessibility. We can
variables, functions, structures etc. is also known as a ternary operator. The method to organize large programs in characters in single or double quotes. never get the addresses of these
Identifiers must be unique. They are conditional statements are the smaller parts, i.e. the modules. Every For example, WASHINGTON would be a variables. “register” keyword is used to
created to give a unique name to an decision-making statements which module has a well defined interface name, but 'WASHINGTON' and declare the register variables.return
entity to identify it during the execution depends upon the output of the toward client modules that specifies “WASHINGTON” would be character statement ends the execution of a
of the program. In C language, a expression. It is represented by two how "services" provided by this module strings. To create a file in a 'C' program function, and returns control to the
number or character or string of symbols, i.e., '?'. I/O refers to the input are made available.C programming following syntax is used, FILE *fp; fp = calling function. Execution resumes in
characters is called a constant. And it - output functions in C language.High language allows coders to define fopen ("file_name", "mode"); In the the calling function at the point
can be any data type. Constants are level I/O Functions.getc ( ) /fgetc() functions to perform special tasks. As above syntax, the file is a data structure immediately following the call. A return
also called as literals. There are two read a character from a functions are defined by users, they are which is defined in the standard library. statement can return a value to the
types of constants − Primary constants file.putw ( )write a number into a called user-defined functions. user- fopen is a standard function which is calling function.Dynamic memory
− Integer, float, and character are called filegetw ( )read number from a filefputs defined functions have contained the used to open a file.Steps for Processing allocation in CThe concept of dynamic
as Primary constants. A variable is ( )write a string into a file.in computer block of statements which are written a FileDeclare a file pointer memory allocation in c language
nothing but a name given to a storage science, control flow is the order in by the user to perform a task: A Multi variable.Open a file using fopen() enables the C programmer to allocate
area that our programs can manipulate. which individual statements, Function Program. A function is a self- function.Process the file using the memory at runtime. Dynamic memory
Each variable in C has a specific type, instructions or function calls of an contained block of code that performs a suitable function.Close the file using allocation in c language is possible by 4
which determines the size and layout of imperative program are executed or particular task. Once a function has fclose() function.The three basic functions of stdlib.h header
the variable's memory; the range of evaluated. The emphasis on explicit been designed and packed, it can be programming constructs sequence is file.malloc()calloc()realloc()free()Before
values that can be stored within that control flow distinguishes an imperative treated as a 'black box' that takes some the order in which instructions occur learning above functions, let's
memory; and the set of operations that programming language from a data from the main program and and are processed.selection determines understand the difference between
can be applied to the variable. Main declarative programming language.An returns a value. The inner details of which path a program takes when it is static memory allocation and dynamic
datatypes. The C language provides the array is defined as the collection of operation are invisible to the rest of the running.iteration is the repeated memory allocation.malloc()allocates
four basic arithmetic type specifiers similar type of data items stored at program: To pass an entire array to a execution of a section of code when a single block of requested
char, int, float and double, and the contiguous memory locations. Arrays function, only the name of the array is program is running.Actual Parameter : memory.calloc()allocates multiple block
modifiers signed, unsigned, short, and are the derived data type in C passed as an argument. result = The variable or expression of requested
long. The following table lists the programming language which can store calculateSum(num); However, notice corresponding to a formal parameter memory.realloc()reallocates the
permissible combinations in specifying the primitive type of data such as int, the use of [] in the function definition. that appears in the function or method memory occupied by malloc() or calloc()
a large set of storage size-specific char, double, float, etc.: In C This informs the compiler that you are call in the calling environment.Formal functions.free() frees the
declarations. A variable declaration is programming, a string is a sequence of passing a one-dimensional array to the parameters are always variables, while dynamically allocated memory.sJump
useful when you are using multiple files characters terminated with a null function.: It is called inside a program actual parameters do not have to be statements in C/C++ are a type of
and you define your variable in one of character \0 . For example: char c[] = "c whenever it is required to call a variables. Parameter Written in Control Statements in C/C++ used to
the files which will be available at the string"; When the compiler encounters function. It is only called by its name in Function Call is Called “Actual interrupt the normal flow of the
time of linking of the program. You will a sequence of characters enclosed in the main() function of a program. We Parameter”. One can use numbers, program. It makes the program jump to
use the keyword extern to declare a the double quotation marks, it appends can pass the parameters to a function expressions, or even function calls as another section of the program
variable at any place. An operator is a a null character \0 at the end by calling in the main() function.: In actual parameters. In above, para1 is unconditionally when encountered. It
symbol that tells the compiler to default.: A one-dimensional array (or programming terminology, a bit field is called the Formal Parameter num1 is can also be used to terminate any loop.
perform specific mathematical or single dimension array) is a type of a data structure that allows the called the Actual Parameter.The call by
logical functions. C language is rich in linear array. Accessing its elements programmer to allocate memory to reference method of passing
built-in operators and provides the involves a single subscript which can structures and unions in bits in order to arguments to a function copies the
following types of operators either represent a row or column utilize computer memory in an efficient address of an argument into the formal
−Arithmetic OperatorsRelational index.A two-dimensional array in C can manner. The syntax of declaring a parameter. Inside the function, the
OperatorsLogical OperatorsBitwise be thought of as a matrix with rows and pointer is to place a * in front of the address is used to access the actual
OperatorsAssignment OperatorsMisc columns. The general syntax used to name. A pointer is associated with a argument used in the call. It means the
Operators. An arithmetic operator declare a two-dimensional array is: A type (such as int and double ) too. changes made to the parameter affect
performs mathematical operations such two-dimensional array is an array of Naming Convention of Pointers: Include the passed argument.ssss method of
as addition, subtraction, multiplication, several one-dimensional arrays. a " p " or " ptr " as prefix or suffix, e.g., passing arguments to a function copies
division etc on numerical values Following is an array with five rows, iPtr , numberPtr , pNumber , pStudent . the actual value of an argument into
(constants and variables).: A relational each row has three columns: int You need to initialize a pointer by the formal parameter of the function.
operator checks the relationship my_array[5][3]; In C/C++, we can assigning it a valid address. This is In this case, changes made to the
between two operands.There are 3 define multidimensional arrays in normally done via the address-of parameter inside the function have no
logical operators in C language. They simple words as an array of arrays. Data operator (&). The address-of operator effect on the argument. By default, C
are, logical AND (&&), logical OR (||) in multidimensional arrays are stored in (&) operates on a variable, and returns programming uses call by value to pass

You might also like