Introduction to C Programming

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Why was C adopted as a system development language?

  • It is easier to learn than other languages.
  • It was the only language available at the time.
  • It produces code that runs nearly as fast as code written in assembly language. (correct)
  • It automatically optimizes code for different hardware platforms.

Which of the following is NOT a component of a basic C program?

  • Comments
  • Classes (correct)
  • Preprocessor Commands
  • Functions

What is the role of the stdio.h file in a C program?

  • It defines the syntax rules for the C language.
  • It is a preprocessor command that includes standard input/output functions. (correct)
  • It contains the definitions for all mathematical functions.
  • It is the main file where the program's execution begins.

What is the purpose of comments in a C program?

<p>To add helping text that the compiler ignores, used for documenting the code. (B)</p> Signup and view all the answers

What is a token in C programming?

<p>The smallest individual unit in a program, such as keywords, identifiers, constants, or symbols. (C)</p> Signup and view all the answers

What is the significance of a semicolon in C programming?

<p>It indicates the end of a logical entity or statement. (A)</p> Signup and view all the answers

Which of the following is NOT a valid data type in C?

<p>string (C)</p> Signup and view all the answers

What is the purpose of the #include directive in C?

<p>To include a header file containing declarations of functions and variables. (D)</p> Signup and view all the answers

What does the term 'whitespace' refer to in C programming?

<p>Blank lines, tabs, newline characters, and comments that separate parts of a statement. (A)</p> Signup and view all the answers

Which of the following is a valid way to define a constant in C?

<p>Both B and C (A)</p> Signup and view all the answers

What is type casting in C?

<p>Converting a variable from one data type to another. (D)</p> Signup and view all the answers

Given the statement int x = sizeof(float);, what will be the value of x on a typical 32-bit system?

<p>4 (A)</p> Signup and view all the answers

What is the role of the return 0; statement in the main() function?

<p>It returns a value to the operating system, where 0 typically signifies successful execution. (D)</p> Signup and view all the answers

In C, what is the difference between a == b and a = b?

<p><code>a == b</code> compares <code>a</code> and <code>b</code> for equality, while <code>a = b</code> assigns the value of <code>b</code> to <code>a</code>. (A)</p> Signup and view all the answers

Which of the following is a valid identifier in C?

<p>variableName (A)</p> Signup and view all the answers

What is the output of the following code snippet?

int a = 10;
printf("%d", a++);

<p>10 (C)</p> Signup and view all the answers

What is the purpose of the extern keyword in C?

<p>To declare a variable that is defined in another file. (D)</p> Signup and view all the answers

What does the term 'string literal' refer to in C?

<p>A sequence of characters enclosed in double quotes. (D)</p> Signup and view all the answers

If int num = 10;, what will be the output of printf("%d", !num);?

<p>0 (A)</p> Signup and view all the answers

Which operator is used to determine the address of a variable?

<p>&amp; (D)</p> Signup and view all the answers

Which of the following is a bitwise operator in C?

<p>&amp; (D)</p> Signup and view all the answers

What is the difference between puts() and printf() in C?

<p><code>printf()</code> can print formatted output, while <code>puts()</code> simply prints a string followed by a newline. (A)</p> Signup and view all the answers

What is the purpose of getchar() function in C?

<p>To read a character from the console. (B)</p> Signup and view all the answers

Which of the following is the correct way to declare a character variable ch and initialize it with the character 'A' in C?

<p>char ch = 'A'; (C)</p> Signup and view all the answers

What is operator precedence in C?

<p>The order in which operators are evaluated in an expression. (C)</p> Signup and view all the answers

In C, what is the difference between #include <file.h> and #include "file.h"?

<p><code>#include &lt;file.h&gt;</code> is for system header files, while <code>#include &quot;file.h&quot;</code> is for user-defined header files. (D)</p> Signup and view all the answers

If you want to print the value of a float variable f with two decimal places using printf, which format specifier would you use?

<p>%.2f (D)</p> Signup and view all the answers

What is the output of the following C code?

int x = 5, y = 2;
float result = (float) x / y;
printf("%.2f", result);

<p>2.50 (D)</p> Signup and view all the answers

