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

C Language: Step 1: Creating A Source Code

C was created in 1972 by Dennis Ritchie. A C program runs by first being compiled from source code into object code, then linked together with libraries to create an executable file. The executable file is then run on a CPU which executes the instructions to produce output. C supports various data types like integers, characters, and floating point numbers. Variables are used to store and name data in memory and operators are used to perform operations on variables.

Uploaded by

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

C Language: Step 1: Creating A Source Code

C was created in 1972 by Dennis Ritchie. A C program runs by first being compiled from source code into object code, then linked together with libraries to create an executable file. The executable file is then run on a CPU which executes the instructions to produce output. C supports various data types like integers, characters, and floating point numbers. Variables are used to store and name data in memory and operators are used to perform operations on variables.

Uploaded by

koniki sireesha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

C LANGUAGE

C was developed and written by Dennis M. Ritchie in the year 1972, and hence he
is known as the founder of C.

Running a c program :

The computer cannot understand the high-level language. It can understand only
low-level language. So, the program written in the high-level language needs to be
converted into the low-level language to make it understandable for the computer.
This conversion is performed using either Interpreter or Compiler.

Step 1: Creating a Source Code


 Type the program
 Save it as FileName.c

Step 2: Compile Source Code


The source file is going to be submitted to the Compiler. On receiving a source
file, the compiler first checks for the Errors. If there are any Errors then compiler
returns List of Errors, if there are no errors then the source code is converted
into object code and stores it as a file with .obj extension. Then the object code is
given to the Linker. The Linker combines both the object code and
specified header file code and generates an Executable file with a .exe extension.

Step 3: Executing / Running Executable File 


The .exe file is submitted to the CPU. On receiving .exe file, CPU performs the
task according to the instruction written in the file. The result generated from
the execution is placed in a window called User Screen.
Important Points

 C program file (Source file) must save with .c extension.


 The compiler converts complete program at a time from high-level language
to low-level language.
 Input to the compiler is .c file and output from the compiler is .exe file, but it
also generates .obj file in this process.
 The compiler converts the file only if there are no errors in the source code.
 CPU places the result in User Screen window.

DATA TYPE: The data-type in a programming language is the collection of data


with values having fixed meaning as well as characteristics.

Some of them are an integer, floating point, character, etc. Usually, programming
languages specify the range values for given data-type.

1. Primitive (Built-in) Data Types:


void, int, char, double and float.
2. Derived Data Types:
Array,  and Pointers.
3. Programmer Defined Data Types:
Structure, Union, and Enumeration.
Void As the name suggests, it holds no value and is generally used for specifying the type of
function or what it returns. If the function has a void type, it means that the function will
not return any value.
Int Used to denote an integer type.

Char Used to denote a character type.

float, double Used to denote a floating point type.

VARIABLE: Variable is used to name the memory location to store the data.

Variable declaration:

Syntax: data-type variable_name;

Example:
int width, height=5;
char letter='A';
float age, area;
double d;

/* actual initialization */width = 10;


age = 26.5;

variable rules:
 A variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-
9, and the underscore character.
 The first character must be a letter or underscore.
 Blank spaces cannot be used in variable names.
 Special characters like #, $ are not allowed.
 C keywords cannot be used as variable names.
 Variable names are case sensitive.
 Values of the variables can be numeric or alphabetic.
 Variable type can be char, int, float, double, or void.

Example:
#include<stdio.h>

void main()
{
/* c program to print value of a variable */ int age = 33;
printf("I am %d years old.\n", age);
}

Operators: Operators are the symbols or few words. Operators can be used to
operate on the data.

1.Arithematic Operators: + addition

- sub

* mul

/ div (Quotient)

% modulus (remainder)

2.Assignment Operator: = assigns left side value to right side variable.

Ex:x=10;//x is variable 10 is value

3.Compund Assignment Operators: Assignment operator can be used with other


operators .+=.-=,*=,/=.%=

Ex:x=10

X+=4(x=x+410+4)

X=14

2.a=5

a-=2(a=a-25-2=3)

4.Relational Operators: Relational operators are used to compare the values.


Relational operators are used to check the condition.

>,<,<=,>=,==,!=.
5.Logical Operators: logical operators can be used to check the multiple conditions
at a time.they are 3 types

1.Logical AND (&&) =returs 1 whenever all conditions are true return 0 atleat one
condition if false.

2.Logical OR (||)=returns 1 if atleast 1 condition is true.

3.Logical NOT(!)=inverse.

OPERATORS

Operators are the symbols or few words, operators can be used to operate on the
data.

They are of 8 types:

1.Arithematic Operators:

+,-,*,/,%

Operator Description Example

+ Adds two operands. A + B = 30

− Subtracts second operand from the first. A − B = -10

* Multiplies both operands. A * B = 200

/ Divides numerator by de-numerator. B/A=2

% Modulus Operator and remainder of after an integer B%A=0


division.
Program:
#include <stdio.h>
void main()
{
int i=3,j=7,k; /* Variables Defining and Assign values */
k=i+j;
printf("sum of two numbers is %d\n", k);
}

Sum of two numbers is 10

2.Assignment Operators:

Compound assignment.

Operator Description Example

= Simple assignment operator. Assigns values from right side operands to C=A+B
left side operand will assign
the value
of A + B
to C

