Elements of Programming Languages, Typical Examples

By Mona Kumari|Updated : July 1st, 2021

 All C programs must have a function in it called main

(a)  Execution starts in function main

(b)  C is case sensitive

(c)  Comments start with /* and end with */.  Comments may span over many lines.

(d)  C is a “free format” language

(e)  All C statements must end in a semicolon (;).

(f)  The #include <stdio.h> instruction instructs the C compiler to insert the entire contents of file stdio.h in its place and comthe resulting file.

All C programs must have a function in it called main

(a)  Execution starts in function main

(b)  C is case sensitive

(c)  Comments start with /* and end with */.  Comments may span over many lines.

(d)  C is a “free format” language

(e)  All C statements must end in a semicolon (;).

(f)  The #include <stdio.h> instruction instructs the C compiler to insert the entire contents of file stdio.h in its place and comthe resulting file.

1. Printf() FUNCTION IN C LANGUAGE

In the C programming language, printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen.

Data Type

Format Specifier

int

%d

char

%c

float

%f

double

%lf

unsigned int

%u

long int

%li

long long int

%lli

unsigned long int

%lu

unsigned long long int

%llu

signed char

%c

unsigned char

%c

long double

%lf

Here's a list of commonly used C data types and their format specifiers.

Example:

Example Program for printf in C

#include <stdio.h>

int main()

{

   char ch = 'A';

   char str[20] = "gradeup2u.co ";

   float flt = 10.234;

   int no = 150;

   double dbl = 20.123456;

   printf("Character is %c \n", ch);

   printf("String is %s \n" , str);

   printf("Float value is %f \n", flt);

   printf("Integer value is %d\n" , no);

   printf("Double value is %lf \n", dbl);

   printf("Double value upto 2 decimal is %2lf \n", dbl);

   printf("Octal value is %o \n", no);

   printf("Hexadecimal value is %x \n", no);

   return 0;

}

Output-

Character is A
String is gradeup2u.co
Float value is 10.234000
Integer value is 150
Double value is 20.123456

Double value upto 2 decimal is 20.12
Octal value is 226
Hexadecimal value is 96

2. SCANF() FUNCTION IN C LANGUAGE

▪ In the C programming language, scanf() function is used to read character, string, numeric data from the keyboard.

▪ Consider the example program where a user enters a character. This value is assigned to the variable “ch” and then displayed.

▪ Then, user enters a string and this value is assigned to the variable “str” and then displayed.

Example-      

          #include <stdio.h>

          int main()

          {

                    char ch;

                   char str[100];

                    printf("Enter any character \n");

                    scanf("%c", &ch);

                    printf("Entered character is %c \n", ch);

                    printf("Enter any string ( upto 100 character ) \n");

                   scanf("%s", &str);

                    printf("Entered string is %s \n", str);

          }

Output-

          Enter any character

          a

          Entered character is a

          Enter any string ( upto 100 character )

          hai

          Entered string is hai

C – DATA TYPES

There are four data types in C language. They are

Types

Data Types

Basic data types

Int, char, float, double

Enumeration data type

enum

Derived data type

Pointer, array, structure, union

Void data type

void

3.1.   Integer type

Integers are used to store whole numbers.

Type

Size(bytes)

Range

int or signed int

2

-32,768 to 32767

unsigned int

2

0 to 65535

short int or signed short int

1

-128 to 127

unsigned short int

1

0 to 255

long int or signed long int

4

-2,147,483,648 to 2,147,483,647

unsigned long int

4

0 to 4,294,967,295

 3.2.   Floating point type

Floating types are used to store real numbers.

Size and range of Integer type on 16-bit machine

Type

Size(bytes)

Range

Float

4

3.4E-38 to 3.4E+38

double

8

1.7E-308 to 1.7E+308

long double

10

3.4E-4932 to 1.1E+4932

3.3.   Character type

Character types are used to store character value.

Size and range of Integer type on 16-bit machine

Type

Size(bytes)

Range

char or signed char

1

-128 to 127

unsigned char

1

0 to 255

 void type

void type means no value. This is usually used to specify the type of functions which returns nothing. We will get acquainted to this datatype as we start learning more advanced topics in C language, like functions, pointers etc.

C TOKENS

The smallest individual units are known as C tokens. C has six types of tokens: Keywords, Identifiers, Constants, Operators, Delimiters / Separators and Special symbols.

4.1.  Keywords: All keywords are basically the sequences of characters that have one or more fixed meanings.

∙     Keywords are predefined words in a C compiler.

∙     Each keyword is meant to perform a specific function in a C program.

∙     Since keywords are referred names for compilers, they can’t be used as variable name.

 

4.2.  Identifiers: A C identifier is a name used to identify a variable, function, or any other user-defined item.