Given the following code, what is the value of area?

#include <stdio.h>
int main() {
    const int LENGTH = 10;
    const int WIDTH = 5;
    int area = LENGTH * WIDTH;
    printf("%d", area);
    return 0;
}

<p>50 (A)</p> Signup and view all the answers

Flashcards

C Programming

A general-purpose, procedural, imperative computer programming language developed in 1972.

Advantages of C

A structured programming language that produces efficient programs, handles low-level activities, and compiles on various platforms.

UNIX OS

The operating system for which C was initially invented.

#include directive

Tells the C compiler to include a header file before compilation.

Signup and view all the flashcards

int main()

The main function where program execution begins.

Signup and view all the flashcards

Comments

Helping text in your C program that the compiler ignores.

Signup and view all the flashcards

printf()

A function in C that causes a message to be displayed on the screen.

Signup and view all the flashcards

return 0;

Terminates the main() function and returns a value of 0.

Signup and view all the flashcards

Token in C

A basic component of a C program, can be a keyword, identifier, constant, string literal, or symbol..

Signup and view all the flashcards

Semicolon (;)

A statement terminator in C; each statement must end with this.

Signup and view all the flashcards

Identifier

A name used to identify a variable, function, or user-defined item.

Signup and view all the flashcards

Keywords

Reserved words in C that cannot be used as constants, variables, or identifiers.

Signup and view all the flashcards

Whitespace in C

Blanks, tabs, newline characters, and comments that separate parts of a statement.

Signup and view all the flashcards

Header File in C

A file with the extension '.h' containing function declarations and macro definitions.

Signup and view all the flashcards

#include directive operation

Directs the C preprocessor to scan a specified file as input.

Signup and view all the flashcards

Data Types in C

An extensive system for declaring variables or functions of different types.

Signup and view all the flashcards

Basic Types

Arithmetic types, further classified into integer and floating-point types.

Signup and view all the flashcards

void Type

A type specifier that indicates no value is available.

Signup and view all the flashcards

Derived Types

Pointer, array, structure, union, and function types.

Signup and view all the flashcards

sizeof operator

Yields the storage size (in bytes) of an object or type.

Signup and view all the flashcards

Variable in C

A name given to a storage area that programs can manipulate.

Signup and view all the flashcards

Variable Definition

Tells the compiler where and how much storage to create for a variable.

Signup and view all the flashcards

Variable Declaration

Provides assurance to the compiler that a variable exists.

Signup and view all the flashcards

Constants

Fixed values that the program may not alter during its execution.

Signup and view all the flashcards

Integer Literal

A decimal, octal, or hexadecimal constant.

Signup and view all the flashcards

Floating-point Literal

Has an integer part, decimal point, fractional part, and exponent part.

Signup and view all the flashcards

Character Constants

Enclosed in single quotes.

Signup and view all the flashcards

String Literals

Enclosed in double quotes.

Signup and view all the flashcards

Defining Constants

Defines constants using the #define preprocessor or the const keyword.

Signup and view all the flashcards

Operator in C

A symbol that tells the compiler to perform specific mathematical or logical functions.

Signup and view all the flashcards

Study Notes

Basics of C Programming

  • C is a general-purpose, procedural, imperative programming language
  • It was developed by Dennis M. Ritchie in 1972 at Bell Telephone Laboratories
  • C was created to develop the UNIX operating system
  • It is among the most widely used languages, often fluctuating in popularity with Java

Why Learn C?

  • C is essential for students and professionals in software development
  • It is easy to learn
  • C is a structured language
  • It produces efficient programs
  • It is capable of handling low-level activities
  • It can be compiled across various computer platforms

Facts About C

  • C was used to write UNIX
  • It is a successor to the B language, introduced in the early 1970s
  • The language was formalized in 1988 by ANSI
  • The UNIX OS was entirely written in C
  • Today, C is a popular System Programming Language
  • Most state-of-the-art software has been implemented using C
  • Popular systems like Linux OS and RDBMS MySQL are written in C

Printing "Hello World" in C

  • Below is the basic code
#include <stdio.h>

