0% found this document useful (0 votes)
17 views12 pages

Unit-1- Programming in C

C basics data types, operators etc

Uploaded by

swarna
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
17 views12 pages

Unit-1- Programming in C

C basics data types, operators etc

Uploaded by

swarna
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 12

C Programming Basics

1. C Character Set

The C language supports the following character set:

 Letters: A-Z, a-z


 Digits: 0-9
 Special Characters: ~ ! @ # $ % ^ & * ( ) _ + { } | : " < > ? , . / ; ' \ [ ]
 Whitespace Characters: space, horizontal tab, newline, carriage return,
form feed
 Other Characters: Escape sequences like \n, \t, \\, etc.

2. Identifiers

Identifiers are names used to identify variables, functions, arrays, etc. They must
follow these rules:

 Must begin with a letter (A-Z or a-z) or an underscore (_).


 Followed by letters, digits (0-9), or underscores.
 Cannot be a C keyword.
 Case-sensitive.
 No length limit, but only the first 31 characters are typically significant.

Examples:

 Valid: variable1, _functionName, age


 Invalid: 1variable, float*, if

3. Keywords

Keywords are reserved words in C with predefined meanings. Examples include:

 Data Types: int, float, double, char


 Control Statements: if, else, switch, for, while, do
 Modifiers: long, short, signed, unsigned
 Others: return, void, static, extern
4. Data Types

C provides several data types for defining variables:

Data Type Description Example


int Integer numbers int age = 25;
float Floating-point numbers float pi = 3.14;
double Double-precision floating-point double x = 5.12345;
char Single character char ch = 'A';
void No value void function()
short, long Modifiers for integer types long distance;

5. Operators

C provides a variety of operators:

 Arithmetic Operators: +, -, *, /, %
 Relational Operators: ==, !=, <, >, <=, >=
 Logical Operators: &&, ||, !
 Bitwise Operators: &, |, ^, ~, <<, >>
 Assignment Operators: =, +=, -=, *=, /=, %=
 Increment/Decrement Operators: ++, --
 Conditional (Ternary) Operator: ? :
 Special Operators: sizeof, &, * (pointer), ->, .

Operators in C allow manipulation of variables and data values. Here's a


breakdown with examples:

1. Arithmetic Operators

Used for mathematical calculations:

 + (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulus).

Example:

int a = 10, b = 3;
printf("Addition: %d\n", a + b); // Output: 13
printf("Modulus: %d\n", a % b); // Output: 1

2. Relational Operators

Used to compare values:

 ==, !=, <, >, <=, >=.

Example:

int x = 5, y = 10;
printf("%d\n", x < y); // Output: 1 (True)
printf("%d\n", x == y); // Output: 0 (False)

3. Logical Operators

Used for logical operations:

 && (AND), || (OR), ! (NOT).

Example:

int a = 1, b = 0;
printf("%d\n", a && b); // Output: 0 (False)
printf("%d\n", !a); // Output: 0 (False)

4. Bitwise Operators

Operate on bits:

 & (AND), | (OR), ^ (XOR), ~ (NOT), << (Left Shift), >> (Right Shift).

Example:

int num = 5; // Binary: 0101


printf("%d\n", num << 1); // Output: 10 (Binary: 1010)

5. Assignment Operators

Assign values:

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


Example:

int a = 10;
a += 5; // Equivalent to a = a + 5
printf("%d\n", a); // Output: 15

6. Increment/Decrement Operators

Modify a variable by 1:

 ++ (Increment), -- (Decrement).

Example:

int x = 5;
printf("%d\n", ++x); // Output: 6 (Pre-increment)
printf("%d\n", x--); // Output: 6 (Post-decrement)

7. Conditional (Ternary) Operator

Shorthand for if-else: condition ? true_value : false_value.

Example:

int a = 10, b = 20;


int max = (a > b) ? a : b;
printf("Max: %d\n", max); // Output: 20

8. Special Operators

 sizeof: Returns size of a variable/type.


 &: Address-of operator.
 *: Dereference operator for pointers.
 ->: Access member of structure via pointer.
 .: Access member of structure.

Example:

int x = 5;
printf("Size of x: %lu\n", sizeof(x)); // Output: 4 (size in bytes on most systems)

These examples cover the practical use of each operator type.


6. Input and Output Functions

 Input Functions:

The scanf function is used to read formatted input from the standard input
(typically the keyboard). It allows the user to input values into variables during
program execution.

int num;

scanf("%d", &num);

 %d specifies that the input should be an integer.

 &num ensures that the value entered is stored in the variable num.

When executed, the program will wait for the user to type an integer, which will
then be assigned to num.

Syntax:

scanf("format_specifier", &variable);

 format_specifier: Indicates the type of input (e.g., %d for integers, %f for


floats, %c for characters).
 &variable: The address-of operator (&) passes the memory address of the
variable where the input value will be stored.


o scanf: Reads formatted input.

int x;

scanf("%d", &x);

printf: Prints formatted output.


The printf function is used in C for outputting formatted text to the standard output
(usually the screen). It allows the programmer to include variables or expressions
within a string using format specifiers, which determine how the values are
displayed. For example:

int num = 5;

printf("The value is %d", num);

In this case, %d is a placeholder for an integer value, and the output will be:

The value is 5

Other common format specifiers include %f for floats, %c for characters, and %s
for strings. The printf function is highly versatile, enabling detailed control over
output formatting.