RULES FOR CONSTRUCTING IDENTIFIER NAME IN C:

∙     First character should be an alphabet or underscore.

∙     Succeeding characters might be digits or letters.

∙     Punctuation and special characters aren’t allowed except underscore.

∙     Identifiers should not be keywords.

4.3.  Constants: Fixed values that do not change during the execution of a C program.

Example: 100 is integer constant, 'a' is character constant, etc.

4.4.  Operators: Operator is a symbol that tells a computer to perform certain mathematical or logical manipulations.

       Example: Arithmetic operators (+, -, *, /), Logical operators, Bitwise operators, etc.

4.5.  Delimiters / Separators: These are used to separate constants, variables and statements.

       Example: comma, semicolon, apostrophes, double quotes, blank space etc.

4.6.  Strings: String constants are specified in double quotes.

                  Example: "gateexam" is string constant.

5. TYPES OF OPERATORS:-

byjusexamprep

Operators

Symbols

Arithmetic operators

+, -, *, /, %, ++, --

Assignment operator

=, +=, -=, *=, etc

Relational operators

<, <=, >, >=, !=, ==

Logical operators

&&, ||, !

Bitwise operators

&, |, ~, ^, <<, >>

Special operators

sizeof(), comma, 🡪

Pointer operators

* - Value at address (indirection), & - Address Operator

  1. OPERATOR PRECEDENCE RELATIONS

Operator precedence relations are given below from highest to lowest order:

Precedence

Operator

Description

Associativity

1

++ --

Suffix/postfix increment and decrement

Left-to-right

()

Function call

[]

Array subscripting

.

Structure and union member access

->

Structure and union member access through pointer

(type){list}

Compound literal(C99)

2

++ --

Prefix increment and decrement

Right-to-left

+ -

Unary plus and minus

! ~

Logical NOT and bitwise NOT

(type)

Type cast

*

Indirection (dereference)

&

Address-of

sizeof

Size-of

_Alignof

Alignment requirement(C11)

3

* / %

Multiplication, division, and remainder

Left-to-right

4

+ -

Addition and subtraction

5

<< >>

Bitwise left shift and right shift

6

< <=

For relational operators < and ≤ respectively

> >=

For relational operators > and ≥ respectively

7

== !=

For relational = and ≠ respectively

8

&

Bitwise AND

9

^

Bitwise XOR (exclusive or)

10

|

Bitwise OR (inclusive or)

11

&&

Logical AND

12

||

Logical OR

13

?:

Ternary conditional

Right-to-Left

14

=

Simple assignment

+= -=

Assignment by sum and difference

*= /= %=

Assignment by product, quotient, and remainder

<<= >>=

Assignment by bitwise left shift and right shift

&= ^= |=

Assignment by bitwise AND, XOR, and OR

15

,

Comma

Left-to-right

  1. C VARIABLE

∙     C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable.

∙     The value of the C variable may get change in the program.

∙     C variables might be belonging to any of the data types like int, float, char etc.

7.1.   DECLARING & INITIALIZING C VARIABLE:

∙     Variables should be declared in the C program before use.

∙     Memory space is not allocated for a variable while declaration. It happens only on variable definition.

∙     Variable initialization means assigning a value to the variable.

Type

Syntax

Variable declaration

data_type variable_name;
Example: int x, y, z; char flat, ch;

Variable initialization

data_type variable_name = value;

Example: int x = 50, y = 30; char flag = ‘x’, ch=’l’;

 THERE ARE THREE TYPES OF VARIABLES IN C PROGRAM THEY ARE,

  1. Local variable
  2. Global variable
  3. Environment variable

7.1.1 LOCAL VARIABLE IN C:

∙   The scope of local variables will be within the function only.

∙   These variables are declared within the function and can’t be accessed outside the function.

7.1.2 GLOBAL VARIABLE IN C:

∙   The scope of global variables will be throughout the program. These variables can be accessed from anywhere in the program.

∙   This variable is defined outside the main function. So, this variable is visible to the main function and all other sub functions.

7.1.3 ENVIRONMENT VARIABLES IN C:

∙   Environment variable is a variable that will be available for all C  applications and C programs.

∙   We can access these variables from anywhere in a C program without declaring and initializing in an application or C program.

∙   The inbuilt functions which are used to access, modify and set these environment variables are called environment functions.

  1. TYPE CONVERSIONS

8.1.  Implicit Type Conversion: There are certain cases in which data will get automatically converted from one type to another:

8.1.1. When data is being stored in a variable, if the data being stored does not match the type of the variable.

8.1.2. The data being stored will be converted to match the type of the storage variable.

8.1.3. When an operation is being performed on data of two different types. The "smaller" data type will be converted to match the "larger" type.