int main() {
    printf("Hello, World! \n");
    return 0;
}

Applications of C

  • C was initially for system development, particularly for operating system programs
  • C produces code that runs nearly as fast as assembly language
  • C is used in:
  • Operating Systems
  • Language Compilers
  • Assemblers
  • Text Editors
  • Print Spoolers
  • Network Drivers
  • Modern Programs
  • Databases
  • Language Interpreters
  • Utilities

Hello World Example: Program Components

  • Preprocessor Commands
  • Functions
  • Variables
  • Statements & Expressions
  • Comments

Code to Print "Hello World"

#include <stdio.h>

int main() {
    printf("Hello, World! \n");
    return 0;
}

Parts of the "Hello World" Program

  • #include <stdio.h> is a preprocessor command that includes the "stdio.h" file before compilation
  • int main() is the main function where execution starts
  • /*...*/is ignored by the compiler, used for comments.
  • printf(...) is a C function that displays "Hello, World!" on the screen
  • return 0; terminates the main function and returns the value 0

Compiling and Executing a C Program

  • Open a text editor and write the code
  • Save the file as hello.c
  • Open a command prompt in the directory where the file is saved
  • Compile the code using gcc hello.c
  • If there are no errors, an executable file a.out will be generated
  • Run the program by typing ./a.out
  • The output "Hello, World!" will be shown on the screen

Tokens in C

  • A C program consists of tokens
  • A token can be a keyword, identifier, constant, string literal, or symbol
  • For example, the statement printf("Hello, World! \n"); has five tokens in it

Semicolons

  • the semicolon is a statement terminator
  • Each statement must end with a semicolon to indicate the end of a logical entity
  • printf("Hello, World! \n"); & return 0; are two statements

Comments

  • Comments are ignored by the compiler and serve as helping text
  • They start with /* and end with */
  • They cannot be nested or occur within string or character literals

Identifiers

  • A C identifier names a variable, function, or user-defined item
  • They start with a letter (A-Z, a-z) or underscore _, followed by letters, underscores, and digits (0-9)
  • Punctuation characters are not allowed
  • C is case-sensitive, so Manpower and manpower are different identifiers

Keywords

  • C has reserved words that cannot be used as constants, variables, or identifiers

Whitespace

  • A blank line (containing only whitespace or a comment) is ignored by the C compiler
  • Whitespace includes blanks, tabs, newline characters, and comments
  • Whitespace separates parts of a statement, such as int age;

Header Files in C

  • Header files have a .h extension
  • They contain C function declarations and macro definitions to be shared across source files
  • There are programmer-written files and compiler-provided files
  • #include preprocessing directive is used to include header files
  • Including a header file is like copying its content, but is done to prevent errors
  • Constants, macros, global variables, and function prototypes are in header files

Include Syntax

  • User and system header files are included using the #include directive
  • System header files #include are searched for in standard system directories, prepend with -I to specify a different location
  • User header files #include "file" are searched for in the current directory

Include Operation

  • The #include directive tells the C preprocessor to scan the specified file before continuing
  • The output from the preprocessor contains the generated output, followed by the included file's output

Data Types

  • Data types in C declare variables or functions of different types
  • The type of variable determines memory space and interpretation

Basic Data Types

  • Basic Types: Arithmetic types
  • Integer types
  • Floating-point types.
  • Enumerated types: Arithmetic types
  • Used to define variables that only assign discrete integer values
  • The type void: Indicates no value is available
  • Derived types:
  • Pointer types
  • Array types
  • Structure types
  • Union types
  • Function types

Aggregate and Function Types

  • Array types and structure types are referred to as aggregate types
  • The type of a function specifies the function's return value

Integer Types Details

  • The details of standard integer types include storage sizes and value ranges
    • Char = 1 byte
    • Unsigned char = 1 byte
    • Signed char = 1 byte
    • Int = 2 or 4 bytes
    • Unsigned int = 2 or 4 bytes
    • Short = 2 bytes
    • Unsigned short = 2 bytes
    • Long = 8 bytes
    • Unsigned long = 8 bytes