+= Add AND assignment operator. It adds the right operand to the left C += A is
operand and assign the result to the left operand. equivalent
to C = C +
A

-= Subtract AND assignment operator. It subtracts the right operand from the C -= A is
left operand and assigns the result to the left operand. equivalent
to C = C -
A

*= Multiply AND assignment operator. It multiplies the right operand with the C *= A is
equivalent
left operand and assigns the result to the left operand. to C = C *
A

/= Divide AND assignment operator. It divides the left operand with the right C /= A is
operand and assigns the result to the left operand. equivalent
to C = C /
A

%= Modulus AND assignment operator. It takes modulus using two operands C %= A is


and assigns the result to the left operand. equivalent
to C = C
%A

<<= Left shift AND assignment operator. C <<= 2


is same
as C = C
<< 2

>>= Right shift AND assignment operator. C >>= 2


is same
as C = C
>> 2

&= Bitwise AND assignment operator. C &= 2 is


same as
C=C&2

^= Bitwise exclusive OR and assignment operator. C ^= 2 is


same as
C=C^2

|= Bitwise inclusive OR and assignment operator. C |= 2 is


same as
C=C|2

3.Relational Operators:
The following table shows all the relational operators supported by C. Assume
variable A holds 10 and variable B holds 20 then −
Show Examples

Operato Description Example


r

== Checks if the values of two operands are equal or not. If yes, then the condition (A == B)
becomes true. is not
true.

!= Checks if the values of two operands are equal or not. If the values are not equal, (A != B)
then the condition becomes true. is true.

> Checks if the value of left operand is greater than the value of right operand. If yes, (A > B)
then the condition becomes true. is not
true.

< Checks if the value of left operand is less than the value of right operand. If yes, (A < B)
then the condition becomes true. is true.

>= Checks if the value of left operand is greater than or equal to the value of right (A >= B)
operand. If yes, then the condition becomes true. is not
true.

<= Checks if the value of left operand is less than or equal to the value of right (A <= B)
operand. If yes, then the condition becomes true. is true.
4.Logical operators:
A holds 1 and variable B holds 0, then −
Show Examples

Operato Description Example


r

&& Called Logical AND operator. If both the operands are non-zero, then the condition (A && B)
becomes true. is false.

|| Called Logical OR Operator. If any of the two operands is non-zero, then the (A || B) is
condition becomes true. true.

! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If !(A &&
a condition is true, then Logical NOT operator will make it false. B) is
true.

5.Bitwise Operators:
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |,
and ^ is as follows −

P q p&q p|q p^q

0 0 0 0 0

0 1 0 1 1

1 1 1 1 0
1 0 0 1 1

Assume A = 60 and B = 13 in binary format, they will be as follows −

A = 0011 1100=60
B = 0000 1101=13
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The following table lists the bitwise operators supported by C. Assume variable 'A'
holds 60 and variable 'B' holds 13, then −
Show Examples

Operato Description Example


r

& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) =
12, i.e.,
0000
1100

| Binary OR Operator copies a bit if it exists in either operand. (A | B) =


61, i.e.,
0011
1101

^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) =
49, i.e.,
0011
0001

~ (~A ) =
~(60),
Binary One's Complement Operator is unary and has the effect of 'flipping' bits.
i.e,. -
0111101

<< Binary Left Shift Operator. The left operands value is moved left by the number of A << 2 =
bits specified by the right operand. 240 i.e.,
1111
0000

>> Binary Right Shift Operator. The left operands value is moved right by the number A >> 2 =
of bits specified by the right operand. 15 i.e.,
0000
1111

6.Conditional Operator:

C offers a ternary operator which is the conditional operator (?: in combination) to


construct conditional expressions.
Operato Description
r

? :;  Conditional Expression

7.Special Operator:

Sizeof: Returns the size of a memory location.

8.Unary Operators:

++ Increment -- Decrement

#include <stdio.h>
//stdio.h is a header file used for input.output purpose.
void main()
{
//set a and b both equal to 5.
int a=5, b=5;
printf("\n%d %d",a--,b--);
printf("\n%d %d",--a,--b);
printf("\n%d %d",a++,b++);
printf("\n%d %d",++a,++b);
}

Conditional statements
Conditional Statements in C programming are used to make decisions based on
the conditions.

1. If statement
2. If-Else statement
3. Nested If-else statement
4. If-Else If ladder
5. Switch statement

if statement:

The statements inside the body of “if” only execute if the given condition returns
true. If the condition returns false then the statements inside “if” are skipped.

Syntax:
if (condition)
{
//Block of C statements here
//These statements will only execute if the condition is true
}

Program:
#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
return 0;
}

if else:

If condition returns true then the statements inside the body of “if” are executed
and the statements inside body of “else” are skipped.

If condition returns false then the statements inside the body of “if” are skipped
and the statements in “else” are executed.

Syntax:
if(condition) {
// Statements inside body of if
}
else {
//Statements inside body of else
}

Program:
#include <stdio.h>
int main()
{
int age;
printf("Enter your age:");
scanf("%d",&age);
if(age >=18)
{
/* This statement will only execute if the
* above condition (age>=18) returns true
*/
printf("You are eligible for voting");
}
else
{
/* This statement will only execute if the
* condition specified in the "if" returns false.
*/
printf("You are not eligible for voting");
}
return 0;
}

You might also like