∙     The following example converts the value of  x to a double precision value before performing the division. Note that if the 3.0 were changed to a simple 3, then integer division would be performed, losing any fractional values in the result.

∙     average = x / 3.0;

8.1.4. When data is passed to or returned from functions.

8.2.  Explicit Type Conversion: Data may also be expressly converted, using the typecast operator. The following example converts the value of x to a double precision value before performing the division. ( y will then be implicitly promoted, following the guidelines listed above. )

8.2.1. average = ( double ) x / y;

8.2.2. Note that x itself is unaffected by this conversion.

  1. C FLOW CONTROL STATEMENTS 

Control statement is one of the instructions, statements or group of statement in a programming language which determines the sequence of execution of other instructions or statements. C provides two styles of flow controls.

  1. Branching (conditional) (deciding what action to take)
  2. Looping(iterative) (deciding how many times to take a certain action)
  3. Unconditional

9.1.   If Statement:

It takes an expression in parenthesis and a statement or block of statements. Expressions will be assumed to be true, if evaluated values are non-zero.

Syntax of if Statement:

(i)

if (condition) statement

(ii)

if (condition)

{

statement1

statement2

:

}

9.1.1. If else statement

if (condition)

{statements}

else

{statements}

9.2.   The switch Statement:

The switch statement tests the value of a given variable (or expression) against a list of case values and when a match is found, a block of statements associated with that case is executed:

switch (control variable)

{

case constant-1: statement(s); 

    break;

case constant-2: statement(s);

     break;

   :

case constant-n: statement(s); 

                                      break;

                                      default: statement(s);

     }

Note: Here constant can be either character or integer

9.3.   The Conditional Operators (?:):

The ? : operators are just like an if-else statement except that because it is an operator we can use it within expressions. ? : are ternary operators in that it takes three values. They are the only ternary operator in C language.         

                 Syntax🡪 Boolean Expression ? First Statement : Second Statement

flag = (x < 0) ? 0 : 1;

This conditional statement can be evaluated as the following with an equivalent if else statement.

 if (x < 0) flag = 0;

 else flag = 1;

                   Example-  int a=100, b=200,c=300,x;

                              x = (a>b) ? ((a>c)? a : c) : ((b>c) ? b : c);

9.4.   Loop Control Structure : 

Loops provide a way to repeat commands and control. This involves repeating some portion of the program either a specified number of times until a particular condition is satisfied.

9.4.1. while Loop:

The syntax of the while loop is:

while (testExpression)

{

    // statements inside the body of the loop

}

  • Variable initialization. (e.g int x = 0;)
  • Condition (e.g while(x <= 10))
  • Variable increment or decrement  ( x++ or x-- or x = x + 2 )
  • (a) Entry Controlled Loop

(b) A while loop in C programming repeatedly executes a target statement as long as a given condition is true.

9.4.2. do while Loop:

The syntax of the do...while loop is:

do

{

   // statements inside the body of the loop

}

while (testExpression);

(a) Exit Controlled Loop

(b) The do while loops runs at least once in any condition(even if the loops control condition is false)

9.4.3. for Loop:

The syntax of the for loop is:

for (initializationStatement; testExpression; updateStatement)

{

    // statements inside the body of loop

}

Example-

#include <stdio.h>

int main()

{

    int num=10, count, sum = 0;

    printf("Enter a positive integer: ");

    scanf("%d", &num);

    // for loop terminates when num is less than count

    for(count = 1; count <= num; ++count)

    {

        sum += count;

    }

 

    printf("Sum = %d", sum);

    return 0;

}                 

                   Output- 55

9.5.   Unconditional Flow Control Statements

(break, continue, goto, return)

9.5.1. The break Statement: The break statement is used to jump out of a loop instantly, without waiting to get back to the conditional test.

byjusexamprep

9.5.2. The continue Statement: The 'continue' statement is used to take the control to the beginning of the loop, by passing the statement inside the loop, which has not yet been executed.

byjusexamprep

9.5.3. goto Statement: C supports an unconditional control statement, goto, to transfer the control from one point to another in a C program.

Below is the syntax for goto statements in C.

{

         …….

         go to label;

         …….

         …….

         LABEL:

         statements;

}

 

Candidates can practice 150+Mock Tests with BYJU'S Exam Prep Test Series for exams like GATE, ESE, NIELIT from the following link:

Click Here to Avail Electronics Engineering Test Series (150+ Mock Tests)

Get unlimited access to 24+ structured Live Courses all 150+ mock tests to boost your GATE 2021 Preparation with Online Classroom Program:

Click here to avail Online Classroom Program for Electronics Engineering

Thanks

Sahi Prep Hai To Life Set Hai.

 
Download BYJU'S Exam Prep, Best gate exam app for Preparation

Comments

write a comment

Follow us for latest updates