Variable Storage and the sizeof operator

  • sizeof operator can be used to check variable size
  • The expressions sizeof(type) yields the storage size of the object or type in bytes
  • Variables are named storage areas
  • Each variable in C has a specific type that determines the size, layout of memory, range of values, and operations
  • Variable names have letters, digits, and underscores, must start with a letter or underscore, and are case-sensitive

Basic Variable Types

  • char: - Typically a single octet(one byte). This is an integer type.
  • int: Most natural size of integer for Machine
  • float: A single-precision floating point value
  • double: A double-precision floating point value
  • void: Represents the absence of type

C Variable Definition

  • A variable definition tells the compiler the type and amount of storage for the variable Datatype variablename_list;
  • type must be a valid C data type including char, w_char, int, float, double, bool, or any user-defined object
  • variable_list may consist of one or more identifier names separated by commas
  • The line int i, j, k; declares and defines variables i, j, and k of type int
  • Variables can be initialized with datatype variable_name = value;

Variable Declaration

  • A variable declaration provides assurance to the compiler that there exists a variable with the given type and name so that the compiler can proceed for further compilation without requiring the complete detail about the variable
  • A variable definition has its meaning at the time of compilation only, the compiler needs actual variable definition at the time of linking the program
  • Variables with static storage duration are implicitly initialized with NULL if no initializer is provided

Initializing Static Variables in C

  • Variables with static storage duration are implicitly initialized with NULL (all bytes have the value 0) if no initializer is used
  • Otherwise the variables are undefined

C: Variable Declaration and Use

  • Variable declaration lets the compiler know of a variable with a give name and type without needing complete details
  • Variable declaration is useful when using multiple files
  • The keyword extern declares a variable
  • Variables can be declared multiple times but defined only once

Constants (Literals)

  • Constants have fixed values that cannot be altered
  • It can be of any basic data type like an integer, a float, a character, or a string
  • Constants behave like variables, but their values cannot be modified post-definition

Integer Literals

  • An integer literal can be decimal, octal, or hexadecimal
  • Prefix: 0x or 0X (hexadecimal), 0 (octal), nothing (decimal)
  • Suffix: U and L (unsigned and long)
  • Suffix can be upper or lowercase

Floating-Point Literals

  • Has an integer part, decimal point, fractional part, and exponent part
  • Can be decimal or exponential
  • Must include decimal point, exponent, or both in decimal form
  • Must include integer part, fractional part, or both in exponential form
  • Signed exponent introduced by e or E

Character Constants

  • Character literals are in single quotes (e.g., 'x') and stored in a char type
  • Can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'), or a universal character (e.g., '\u02C0')
  • Characters with backslashes have special meanings (e.g., newline \n, tab \t)

String Literals

  • String literals are enclosed in double quotes ""
  • A string contains characters (plain, escape sequences, and universal)
  • Long lines can be broken into multiple lines using string literals and separating them with white spaces
  • All three forms are identical strings

Defining Constants in C

  • There are two ways to define constants
  • Using the #define preprocessor
  • Using the const keyword

Using the #define Preprocessor

  • The syntax to define constant is #define identifier value

Using the const Keyword

  • Using the const keyword means you can declare a type for the constant
  • The syntax is const type variable = value;

Operators in C

  • Operators are symbols that instruct the compiler to perform mathematical or logical functions
  • C provides:
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators

Arithmetic Operators

  • +: Adds two operands
  • -: Subtracts the second operand from the first
  • *: Multiplies both operands
  • /: Divides numerator by de-numerator
  • %: Modulus Operator
  • ++: Increment operator
  • --: Decrement operator

Relational Operators

  • == Checks if the values of two operands are equal or not
  • != Checks if the values of two operands are equal or not.
  • > Checks if the value of left operand is greater than the value of right operand
  • < Checks if the value of left operand is less than the value of right operand
  • >= Checks if the value of left operand is greater than or equal to the value of right operand
  • <= Checks if the value of left operand is less than or equal to the value of right operand

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

CS131 - Week 1 and 2 (Hard)
18 questions
C Programming: History and Advantages
9 questions
CMPUT 201: Understanding Unix and C
23 questions
Use Quizgecko on...
Browser
Browser