printf("Value is %d", x);

Common format specifiers:

 %d for integers
 %f for floats
 %c for characters
 %s for strings

. Preprocessor Directives

Preprocessor Directives are commands in C that are executed by the preprocessor


before the program is compiled. These directives allow you to include files, define
constants, and conditionally compile parts of your code. They all start with a #
symbol.

Types of Preprocessor Directives:

1. Include Header Files:


o Used to include standard or user-defined libraries in the program.
o Example:

#include<stdio.h> // Includes standard input-output library


#include<math.h> // Includes math functions
2. Define Macros:
o Allows defining constants or reusable code snippets.
o Example:

#define PI 3.14 // PI is replaced by 3.14 wherever it appears


#define MAX(a, b) ((a) > (b) ? (a) : (b)) // Macro for finding max

3. Conditional Compilation:
o Enables or disables certain parts of the code during compilation based
on conditions.
o Example:

#ifdef DEBUG
printf("Debugging Mode\n"); // Only included if DEBUG is defined
#endif

Preprocessor directives do not produce any executable code; they are instructions
to the compiler to prepare the source code before actual compilation. This makes
them a powerful tool for modular, maintainable, and conditional programming.

Preprocessor directives are commands that are processed before the compilation of
the program. They begin with #.

 Include Header Files:


 #include<stdio.h>
#include<math.h>

 Define Macros:

#define PI 3.14

 Conditional Compilation:
 #ifdef DEBUG
 printf("Debugging Mode\n");
#endif

8. Storage Classes
Storage in C programming refers to the way variables are allocated, managed, and
maintained in memory. The storage class of a variable defines several key
properties:

1. Scope: Where the variable can be accessed within the program.


2. Lifetime: How long the variable's value persists during program execution.
3. Visibility: Whether the variable is accessible outside its defining file or
function.

C provides four primary storage classes:

 auto: Automatically allocated for local variables (default for variables


declared inside functions).
 register: Suggests storing the variable in a CPU register for faster access
(used for frequently accessed variables).
 static: Retains the variable's value across multiple function calls (persists for
the program's lifetime).
 extern: Declares a variable that is defined in another file or scope (used for
global variables).

Each storage class serves different purposes, allowing efficient memory and
performance management in programs.

Storage classes define the scope, lifetime, and visibility of a variable.

 auto: Default for local variables.

auto int x = 10;


printf("x = %d\n", x);

 register: Stores variable in CPU register for faster access.

register int counter;


for (counter = 0; counter < 10; counter++) {
printf("Counter = %d\n", counter);
}

 static: Retains variable value between function calls.


void increment() {

static int count = 0;

count++;

printf("Count = %d\n", count);

int main() {

increment();

increment();

increment();

return 0;

 extern: Declares a global variable or function.

extern int globalVar;


void display() {
printf("GlobalVar = %d\n", globalVar);
}
int globalVar = 100;
int main() {
display();
return 0;
}

Summary

This document introduces the fundamental concepts of C programming, including


character sets, identifiers, keywords, data types, operators, input/output functions,
preprocessor directives, and storage classes. Mastering these basics forms the
foundation for further programming in C.
1. Character Sets

 Definition: Character sets in C programming refer to the set of characters that can be
used to write the programs. This includes letters, digits, symbols, and control characters.
 Examples: A-Z, a-z, 0-9, special characters like @, #, $, etc.

2. Identifiers

 Definition: Identifiers are names given to various program elements such as variables,
functions, arrays, etc.
 Rules: Must begin with a letter or an underscore (_) followed by letters, digits, or
underscores. They are case-sensitive.
 Examples: main, total_sum, _index

3. Keywords

 Definition: Keywords are reserved words in C that have special meaning and cannot be
used as identifiers.
 Examples: int, return, if, else, while, for, break, continue

4. Data Types

 Definition: Data types specify the type of data that a variable can hold.
 Categories:
o Basic Data Types: int, char, float, double
o Derived Data Types: Arrays, Pointers, Structures, Unions
o Enumeration Data Type: enum
o Void Data Type: void

5. Operators

 Definition: Operators are symbols that perform operations on variables and values.
 Types:
o Arithmetic Operators: +, -, *, /, %
o Relational Operators: ==, !=, >, <, >=, <=
o Logical Operators: &&, ||, !
o Bitwise Operators: &, |, ^, ~, <<, >>
o Assignment Operators: =, +=, -=, *=, /=, %=
o Increment/Decrement Operators: ++, --
o Conditional (Ternary) Operator: ?:

6. Input/Output Functions

 Definition: Input/Output functions are used to get input from the user and display output
to the user.
 Functions:
o Input Functions: scanf(), getchar(), gets()
o Output Functions: printf(), putchar(), puts()

7. Preprocessor Directives

 Definition: Preprocessor directives are commands that are processed before the actual
compilation of code begins.
 Examples: #include, #define, #if, #else, #endif, #ifdef, #ifndef

8. Storage Classes

 Definition: Storage classes define the scope, visibility, and lifetime of variables/functions
within a C program.
 Types:
o Automatic (auto): Default storage class for local variables.
o Register (register): Suggests that the variable be stored in a CPU register.
o Static (static): Preserves the value of a variable across function calls.
o External (extern): Allows variables/functions to be accessed across multiple
files.

You might also like